数据寄存器在 Assembly(linux、32 位、nasm)中是如何工作的?

问题描述 投票:0回答:1

最近接触逆向工程,想了解更多关于ASM的知识。因此,我查阅了一些教程,但发现了有关 dx 寄存器的令人困惑的信息。

我在这里写了一个小的“Hello World”例子:

section .text
    global _start

_start:
    mov edx, mes_len ; Store where the bytes end for the data in the data register
    mov ecx, message ; Store the reference to the message that we're going to write in the counter register
    mov ebx, 1 ; Tell tha base register that we want to push to the stdout
    mov eax, 4 ; Move 4 into the Accumulator which is the equivalent of calling sys write on linux
    int 0x80 ; int means interrupt, 0x80 means interrupt the kernel to make our system calls

    mov eax, 1 ; Move 1 into accumulator, which is sys exit on linux
    mov ebx, 0 ; exit code 0
    int 0x80

section .data
    message: db "Hello, World", 10, 0 ; 10 is the newline character and 0 is the null character
    mes_len: equ $ - message ; "$" means current position, then we subtract that from the length of our message

和线

mov edx, mes_len ; Store where the bytes end for the data in the data register

对我来说没有意义。根据 this source,edx 与累加器一起用于除法或乘法等复杂计算,但也用于 I/O。然后它声明数据寄存器保存消息的地址,但是累加器不也保存消息的地址吗?

任何帮助将不胜感激。

linux assembly x86 nasm
1个回答
2
投票

Intel寄存器是通用寄存器。由于历史/遗留原因,其中一些与特定角色相关联,但随着英特尔 CPU 几十年来的不断发展,这种情况越来越少。某些特殊用途仍然存在,例如在循环计数器的角色中使用

CX
/
ECX
,使用
DX
/
EDX
作为累加器的扩展等

中断是完全不同的东西:它们本质上是函数调用。因此,无论谁实现中断(在您的情况下,内核)都可以自由定义调用中断时每个寄存器应包含的内容,以及中断返回时每个寄存器将包含的内容。因此,他们可以随意为寄存器分配角色,这些角色与寄存器的历史角色没有任何关系。

在您链接到的文本中,我无法找到

EAX
EDX
用于包含地址的任何参考。可能没有要求
EDX
包含内存地址的 CPU 指令,(
ESI
EDI
通常扮演这样的角色,)但是中断绝对可以自由地要求
EDX
包含内存地址。

© www.soinside.com 2019 - 2024. All rights reserved.