Author Topic: org directive  (Read 4892 times)

Pokerice

  • Regular Member
  • *
  • Posts: 25
org directive
« on: May 22, 2014, 09:08:13 AM »
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

  • Member
  • *****
  • Posts: 2170
    • https://github.com/nidud/asmc
Re: org directive
« Reply #1 on: May 22, 2014, 11:15:12 AM »
Quote
All I could find is how ORG basically sets up the location counter to where your want your program will first start at.

Or to insert code at a specific location:

Code: [Select]
.data
;
; ZIP-Headers laid over each other to conserve memory
;
; End of central directory record: 22 byte
;
ze_pkzip DW ? ; end of central dir signature
ze_zipid DW ?
ze_disk_cur DW ? ; number of this disk
ze_disk_one DW ? ; number at start of the central directory
ze_entry_cur DW ? ; central directory on this disk
ze_entry_dir DW ? ; total entries in the central directory
ze_size_cent DD ? ; size central directory
ze_off_cent DD ? ; offset central directory
ze_comment_size DW ? ; .ZIP file comment length

org ze_pkzip ; Local file header: 30 byte

lz_pkzip DW ? ; local file header signature
lz_zipid DW ?
lz_version DW ? ; version needed to extract
lz_flag DW ? ; general purpose bit flag
lz_method DW ? ; compression method
lz_time DW ? ; last mod file time
lz_date DW ? ; last mod file date
lz_crc DD ? ; crc-32
lz_csize DD ? ; compressed size
lz_fsize DD ? ; uncompressed size
lz_fnsize DW ? ; file name length
lz_extsize DW ? ; extra field length

org ze_pkzip ; Central directory structure: 46 byte

cz_pkzip DW ? ; central file header signature
cz_zipid DW ?
cz_version_made DW ? ; version made by
cz_version_need DW ? ; version needed to extract
cz_bitflag DW ? ; general purpose bit flag
cz_method DW ? ; compression method
cz_time DW ? ; last mod file time
cz_date DW ? ; last mod file date
cz_crc DD ? ; crc-32
cz_csize DD ? ; compressed size
cz_fsize DD ? ; uncompressed size
cz_fnsize DW ? ; file name length
cz_extsize DW ? ; extra field length
cz_cmtsize DW ? ; file comment length
cz_disk DW ? ; disk number start
cz_int_attrib DW ? ; internal file attributes
cz_ext_attrib DW ? ; external file attributes
cz_ext_attribdx DW ?
cz_off_local DD ? ; relative offset of local header

ORG will usually confuse the debugger, especially in the code segment.

Pokerice

  • Regular Member
  • *
  • Posts: 25
Re: org directive
« Reply #2 on: May 22, 2014, 03:21:45 PM »
Thanks! 8)

dedndave

  • Member
  • *****
  • Posts: 8829
  • Still using Abacus 2.0
    • DednDave
Re: org directive
« Reply #3 on: May 22, 2014, 11:32:38 PM »
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
Code: [Select]
        ORG     100h
and, you might see it in SEGMENT templates
Code: [Select]
_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
Code: [Select]
        ORG     7C00h

Gunther

  • Member
  • *****
  • Posts: 3722
  • Forgive your enemies, but never forget their names
Re: org directive
« Reply #4 on: May 23, 2014, 02:20:30 AM »
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
Get your facts first, and then you can distort them.

Zen

  • Member
  • ****
  • Posts: 962
  • slightly red-shifted
Re: org directive
« Reply #5 on: May 23, 2014, 05:25:59 AM »
What EXACTLY is the location counter ???
Zen

dedndave

  • Member
  • *****
  • Posts: 8829
  • Still using Abacus 2.0
    • DednDave
Re: org directive
« Reply #6 on: May 23, 2014, 07:31:15 AM »
it's a counter that the assembler uses to assign addresses

pretend you are the assembler...
you see...
Code: [Select]
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...
Code: [Select]
szString db 'Hello World',0you 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
Code: [Select]
    jnz     SomeLabelif 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

  • Member
  • ***
  • Posts: 466
Re: org directive
« Reply #7 on: May 23, 2014, 11:14:51 PM »
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:

Code: [Select]
            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

  • Member
  • ****
  • Posts: 962
  • slightly red-shifted
Re: org directive
« Reply #8 on: June 01, 2014, 02:27:40 AM »
Thanks, DAVE,...always so informative and helpful,...
Zen