在网上找到一个打印“Hello, world”的NASM程序。请解释为什么_start在程序开始时需要一个标签,如果没有它一切都可以工作?(如果我没记错的话)
global _start
section .text
_start: mov rax, 1 ; system call for write
mov rdi, 1 ; file handle 1 is stdout
mov rsi, message ; address of string to output
mov rdx, 13 ; number of bytes
syscall ; invoke operating system to do the write
mov rax, 60 ; system call for exit
xor rdi, rdi ; exit code 0
syscall ; invoke operating system to exit
section .data
message: db "Hello, World", 10 ; note the newline at the end
让文件名
test.asm,去掉全局标签_start:我们收集:
我们看到链接器正在寻找标签
_start,但没有找到它,所以它选择了第一个合适的地址,即代码部分的开头.text。在反汇编器下是这样的:也就是说,原则上,程序将构建(甚至在这种情况下工作),但入口点将是链接器选择的地址。
实际上,当需要标签时
_start:在任何情况下,最好不要希望链接器正确地猜测到它想要什么,而是明确指定入口点。