I have the following assembly code
.386
.model flat, c
.stack 100h
printf PROTO arg1:Ptr Byte, printlist:VARARG
scanf PROTO arg2:Ptr Byte, printlist:VARARG
.data
in1fmt byte "%d",0
msg2fmt byte 0Ah,"%s%d",0
msg3 byte "EAX is : ",0
number sdword 10
.code
main proc
mov eax, 90
INVOKE printf, ADDR msg2fmt, ADDR msg3, eax
INVOKE printf, ADDR msg2fmt, ADDR msg3, eax
mov eax, number
INVOKE printf, ADDR msg2fmt, ADDR msg3, eax
INVOKE printf, ADDR msg2fmt, ADDR msg3, eax
sub eax, 1
INVOKE printf, ADDR msg2fmt, ADDR msg3, eax
INVOKE printf, ADDR msg2fmt, ADDR msg3, eax
ret
main endp
end
For some reason the output of EAX constantly changes, not as expected.
The output I would expect here:
EAX is : 90
EAX is : 90
EAX is : 10
EAX is : 10
EAX is : 9
EAX is : 9
The output I get:
EAX is : 90
EAX is : 12
EAX is : 10
EAX is : 12
EAX is : 11
EAX is : 12
It was my understanding if eax is assigned a value it should be relatively safe until something else uses eax?
eaxcontains the return value of a function call, so of course after callingprintfits value changes.More in general, you should learn about caller-saved registers vs callee-saved registers.