数据寄存器EAX,EBX,ECX和EDX是否可互换 [英] Are the data registers EAX, EBX, ECX and EDX interchangeable

查看:384
本文介绍了数据寄存器EAX,EBX,ECX和EDX是否可互换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在进入汇编语言编程的世界.我试图了解以下内容: https://www.tutorialspoint.com/assembly_programming

I'm stepping into the world of Assembly Language Programming. I'm trying to understand everything found on: https://www.tutorialspoint.com/assembly_programming

我遇到了以下代码:

section .text
     global _start      ;must be declared for using gcc
_start: ;tell linker entry point

;This part works fine.
;mov    edx, len    ;message length
;mov    ecx, msg    ;message to write

;This does not work because I interchanged edx and ecx.
mov ecx, len    ;message length
mov edx, 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, Kaunda!',0xa    ;our dear string
len equ $ - msg         ;length of our dear string

我可以选择将变量'len'或'msg'放入任何数据寄存器(EAX,EBX,ECX和EDX)中.

Can I choose to put the variable 'len' or 'msg' in any of the data registers (EAX, EBX, ECX and EDX).

订购字词:

为什么将变量len的内容传递给EDX寄存器而不是ECX或任何其他寄存器?是否有明确的指南来知道哪个变量进入哪个寄存器?

WHY is the content of variable len transferfed into EDX register and not ECX or any other register? Is there a clear guideline to know which variable goes into which register?

我已经阅读了有关每个寄存器EAX,EBX,ECX和EDX的功能的信息,但我仍不清楚.它们的功能与我相似.

I've read about about the functions of each of the registers EAX, EBX, ECX and EDX but I'm still not clear. Their functions looks similar to me.

更新:我正在从 https://www.tutorialspoint.com/compile_assembly_online运行代码. php

我认为这是Linux环境

I think that is Linux environment

推荐答案

发出int 0x80时,程序被中断,内核检查寄存器的状态.它从eax获取您要执行的系统调用的编号,并从其他寄存器获取其他数据.例如,对于write系统调用,它从ebx抓取文件描述符,指向要从ecx写入的缓冲区的指针以及要从edx写入的字节数.内核不知道您的意图是什么,它只是愚蠢地获取了寄存器中的内容,因此对您使用哪个寄存器很重要.

When you issue an int 0x80, your program is interrupted and the kernel inspects the state of the registers. From eax it grabs the number of the system call you want to execute and from the other registers it grabs additional data. For example, for the write system call, it grabs the file descriptor from ebx, a pointer to the buffer you want to write from ecx and the number of bytes you want to write from edx. The kernel does not know what your intentions are, it just stupidly grabs whatever is in the registers, so it does matters which registers you use.

但是,一般而言,使用哪个寄存器的值并不重要.在您自己的代码中,只要不与其他人的代码交互,您就可以随意使用几乎所有寄存器(除了诸如esp之类的寄存器)用于您想要的目的.

However, in general, it does not matter what registers you use for what values. In your own code, you are free to use almost all registers (except for such as registers as esp) for whatever purpose you want as long as you don't interact with other peoples code.

使用哪个寄存器的唯一重要地方是当您想与其他人编写的代码进行交互时,例如,在调用函数或操作系统时,或者在编写将由其他人调用的函数时.在这种情况下,您必须将相关寄存器设置为预期值,或者可能保留其内容.

The only places where it matters which registers are used is when you want to interact with code written by other people, such as when calling functions or the operating system or when writing functions that are going to be called by others. In such cases, you have to set the relevant registers to the expected values or possibly preserve their contents.

例如,当您编写由其他人的代码调用的函数时,期望您在eax中返回函数的结果并保留寄存器ebxesiedi的内容,espebp.如果您出于个人目的使用这些寄存器,则必须先将它们的值保存在某个位置(例如,在堆栈上),然后将它们恢复为原始值,然后再返回.

For example, when you write a function called by other people's code, it is expected that you return the result of your function in eax and preserve the contents of the registers ebx, esi, edi, esp, and ebp. If you use these registers for your own purposes, you have to first save their values somewhere (e.g. on the stack) and restore them to their original values before returning.

还有一些指令期望操作数位于某些寄存器中(例如stosidiv),但是对于大多数指令,您可以自由选择所需的任何寄存器.

There are also some instructions that expect there operands to be in certain registers (such as stos or idiv), but for most instructions you are free to choose whatever registers you want.

在重要的情况下,在 Application Binary Interface(ABI)文档中记录了用于什么目的的寄存器规则.可以将本文档理解为所有程序员之间的协议,涉及在调用函数或操作系统时期望在哪个寄存器中使用哪个数据.严格遵守ABI是使您的代码在由他人的代码调用/调用时正确工作的必要条件.

In the cases where it matters, the rules which registers are used for what purpose are written down in an Application Binary Interface (ABI) document. This document can be understood as an agreement between all programmers as to which data to expect in which registers when calling functions or the operating system. Strict adherence to the ABI is necessary to make your code work correctly when calling/called by other people's code.

在i386(您当前正在为其编程的体系结构)上,Linux使用 i386 SysV ABI ..通常,每个操作系统对每种体系结构都使用不同的ABI,因此在为新的操作系统或体系结构编写代码之前,请务必签出相关的ABI.

On i386, the architecture you are currently programming for, Linux uses the i386 SysV ABI.. Generally, each operating system uses a different ABI for each architecture, so before writing code for a new operating system or architecture be sure to check out the relevant ABI.

这篇关于数据寄存器EAX,EBX,ECX和EDX是否可互换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆