ORG Directive and Real Mode Memory Segmentation

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
inixsoftware
Member
Member
Posts: 32
Joined: Fri Jan 31, 2014 8:21 am

ORG Directive and Real Mode Memory Segmentation

Post by inixsoftware »

From the MikeOS tutorial:

Code: Select all

[BITS 16]

mov ax, 07C0h 		; Set up 4K stack space after this bootloader
add ax, 288		; (4096 + 512) / 16 bytes per paragraph
mov ss, ax
mov sp, 4096

mov ax, 07C0h		; Set data segment to where we're loaded
mov ds, ax
From my understanding, the segment is 0x08e0:0x0000 or just 0x08e00 when the calculation is performed.
However, when the [ORG 0x7c00] directive is added, does it affect segmentation offset?

If it does, then is this code, essentially equivalent to the original:

Code: Select all

[BITS 16]
[ORG 0x7c00]

mov ax, 0x120
mov ss, ax
mov sp, 0x1000

; The segment is: 0x0120:0x7c00 right? So that translates to 0x1200 + 0x7c00 = 0x8e00, right?
; For the data segment, it is: 0x000:0x7c00 which is just 0x7c00.

mov ax, 0
mov ds, ax

Is this correct, or have I made some segmentation mistake?
mikegonta
Member
Member
Posts: 229
Joined: Thu May 19, 2011 5:13 am
Contact:

Re: ORG Directive and Real Mode Memory Segmentation

Post by mikegonta »

inixsoftware wrote:From the MikeOS tutorial:

Code: Select all

[BITS 16]

mov ax, 07C0h 		; Set up 4K stack space after this bootloader
add ax, 288		; (4096 + 512) / 16 bytes per paragraph
mov ss, ax
mov sp, 4096

mov ax, 07C0h		; Set data segment to where we're loaded
mov ds, ax
From my understanding, the segment is 0x08e0:0x0000 or just 0x08e00 when the calculation is performed.
Actually, the stack is located at 0x8E0:0x1000 or the absolute address of 0x9E00.
inixsoftware wrote: However, when the [ORG 0x7c00] directive is added, does it affect segmentation offset?
The org statement is used by the assembler to calculate the assembled offset relative to the segment. This is how simple code can be
assembled without linking.
Mike Gonta
look and see - many look but few see

https://mikegonta.com
inixsoftware
Member
Member
Posts: 32
Joined: Fri Jan 31, 2014 8:21 am

Re: ORG Directive and Real Mode Memory Segmentation

Post by inixsoftware »

So, does the [ORG 0x7c00] have any effect on the code initializing the SS:SP segment?
Also, would this code be the same:

Code: Select all

[BITS 16]
[ORG 0x7c00]

start:
   mov ax, 0x07c0
   add ax, 0x0120

   mov ss, ax
   mov sp, 0x1000

   xor ax, ax
   mov ds, ax
as the original code:

Code: Select all

[BITS 16]

start:
   mov ax, 07C0h		; Set up 4K stack space after this bootloader
   add ax, 288		; (4096 + 512) / 16 bytes per paragraph
   mov ss, ax
   mov sp, 4096

   mov ax, 07C0h		; Set data segment to where we're loaded
   mov ds, ax

jnc100
Member
Member
Posts: 775
Joined: Mon Apr 09, 2007 12:10 pm
Location: London, UK
Contact:

Re: ORG Directive and Real Mode Memory Segmentation

Post by jnc100 »

ORG only has an effect when you use labels in your source code. As you are using hard-coded numbers, it does not affect the outputted code (and so your two different versions will assemble differently). When you use labels, the assembler calculates their address based on how far into the file it finds them. If you optionally provide an ORG address, it will add this number to the addresses of the labels it calculates.

Note that this will probably have less effect than you think - many instructions which use labels (e.g. call/jmp) will actually use relative addresses i.e. the difference between two code points in the same file. Changing the ORG here will not change the difference between them.

Regards,
John.
User avatar
SpyderTL
Member
Member
Posts: 1074
Joined: Sun Sep 19, 2010 10:05 pm

Re: ORG Directive and Real Mode Memory Segmentation

Post by SpyderTL »

I doubt SS:SP will be affected by the [ORG] statement at all.

This is because the SS and SP registers are set by either the BIOS (if you are writing a boot loader), or the OS (if you are writing an application). You can change SS and SP in your code if you want, but there is no way, in your code, to tell the caller what SS/SP values you want to use.
Project: OZone
Source: GitHub
Current Task: LIB/OBJ file support
"The more they overthink the plumbing, the easier it is to stop up the drain." - Montgomery Scott
Post Reply