我是x86汇编的新手,最近一直在使用nasm进行实验,并在Windows 10机器上运行程序。
我有以下代码:
global _start
extern _GetStdHandle@4
extern _WriteFile@20
extern _ExitProcess@4
section .data
message db "1234"
section .text
_start:
call print
call _ExitProcess@4
print:
; DWORD bytes;
mov ebp, esp
sub esp, 4
; hStdOut = GetstdHandle( STD_OUTPUT_HANDLE)
push -11
call _GetStdHandle@4
mov ebx, eax
; WriteFile( hstdOut, message, length(message), &bytes, 0);
push 0
lea eax, [ebp-4]
push eax
push 4
push message
push ebx
call _WriteFile@20
mov esp, ebp
ret
; ExitProcess(0)
我将使用以下命令进行组装:
nasm -f win32 out.asm
link out.obj /entry:start /subsystem:console "C:\Program Files (x86)\Windows Kits\10\Lib\10.0.18362.0\um\x86\kernel32.lib"
当在命令提示符上运行时,它会按预期输出“1234”。
现在,当组装和运行以下代码时,程序直接推送“1234”,而不是消息:
global _start
extern _GetStdHandle@4
extern _WriteFile@20
extern _ExitProcess@4
section .data
message db "1234"
section .text
_start:
call print
call _ExitProcess@4
print:
; DWORD bytes;
mov ebp, esp
sub esp, 4
; hStdOut = GetstdHandle( STD_OUTPUT_HANDLE)
push -11
call _GetStdHandle@4
mov ebx, eax
; WriteFile( hstdOut, message, length(message), &bytes, 0);
push 0
lea eax, [ebp-4]
push eax
push 4
push "1234"
push ebx
call _WriteFile@20
mov esp, ebp
ret
它没有输出任何内容
为什么?消息有什么信息是“1234”没有的?当推送消息时,程序是否只是推送存储“1234”的内存的地址?如果是这样,我能否将“1234”存储在其他地方,然后推送其地址而不创建变量?
message db "1234"
,message
是存放"1234"
开头字节的地址。当你想要访问该地址上存储的内容时,可以通过在[...]
中引用变量来解引用它,例如[message]
。 - David C. Rankin