我正在尝试用 AT&T 语法汇编编写一个测试程序,将某些字符的 ascii 值压入堆栈,然后打印它们。我的代码如下:
.text
.global main
main:
push $0
push $10
push $65
mov $4,%eax
mov $1,%ebx
mov %esp,%ecx
mov $3,%edx
int $0x80
mov $1,%eax
int $0x80
我希望这个程序做的是将 0、10 和 65 压入堆栈,以便堆栈为 65、10、0(大写 A、换行符、字符串结尾)。将 eax
和 ebx
设置为写入标准输出的值,并将 ecx
设置为堆栈指针,以便它写入堆栈中的内容,同时edx
是栈上内容的长度(即 3)。当我运行它时,绝对没有任何反应。你能帮我理解我做错了什么以及这里实际发生了什么吗?
最佳答案
每个 push
指令将 4 个字节压入堆栈。所以,这就是 int 0x80
之前堆栈的样子:
....
---------- (highest address)
| 0x00 |
----------
| 0x00 |
----------
| 0x00 |
----------
| 0x00 |
---------- <-- push $0
| 0x00 |
----------
| 0x00 |
----------
| 0x00 |
----------
| 0x0A |
---------- <-- push $10
| 0x00 |
----------
| 0x00 |
----------
| 0x00 |
----------
| 0x41 |
---------- <-- push $65 <-- ESP
但是,此时您希望堆栈看起来像:
....
----------
| 0x00 |
----------
| 0x0A |
----------
| 0x41 |
---------- <-- ESP
这可以通过将您的 push
指令替换为以下指令来实现:
push $0x0a41
即:
.text
.global main
main:
push $0x0a41
mov $4,%eax ;system call write()
mov $1,%ebx ;file descriptor (stdout)
mov %esp,%ecx ;string
mov $3,%edx ;length of the string
int $0x80
mov $1,%eax
int $0x80
在 edx
寄存器中,您已经指定了字符串的长度(即:3
),NUL 字符毫无意义(您实际上是将它发送到 stdout
)。
关于linux - 从堆栈写入字符的汇编程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48327001/