简体   繁体   English

MOV,LEA和OFFSET之间的差异

[英]Differences between MOV, LEA and OFFSET

Sure MOV "moves" (copies actually) something, but how ? 当然, MOV “移动”(实际上是复制)某些东西,但是如何? Does it put the actual value from the source into the destination or puts some kind of address. 它是将来自源的实际值放入目的地还是放入某种地址。

This question came to me when I saw in Jeff Duntemann introductive assembly book that he is using interrupt 80h of Linux like this: 当我在杰夫·邓特曼(Jeff Duntemann)的介绍性汇编书中看到他正在使用Linux的中断80h时,这个问题浮现出来:

mov eax,4    ; Specify sys_write call
mov ebx,1    ; Specify File Descriptor 1: Standard output
mov ecx,Buff ; Pass address of the character to write
mov edx,1    ; Pass number of chars to write
int 80h      ; Call sys_write

I've had little practice with TASM before I started reading , but knew about the instruction LEA.So when I saw: 在开始阅读之前,我几乎没有使用TASM进行任何练习,但是了解LEA的说明。因此,当我看到:

mov ecx,Buff ; Pass address of the character to write

It blew me away since I used LEA (Load Effective Address) or OFFSET to put addresses into registers and he is using MOV . 由于我使用LEA (负载有效地址)或OFFSET将地址放入寄存器,而他正在使用MOV这让我MOV

Are both forms correct? 两种形式都正确吗? He is using NASM, though, so is it because of the Assembler? 但是,他正在使用NASM,是因为汇编程序吗? I am very confused right now, since I was used to see that MOV puts the value not an address. 我现在很困惑,因为我习惯于看MOV将值放在地址之外。

The MOV (move) instruction transfer data between memory and registers or between registers. MOV (移动)指令在存储器和寄存器之间或寄存器之间传输数据。 The MOV instruction performs basic load data and store data operations between memory and the processor's registers and data movement operations between registers.The MOV instruction cannot move data from one memory location to another or from one segment register to another segment register. MOV指令执行基本的加载数据并在内存和处理器的寄存器之间存储数据操作以及在寄存器之间的数据移动操作MOV指令不能将数据从一个存储位置移动到另一个或从一个段寄存器移动到另一个段寄存器。 Memory-to-memory moves are performed with the MOVS (string move) instruction. 内存到内存的移动是通过MOVS (字符串移动)指令执行的。

在此处输入图片说明

The LEA (load effective address) instruction computes the effective address in memory (offset within a segment) of a source operand and places it in a general-purpose register. LEA (加载有效地址)指令计算源操作数的内存(段中的偏移量)中的有效地址,并将其放置在通用寄存器中。 This instruction can interpret any of the processor's addressing modes and can perform any indexing or scaling that may be needed. 该指令可以解释任何处理器的寻址模式,并可以执行可能需要的任何索引或缩放。

Intel 64 and IA-32 architectures Software Developer's manual 英特尔64和IA-32架构软件开发人员手册

It's also highly useful as a 3-operand non-destructive add , doing eg ecx = eax + edx*4 - 15 作为三操作数无损添加 ,它也非常有用,例如ecx = eax + edx*4 - 15


NASM avoids this undesirable situation by having a much simpler syntax for memory references. NASM通过使用更简单的内存引用语法避免了这种不良情况。 The rule is simply that any access to the contents of a memory location requires square brackets around the address, and any access to the address of a variable doesn't. 规则很简单,对存储位置内容的任何访问都需要在地址周围加上方括号,而对变量地址的任何访问都不需要。 So an instruction of the form mov ax, foo will always refer to a compile−time constant, whether it's an EQU or the address of a variable; 因此,形式为mov ax, foo的指令将始终引用编译时常量,无论它是EQU还是变量的地址; and to access the contents of the variable bar, you must code mov ax, [bar] . 要访问变量栏的内容,必须对mov ax, [bar]编码。 This also means that NASM has no need for MASM’s OFFSET keyword, since the MASM code mov ax, offset bar means exactly the same thing as NASM’s mov ax,bar . 这也意味着NASM不需要MASM的 OFFSET关键字,因为MASM代码mov ax, offset bar含义与NASM的 mov ax,bar完全相同。

Nasm documentation nasm文档


From all the above you may see that that there are different ways to get the address of a variable in asm and these ways may and will differ in different assembly languages. 从以上所有内容中,您可能会发现,有多种方法可以在asm中获取变量的地址,并且这些方法在不同的汇编语言中可能也将有所不同。 To find an answer to your question it is good idea to check the documentation (assembly languages are usually well documented). 要找到问题的答案,最好检查一下文档(汇编语言通常都记录在案)。

You should always remember in programming there are always several ways one particular problem can be solved. 您应该永远记住,在编程中总有几种方法可以解决一个特定的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM