I have an assembly language program that attempts to print out the value of a Window Environment Variable, namely "%AppData%". Unfortunately, my program only crashes. I suspect the problem is caused by an incorrect stack reservation. Here is my code:
includelib \Masm64\Lib\Kernel32.lib
includelib \Masm64\Lib\User32.lib
extern GetEnvironmentVariableA : proc
extern MessageBoxA : proc
extern ExitProcess : proc
dseg segment para 'DATA'
capt db 'Debug', 0
msg db 0 dup(30h)
evar db '%AppData%', 0
dseg ends
cseg segment para 'CODE'
start proc
sub rsp, 18h
mov r8d, 30h
lea rdx, [msg]
lea rcx, [evar]
call GetEnvironmentVariableA
sub rsp, 28h
xor r9d, r9d
lea r8, [capt]
lea rdx, [msg]
xor rcx, rcx
call MessageBoxA
fini: call ExitProcess
start endp
cseg ends
end
Any suggestions?
MSDN specifies: "The caller is responsible for allocating space for parameters to the callee, and must always allocate sufficient space for the 4 register parameters, even if the callee doesn't have that many parameters."
You are only allocating 18h bytes before the first call - maybe you should allocate 20h? Visual C seems to always allocate 28h bytes. You don't need to allocate it again before each function call, though.
Additionally, GetEnvironmentVariable only takes in the name of the variable, without the percent characters.
The following modified code seems to work:
includelib \Masm64\Lib\Kernel32.lib
includelib \Masm64\Lib\User32.lib
extern GetEnvironmentVariableA : proc
extern MessageBoxA : proc
extern ExitProcess : proc
dseg segment para 'DATA'
capt db 'Debug', 0
msg db 0 dup(30h)
evar db 'AppData', 0
dseg ends
cseg segment para 'CODE'
start proc
sub rsp, 28h
mov r8d, 30h
lea rdx, [msg]
lea rcx, [evar]
call GetEnvironmentVariableA
xor r9d, r9d
lea r8, [capt]
lea rdx, [msg]
xor rcx, rcx
call MessageBoxA
fini: call ExitProcess
start endp
cseg ends
end
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.