在MASM32混淆括号 [英] Confusing brackets in MASM32

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

问题描述

我试图去交手MASM32者,按以下困惑:

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

我以为支架被用于间接所以如果我有一个pre定义的变量

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

 .data
   item dd 42

然后

 mov ebx, item

会把'项',即数字42,内容为EBX以及

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

 mov ebx, [item]

会把'项',即,其中42存储的地址复制到EBX。

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

但下面code在一个控制台应用程序:

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知道,因为你是如何定义的符号项目是类型的存储位置 DWORD 。当你作为一个操作都知道,你(可能)的意思是你要存储在地址的值,而不是地址的值使用它。所以,如果你使用项目 [项目] MASM假定你的意思是以后也没关系。如果你真的想项目的地址,而不是你需要使用 OFFSET项目

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.

在另一方面,如果你已经定义项目恒定使用项目= 42 然后 MOV EBX,项目将加载立即值。因为这种模糊的,你需要知道如何项目定义,以确定它是否立即数或内存操作数,这是避免使用裸符号作为一个操作数好主意。

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.

我要补充的方括号 [] 的意思是pretty多没事的时候你仅仅使用符号MASM。当你与寄存器使用他们他们只意味着什么。下面是一些例子:

I should add that the square brackets [] mean pretty much nothing to MASM when you're just using symbols. 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, 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天全站免登陆