I am trying to increment the eax variable by 2 within a procedure followed by pushing the eax value onto the stack/passing the result to printf:
; nasm -f elf test.asm && gcc -m32 -o test test.o
section .text
global main
extern printf
add_by_2:
add eax, 2
push eax
push message
call printf
add esp, 8
ret
main:
mov eax, 1
call add_by_2
call add_by_2
call add_by_2
ret
message db "%d", 0xa, 3, 0
After the third call the eax variable is not incremented by 2.
stdout:
3
5
5
Expected stdout:
3
5
7
What is wrong with this code?
How do I correct the code to correctly increment eax by 2 even after the third call to add_by_2
.
I am open to any method to increment the number stored in eax by two (even if I need to use other registers).