Page 1 of 1

ORG Directive and Real Mode Memory Segmentation

Posted: Mon Aug 03, 2015 9:38 am
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?

Re: ORG Directive and Real Mode Memory Segmentation

Posted: Mon Aug 03, 2015 11:58 am
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.

Re: ORG Directive and Real Mode Memory Segmentation

Posted: Mon Aug 03, 2015 12:08 pm
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


Re: ORG Directive and Real Mode Memory Segmentation

Posted: Mon Aug 03, 2015 12:43 pm
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.

Re: ORG Directive and Real Mode Memory Segmentation

Posted: Mon Aug 03, 2015 12:50 pm
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.