assembly - MASM: Using Current Location Counter ($) in .data declaration -


i met problem current location counter in masm.

here assembly code, , used visual studio 2013 express assembling

.386 .model flat,stdcall .stack 8192 exitprocess proto,dwexitcode:dword  .data ptr1 dword $ ptr2 dword $ ptr5 dword $   .code main proc     mov eax, $     mov eax, $     invoke exitprocess,0 main endp end main 

in opinion, think ptr1, ptr2, , ptr5 should have own location value.

but it's not correct in fact.

when in debugging mode, variables show same result.

ptr1, ptr2, ptr5 have same address , there no offset between them.

what's problem when using $ declaration ?

your problem seems bug in masm (or microsoft put it, "feature"). problem isn't dword directives aren't generating object code or aren't advancing assembler's location counter. if former true wouldn't show in executable @ all, , if later true have same address.

the problem masm incorrectly uses offset of start of current segment (in generated object file) value of $ instead of current location counter in contexts when used in data definition. following code, based on example, demonstrates (and shows simple solution):

        .386         public  ptr1, ptr2, ptr5, ptr6, len  _data   segment         mov     eax, $         mov     eax, $ ptr1    dword   $ ptr2    dword   $ ptr5    dword   offset $ ptr6    dword   ptr6 len     dword   $ - ptr1         mov     eax, $ _data   ends          end 

here's how ida disassembles object file created masm above assembly code:

.data:00000000 $$000000: .data:00000000                 mov     eax, offset $$000000 .data:00000005 $$000005: .data:00000005                 mov     eax, offset $$000005 .data:0000000a ptr1            dd offset $$000000 .data:0000000e ptr2            dd offset $$000000 .data:00000012 ptr5            dd offset $$000000 .data:00000016 ptr6            dd offset ptr6 .data:0000001a len             dd 16 .data:0000001e $$00001e: .data:0000001e                 mov     eax, offset $$00001e 

you'll notice mov eax, $ instructions show location counter being correctly advanced dword directives. you'll notice ptr1, ptr2 , ptr5 have been initialized $$000000 @ start of segment, ignoring fact both previous mov instructions , dword directives have advanced location counter.

on other hand, masm evaluate $ - ptr1 correctly. calculates distance between ptr1 , current location counter, 16, total length in bytes of previous 4 dword directives. means in @ least in context masm uses correct value of $.

finally example shows how work around problem. use named label instead of $, in line ptr6 dword ptr6. results in assembler correctly generating pointer initialized point @ itself.


Comments

Popular posts from this blog

python - How to insert QWidgets in the middle of a Layout? -

python - serve multiple gunicorn django instances under nginx ubuntu -

module - Prestashop displayPaymentReturn hook url -