简单程序的装配问题

发布于 2024-09-30 22:00:28 字数 389 浏览 0 评论 0原文

我正在尝试从头开始学习汇编。我已经阅读了相当多的内容,但即使是我在参考书中找到的以下简单程序也让我难住了:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

ま昔日黯然 2024-10-07 22:00:28

int 0x80 是某些(a) 类 UNIX 操作系统中用于进行系统调用的机制。

对于这些调用,寄存器用于特定值。从 syscalls 文件中:

0 STD NOHIDE { int nosys(void); } syscall nosys_args int
1 STD NOHIDE { void exit(int rval); } exit rexit_args void
2 STD POSIX  { int fork(void); }
3 STD POSIX  { ssize_t read(int fd, void *buf, size_t nbyte); }
4 STD POSIX  { ssize_t write(int fd, const void *buf, size_t nbyte); }

您可以看到数字 4 是 write 调用,需要其他三个参数。第 1 号是 exit 并且只需要返回代码。

进行调用时,eax 是您正在进行的系统调用,而 ebxecxedx 是三个参数(假设它们都需要 - 例如 exit 仅需要一个)。

因此,您可以对代码进行如下注释:

move edx, len   ; length of message (nbyte).
move ecx, msg   ; message to print (buf).
move ebx, 1     ; file descriptor 1 (stdout).
move eax, 4     ; write syscall.
int  0x80       ; do it.

move ebx, 0     ; exit code (rval).
move eax, 1     ; exit syscall.
int 0x80        ; do it.

(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:

0 STD NOHIDE { int nosys(void); } syscall nosys_args int
1 STD NOHIDE { void exit(int rval); } exit rexit_args void
2 STD POSIX  { int fork(void); }
3 STD POSIX  { ssize_t read(int fd, void *buf, size_t nbyte); }
4 STD POSIX  { ssize_t write(int fd, const void *buf, size_t nbyte); }

you can see that number 4 is the write call and needs three other parameters. Number 1 is exit and needs only the return code.

When making the call, eax is the syscall that you're making while ebx, ecx and edx are the three parameters (assuming they're all needed - exit for example only needs one).

So, you could comment the code as follows:

move edx, len   ; length of message (nbyte).
move ecx, msg   ; message to print (buf).
move ebx, 1     ; file descriptor 1 (stdout).
move eax, 4     ; write syscall.
int  0x80       ; do it.

move ebx, 0     ; exit code (rval).
move eax, 1     ; exit syscall.
int 0x80        ; do it.

(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 than int 0x80.

林空鹿饮溪 2024-10-07 22:00:28

IIRC int 0x80 指令用于通过中断向量调用系统调用。在您的示例中,ebxeax 中的值用于指定您要调用的系统调用(可能是标准输出上的打印操作)。

按照惯例,系统调用知道 edxecx 应该包含要打印的内容。

IIRC the int 0x80 instruction is used to invoke a syscall by using the interrupt vector. In your example the values in ebx and eax are used to specify which syscall you are gonna call (probably the print operation on stdout).

The syscall knows by convenction that edx and ecx should contain what is gonna be printed.

乙白 2024-10-07 22:00:28

在许多系统上,int 80h系统调用门。系统调用号位于eax中。 ebxecxedx 包含其他参数:

move edx, len
move ecx, msg
move ebx, 1    ; fd 1 is stdout
move eax, 4    ; syscall 4 is write
int  0x80      ; write(1, msg, len)
move ebx, 0
move eax, 1    ; syscall 1 is exit
int 0x80       ; exit(0)

On many systems, int 80h is the system call gate. The syscall number is in eax. ebx, ecx and edx contain additional parameters:

move edx, len
move ecx, msg
move ebx, 1    ; fd 1 is stdout
move eax, 4    ; syscall 4 is write
int  0x80      ; write(1, msg, len)
move ebx, 0
move eax, 1    ; syscall 1 is exit
int 0x80       ; exit(0)
天暗了我发光 2024-10-07 22:00:28

当您调用系统调用(助记符“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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文