Could you tell me why the following program goes wrong when I run it:
Quote; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
; Build this with the "Project" menu using
; "Console Assemble and Link"
; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
include \masm32\include\masm32rt.inc
.data
.data?
a dd ? ; Uninitialised single 32 bit space
b dd ? ; Uninitialised single 32 bit space
.code ; Tell MASM where the code starts
; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
start: ; The CODE entry point to the program
print chr$("eax is: ")
xor eax, eax
mov ax, 4560h
push ax
mov ax, word ptr [esp]
movzx eax, ax
print uhex$(eax),13,10
inkey
exit
; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
end start ; Tell MASM where the program ends
but if I modify it, it works:
Quote; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
; Build this with the "Project" menu using
; "Console Assemble and Link"
; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
include \masm32\include\masm32rt.inc
.data
.data?
a dd ? ; Uninitialised single 32 bit space
b dd ? ; Uninitialised single 32 bit space
.code ; Tell MASM where the code starts
; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
start: ; The CODE entry point to the program
print chr$("eax is: ")
xor eax, eax
mov ax, 4560h
push eax
mov eax, dword ptr [esp]
print uhex$(eax),13,10
inkey
exit
; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
end start ; Tell MASM where the program ends
How can I debug with Ollydbg to find out the cause of this problem?
By using int 3, I see that before "print uhex$(eax),13,10", eax has already been 00004560. But why is it not printed?
QuoteEAX 00004560
You are breaking the stack alignment (push WORD ...): Windows claims the stack to be aligned by 4.
Quote from: qWord on November 29, 2011, 07:54:05 AM
You are breaking the stack alignment (push WORD ...): Windows claims the stack to be aligned by 4.
Thank you qWord very much :bg .
From the Intel® 64 and IA-32 Architectures Software Developer's Manual:
QuoteThe stack-address size determines the width of the stack pointer when writing to the stack in memory and when decrementing the stack pointer. (As stated above, the amount by which the stack pointer is decremented is determined by the operand size.) If the operand size is less than the stack-address size, the PUSH instruction may result in a misaligned stack pointer (a stack pointer that is not aligned on a doubleword or quadword boundary).
I have changed it again and now it works :bg :
Quote; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
; Build this with the "Project" menu using
; "Console Assemble and Link"
; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
include \masm32\include\masm32rt.inc
.data
.data?
.code ; Tell MASM where the code starts
; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
start: ; The CODE entry point to the program
print chr$("eax is: ")
xor eax, eax
mov ax, 4560h
push ax
sub esp, 2
mov ax, word ptr [esp+2]
movzx eax, ax
print uhex$(eax),13,10
inkey
exit
; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
end start ; Tell MASM where the program ends
print chr$("eax is: ")
xor eax, eax
mov ax, 4560h
push ax
mov ax, word ptr [esp+2]
movzx eax, ax
add esp, 2
print uhex$(eax),13,10
inkey
exit
or, better yet...
print chr$("eax is: ")
sub esp,4
mov word ptr [esp],4560h
movzx eax, word ptr [esp]
print uhex$(eax),13,10
pop eax
inkey
exit