linux - 需要了解基本 assembly 的指导

标签 linux assembly x86 nasm

为了掌握如何在汇编中进行一些简单的编程,我涉足了很多。我正在复习一个 hello world 程序教程,他们解释的大部分内容都是有道理的,但他们实际上是在掩盖它。我需要一些帮助来理解程序的一些不同部分。这是他们的教程示例 -

section .text
    global main     ;must be declared for linker (ld)
main:               ;tells linker entry point
    mov edx,len     ;message length
    mov ecx,msg     ;message to write
    mov ebx,1       ;file descriptor (stdout)
    mov eax,4       ;system call number (sys_write)
    int 0x80        ;call kernel

    mov eax,1       ;system call number (sys_exit)
    int 0x80        ;call kernel

section .data
msg db 'Hello, world!', 0xa  ;our dear string
len equ $ - msg              ;length of our dear string

有文本部分和数据部分。数据部分似乎包含我们为程序定义的用户信息。看起来程序的“框架”在文本部分,“肉”在数据部分......?我假设程序在编译时执行文本部分,并将数据部分中的数据填充到文本部分? bss/text/data 部分交互对我来说有点陌生。同样在 msg 和 len.... 变量的数据部分?被提到,他们后面是一些我不知道该怎么做的信息。 msg后面跟着db,什么意思?然后是文字,然后是0xa,0xa是干什么用的?另外 len 后面跟着 equ,这是否意味着等于? len 等于美元符号减去 msg 变量?美元符号是什么?一种运算符?还有文本部分中的说明, mov ebx,1 显然,或者似乎告诉程序使用 STDOUT?将 1 移动到 ebx 寄存器是设置标准输出的标准指令吗?

也许有人可以推荐更详尽的教程?我希望通过组装弄脏,并且需要自学一些......“核心基础知识”,如果你愿意的话。感谢所有的帮助!

最佳答案

[注意 - 我不知道您使用的是哪种汇编程序方言,所以我只是对这些东西的某些部分进行了一些“最佳猜测”。如果有人可以帮助澄清,那就太好了。]

It looks like the "frame" of the program is in the text section and the "meat" is in the data section... ?

文本部分包含组成程序的可执行指令。数据部分包含所述程序将要操作的数据。之所以有两个不同的部分是为了让程序加载器和操作系统能够为您提供一些保护。例如,文本部分可以加载到只读内存中,数据部分可以加载到标记为“不可执行”的内存中,因此代码不会意外(或恶意)从该区域执行。

I assume the program when compiled executes the text section with data from the data section filled into the text section?

该程序(文本部分中的说明)通常会引用符号并操作数据部分中的数据,如果这是您的要求的话。

The bss/text/data section interaction is kind of foreign to me.

BSS 部分类似于数据部分,除了它都是零初始化的。这意味着它实际上不需要占用可执行文件中的空间。程序加载器只需要在内存中创建一个大小合适的零字节 block 。您的程序没有 BSS 部分。

Also in the data section where the msg and len.... variables? are mentioned, they are followed by some information i'm not sure what to make of. msg is followed by db, what does this mean?

msglen 是某种变量,是的。 msg 是一个全局变量,指向后面的字符串 - db 表示 data byte,表示汇编程序应该只发出字面量字节跟随。 len 被设置为字符串的长度(更多内容见下文)。

Then the text, and then 0xa, what is the 0xa for?

0x0a 是 ASCII 换行符的十六进制值。

Also len is followed by equ, does this mean equals?

是的。

len equals dollarsign minus msg variable? What is the dollar sign? A sort of operator?

$ 表示“当前位置”。当汇编程序开始工作时,它会跟踪在计数器中生成了多少字节的数据和代码。所以这段代码表示:“从当前位置减去 msg 标签的位置,并将该数字存储为 len”。由于“当前位置”刚好超过字符串的末尾,因此您可以得到那里的长度。

Also the instructions in the text section, mov ebx,1 apparently, or seems to tell the program to utilize STDOUT? Is moving 1 to the ebx register a standard instruction for setting stdout?

程序正在通过int 0x80 指令进行系统调用。在此之前,它必须以操作系统期望的方式进行设置 - 在这种情况下,看起来像在 ebx1 中放置一个 1 来表示 stdout,以及其他三个寄存器 - edx 中的消息长度,ecx 中指向消息的指针,以及 eax 中的系统调用号>。我猜你使用的是 Linux - 你可以毫不费力地从谷歌查找系统调用表,我敢肯定。

Perhaps someone has a little more thorough tutorial to recommend?

抱歉,我没有想到。

关于linux - 需要了解基本 assembly 的指导,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17074663/

相关文章:

linux - 如何在汇编中编写交换函数?

assembly - x86-64 和远程调用/跳转

java - 我无法在我的 cent os 机器上运行 jmeter

linux - 在 linux 中出现异常时自动重启或正确处理 socket.io 服务器

linux - 如何使用 bash shell 处理目录中的文件

assembly - 是否可以在程序集中进行自定义中断?

assembly - 为什么有些架构没有 MOV?

c - 混合 SSE 整数/浮点 SIMD 指令时,我会受到性能损失吗

c - 不了解 PIPE 计划

c - NASM x86 使用 extern printf 打印整数