MASM32 中的混淆括号 [英] Confusing brackets in MASM32

查看:26
本文介绍了MASM32 中的混淆括号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试掌握 MASM32,但对以下内容感到困惑:

I am trying to get to grips with MASM32 and am confused by the following:

我认为括号用于间接,所以如果我有一个预定义的变量

I thought that brackets were used for indirection so if I have the a pre-defined variable

 .data
   item dd 42

然后

 mov ebx, item

将'item'的内容,即数字42,放入ebx和

would put the contents of 'item', i.e. the number 42, into ebx and

 mov ebx, [item]

将'item'的地址,即42的存储位置,放入ebx中.

would put the address of 'item', i.e. where the 42 is stored, into ebx.

但是控制台应用程序中的以下代码:

But the following code in a console app:

 mov ebx, item
 invoke dwtoa, ebx, ADDR valuestr 
 invoke StdOut, ADDR valuestr
 mov ebx, [item]
 invoke dwtoa, ebx, ADDR valuestr 
 invoke StdOut, ADDR valuestr

打印 42 两次.要获取项目"的地址,我似乎需要

prints 42 twice. To get the address of 'item' I seem to need

 mov ebx, [OFFSET item]
 invoke dwtoa, ebx, ADDR valuestr 
 invoke StdOut, ADDR valuestr

谁能解释一下 MASM 中方括号的含义,或者给我一个很好的参考.

Can anybody explain what square brackets are for in MASM, or point me at a good reference.

推荐答案

MASM 对于有类型的汇编语言来说是不寻常的.MASM 知道,因为您如何定义符号 item,它是 DWORD 类型的内存位置.当您将它用作操作数时,您知道您(可能)的意思是您希望将值存储在地址中,而不是地址的值.因此,使用 item 还是 [item] 并不重要,MASM 假定您指的是后者.如果您确实想要项目的地址,则需要使用 OFFSET item.

MASM is unusual for an assembly language in that is has types. MASM knows because of how you defined the symbol item that is a memory location of type DWORD. When you use it as an operand knows that you (probably) mean that you want the value stored at the address, not the value of the address. So it doesn't matter if you use item or [item] MASM assumes you mean the later. If you really want the address of item instead you need to use OFFSET item.

另一方面,如果您使用 item = 42item 定义为常量,则 mov ebx, item 将加载立即值.由于这种歧义,您需要知道如何定义 item 以确定它是立即操作数还是内存操作数,最好避免使用裸符号作为操作数.

On the other hand if you had defined item as constant using item = 42 then mov ebx, item would load the immediate value. Because of this ambiguity, you need to know how item was defined to determine if it's an immediate operand or a memory operand, it's good idea to avoid using a bare symbol as an operand.

我应该补充一点,当您只使用符号或数字时,方括号 [] 对 MASM 几乎没有任何意义.它们仅在您将它们与寄存器一起使用时才有意义.以下是一些示例:

I should add that the square brackets [] mean pretty much nothing to MASM when you're just using symbols or numbers. They only mean something when you use them with registers. Here's some examples:

item    DD  42
const   =   43

    mov eax, item             ; memory operand (the value stored at item)
    mov eax, [item]           ; memory operand
    mov eax, OFFSET item      ; immediate operand (the address of item)
    mov eax, [OFFSET item]    ; immediate operand

    mov eax, const            ; immediate operand (43)
    mov eax, [const]          ; immediate operand
    mov eax, ds:[const]       ; memory operand (the value at address 43)
    mov eax, fs:30h           ; memory operand (the value at address 30h + fs base)
    mov eax, OFFSET const     ; immediate operand
    mov eax, [OFFSET const]   ; immediate operand

    mov eax, 42               ; immediate operand
    mov eax, ebx              ; register operand (the value of EBX)
    mov eax, [ebx]            ; indirect operand (the value pointed to by EBX)

所以没有寄存器方括号只显示你的意图.如果您打算将它们用作内存操作数,您应该在符号周围放置方括号,并且将 OFFSET 与您打算用作立即值的符号一起使用.

So without registers square brackets only show your intent. You should put square brackets around symbols if you intend to use them as memory operands, and use OFFSET with symbols you intend to use as immediate values.

这篇关于MASM32 中的混淆括号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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