【发布时间】:2020-10-15 00:56:36
【问题描述】:
使用第 7 代 i5 处理器,nasm,ld。
使用通用寄存器和系统调用编写,程序在 Ubuntu 上运行。一个例子是一个简单的 hello world 程序。
global _start
section .text
_start:
mov eax, 0x4 ; write(int fd, char *buf, int len)
mov ebx, 0x1 ; fd
mov ecx, message ; *buf
mov edx, message_length ; len
int 0x80 ; syscall
mov eax, 0x1 ; exit(int status)
mov ebx, 0x0 ; status
int 0x80 ; syscall
section .data
message db "hello world", 0xA
message_length equ $-message
nasm -f elf64 -o hello_world.o hello_world.s
ld hello_world.o -o hello_world
./hello_world
输出:hello_world
但是,使用intel系统指令编写的程序不起作用。
global _start
section .text
_start:
CLI
HLT
nasm -f elf64 halt.s -o halt.o
ld halt.o -o halt
./halt
输出:Segmentation fault (core dumped)
- 是什么阻止了这段代码以这种方式编译和运行?
- 如何编译和运行这段代码?
【问题讨论】:
-
操作系统阻止它。内核的工作是阻止您做可能干扰系统或其他进程操作的事情,除非您被授权这样做。要运行
cli和hlt,您需要是root 用户并拥有io privileges
标签: linux assembly x86 segmentation-fault