我正在学习汇编语言,使用以下“Hello World”程序
section .text
global _start ;must be declared for linker (ld)
_start: ;tells linker entry point
mov edx,len ;message length
mov ecx,msg ;message to write
mov ebx,1 ;file descriptor (stdout)
mov eax,4 ;system call number (sys_write)
int 0x80 ;call kernel
mov eax,1 ;system call number (sys_exit)
int 0x80 ;call kernel
section .data
msg db 'Hello, world!', 0xa ;our string
len equ $ - msg ;length of our string
我最初的问题是字符串的长度指的是什么。它是指字符数还是内存中的长度(字节数)?为了检查这一点,我想要打印变量len。我该怎么做呢?我天真地尝试定义了一个新变量。
len2 equ $ - len
然后改为运行
mov edx,len2 ;message length
mov ecx,len ;message to write
mov ebx,1 ;file descriptor (stdout)
mov eax,4 ;system call number (sys_write)
int 0x80 ;call kernel
我尝试打印长度,但没有输出结果。我该如何打印len表示的数字?
stdout
写入字符。一个数字是一个十进制值,不是一串可以直接写入的字符,您必须将整数或无符号值转换为其字符表示形式,然后将其写入stdout
。 - David C. Rankinprintf
之类的东西来打印数字。printf
将为您执行数字到字符串的转换,否则您必须自己编写代码。 - Michael PetchSYS_write
尝试将其用作指针,因此返回-EFAULT
。(系统调用使用错误指针会返回错误,而不是引发 SIGSEGV。)请使用调试器和/或strace
。请参阅 x86 标签 wiki 底部。 - Peter Cordes