2

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
Seki
  • 11,135
  • 7
  • 46
  • 70
CSStudent
  • 115
  • 1
  • 2
  • 9
  • 1
    *now I need to add the registers*. Not really. `mov eax, 100` puts `100` into `eax`. Then `add eax, 200` adds `200` to `eax`, making `eax` contain 300. With assembly language, what you see is what you get. – lurker Nov 12 '15 at 18:29
  • 1
    downvoting because this seems too trivial. The community is more forgiving of trivial questions about asm than most languages, but this is like asking "how would I add another `x += 300;` to my existing C program"? Don't all procedural languages work by stringing statements one after the other? I'm not trying to be rude, because obviously you have to start somewhere while learning. I'm just saying that you should keep reading your asm book, or maybe look for another one if the answer isn't clear yet, because this doesn't seem like a good SO question. Come back later when you know more :) – Peter Cordes Nov 12 '15 at 20:16
  • 1
    Although I agree with @PeterCordes , I'll remain neutral about the vote. Had there been no code between `main PROC` and `exit` I would have readily downvoted and voted to close. Yes, it is a trivial task, with a trivial answer, and unfortunately the solution is itself trivial. There is an opportunity to also present some ideas that may help (like printing the number to the screen using Irvine's library etc). This would have been a duplicate of other questions had it been about adding 2 numbers. – Michael Petch Nov 12 '15 at 20:27

3 Answers3

4

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
Michael Petch
  • 46,082
  • 8
  • 107
  • 198
  • It's more akin to `eax += 200`, `eax += ebx`, and `eax += ecx`. – C. K. Young Nov 12 '15 at 20:02
  • I was unsure if the OP had any knowledge of C so I expanded it out so it would be clear. I didn't want to explain `+=` if they hadn't seen it before. It was meant to be more pseudo codish than C-ish. – Michael Petch Nov 12 '15 at 20:04
  • Sure, but I wanted to avoid the `a = b + c` construction because x86 does not have 3-operand add. – C. K. Young Nov 12 '15 at 20:04
3

Just add more add instructions:

add eax, 300
add eax, 400
add eax, 500

You also have to print out the result somehow, before your exit line.

C. K. Young
  • 219,335
  • 46
  • 382
  • 435
  • He has `exit` there which is presumably a macro coming from the `Irvine32.inc`. So that's taken care of :) – Jester Nov 12 '15 at 18:34
  • @Jester Ah, thanks! The indentation threw me off, sorry. (That's what being a Schemer does to you.) – C. K. Young Nov 12 '15 at 18:35
  • But it's nevertheless a valid point and might help somebody else :) – Jester Nov 12 '15 at 18:35
  • @Jester , yes `exit` is defined by one of the files _Irvine32.inc_ includes. It is simply defined as `exit EQU ; exit program` – Michael Petch Nov 12 '15 at 19:32
  • 2
    With Irvine32 library you can print out the contents of _EAX_ by doing a `call WriteInt` for signed numbers or `call WriteDec` for unsigned. – Michael Petch Nov 12 '15 at 19:45
0

I think you should keep adding add the numbers to the eax register right after the mov instruction. From the little i know this is what the eax is for.

section .text


global __start
__start:
mov eax, 100
add  eax, 200
add eax, 300
add eax, 400
add eax, 500

; you can print it out here

mov eax, 1
Int 0x80

I hope this helps.