0

So I have this assignment where I have to write the following code in assembly language and then explain what happened with the EAX register.

I have already done so, but I get confused at myWord+2 because I do not understand why the eax register is 00000003? I thought we have to sum up the list and then add 2 to the eax.

This is my code

; summation of the list values
.386
.model flat, stdcall
.stack 4096
ExitProcess PROTO, dwExitCode: DWORD

.data
    myWord WORD 4 DUP(1,2,3,4,5)

.code
main PROC
    mov eax, 0          ; zeroing eax
    mov ax, myWord+0    ; sum up the list values

    add ax, myWord + 2
    add ax, myWord + 4
    add ax, myWord + 6
    add ax, myWord + 8

    INVOKE ExitProcess, 0
main ENDP
END main

and this is my EAX register when I debug it EAX = 00000003

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
  • 1
    what assembler are you using? Most require label definitions have a `:` after them. – Chris Dodd Oct 04 '22 at 01:30
  • @ChrisDodd: NASM doesn't require it, but it's a good idea to disambiguate against typos in mnemonics for instructions with no operands. (NASM warns by default about a non-instruction on a line by itself being interpreted as a label). MASM unfortunately *requires* you not to use a `:` in `.data`, at least some versions did, because the `word ...` is associated with the name as a "variable", so for example `add myWord, 1` implies an operand-size of `word ptr`, and MASM even complains of a mismatch if you don't override the size in `mov eax, [myWord]`. – Peter Cordes Nov 16 '22 at 06:00
  • I don't know what `4 DUP(1,2,3,4,5)` does. Check memory with a debugger. I wondered if it would be `word 1,1,1,1, 2,2,2,2,` etc, repeating every element 4 times. But no, that would give eax=4*1 + 2 = 6. And `1,2,3,4,5, 1,2,3,4,5, ...` wouldn't explain the result either. (Assuming that EAX is being looked at after the final `add`.) Oh, the title says *after `add, myWord+2`*, so it probably is repeating the sequence 4 times, 1+2 = 3. Seems straightforward to me. `myWord+2` is the address of the 2nd element of the array (which MASM implicitly derefs), `myWord+0` is the first. – Peter Cordes Nov 16 '22 at 06:04

0 Answers0