Size of address given with ORG in 16 bit mode

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
shahsunny712
Posts: 20
Joined: Wed Jan 29, 2014 11:57 am

Size of address given with ORG in 16 bit mode

Post by shahsunny712 »

I have been trying to figure out what is the size of the address that can be given with the ORG directive in 8086 (or real mode), since it is a 16-bit environment but 8086 can address 20-bit memory.

Is the address given with ORG supposed to be within the current segment ? NASM's documentation doesn't mention it. What will happen if I do give a 20 bit address ? (I tried this, but there were no assembler errors/warnings. Didn't try to run that though). Will the start/end nibble be truncated or will it be undefined behaviour ?
User avatar
iansjack
Member
Member
Posts: 4711
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

Re: Size of address given with ORG in 16 bit mode

Post by iansjack »

Why didn't you try to run the program?
madanra
Member
Member
Posts: 149
Joined: Mon Sep 07, 2009 12:01 pm

Re: Size of address given with ORG in 16 bit mode

Post by madanra »

You could try disassembling it with ndisasm to see what was actually produced. Running it is the most effective way of finding out what it does though :)
User avatar
Bender
Member
Member
Posts: 449
Joined: Wed Aug 21, 2013 3:53 am
Libera.chat IRC: bender|
Location: Asia, Singapore

Re: Size of address given with ORG in 16 bit mode

Post by Bender »

Is the address given with ORG supposed to be within the current segment ? NASM's documentation doesn't mention it.
The NASM Manual (Chapter 7) gives you this info.
7.1.1 ORG: Binary File Program Origin

The bin format provides an additional directive to the list given in chapter 6: ORG. The function of the ORG directive is to specify the origin address which NASM will assume the program begins at when it is loaded into memory.

For example, the following code will generate the longword 0x00000104:

org 0x100
dd label
label:
Unlike the ORG directive provided by MASM-compatible assemblers, which allows you to jump around in the object file and overwrite code you have already generated, NASM's ORG does exactly what the directive says: origin. Its sole function is to specify one offset which is added to all internal address references within the section; it does not permit any of the trickery that MASM's version does. See section 12.1.3 for further comments.
The ORG directive simply adds the number specified to all label references. And yes ORG specifies the offset within a segment.
I think the sane limit of ORG under 16-bit mode should be 64KB (0xFFFF).
BTW Run the program and see what happens, it's always good to run and see the result in front of your eyes. :)
"In a time of universal deceit - telling the truth is a revolutionary act." -- George Orwell
(R3X Runtime VM)(CHIP8 Interpreter OS)
linguofreak
Member
Member
Posts: 510
Joined: Wed Mar 09, 2011 3:55 am

Re: Size of address given with ORG in 16 bit mode

Post by linguofreak »

shahsunny712 wrote:I have been trying to figure out what is the size of the address that can be given with the ORG directive in 8086 (or real mode), since it is a 16-bit environment but 8086 can address 20-bit memory.

Is the address given with ORG supposed to be within the current segment ? NASM's documentation doesn't mention it. What will happen if I do give a 20 bit address ? (I tried this, but there were no assembler errors/warnings. Didn't try to run that though). Will the start/end nibble be truncated or will it be undefined behaviour ?
In real mode you will never have a 20 bit address*. You will either have a straight 16 bit offset into an implied segment, or a segment:offset pair (32 bits total, 16 bits each for the segment and offset).

As others have mentioned, the NASM ORG directive is a 16-bit offset.

*Unless your program calculates that address itself from a segment:offset pair, and then converts it back to a segment:offset pair before using it.
Antti
Member
Member
Posts: 923
Joined: Thu Jul 05, 2012 5:12 am
Location: Finland

Re: Size of address given with ORG in 16 bit mode

Post by Antti »

This problem is exactly something that should be solved before running the program. Are you all sure that running the program reliably shows the problem? Or could that be that it accidentally happens to work? Normal code does not rely on the ORG directive as much as one may think.

Analysing the assembler output is a better option unless the behavior can be verified by looking at the documentation.
Post Reply