How to add values?

CSStudent picture CSStudent · Nov 12, 2015 · Viewed 19k times · Source

New to Assembly language, reading a book here. I'm trying to do an simple basic exercise. Using appropriate registers, I have to add 100, 200, 300, 400, 500. Don't know where to start with this program. This is the outline of the program, now I need to add the registers. This is what I have, from what I understand from the book. Don't know how to keep adding.

(AddSub.asm)
INCLUDE Irvine32.inc

.code   

main PROC

    mov eax, 100
    add eax, 200

exit
main ENDP
END main

Answer

Michael Petch picture Michael Petch · Nov 12, 2015

If you have experience with higher level languages like C, then these lines:

mov eax, 100
add eax, 200

Would do something akin to:

int eax;
eax = 100;       /* mov 100 to EAX */
eax = eax + 200; /* add 200 to EAX */

If you want to add other numbers you keep adding to EAX like:

add eax, 300
add eax, 400

You can use other registers besides EAX (like EBX,ECX,EDX,ESI,EDI). You can also add these registers together. For example

mov eax, 100
mov ebx, 200
mov ecx, 300
add eax, ebx
add eax, ecx

This would be akin to:

int eax = 100;
int ebx = 200;
int ecx = 300;

eax = eax + ebx; /* add EBX to EAX */
eax = eax + ecx; /* add ECX to EAX */

Which would result in a value of 600 in EAX

Using Irvine32 library you can print the contents of EAX as a signed integer by calling the WriteInt function like so:

call WriteInt