21,497
社区成员




.code32
.section .data
output:
.asciz "hello world.\n"
.section .text
.globl main
main:
nop
pushl $output
call printf
popl %eax
pushl $0
call exit
.section .data
string:
.asciz "hello, world\n"
.section .text
.globl _start
_start:
nop
movl $string, %edi
movl $0, %eax
call printf
movl $60, %eax
movl %eax, %ebx
syscall
以上代码输出正确,链接的是64位的库
如果我把call printf前的movl $0, %eax删除的话,就会出现段错误。不知这个movl $0, %eax是个什么作用呢?它又不是作为printf的参数。
System V AMD64 ABI
The calling convention of the System V AMD64 ABI[11] is followed on Solaris, GNU/Linux, FreeBSD, Mac OS X, and other UNIX-like or POSIX-compliant operating systems. The first six integer or pointer arguments are passed in registers RDI, RSI, RDX, RCX, R8, and R9, while XMM0, XMM1, XMM2, XMM3, XMM4, XMM5, XMM6 and XMM7 are used for floating point arguments. For system calls, R10 is used instead of RCX
.section .data
string:
.asciz "hello, world\n"
.section .text
.globl _start
_start:
nop
movl $string, %edi
movl $0, %eax
call printf
movl $60, %eax
movl %eax, %ebx
syscall
以上代码输出正确,链接的是64位的库
如果我把call printf前的movl $0, %eax删除的话,就会出现段错误。不知这个movl $0, %eax是个什么作用呢?它又不是作为printf的参数。
System V AMD64 ABI
The calling convention of the System V AMD64 ABI[11] is followed on Solaris, GNU/Linux, FreeBSD, Mac OS X, and other UNIX-like or POSIX-compliant operating systems. The first six integer or pointer arguments are passed in registers RDI, RSI, RDX, RCX, R8, and R9, while XMM0, XMM1, XMM2, XMM3, XMM4, XMM5, XMM6 and XMM7 are used for floating point arguments. For system calls, R10 is used instead of RCX
[feng@www Assembly]$ cat he.s
.code32
.section .data
output:
.asciz "hello world!\n"
.section .text
.globl _start
_start:
nop
pushl $output
call printf
movl $1, %eax
movl $0, %ebx
int $0x80
编译&链接:
$ as -gstabs he.s -o he.o
$ ld -lc -I /lib64/ld-linux-x86-64.so.2 he.o
运行:
$ ./a.out
段错误 (core dumped)
反汇编:
[fe@www Assembly]$ objdump -d a.out
a.out: file format elf64-x86-64
Disassembly of section .plt:
0000000000400220 <printf@plt-0x10>:
400220: ff 35 7a 01 20 00 pushq 0x20017a(%rip) # 6003a0 <_GLOBAL_OFFSET_TABLE_+0x8>
400226: ff 25 7c 01 20 00 jmpq *0x20017c(%rip) # 6003a8 <_GLOBAL_OFFSET_TABLE_+0x10>
40022c: 0f 1f 40 00 nopl 0x0(%rax)
0000000000400230 <printf@plt>:
400230: ff 25 7a 01 20 00 jmpq *0x20017a(%rip) # 6003b0 <_GLOBAL_OFFSET_TABLE_+0x18>
400236: 68 00 00 00 00 pushq $0x0
40023b: e9 e0 ff ff ff jmpq 400220 <printf@plt-0x10>
Disassembly of section .text:
0000000000400240 <_start>:
400240: 90 nop
400241: 68 b8 03 60 00 pushq $0x6003b8
400246: e8 e5 ff ff ff callq 400230 <printf@plt>
40024b: b8 01 00 00 00 mov $0x1,%eax
400250: bb 00 00 00 00 mov $0x0,%ebx
400255: cd 80 int $0x80