简单程序的装配问题
我正在尝试从头开始学习汇编。我已经阅读了相当多的内容,但即使是我在参考书中找到的以下简单程序也让我难住了:
section .data
msg db "Hello!", 0xa
len equ $ - msg
section .text
global _start
_start:
move edx, len
move ecx, msg
move ebx, 1
move eax, 4
int 0x80
move ebx, 0
move eax, 1
int 0x80
现在显然这应该打印“Hello”。 但我什至不知道任何阶段发生了什么。 前两个阶段将消息长度和消息放入两个寄存器中,这些寄存器不再使用。我不明白为什么。
我不知道为什么需要四个不同的寄存器。
I am trying to learn assembly from scratch. I have been reading up quite a bit, but even the following simple program I found in a reference book has me stumped:
section .data
msg db "Hello!", 0xa
len equ $ - msg
section .text
global _start
_start:
move edx, len
move ecx, msg
move ebx, 1
move eax, 4
int 0x80
move ebx, 0
move eax, 1
int 0x80
Now apparently this is supposed to print "Hello".
But I don't even know whats happening at any of the stages.
The first two stages put the message length and messgae in two registers, which are never used again. I don't understand why.
I don't know why four different registers are needed.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
int 0x80
是某些(a) 类 UNIX 操作系统中用于进行系统调用的机制。对于这些调用,寄存器用于特定值。从
syscalls
文件中:您可以看到数字 4 是
write
调用,需要其他三个参数。第 1 号是exit
并且只需要返回代码。进行调用时,
eax
是您正在进行的系统调用,而ebx
、ecx
和edx
是三个参数(假设它们都需要 - 例如exit
仅需要一个)。因此,您可以对代码进行如下注释:
(a) Linux 的更高版本引入了一个新接口,它可以根据提供最佳速度的方式使用不同的方法。例如,如果您使用
sysenter
而不是int 0x80
,某些 Intel 芯片会更快。int 0x80
is a mechanism in some(a) UNIX-like operating systems for making system calls.For these calls, the registers are used for specific values. From the
syscalls
file:you can see that number 4 is the
write
call and needs three other parameters. Number 1 isexit
and needs only the return code.When making the call,
eax
is the syscall that you're making whileebx
,ecx
andedx
are the three parameters (assuming they're all needed -exit
for example only needs one).So, you could comment the code as follows:
(a) Later versions of Linux introduced a new interface which can use different methods based on which provides the best speed. For example, some Intel chips are much faster if you use
sysenter
rather thanint 0x80
.IIRC
int 0x80
指令用于通过中断向量调用系统调用。在您的示例中,ebx
和eax
中的值用于指定您要调用的系统调用(可能是标准输出上的打印操作)。按照惯例,系统调用知道
edx
和ecx
应该包含要打印的内容。IIRC the
int 0x80
instruction is used to invoke a syscall by using the interrupt vector. In your example the values inebx
andeax
are used to specify which syscall you are gonna call (probably the print operation on stdout).The syscall knows by convenction that
edx
andecx
should contain what is gonna be printed.在许多系统上,int 80h 是系统调用门。系统调用号位于
eax
中。ebx
、ecx
和edx
包含其他参数:On many systems, int 80h is the system call gate. The syscall number is in
eax
.ebx
,ecx
andedx
contain additional parameters:当您调用系统调用(助记符“int”)时,会生成系统中断。它有点“跳转”到系统函数,在这种情况下,打印输出(取决于 eax)。
该中断使用所有这些寄存器来知道要做什么。中断读取 eax,检查您想要什么功能并使用其他寄存器来执行此操作。
eax 是函数号,4 表示 sys_write,它将字符串写入流/文件描述符。
现在它知道您想向某个地方写入一些内容,然后它使用其他寄存器来存储这些信息。
对于 eax = 4 和 int 0x80,这是其他寄存器的含义:
ebx = 输出 (1 = stdout)
ecx = 字符串的地址
edx = 字符串长度
您可以阅读以下内容:
http://www. intel.com/Assets/ja_JP/PDF/manual/253665.pdf
第 6.4 节
它有一些关于中断和异常的内容。
并且您可以开始编写 Intel 80x86 汇编代码,这更简单,更容易理解,这里有一些链接:
助记符/代码表备忘单:
http://www.jegerlehner.ch/intel/
一些介绍网站:
http://mysite.du.edu/~etuttle/math/8086.htm
http://www.malware.org/teaching/ assembly.htm
When you call a system call, the 'int' mnemonic, a system interruption is generated. It kinda "jumps" to a system function, which, in this case, prints output (depends on eax).
This interruption uses all those registers to know what to do. The interrupt reads eax, check what function you want and uses the other registers to do so.
eax is the function number, 4 means sys_write, which writes a string to a stream/file descriptor.
Now it knows you want to write something to some place, then it uses the other registers to those informations.
for eax = 4 and int 0x80 this is the meaning for the other registers:
ebx = output (1 = stdout)
ecx = address of the string
edx = length of the string
You can read this:
http://www.intel.com/Assets/ja_JP/PDF/manual/253665.pdf
Section 6.4
It has some stuff about Interruptins and Exceptions.
And you can start writing Intel 80x86 assembly code, which is simpler and easier to understand, here are some links:
Mnemonics/Code tables cheatsheet:
http://www.jegerlehner.ch/intel/
Some introduction sites:
http://mysite.du.edu/~etuttle/math/8086.htm
http://www.malware.org/teaching/assembly.htm