以下链接解释了UNIX(BSD风格)和Linux的x86-32系统调用约定:
但是UNIX和Linux上的x86-64系统调用约定是什么?
我遇到了一个有趣的问题.我忘了我正在使用64位机器和操作系统并写了一个32位汇编代码.我不知道如何编写64位代码.
这是Linux上Gnu Assembler(AT&T语法)的x86 32位汇编代码.
//hello.S
#include <asm/unistd.h>
#include <syscall.h>
#define STDOUT 1
.data
hellostr:
.ascii "hello wolrd\n";
helloend:
.text
.globl _start
_start:
movl $(SYS_write) , %eax //ssize_t write(int fd, const void *buf, size_t count);
movl $(STDOUT) , %ebx
movl $hellostr , %ecx
movl $(helloend-hellostr) , %edx
int $0x80
movl $(SYS_exit), %eax //void _exit(int status);
xorl %ebx, %ebx
int $0x80
ret
Run Code Online (Sandbox Code Playgroud)
现在,这个代码应该在32位处理器和32位操作系统上正常运行吗?我们知道64位处理器向后兼容32位处理器.所以,这也不是问题.问题出现是因为64位操作系统和32位操作系统中的系统调用和调用机制不同.我不知道为什么但是他们改变了32位linux和64位linux之间的系统调用号码.
asm/unistd_32.h定义:
#define __NR_write 4
#define __NR_exit 1
Run Code Online (Sandbox Code Playgroud)
asm/unistd_64.h定义:
#define __NR_write 1
#define __NR_exit 60
Run Code Online (Sandbox Code Playgroud)
无论如何使用宏而不是直接数字都可以获得回报.它确保正确的系统呼叫号码.
当我组装和链接并运行程序时.
$cpp hello.S hello.s …Run Code Online (Sandbox Code Playgroud) 有谁知道在汇编语言中为Linux系统调用找到汇总表或备忘单的位置?我通过int 0x80指令调用Linux系统调用,我需要不时地引用哪个寄存器包含什么值.
谢谢.
我知道这int 0x80会在 linux 中造成中断。但是,我不明白这段代码是如何工作的。它会返回一些东西吗?
代表什么$ - msg?
global _start
section .data
msg db "Hello, world!", 0x0a
len equ $ - msg
section .text
_start:
mov eax, 4
mov ebx, 1
mov ecx, msg
mov edx, len
int 0x80 ;What is this?
mov eax, 1
mov ebx, 0
int 0x80 ;and what is this?
Run Code Online (Sandbox Code Playgroud)