I have a question about the sum in registers 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 doubt is when I use add
with b
, am I adding only the value of the variable, or am I adding the value and the address in memory, because I understand that to obtain the specific value it must be enclosed in []
.
In MASM (and TASM in MASM-compatible mode), when you type:
a
andb
are labels that represent the address of the storage assigned to the double words10000h
and40000h
respectively.When a label is used as an operand, MASM knows that the label represents a memory address, and decides that the parameter is a memory reference. To force the memory address to be used as an immediate value, it can be used
OFFSET
preceding the tag. Summarizing:Instead, when you write:
foo
andbar
are symbolic constants that represent 42 and 66, with no space reserved for 42 and 66. When using a symbolic constant as a parameter, MASM treats the parameter as an immediate value.The fact that the meaning of a statement 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 memory reference, and if there are no brackets they always treat the parameter as an immediate value.
With the instruction
a dword 10000h
you are defining a 4-byte memory zone that contains the value 10000h, in this casea
it refers to the address of that memory zone (which will be the one that the compiler decides) and withmov eax,a
the accumulator you are loading the address memory, not the contents of memory.It's not clear from your question what you want to achieve, but it will probably be one of two things:
Load the accumulator with the value stored in the memory area defined by
a
(ie, have the processor read 4 bytes froma
and store the result ineax
). In that case you should usemov eax,[a]
.Define
a
as a constant, which will be directly replaced by the corresponding value at compile time. In that case you should define the constant asa equ 10000h
(don't usedword
). The compiled code will then be equivalent tomov eax,10000h
.And the same applies to instruction
add
.