为什么使用sys_write的尝试没有做任何事情? [英] Why doesn't this attempt at using sys_write do anything?
问题描述
这里是:
.SECTION .data
msg: .string "AAAA"
.SECTION .text
.globl _start
_start:
mov $1, %rax
mov $1, %rdi
mov msg, %rsi
mov $4, %rdx
syscall
此代码不仅不会不是 segfault,而且不会输出任何内容.
根据我所读的内容,程序应调用sys_exit,否则它将出现段错误,但这不会发生.
Not only does this code not segfault, it also outputs nothing.
According to what I've read, a program should call sys_exit, or it would segfault, but this does not happen.
推荐答案
mov msg, %rsi
此指令会将"msg"处的数据解释为64位值,并将该值加载到寄存器rsi
中.该指令不会将"msg"的地址加载到寄存器rsi
中.可以通过以下方式完成(请注意$
):
This instruction will interpret the data at "msg" as 64-bit value and load that value into the register rsi
. The instruction does NOT load the address of "msg" into register rsi
. This could be done by (note the $
):
mov $msg, %rsi
根据我所读的内容,程序应调用sys_exit,否则它将出现段错误,但这不会发生.
According to what I've read, a program should call sys_exit, or it would segfault, but this does not happen.
您必须了解为什么发生段错误:
You have to be aware why the segfault happens:
CPU不知道程序的结尾"在哪里. CPU也无法区分指令和数据.
The CPU does not know where the "end" of your program is. The CPU can also not distinguish between instructions and data.
例如,字节0x8A, 0x07
可能表示mov (%rdi),%al
,或者它们表示数字1930
-CPU不知道.
The bytes 0x8A, 0x07
for example may mean mov (%rdi),%al
or they may represent the number 1930
- the CPU does not know.
到达程序末尾时,CPU将尝试在程序后 之后读取字节并将其解释为指令.
When reaching the end of your program the CPU will try to read the bytes after your program and interpret them as instruction.
现在可能出现三种情况:
Now three scenarios are possible:
-
由于x86系统上的RAM以4096字节的块进行管理.因此,根据程序的长度,您的程序最多可以跟随4095字节的未使用" RAM.
As RAM is managed in 4096 byte blocks on x86 systems. So depending on the length of your program up to 4095 bytes of "unused" RAM are following your program.
CPU将RAM中的(随机)字节解释为(汇编程序)指令并执行这些指令.
The CPU will interpret the (random) bytes in the RAM as (assembler) instructions and execute these instructions.
到达4096字节块的末尾时,发生段错误.
When reaching the end of the 4096 byte block a segfault is happening.
4095个字节包含导致段错误的指令(在到达块的末尾之前).
The 4095 bytes contain an instruction that causes a segfault (before the end of the block is reached).
4095个字节代表导致程序退出而没有任何异常或无穷循环的指令.
The 4095 bytes represent instructions which cause the program to exit without any exception or an endless loop.
所以也许您的情况是第三种情况.
So maybe in your case the 3rd situation is the case.
这篇关于为什么使用sys_write的尝试没有做任何事情?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!