Add two variables in MASM Assembler

8

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

 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 am adding only the value of the variable, or I am adding the value and address in memory, because I understand that to obtain the value in particular it must be enclosed between [] .

    
asked by julian salas 17.02.2016 в 17:38
source

2 answers

8

In MASM (and TASM in MASM compatible mode), when you type:

a dword 10000h
b dword 40000h

a and b are labels that represent the storage address assigned to the double words 10000h and 40000h respectively.

When a label is used as an operand, MASM knows that label represents a memory address, and decides that the parameter is a reference to memory. To force the memory address to be used as an immediate value, OFFSET can be used preceding the label. Summing up:

mov eax, a          ; mueve 10000h a eax
mov eax, OFFSET a   ; mueve la dirección donde está 10000h a eax
mov eax, [a]        ; en MASM equivale a MOV eax, a

Instead, when you type:

foo EQU 42
bar EQU 66

foo and bar are symbolic constants representing 42 and 66, and space is not reserved for 42 and 66. By using a symbolic constant as a parameter, MASM treats the parameter as an immediate value.

mov eax, foo        ; mueve 42 a eax

The fact that the meaning of an instruction is different depending on whether its parameters are labels or constants can be confusing. Other assemblers, such as NASM (also Yasm, which uses NASM syntax), TASM in IDEAL mode, or fasm, require that brackets be used to treat a parameter as a reference to memory, and if there are no brackets they always treat the parameter as an immediate value.

    
answered by 17.02.2016 / 21:36
source
7

With the instruction a dword 10000h you are defining a memory area of 4 bytes that contains the value 10000h, in this case a refers to the address of that memory area (which will be the one that the compiler decides) and with mov eax,a you are loading the memory address in the accumulator, no the contents of the memory.

Because of your question, it is not clear what you want to achieve, but it will probably be one of two things:

  • Load the accumulator with the stored value in the memory area defined by a (that is, have the processor read 4 bytes from a and store the result in eax ). In that case you must use mov eax,[a] .

  • Define a as a constant, which will be replaced directly by the corresponding value at compile time. In that case you must define the constant as a equ 10000h (do not use dword ). The compiled code will then be equivalent to mov eax,10000h .

  • And the same applies to the instruction add .

        
    answered by 17.02.2016 в 18:20