调用 libc stdio printf,实现 int main(){ return printf(message); }
; ----------------------------------------------------------------------------
; helloworld.asm
;
; This is a Win32 console program that writes "Hello, World" on one line and
; then exits. It needs to be linked with a C library.
; ----------------------------------------------------------------------------
global _main
extern _printf
section .text
_main:
push message
call _printf
add esp, 4
ret
message:
db 'Hello, World', 10, 0
If you want to do it without help you need to talk to your video hardware directly, likely writing bitmaps of the letters of "Hello world" into a framebuffer. Even then the video card is doing the work of translating those memory values into DisplayPort/HDMI/DVI/VGA signals.
请注意,实际上,这些东西没有一个在 ASM 中比在 C 中更有趣。“ hello world”程序归结为一个函数调用。关于 ASM 的一个好处是,您可以相当容易地使用任何您想要的 ABI; 您只需要知道 ABI 是什么。
Why does x64 Windows need to reserve 28h bytes of stack space before a call? That's 32 bytes (0x20) of shadow space aka home space, as required by the calling convention. And another 8 bytes to re-align the stack by 16, because the calling convention requires RSP be 16-byte aligned 之前 a call. (Our main's caller (in the CRT startup code) did that. The 8-byte return address means that RSP is 8 bytes away from a 16-byte boundary on entry to a function.)
include 'win32ax.inc'
.code
start:
invoke MessageBox,HWND_DESKTOP,"Hi! I'm the example program!",invoke GetCommandLine,MB_OK
invoke ExitProcess,0
.end start
where Path _ to _ link could be C: Program Files (x86) Microsoft Visual Studio 10.0 VC bin or wherever is your link.exe program in your machine,
Path _ to _ libs 可以是 C: Program Files (x86) Windows Kit 8.1 Lib winv6.3 um x64,也可以是库所在的任何地方(在这种情况下,kernel32.lib 和 user32.lib 都在同一个位置,否则对于每个需要的路径都使用一个选项) ,而且必须使用 /大型地址软件: 不选项来避免链接器抱怨地址太长(对于 user32.lib 来说是这种情况)。
此外,如果从命令提示符调用 Visual 的链接器,则需要先设置环境(运行一次 vcvarsall.bat 和/或参见 MS C + + 2010和 mspdb100.dll)。
(Using default rel makes the lea instructions work from anywhere, including outside the low 2GiB of virtual address space. But the call MessageBoxA is still a direct call rel32 that can only reach instructions +-2GiB away from itself.)
The best examples are those with fasm, because fasm doesn't use a linker, which hides the complexity of windows programming by another opaque layer of complexity.
If you're content with a program that writes into a gui window, then there is an example for that in fasm's example directory.
中声明的所有设施。资料部分。对于控制台程序,您需要 _ GetStdHandle 来查找标准输入和标准输出的文件描述符(使用像 STD _ INPUT _ HANDLE 这样的符号名称,这些符号名称可以在包含文件 win32a.inc 中找到)。
Once you have the file descriptors you can do WriteFile and ReadFile.
所有的函数都在内核32文档中有描述。您可能已经注意到了这一点,否则不会尝试汇编程序。
总之: 有一个与 windows 操作系统配对的 asci 名称表。
在启动过程中,这会转换成一个可调用地址表,您可以在程序中使用它。