0
section .text
global _start
_start:
    mov eax,'3'
    sub eax,'0'
    mov ebx,'4'
    sub ebx,'0'
    add eax,ebx
    add eax,'0'
 ;  mov [sum],eax
 ; mov ecx,sum
    mov ecx,eax
    mov eax,4
    mov ebx,1
    mov edx,1
    int 0x80

    mov eax,1
    int 0x80

section .bss
sum resb 1

This x86 IA-32 Adds two numbers. When I move accumulator register result to a variable in memory and then from that variable to ecx and call interrupt it prints as expected. However If i directly move ecx,eax it doesn't work as expected.

Also when trying to print something only if second operand is from memory it works. Am i missing something.

P.S I've seen similar posts in SO, and couldn't find any duplicate

  • The `write` system call takes a pointer to bytes in memory. Notice that `mov ecx,sum` puts the address into ECX, not the value. `mov ecx,[sum]` would be equivalent to `mov ecx, eax`. Also, `resb 1` only reserves 1 byte but you store 4. Use `mov [sum], al` – Peter Cordes May 25 '20 at 07:44
  • @PeterCordes : I get why mov ecx,eax is not working. But How mov [sum],al is 4 bytes? eax is 32 bit, ax is 16 and al,ah are 8bit (byte) each. Isn't it? So reserving one byte is enough right? – theuserrandom1 May 25 '20 at 08:01
  • 1 byte *is* enough for `mov [sum],al`, that's why I said you should change to using that. Your commented "working" code had `mov [sum],eax` which stores 4 bytes, 3 past the end of `resb 1`. – Peter Cordes May 25 '20 at 08:02
  • Got it. Thanks a lot. – theuserrandom1 May 25 '20 at 08:09

0 Answers0