3

I have the next program in Assembler MASM, I have a question about the sum records for Assembler MASM

 TITLE Suma variables
 INCLUDE Irvine32.inc
 .data
 a dword 10000h
 b dword 40000h
 valorFinal dword ?
.code
 main PROC
       mov eax,a ; empieza con 10000h
       add eax,b ; suma 40000h
       mov valorFinal,eax ; 
      call DumpRegs
      exit
 main ENDP
 END main

My question is when I use add with b, I'm adding only the value of the variable, or am I adding value and address in memory, because I understand that to get the particular value must be enclosed in [].

Michael Petch
  • 46,082
  • 8
  • 107
  • 198
Peter Jason
  • 33
  • 1
  • 5

1 Answers1

5

because I understand that to get the particular value must be enclosed in []

In NASM syntax you would need brackets.
In MASM/TASM syntax you don't, and add eax,b means the same thing as add eax,[b] (assuming that b is a label and not something like b EQU 42).

If you wanted to add b's address to eax in MASM/TASM syntax you would write:

add eax, offset b
Michael
  • 57,169
  • 9
  • 80
  • 125
  • `b equ 42` is still a symbol with an immediate value. A label for a `dd` has an immediate value equal to its address. In GNU asm syntax, you can modify the "value" of a label with an assembler directive. It's the same whether the value is an address or not. So if you think about it that way, `b equ 42` is conceptually the same as if `b` was a symbol defined by a label. As you say, MASM/TASM implicitly dereferences symbols that are labels (addresses), which makes it impossible to know what an instruction is doing without looking at other lines. – Peter Cordes Feb 26 '16 at 09:29
  • I guess my point is, you can always use brackets when using a memory operand, for clarity and consistency with NASM syntax. There's no advantage to leaving them out, is there? I'd suggest that one should always use them, to make it easy to spot memory operands in code. – Peter Cordes Feb 26 '16 at 09:32
  • 2
    Well, that's a matter of personal preference. In my troubled past I've written fairly large amounts of MASM-syntax assembly (like tens of thousands of LOC), and I would often use the bracket-less style and can't recall ever introducing any bugs because of that. I would use `ALL_UPPER_CASE` for equates to differentiate them. My point about equates in my answer was that `add eax, b` and `add eax, [b]` no longer mean the same thing if `b` was an equate for some integer literal, like 42. – Michael Feb 26 '16 at 10:07
  • 1
    That's fair. If you're used to thinking the MASM way, I'm sure it works fine and you know what you mean. It mostly just sucks for Stackoverflow, where we're trying to talk about asm in as generic a context as possible. I personally like NASM's design decision to not have labels imply operand size, and to always require brackets for memory refs, is a good one. Thinking that way makes MASM just seem weird to me. – Peter Cordes Feb 26 '16 at 10:53