"的Hello World"不使用C printf函数 [英] "Hello World" function without using C printf
问题描述
更新时间:
这是我的第二个一天 NASM
工作。在充分理解这一点。
It's my second day working with NASM
. After thoroughly understanding this
section .programFlow
global _start
_start:
mov edx,len
mov ecx,msg
mov ebx,0x1 ;select STDOUT stream
mov eax,0x4 ;select SYS_WRITE call
int 0x80 ;invoke SYS_WRITE
mov ebx,0x0 ;select EXIT_CODE_0
mov eax,0x1 ;select SYS_EXIT call
int 0x80 ;invoke SYS_EXIT
section .programData
msg: db "Hello World!",0xa
len: equ $ - msg
我想换一个汇编函数里面这个东西。所有(或大部分)在网络上的例子都使用的extern
和调用的的printf
函数 C
(见code以下) - 我不希望出现这种情况。我想学习,而无需使用创建组装一个Hello World功能 C
的printf
(甚至其他外部函数呼叫)。
I wanted to wrap this stuff inside an assembly function. All (or most of) the examples on the web are using extern
and calling printf
function of C
(see code below) - and I don't want that. I want to learn to create a "Hello World" function in assembly without using C
printf
(or even other external function calls).
global _main
extern _printf
section .text
_main:
push message
call _printf
add esp, 4
ret
section .data
message: db "Hello, World", 10, 0
更新
我在练组装用于Linux,但因为我没有自己的Linux机器,我在这里运行我的程序集code的 compile_assembly_online 。
I am practicing assembly for Linux, but since I do not own a Linux box, I am running my assembly code here compile_assembly_online.
推荐答案
0x80的INT
将无法在Windows或DOS下工作,只是因为这是一个Linux的事情。所以这是有改变的第一个的事情。
int 0x80
won't work in Windows or DOS simply because it's a Linux thing. So that's the first thing that has to change.
在做Windows下,在某些时候你会需要调用Windows API函数,如(在这种情况下)的 WriteConsole()
。作为希望的绕过C库。
In terms of doing it under Windows, at some point you're going to need to call a Windows API function, such as (in this case) WriteConsole()
. That's bypassing the C library as desired.
它的确实的使用OS做繁重的获得输出画面但那是一样的 INT 0x80的
,是可能需要无论是Linux的,Windows或DOS。
It does use the OS to do the heavy lifting in getting output to the "screen" but that's the same as int 0x80
and is probably required whether it's Linux, Windows or DOS.
如果它的是的正版DOS,开始您的最好的地方就是优秀拉尔夫布朗的中断列表,特别 INT21 / FN9 。
If it is genuine DOS, your best place to start is the excellent Ralf Brown's Interrupt List, specifically Int21/Fn9.
这篇关于"的Hello World"不使用C printf函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!