汇编语言中的寻址模式 (IA-32 NASM) [英] Addressing Modes in Assembly Language (IA-32 NASM)

查看:11
本文介绍了汇编语言中的寻址模式 (IA-32 NASM)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于这方面的网络资源很少,为了将来搜索的方便,我将首先列出 IA-32 汇编语言 (NASM) 的地址模式,然后跟进一个快速问题.

  1. 注册地址
    • mov eax, ebx:将 ebx 中的内容复制到 eax 中
    • mov esi, var:将 var 的地址(比如 0x0040120e)复制到 esi 中
  2. 立即寻址(第二个操作数是立即数)
    • mov bx, 20: 16位寄存器bx获取实际值20
  3. 直接内存寻址(通过指定地址直接从内存中加载)
    • mov ax, [1000h]:将一个 2 字节的对象从地址 4096(十六进制为 0x1000)的字节加载到名为ax"的 16 位寄存器中
    • mov [1000h], ax:地址1000h的内存获取ax的值
  4. 直接偏移寻址(同3,只是用算术修改地址)
    • 移动,[byte_tbl+2]
  5. 寄存器间接(通过使用存储在寄存器中的地址访问内存)
    • mov ax, [di]:将di指定的内存地址处的值复制到ax中
    • mov dword [eax], var1:将var1中的值复制到eax指定的内存槽中

请注意,以上内容适用于 NASM.对于 MASM/TASM,您将使用 "mov esi, OFFSET foo" 来获取地址,而 "mov esi, foo" 和 "mov esi, [foo]" 都将获得值(信任@Michael).

那么,进入我的问题.它与以下教程第 29 页底部的示例有关:http://www.tutorialspoint.com/assembly_programming/assembly_tutorial.pdf

它基本上列出了以下代码作为间接内存寻址的示例.

MY_TABLE TIMES 10 DW 0 ;分配 10 个字(2 个字节),每个字都初始化为 0MOV EBX, [MY_TABLE] ;EBX 中 MY_TABLE 的有效地址MOV [EBX], 110 ;MY_TABLE[0] = 110添加 EBX, 2 ;EBX = EBX +2MOV [EBX], 123 ;MY_TABLE[1] = 123

我的问题:

  1. MOV EBX, [MY_TABLE]"实际上不应该是MOV EBX, MY_TABLE",因为我们想把表的地址放在EBX中,而不是值本身?
  2. 肯定是 MY_TABLE[2] 最后等于 123,而不是 MY_TABLE[1]?

解决方案

  1. 在 NASM 语法中,该指令应该是 MOV EBX, MY_TABLE.MOV EBX, [MY_TABLE] 要做的是将位于 MY_TABLE 的前 4 个字节加载到 EBX 中.另一种选择是使用 LEA,如LEA EBX,[MY_TABLE].

  2. 在这种情况下,教程是正确的.MY_TABLE 被定义为一个单词数组.x86上的一个字是2个字节,所以MY_TABLE的第二个元素确实位于MY_TABLE + 2.

As the web-resources on this is sparse, I will, for the benefit of future searches, begin by listing the address modes for IA-32 Assembly Language (NASM) and then follow up with a quick question.

  1. Register addressing
    • mov eax, ebx: Copies what is in ebx into eax
    • mov esi, var: Copies address of var (say 0x0040120e) into esi
  2. Immediate addressing (second operand is an immediate constant)
    • mov bx, 20: 16-bit register bx gets the actual value 20
  3. Direct memory addressing (directly loads from memory through a specified address)
    • mov ax, [1000h]: loads a 2-byte object from the byte at address 4096 (0x1000 in hexadecimal) into a 16-bit register called 'ax'
    • mov [1000h], ax: memory at address 1000h gets the value of ax
  4. Direct offset addressing (same as 3, just using arithmetics to modify address)
    • mov al, [byte_tbl+2]
  5. Register indirect (accessing memory by using addresses stored in registers)
    • mov ax, [di]: copies value at memory address specified by di, into ax
    • mov dword [eax], var1: copies value in var1 into the memory slot specified by eax

Please note that the above is for NASM. For MASM/TASM you'd use "mov esi, OFFSET foo" to get the address, while "mov esi, foo" and "mov esi, [foo]" both would get the value (creds to @Michael).

So, onto my question. It is in in relation to an example at the bottom of page 29 of the following tutorial: http://www.tutorialspoint.com/assembly_programming/assembly_tutorial.pdf

It basically lists the below code as an example of indirect memory addressing.

MY_TABLE TIMES 10 DW 0 ; Allocates 10 words (2 bytes) each initialized to 0 
MOV EBX, [MY_TABLE] ; Effective Address of MY_TABLE in EBX 
MOV [EBX], 110 ; MY_TABLE[0] = 110 
ADD EBX, 2 ; EBX = EBX +2 
MOV [EBX], 123 ; MY_TABLE[1] = 123 

My questions:

  1. Should not "MOV EBX, [MY_TABLE]" in fact be "MOV EBX, MY_TABLE", as we want to put the address of the table in EBX, not the value itself?
  2. Surely it is MY_TABLE[2] that is equal to 123 at the end, not MY_TABLE[1]?

解决方案

  1. In NASM syntax, that instruction should be MOV EBX, MY_TABLE. What MOV EBX, [MY_TABLE] would do is load the first 4 bytes located at MY_TABLE into EBX. Another alternative would be to use LEA, as in LEA EBX, [MY_TABLE].

  2. In this case the tutorial is right. MY_TABLE is defined as an array of words. A word on the x86 is 2 bytes, so the second element of MY_TABLE is indeed located at MY_TABLE + 2.

这篇关于汇编语言中的寻址模式 (IA-32 NASM)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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