News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

INVOKE and ADDR directives

Started by guro, April 06, 2011, 11:14:46 AM

Previous topic - Next topic

redskull

I'd like to point out that it's the linker that does the heavy lifting of keeping track of the 'real' locations, so OFFSET can only ever be relative to some amorphous point in the source file (i.e. the start of the segment as zero).  For instance, in this listing both offsets are still zero:

.386
.MODEL FLAT, stdcall
option casemap:none

00000000 .data

00000000 00000000 foo1  DWORD 0

00000000 .code
00000000 start:
00000000  B8 00000000 R mov eax, OFFSET foo1
00000005  B9 00000000 R mov ecx, OFFSET start
0000000A  C3 ret

end start


Strange women, lying in ponds, distributing swords, is no basis for a system of government

donkey

You know this thingy about the OFFSET operator all comes down to "Introduction to Computer Science"

http://www.c-jump.com/CIS77/ASM/Instructions/lecture.html#I77_0180_offset_operator

QuoteThe OFFSET operator returns the offset of a memory location relative to the beginning of the segment to which the location belongs

The load address of the executable thing is just a pig, you can try to put lipstick on the pig but its still a pig.
"Ahhh, what an awful dream. Ones and zeroes everywhere...[shudder] and I thought I saw a two." -- Bender
"It was just a dream, Bender. There's no such thing as two". -- Fry
-- Futurama

Donkey's Stable


MichaelW

Quote from: donkey on April 07, 2011, 02:41:34 PM
You know this thingy about the OFFSET operator all comes down to "Introduction to Computer Science"

http://www.c-jump.com/CIS77/ASM/Instructions/lecture.html#I77_0180_offset_operator

QuoteThe OFFSET operator returns the offset of a memory location relative to the beginning of the segment to which the location belongs

Not to add fuel to the fire here, but without qualification that statement is not correct. The OFFSET operator returns the value of the location counter.

;===================================================================================
    include \masm32\include\masm32rt.inc
;===================================================================================

printf MACRO format:REQ, args:VARARG
    IFNB <args>
        invoke crt_printf, cfm$(format), args
    ELSE
        invoke crt_printf, cfm$(format)
    ENDIF
    EXITM <>
ENDM

;===================================================================================
    .data
        data0 dd 0
        data1 dd 0
        org 10h
        data2 dd 0
        org 0
        data3 dd 0
    .code
;===================================================================================
start:
;===================================================================================

  code0:

    mov eax, OFFSET data0
    mov eax, OFFSET data1
    mov eax, OFFSET data2
    mov eax, OFFSET data3

    printf( "OFFSET data0 : %Xh\n", OFFSET data0 )
    printf( "OFFSET data1 : %Xh\n", OFFSET data1 )
    printf( "OFFSET data2 : %Xh\n", OFFSET data2 )
    printf( "OFFSET data3 : %Xh\n\n", OFFSET data3 )

  code1:
  code2:
    org 10h
  code3:
    org 0
  code4:
    org code1
  code5:

    mov eax, OFFSET code0
    mov eax, OFFSET code1
    mov eax, OFFSET code2
    mov eax, OFFSET code3
    mov eax, OFFSET code4
    mov eax, OFFSET code5

    printf( "OFFSET code0 : %Xh\n", OFFSET code0 )
    printf( "OFFSET code1 : %Xh\n", OFFSET code1 )
    printf( "OFFSET code2 : %Xh\n", OFFSET code2 )
    printf( "OFFSET code3 : %Xh\n", OFFSET code3 )
    printf( "OFFSET code4 : %Xh\n", OFFSET code4 )
    printf( "OFFSET code5 : %Xh\n\n", OFFSET code5 )

    inkey "Press any key to exit..."
    exit

;===================================================================================
end start


From the listing:

00000000  B8 00000000 R     mov eax, OFFSET data0
00000005  B8 00000004 R     mov eax, OFFSET data1
0000000A  B8 00000010 R     mov eax, OFFSET data2
0000000F  B8 00000000 R     mov eax, OFFSET data3

00000060  B8 00000000 R     mov eax, OFFSET code0
00000065  B8 00000060 R     mov eax, OFFSET code1
0000006A  B8 00000060 R     mov eax, OFFSET code2
0000006F  B8 00000010 R     mov eax, OFFSET code3
00000074  B8 00000000 R     mov eax, OFFSET code4
00000079  B8 00000060 R     mov eax, OFFSET code5


At run time:

OFFSET data0 : 403000h
OFFSET data1 : 403004h
OFFSET data2 : 403010h
OFFSET data3 : 403000h

OFFSET code0 : 401000h
OFFSET code1 : 401060h
OFFSET code2 : 401060h
OFFSET code3 : 401010h
OFFSET code4 : 401000h
OFFSET code5 : 401060h

eschew obfuscation

donkey

Michael,

You are absolutely correct, and I agree that the ORG statement will "screw" with the FLAT model at source level, however, it only adjusts how the assembler/linker calculates the OFFSETs, it does not change the fact that in a FLAT model the OFFSET is relative to the segment. This is like saying that the number 400 is different than 400 because the formula used to get it was 2*200 instead of 4*100.
"Ahhh, what an awful dream. Ones and zeroes everywhere...[shudder] and I thought I saw a two." -- Bender
"It was just a dream, Bender. There's no such thing as two". -- Fry
-- Futurama

Donkey's Stable

dedndave

#35
it is relative to the segment register at runtime
however, at assemble time, it is relative to the segment or group that contains the reference
i remember, in the old days, i sometimes had to specify it as OFFSET DGROUP:SomeLabel
because the segment base was not the same as the segment register - it pointed to the base of DGROUP
to save some typing, i did this...
ODG     EQU     OFFSET DGROUP
OFS     EQU     OFFSET
;
;
;
        mov     ax,DGROUP
        mov     ds,ax
;
;
        mov     si,ODG:SomeLabel
;or
        mov     bx,OFS SomeOtherLabel   ;when applicable


correction:
Quoteit is relative to the segment register at runtime
hopefully, the segment referenced in the assembly source is in the segment selector at runtime   :bg

oex

I was considering this some time ago but thought I'd look foolish if I didnt know how this worked :lol....

invoke Callback, oexPROC
invoke Callback, OFFSET oexPROC
invoke Callback, ADDR oexPROC

All return the correct proc address in 32 bit code right?

ADDR destroys eax

EDIT: Nobody's saying anything.... Now I'm starting to feel foolish :lol
We are all of us insane, just to varying degrees and intelligently balanced through networking

http://www.hereford.tv

dedndave

Vector  dd oexPROC
;or
        mov     eax,oexPROC
;or
        invoke  Callback,oexPROC

the assembler knows it is a code address because it is a ":" or PROC label
so, OFFSET is implied, or more accurately, OFFSET .text (or whatever the code segment is named)


oex

Ah kk so long as the assembler handles it and I havent missed something important that's what matters :bg.... Just seemed odd to have 3 possible reserved word methods for effectively 1 thing....
We are all of us insane, just to varying degrees and intelligently balanced through networking

http://www.hereford.tv

baltoro

Baltoro

jj2007

Quote from: oex on April 07, 2011, 07:00:06 PM

Only when addressing a LOCAL variable - which is not the case for a call.

oex

Sorry jj can you try that explanation again.... Which one(s) of the 3 were wrong.... What is LOCAL scope of a PROC label? Are not all PROCs GLOBAL scope?....
We are all of us insane, just to varying degrees and intelligently balanced through networking

http://www.hereford.tv

jj2007

None is wrong:
invoke Callback, oexPROC
invoke Callback, OFFSET oexPROC
invoke Callback, ADDR oexPROC


... but "ADDR destroys eax" happens only when ADDR refers to a local variable. The invoke macro uses internally OFFSET GlobalVar if you write ADDR GlobalVar. For a local var, in contrast, it uses
lea eax, [ebp+n]
push eax


Tip: Write a little proggie, and look at it through Olly.

oex

Quote from: jj2007 on April 07, 2011, 09:40:15 PM
Tip: Write a little proggie, and look at it through Olly.

:lol yep I'm just being lazy.... I dont use Olly but I had tested partially with my own code I just wondered if I'd missed something :lol

ty for the infos it clarifies it for me :bg
We are all of us insane, just to varying degrees and intelligently balanced through networking

http://www.hereford.tv

Tedd

Trust a group of assembler programmers to take a basic question and turn it into a multi-page argument ::)

I think most of this belongs in The Colosseum? And let's hope you haven't scared guro from ever asking a question again :bdg


Next question: what does mov REALLY do? :bg
No snowflake in an avalanche feels responsible.