下面的代码应该显示一个简单的";你好,世界"消息框。但由于某种原因,没有显示任何消息框。代码编译、构建和执行都没有错误。调试器显示了四个参数传递寄存器RCX、RDX、R8和R9,它们在函数调用和函数本身在RAX中返回0之前的值应该是正确的。
%include "io64.inc"
; hellow.asm
;%include "win32n.inc"
extern ExitProcess
extern MessageBoxA
section .data
msg db 'Welcome to Windows World!',0
cap db "Windows 10 says:",0
UINT dd "0x00000040",0
section .text
global main
main:
push rbp
mov rbp,rsp
;int MessageBoxA(
; HWND hWnd, owner window
; LPCSTR lpText, text to display
; LPCSTR lpCaption, window caption
; UINT uType window behaviour
; )
mov rcx,0 ; no window owner
lea rdx,[msg] ; lpText
lea r8,[cap] ; lpCaption
lea r9d,[UINT] ; window with OK button
sub rsp,32 ; shadowspace
call MessageBoxA ; returns IDOK=1 if OK button selected
add rsp,32
leave
ret
感谢Michael Petch,我得到了消息框不显示的原因。在"数据"部分;UINT dd 0x00000040";被给出为";字符编码";。真是个愚蠢的错误!为了纠正这一点,我必须将其转换为绝对值"equ";就是这么做。所以"UINT equ 0x00000040";是所讨论的函数所期望的正确输入。
更正后的代码为:
%include "io64.inc"
; hellow.asm
;%include "win32n.inc"
extern ExitProcess
extern MessageBoxA
section .data
msg db 'Welcome to Windows World!',0
cap db "Windows 10 says:",0
UINT equ 0x00000040
section .text
global main
main:
push rbp
mov rbp,rsp
;int MessageBoxA(
; HWND hWnd, owner window
; LPCSTR lpText, text to display
; LPCSTR lpCaption, window caption
; UINT uType window behaviour
; )
mov rcx,0 ; no window owner
lea rdx,[msg] ; lpText
lea r8,[cap] ; lpCaption
lea r9d,[UINT] ; window with OK button
sub rsp,32 ; shadowspace
call MessageBoxA ; returns IDOK=1 if OK button selected
add rsp,32
leave
ret
根据Peter Cordes的建议,更合适的编码:
%include "io64.inc"
; hellow.asm
;%include "win32n.inc"
extern ExitProcess
extern MessageBoxA
section .data
msg db 'Welcome to Windows World!',0
cap db "Windows 10 says:",0
MsgBoxOK equ 0x00000040
section .text
global main
main:
push rbp
mov rbp,rsp
;int MessageBoxA(
; HWND hWnd, owner window
; LPCSTR lpText, text to display
; LPCSTR lpCaption, window caption
; UINT uType window behaviour
; )
mov rcx,0 ; no window owner
lea rdx,[msg] ; lpText
lea r8,[cap] ; lpCaption
mov r9d,MsgBoxOK ; window with OK button and i icon
sub rsp,32 ; shadowspace
call MessageBoxA ; returns IDOK=1 if OK button selected
add rsp,32
leave
ret