Windows下如何用汇编程序编写hello world? [英] How to write hello world in assembler under Windows?
问题描述
我想在 Windows 下用汇编语言编写一些基本的东西,我使用的是 NASM,但我什么也做不了.
I wanted to write something basic in assembly under Windows, I'm using NASM, but I can't get anything working.
如何在 Windows 上不借助 C 函数编写和编译 hello world?
How to write and compile hello world without the help of C functions on Windows?
推荐答案
调用libc stdio printf
,实现int main(){ return printf(message);}
Calling libc stdio printf
, implementing 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
然后运行
nasm -fwin32 helloworld.asm
gcc helloworld.obj
a
<小时>
还有 The Clueless Newbies不使用 C 库的 Nasm 中的 Hello World 指南.然后代码看起来像这样.
There's also The Clueless Newbies Guide to Hello World in Nasm without the use of a C library. Then the code would look like this.
具有 MS-DOS 系统调用的 16 位代码:在 DOS 模拟器或支持 NTVDM 的 32 位 Windows 中工作.不能在任何 64 位 Windows 下直接"(透明地)运行,因为 x86-64 内核不能使用 vm86 模式.
16-bit code with MS-DOS system calls: works in DOS emulators or in 32-bit Windows with NTVDM support. Can't be run "directly" (transparently) under any 64-bit Windows, because an x86-64 kernel can't use vm86 mode.
org 100h
mov dx,msg
mov ah,9
int 21h
mov ah,4Ch
int 21h
msg db 'Hello, World!',0Dh,0Ah,'$'
将其构建到 .com
可执行文件中,以便将在 cs:100h
加载,所有段寄存器彼此相等(微型内存模型).
Build this into a .com
executable so it will be loaded at cs:100h
with all segment registers equal to each other (tiny memory model).
祝你好运.
这篇关于Windows下如何用汇编程序编写hello world?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!