学习这一点的简单方法之一是让 C 编译器为您生成一个汇编程序模板。我们从 C 中的等价物开始:
main()
{
write(1, "hi!\n", 4);
return 0;
}
然后我们让编译器为我们创建汇编代码:
cc -S hello.c
生成的汇编代码应该可以进行汇编(并与 C 库链接)。
.file "hello.c"
.section .rodata
.LC0:
.string "hi!\n"
.text
.p2align 4,,15
.globl main
.type main, @function
main:
.LFB2:
pushq %rbp
.LCFI0:
movq %rsp, %rbp
.LCFI1:
movl $4, %edx
movl $.LC0, %esi
movl $1, %edi
movl $0, %eax
call write
movl $0, %eax
leave
ret
.LFE2:
.size main, .-main
.section .eh_frame,"a",@progbits
.Lframe1:
.long .LECIE1-.LSCIE1
.LSCIE1:
.long 0x0
.byte 0x1
.string "zR"
.uleb128 0x1
.sleb128 -8
.byte 0x10
.uleb128 0x1
.byte 0x3
.byte 0xc
.uleb128 0x7
.uleb128 0x8
.byte 0x90
.uleb128 0x1
.align 8
.LECIE1:
.LSFDE1:
.long .LEFDE1-.LASFDE1
.LASFDE1:
.long .LASFDE1-.Lframe1
.long .LFB2
.long .LFE2-.LFB2
.uleb128 0x0
.byte 0x4
.long .LCFI0-.LFB2
.byte 0xe
.uleb128 0x10
.byte 0x86
.uleb128 0x2
.byte 0x4
.long .LCFI1-.LCFI0
.byte 0xd
.uleb128 0x6
.align 8
.LEFDE1:
.ident "GCC: (GNU) 4.2.1 20070831 patched [FreeBSD]"
.section .note.GNU-stack,"",@progbits
目前尚不清楚(对我而言)您真正需要多少超出 .size 指令的尾随内容,但这应该让您顺利上路。