abcdefghi019283
abcdefghi019283

Reputation: 105

Assembly - Why do these code chunks different output?

Why do these operations not the same outcome:

str db "%d",0

mov eax, 67305985
push eax
push str
call [printf]
pop eax
pop eax

Prints 67305985, just as it should

str db "%d",0

mov eax, 67305985
mov ebx, eax
shr ebx, 16
mov [esp-1], bh
mov [esp-2], bl
mov ebx, eax
mov [esp-3], bh
mov [esp-4], bl
push str
call [printf]
pop eax
pop eax

Prints 1964082724 which I dont understand

Upvotes: 1

Views: 76

Answers (1)

Sep Roland
Sep Roland

Reputation: 39166

The stack grows downward. The memory below to where ESP is pointing can be used freely by all kinds of interruptions.

You need to reserve the memory below ESP for your own use. Just subtract 4 from ESP.

mov eax, 67305985

sub esp, 4
mov [esp], al
mov [esp+1], ah
shr eax, 16
mov [esp+2], al
mov [esp+3], ah
push str
call [printf]
pop eax
pop eax
...

If you first store the low bytes, you don't need the extra register.

Upvotes: 2

Related Questions