Linux汇编-笔记-NASM

打印Hello world

section .rodata
    hello_world db "hello world", 0x0a ;0x0a代表结束符    
    hello_world_len equ $ - hello_world ;长度,需要一个buffer
section .text
    global _start
    _start:
        mov eax, 4  ;4代表sys_write
        mov ebx, 1  ;1代表std_out, 0代表stdin 2代表stderr
        mov ecx, hello_world
        mov edx, hello_world_len
        int 0x80  ;代表syswrite这个调用终止        
        mov eax, 1 ;代表sys_exit
        mov ebx, 0 ; 同上面的ebx
        int 0x80 ;调用终止,程序退出

GNU的ld链接器默认入口符号是_start并且要将其设置为全局符号,用其他符号在链接时,使用ld -e 指定。

这里没有用到寄存器,因为是直接通过系统内核将hello world打印出来,没有调用系统函数printf,其中,用到了四个寄存器传参,他们的赋值顺序可以颠倒。

Linux系统调用号:

编译链接方法

这里是采用nasm+intel语法写的,因此使用nasm编译,ld链接

因32位比较特殊,64位比较简单。这里以32位举例

编译: nasm -f elf32 xxx.asm

链接: ld -m elf_i386 xxx.o -o xxx.out

Reference

在 Linux 上用 x86-64 汇编语言写 Hello World 程序(上) | 郭帅的个人博客

linux汇编知识总结(GAS和NASM汇编)_gas 汇编_鱼日天的博客-CSDN博客

GitHub-https://github.com/cirosantilli/arm-assembly-cheat