YASM/NASM x86 汇编中立即数与方括号的基本使用 [英] Basic use of immediates vs. square brackets in YASM/NASM x86 assembly

查看:49
本文介绍了YASM/NASM x86 汇编中立即数与方括号的基本使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我声明了以下内容:

Suppose I have the following declared:

section .bss
buffer    resb     1

这些说明在section .text:

mov    al, 5                    ; mov-immediate
mov    [buffer], al             ; store
mov    bl, [buffer]             ; load
mov    cl, buffer               ; mov-immediate?

我是否正确理解 bl 将包含值 5,而 cl 将包含变量 buffer 的内存地址?

Am I correct in understanding that bl will contain the value 5, and cl will contain the memory address of the variable buffer?

我对

  • 将立即数移入寄存器,
  • 将寄存器移动到立即数(输入什么,数据还是地址?)和
  • 将立即数移入不带括号的寄存器
    • 例如,mov cl, buffer vs mov cl, [buffer]

    更新:阅读回复后,我认为以下摘要是准确的:

    UPDATE: After reading the responses, I suppose the following summary is accurate:

    • mov edi, array 将第零个数组索引的内存地址放入edi.即标签地址.
    • mov byte [edi], 3 将 VALUE 3 放入数组的第零个索引
    • add edi, 3之后,edi现在包含了数组第三个索引的内存地址
    • mov al, [array] 将第零个索引处的 DATA 加载到 al 中.
    • mov al, [array+3] 将第三个索引处的 DATA 加载到 al 中.
    • mov [al], [array] 无效,因为 x86 无法编码 2 个显式内存操作数,因为 al 只有 8 位,即使在 16 位寻址模式下也无法使用.引用内存位置的内容.(x86 寻址模式)
    • mov array, 3 无效,因为你不能说嘿,我不喜欢存储 array 的偏移量,所以我会称之为 3".立即数只能是源操作数.
    • mov byte [array], 3 将值 3 放入数组的第零个索引(第一个字节).需要byte说明符避免字节/字/双字之间的歧义,用于具有内存、立即操作数的指令.否则,这将是汇编时错误(操作数大小不明确).
    • mov edi, array puts the memory address of the zeroth array index in edi. i.e. the label address.
    • mov byte [edi], 3 puts the VALUE 3 into the zeroth index of the array
    • after add edi, 3, edi now contains the memory address of the 3rd index of the array
    • mov al, [array] loads the DATA at the zeroth index into al.
    • mov al, [array+3] loads the DATA at the third index into al.
    • mov [al], [array] is invalid because x86 can't encode 2 explicit memory operands, and because al is only 8 bits and can't be used even in a 16-bit addressing mode. Referencing the contents of a memory location. (x86 addressing modes)
    • mov array, 3 is invalid, because you can't say "Hey, I don't like the offset at which array is stored, so I'll call it 3". An immediate can only be a source operand.
    • mov byte [array], 3 puts the value 3 into the zeroth index (first byte) of the array. The byte specifier is needed to avoid ambiguity between byte/word/dword for instructions with memory, immediate operands. That would be an assemble-time error (ambiguous operand size) otherwise.

    请说明这些是否有误.(编者注:我修复了语法错误/歧义,因此有效的实际上有效的 NASM 语法.并链接了其他问答以了解详细信息)

    Please mention if any of these is false. (editor's note: I fixed syntax errors / ambiguities so the valid ones actually are valid NASM syntax. And linked other Q&As for details)

    推荐答案

    确实,你的想法是对的,就是bl会包含5和cl的buffer的内存地址(其实labelbuffer本身就是一个内存地址).

    Indeed, your thought is correct.That is, bl will contain 5 and cl the memory address of buffer(in fact the label buffer is a memory address itself).

    现在,让我解释一下您提到的操作之间的区别:

    Now, let me explain the differences between the operations you mentioned:

    • 可以使用 mov reg,imm 将立即数移动到寄存器中.可能令人困惑的是标签(例如缓冲区)本身就是包含地址的立即数.

    • moving an immediate into a register can be done using mov reg,imm.What may be confusing is that labels e.g buffer are immediate values themselves that contain an address.

    您不能真正将寄存器移动到立即数,因为立即数是常量,例如 2FF1Ah.您可以做的是将寄存器移动到常量指向的地方.你可以像 mov [const], reg .

    You cannot really move a register into an immediate, since immediate values are constants, like 2 or FF1Ah.What you can do is move a register to the place where the constant points to.You can do it like mov [const], reg .

    你也可以使用间接寻址,比如mov reg2,[reg1],前提是reg1指向一个有效的位置,它会将reg1指向的值传送给reg2.

    You can also use indirect addressing like mov reg2,[reg1] provided reg1 points to a valid location, and it will transfer the value pointed by reg1 to reg2.

    因此,mov cl, buffer 会将缓冲区的地址 移动到 cl(这可能会也可能不会给出正确的地址,因为 cl 只有一个字节长), 而 mov cl, [buffer] 将获得实际值.

    So, mov cl, buffer will move the address of buffer to cl(which may or may not give the correct address, since cl is only one byte long) , whereas mov cl, [buffer] will get the actual value.

    • 当你使用[a]时,那么你指的是a指向的地方的值.例如,如果a是F5B1,那么[a]指的是F5B1中的地址F5B1em>内存.
    • 标签是地址,即像 F5B1 这样的值.
    • 存储在寄存器中的值不必引用为 [reg],因为寄存器没有地址.实际上,可以将寄存器视为立即值.
    • When you use [a], then you refer to the value at the place where a points to.For example, if a is F5B1, then [a] refers to the address F5B1 in RAM.
    • Labels are addresses,i.e values like F5B1.
    • Values stored in registers do not have to be referenced to as [reg] because registers do not have addresses.In fact, registers can be thought of as immediate values.

    这篇关于YASM/NASM x86 汇编中立即数与方括号的基本使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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