Take This Life

Take This Life (https://www.takethislife.com/)
-   Science and Technology (https://www.takethislife.com/science-technology/)
-   -   Assembly programming (https://www.takethislife.com/science-technology/assembly-programming-287836/)

Ehisdi 07-03-18 03:04 AM

Assembly programming
 
Now this won't be concise, nor will it be platform independent, but it may help my brain pick up some things that I've forgotten, or just can't concentrate on...So this may take quite a while to pull that information out of my noggin.


System: Slackware 14.0 and 14.2
Bits: 64
Assembler: Nasm and Yasm
Assembly flavour: Intel syntax.

General purpose registers on a 64 bit system...


AH : AL = {AX} =>{00:00}

EAX = 00:00:00:00

RAX = 00:00:00:00:00:00:00:00

If memory serves, BH, BL, CH, CL, and not sure about DH, DL exist, but I haven't had the need to use those since the old days. So they are a tad unnecessary.

But I do use the following.

BX < EBX < RBX (Using the less than sign to show that they are smaller/larger than the others. And that they follow the same rules as the AX < EAX < RAX.

CX < ECX < RCX
DX < EDX < RDX

R# (I don't remember these off hand, and I need to get ready for bed, so I'll come back to this when I can).

SP < ESP < RSP (Why would someone use the lesser? I don't know. I could, but I really have no need to do so).

BP < EBP < RBP (Again, not really necessary, but can be used if one wants...)

There's a rule about the nSP and nBP registers, don't forget them, else your program will crash like there's no tomorrow.

The other registers, MMX, YMMX or whatever registers, I haven't used these as of yet. It's on my todo list, but I'm getting older, so I may never touch them. Eh, c'est la vie.

The FLAGS register is a special register, which is used by several instructions, primarily flow control.

Lets take a simple C if statement and translate it into assembly...(Also, lets see if the code tags work).

Code:

if (a == 0)
  b = 15;
else
  b = 5

MOV AX, 5      ; There's a reason I'm doing these next two lines.
MOV CX, 3
TEST BX, BX    ; We're saying that the variable 'a' is in BX.
JNZ if_done      ; If BX does not equal 0, or 'a' does not equal zero, then we're done.
MUL CX          ; Else multiply by 3.
if_done:          ; We finished the if statement.

; ------------------------------------------------------------------------
; Here's another way this same if statement can be written.

MOV AX, 5
TEST BX, BX
JNZ if_done
OR AX, 10
if_done:

; ------------------------------------------------------------------------
; And lastly, a slightly quicker one, but reliant on AX being empty.

TEST BX, BX
JNZ if_done
MOV AX, 15
if_done:
OR AX, 5

If you note, I wrote the assembly backwards. Instead of doing "if (a==0) b=15; else b=5;" I did "if (a!=0) b=5; else b=15;". The reason for that is, when writing assembly you try to simplify everything a lot. So sometimes, what you write in c, c++, whatever language won't be how you'd write it in assembly.

I think I may have messed something up there...But I'm finally tired, and it's only 2am...Which is good enough for me...So I'll come back to this some other time...Hopefully...
Oh, if anyone has any questions, go for it. I'd love to talk with someone, and it'd sure help to get my brain to function, heh.

EmpatheticThoughts 07-03-18 03:20 AM

If you don't mind me asking, how long have you been programming?

Ehisdi 07-03-18 11:56 AM

Quote:

Originally Posted by EmpatheticThoughts (Post 2908064)
If you don't mind me asking, how long have you been programming?

I think 99 is when I first got into it quite a bit, as I wasn't allowed to do much nor had enough documentation to learn the Atari XE or this small keyboard only computer my mom had back in the late 80's.

For c and assembly, though, that was much later. Around 2008 - 2010, right before my brain decided I didn't need to think coherently anymore, heh.

If I can immerse myself into the code, though, it gets really really easy to do regardless which language I choose. The hard part is getting started.

Still would like to finish at least this one bit of code in assembly. I've been wanting to write my own big number library that uses only the set of integers. I did have the multiplication, subtraction, addition, and few other initializing routines written up, but I realized late in the development that I had two major flaws in it. If I could just get my darn mind to concentrate on it, I could make a few changes without having to rewrite everything. Kind of aggravating, heh.

Honestly, though, I have to put programming on the side for the moment. Since I've been out of work for so long, I'm needing to find something that my aching body will let me do to make at least some money, and programming (which I love the most that my body can do on a regular basis) just isn't a viable option here. So, arts and crafts are my best bet (And my creativity has been shot to all hell from the poor marriage and pains I've been in since 2000).

EmpatheticThoughts 07-03-18 07:12 PM

Quote:

If I can immerse myself into the code, though, it gets really really easy to do regardless which language I choose. The hard part is getting started.
This is good to hear. I consider myself to be a relatively lazy person (mostly because of social anxiety) but I have a degree in math. How long do you think it would take me to be able to pick it up?

Quote:

Honestly, though, I have to put programming on the side for the moment. Since I've been out of work for so long, I'm needing to find something that my aching body will let me do to make at least some money, and programming (which I love the most that my body can do on a regular basis) just isn't a viable option here. So, arts and crafts are my best bet (And my creativity has been shot to all hell from the poor marriage and pains I've been in since 2000).
Where is programming not a viable option??

Ehisdi 07-03-18 10:32 PM

Quote:

Originally Posted by EmpatheticThoughts (Post 2908104)
This is good to hear. I consider myself to be a relatively lazy person (mostly because of social anxiety) but I have a degree in math. How long do you think it would take me to be able to pick it up?

Depends on a couple of factors. If you plan on aiming higher than a hobby, opensource, or tinkering around, then I'd suggest looking into C++ as that would help with java and C# which would be of far greater benefit. Now, if you're interested in digging down deep into coding, or hobby, opensource, and/or tinkering, then c and assembly would be beneficial.

As for picking up, that's a little tricky, and each person learns in a different way. For example, most people can pick up the use of "*" in c as both multiplication and pointer data type with ease, for me I didn't get used to the pointer types until I learned assembly. Also, depending on what I'm writing, I'm far more comfortable in assembly than I am in C, but for legibility, C is much much higher on that list.

Code:


\\  C code here.    This is easier to read.
a = 0;
for(x=1;x<30;x++) a=a+x;

; Assembly  This is easier to understand.

MOV RCX, 29      ; Let RCX = 29, and since 29 < 30, this is the maximum.
XOR AX, AX        ; This sets AX = 0
Begin_Loop:        ; The start of the loop
ADD AX, CX        ; This is basically a = a + x, or AX = AX + CX.
loop Begin_Loop  ; This is basically, CX = CX - 1 then go to Begin_Loop.

\\ C code of the assembly, but written more approximately with the assembly.
\\ It does exactly the same thing, it's just not as legible.
for(x=29;x>0;x--) a = a + x;

Anyhoo, as long as you can read, you can pick up any programming language. There are a lot of materials on line, and there are a great many forums that would help with learning assembly. There's even a few debuggers out there that would help a lot with that as well. As for a math degree helping in programming, that's not really that important. I say that, but, and this but is a biggy, it comes in handy for dealing with algorithms and speeding up code. This is one area that maybe delving into some minor assembly would be highly beneficial. You have to really really REEEEAAAAAAAAALLLLY think about how you might want to handle a routine.

Here's my bigmul routine that I wrote in assembly.

Quote:

Where is programming not a viable option??
Western Kansas. Now if I could care enough, I could always write a game, I found the allegro library which I could use to write games, and I do enjoy 2D RTS type games, but two things keep me from doing so, 1. Pain and stress. I'm in such an amount of pain and so stressed that my creativity is just gone. 2. Nobody, and I mean nobody would allow me the time necessary to learn the necessary elements to get it going. I'm the cook, person who is supposed to take care of everyone and everything at any given moment, and if I try, I get in trouble for it. That's been my life since I was young. It's stupid to be a mathematician, oceanographer, lexicographer, botanist. You're too retarded to go to college. You're too lazy to make anything of yourself. You don't get to do what you want, you have to hurt like hell working for a horrible boss for the rest of your life. These are the kinds of things that were fed into my head ever since I was little, and lo and behold, I made them true...Sheesh...And to make matters worse, I married a woman who believed the same...So yeah, my mind is kind of messed up from years of trying to keep my mentality from going insane, then as soon as I met my wife in 2000, I began working at a job that just tore my body to shreds...Heh, I'm a mess.

EmpatheticThoughts 07-04-18 01:18 AM

Quote:

Originally Posted by Ehisdi (Post 2908178)
1. Pain and stress. I'm in such an amount of pain and so stressed that my creativity is just gone. 2. Nobody, and I mean nobody would allow me the time necessary to learn the necessary elements to get it going. I'm the cook, person who is supposed to take care of everyone and everything at any given moment, and if I try, I get in trouble for it. That's been my life since I was young. It's stupid to be a mathematician, oceanographer, lexicographer, botanist. You're too retarded to go to college. You're too lazy to make anything of yourself. You don't get to do what you want, you have to hurt like hell working for a horrible boss for the rest of your life. These are the kinds of things that were fed into my head ever since I was little, and lo and behold, I made them true...Sheesh...And to make matters worse, I married a woman who believed the same...So yeah, my mind is kind of messed up from years of trying to keep my mentality from going insane, then as soon as I met my wife in 2000, I began working at a job that just tore my body to shreds...Heh, I'm a mess.

You're not mess. Thank you for talking to me. I can relate to the putdowns. I struggle with feeling good about myself from somethings that happened in my childhood. Hi, I'm ET, I also have self-sabotaged :wave:

You don't need to use a lot of logic for programming? I know algorithms use logic.

Ehisdi 07-04-18 02:31 AM

Quote:

Originally Posted by EmpatheticThoughts (Post 2908192)
You're not mess. Thank you for talking to me. I can relate to the putdowns. I struggle with feeling good about myself from somethings that happened in my childhood. Hi, I'm ET, I also have self-sabotaged :wave:

Yay, heh.

Quote:

You don't need to use a lot of logic for programming? I know algorithms use logic.
Well, well disciplined logic verses disorganized logic. Just imagine a drunk trying to explain something as simple as how to drive verses when he's sober. The logic is still there, but the ability to explain it efficiently is not. (If that makes sense, :eek:).

Heh, It's one of the few things that causes us low level programmers to get antsy whenever we talk with high level programmers. I know a kid who writes C# and java programs and he doesn't quite grasp boolean operands, which in low level programming are something of a necessity to speed up code, and algorithms quite often.

EmpatheticThoughts 07-04-18 03:30 AM

You're making me excited to start pick this up again! I understood your explanation of disciplined knowledge vs disorganized logic. You're saying basic programming is more intuitive and straightforward.

Ehisdi 07-04-18 03:39 AM

You could say that, but as the old adage goes, "what's normal for the spider is chaos for the fly", or in me and my wife's positions, "What's normal for the programmer is pointless to the current Dr. Who episode", heh.

Ehisdi 07-15-18 02:42 AM

itob (Integer to binary string)
 
Eureka, as I holler naked through the streets of Syracuse.

Talked with the wife and my mind seems to be coming back to me, well at least my calculative part of my mind. Conversing, on the other hand, nope. That's still beyond my mental capacity.

Anyway, I wrote a small little routine in assembly and thought I'd share it and give some insight into what I did, and why I did it (having to relearn things, so there may be some stuff that would make these things better).

Code:

;
;  itob "Integer to Binary string"

;  c calling: itob(char *destination, int num)
;
;  Assembly: itob  RDI, RSI
;  Registers used: RDI, RSI, RCX
;
section .text
  global itob

itob:
    MOV      RCX, 32      ; Begin our loop for an integer.

    MOV BYTE [RDI], 0x30  ; Set the first character to the 0 character.
itob_find_c:
    SHL      ESI, 1      ; Shift until we find our first 1.
    JC      itob_begin  ; Then begin
  LOOP      itob_find_c  ; Continue searching
    INC      RDI
    JMP      itob_finish  ; Else there are no 1's then exit with 0.
itob_begin:
    ADC BYTE [RDI], 0      ; Add the 1
    INC      RDI          ; Move to next character writing position.
    MOV BYTE [RDI], 0x30  ; Set to the 0 character.
    SHL      ESI , 1      ; Else check for the next character.
  LOOP      itob_begin  ; Continue at itob_begin.

itob_finish:
    MOV BYTE [RDI], 0x0        ; Null terminate the string.
    RET

What happens? And why?

When the routine is called, the first thing that is done is RCX is set for the LOOP instruction. (Note: I didn't have to use RCX, I could have written ECX as the upper 32bits would have automagically been set to 0. Meh, force of habit).

The LOOP instruction decrements the RCX value, and I just read that one should refrain from using the LOOP instruction, O_o, however, in this code, it seems to be the best suited, at least at the moment. (If I can keep thinking on this, I definitely will be able to remember things and possibly find a better method than this).

Ok, so lets go through what is being done...
  1. RCX = 32, set this to the length of an integer (at least on my AMD system).
  2. Set the first character to '0'.
    • Since '0' is 0x30, and '1' is 0x31 in ascii characters. All we have to do is 0x30+1 to get '1'.
    • Also, by initializing it to '0', if 0 is passed to the routine, it will automagically have '0' set for the integer 0.
  3. SHL ESI, 1 : Shift ESI to the left (0010 becomes 0100)
  4. JC itob_begin : If the bit removed was a 1, then lets begin creating our string.
  5. LOOP itob_find_c : Return to itob_find_c until the first 1 is found.
  6. Increment RDI to the next character to be written (this is where the NULL terminating character will be written to, as there were no 1's to be found).
  7. JMP itob_finish : We're done, there's nothing more to do.
  8. ADC BYTE [RDI], 0 : If a 1 was found, then add it to our '0' character, else just add 0 to '0'.
    (I think this is where I could simplify this code down by quite a bit, but I'm unsure at the moment).
  9. INC RDI : Move to the next character write position. If at end of string, then this is where we add the NULL terminating character.
  10. MOV BYTE [RDI], 0x30 : Write the '0' character.
    Why would I use both MOV and ADC? Because there may already be data in [RDI], to which ADC would merely ADD the carry with 0x30 "with" the data that already exists in [RDI] So the MOV statement clears that.
  11. SHL ESI, 1 : Shift ESI to the left.
  12. LOOP itob_begin : Return to itob_begin til RCX = 0.
  13. MOV BYTE [RDI], 0x0 : Set the last character to the NULL terminating character.
  14. RET : return out of this routine.

Ok, I really need to try to get to sleep, but I hope some nerd out there will enjoy this. Again, if by perchance anyone has any questions, ask away. I really really could use the practice of explaining things to others, heh.


All times are GMT -5. The time now is 05:45 PM.

Powered by vBulletin® Version 3.8.8
Copyright ©2000 - 2020, vBulletin Solutions, Inc.
Shoutbox provided by vBShout v6.2.1 (Lite) - vBulletin Mods & Addons Copyright © 2020 DragonByte Technologies Ltd.
vBulletin Security provided by vBSecurity v2.2.2 (Pro) - vBulletin Mods & Addons Copyright © 2020 DragonByte Technologies Ltd.


Content Relevant URLs by vBSEO 3.3.2