Linux汇编语言程序设计之32位与64位汇编系统调用

Linux系统中64位汇编和32位汇编的系统调用主要有以下不同:
1、系统调用号不同
32位汇编语言:定义:/usr/include/asm/unistd_32.h

#define __NR_read 3
#define __NR_write 4
#define __NR_open 5
#define __NR_close 6
#define __NR_exit 1

64位汇编语言:定义:/usr/include/asm/unistd_64.h
#define __NR_read 0
#define __NR_write 1
#define __NR_open 2
#define __NR_close 3
#define __NR_exit 60

2、系统调用所使用的寄存器不同
32位汇编语言使用eax传递系统调用号,使用ebx、ecx、edx传递前三个参数;
64位汇编语言使用rax传递系统调用号,使用rdi、rsi、rdx传递前三个参数。

3、系统调用方式不一样
32位汇编语言:int $0x80;
64位汇编语言:syscall。

Linux 32位汇编语言系统调用示例:

.section .data
msg:
	msg:	.string "Hello World\n"
	len = . - msg

.section .text
.globl _start
_start:
	
	# call sys_write
	movl $4, %eax
	movl $1, %ebx
	movl $msg, %ecx
	movl $len, %edx
	int  $0x80
	
	# call sys_exit
	movl $1,%eax
	movl $0,%ebx
	int  $0x80

编译运行:

[ycxie@localhost Workspace]$ as -o demo_v32.o demo_v32.s
[ycxie@localhost Workspace]$ ld -o demo_v32 demo_v32.o
[ycxie@localhost Workspace]$ ./demo_v32
Hello World

Linux 64位汇编语言系统调用示例:

.section .rodata
	msg:	.string "Hello World\n"
	len = . - msg

.section .text
.global _start
_start:
	
	# call sys_write
	movq $1, %rax
	movq $1, %rdi
	movq $msg, %rsi
	movq $len, %rdx
	syscall
	
	# call sys_exit
	movq $60, %rax
	movq $0, %rdi
	syscall

编译运行:

[ycxie@localhost Workspace]$ as -o demo_v64.o demo_v64.s
[ycxie@localhost Workspace]$ ld -o demo_v64 demo_v64.o
[ycxie@localhost Workspace]$ ./demo_v64
Hello World

Leave a Reply

Your email address will not be published. Required fields are marked *