News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests

Main Menu

org directive

Started by Pokerice, May 22, 2014, 09:08:13 AM

Previous topic - Next topic

Pokerice

Hello again, I am having trouble using the directive ORG. Can someone explain to me how it works in detail? All I could find is how ORG basically sets up the location counter to where your want your program will first start at.

Debugging it in OllyDbg, it seems using ORG always leads to access violation unless it is ORG 0. Am I not suppose to use ORG? And where is the location counter in OllyDbg?

nidud

#1
deleted

Pokerice


dedndave

i haven't seen much use for ORG in win32 code
perhaps, if you are writing an error handler or something

in 16-bit code, ORG was used in .COM files to start at the end of the PSP
        ORG     100h

and, you might see it in SEGMENT templates
_BIOS SEGMENT PARA PUBLIC 'DATA'

        ORG     10h
wEquipment dw ?

_BIOS ENDS


another place i used to use ORG was when writing ROM-able code, such as BIOS
you can specify hard addresses - although, MASM always seemed to choke a little bit
i was able to use offsets and make it work

and, in a similar fashion, you might have seen ORG used in boot sector code   :P
        ORG     7C00h

Gunther

That's a quote from Programmer's Guide, Microsoft® MASM, p. 120.

Quote
The ALIGN, EVEN, and ORG directives can modify how field offsets are placed during structure definition. The EVEN and ALIGN directives insert padding bytes to round the field offset up to the specified alignment boundary. The ORG directive changes the offset of the next field to a given value, either positive or negative. If you use ORG when declaring a structure, you cannot define a structure of that type. ORG is useful when accessing existing data structures, such as a stack frame created by a high-level language.

HTH
Gunther
You have to know the facts before you can distort them.

Zen

What EXACTLY is the location counter ???
Zen

dedndave

it's a counter that the assembler uses to assign addresses

pretend you are the assembler...
you see...
dwDwordVar dw ?
you assign the address of dwDwordVar symbol from the value of the current location counter (for that section)
you add it's size (4 bytes)
that is the new location counter value
now, you see...
szString db 'Hello World',0
you assign the address of szString symbol from the value of the current location counter
you add it's size (12 bytes)
that is the new location counter value

all seems simple enough, and it is, especially in the data section

it's a little more meaningful in the code section
because label addresses, more specifically the distances, may alter what form of instruction are used
the best example of this is conditional braches
you see
    jnz     SomeLabel
if SomeLabel is a backward branch, the location has already been assigned
the assembler knows right away what the distance is, and knows which form of JNZ to use
if SomeLabel is a forward branch, we haven't assigned it an address, yet
we don't know the distance
newer versions of masm handle it better than older versions   :P

with the 8088, all conditional branches were short (+127 to -128 byte distance)
if the location counter came to a point that was too far, you simply got
Relative Jump Out of Range

if you were having a really bad day, you might get
Phase Error Between Passes
:(

jimg

Here's a fragment of code out of MergeSort.  I use org to set aside a block of code memory that I will fill in with instructions at execution time, depending upon what the user wants to do:

            align 4
        TestLoc1:
            ; comparison routine will be inserted here
            ; eg. cmp [esi],[eax]   ; check next value to see direction of sequence
            org $+maxsize
            ; and will jump to here when done

            jl DoReversed   ; possible reversed sequence
            align 4         ; this one should already have been aligned
        DoNormal:           ; this section marks out an increasing stream
            mov eax,[esi]   ; get new larger value
            add esi,4       ; address of next value to test
            sub rcount,1    ; count down available values
            jz DoneNewData  ; out of new values, go merge what we have
            align 4
        TestLoc2:
            ; comparison routine will be inserted here
            ; eg. cmp [esi],[eax]   ; check next value to see direction of sequence
            ; and will jump to here when done
            org $+maxsize

            jge DoNormal    ; ok, go save it and test another

            cmp ebx,last[0] ; have we filled up a level?
            je DoneNewData  ; all done getting new data for now

Zen

Thanks, DAVE,...always so informative and helpful,...
Zen