Page 1 of 1

ORG and Segment descriptors

Posted: Tue Sep 07, 2004 4:50 pm
by chris
I've written a simple bootloader that reads another small program off the second sector of my floppy. I use the ORG directive in the bootloader, and set some segment registers equal to CS in both. At their current state they both work. What I want to know is why I don't need the ORG directive in prog.s. Also, do I really need to set the segment registers to CS? If so, how come?

loader.s

Code: Select all

BITS 16
ORG 07C00h

        jmp 00000h:main

main:
        mov     ax, cs
        mov     ds, ax
        mov     es, ax

reset:
        xor     ax, ax
        xor     dl, dl
        int     13h
        jc      reset

loadSector:
        mov     ax, 1000h
        mov     es, ax
        mov     bx, 0

        mov     ah, 2
        mov     al, 1
        mov     ch, 0
        mov     cl, 2
        mov     dh, 0
        mov     dl, 0
        int     13h

        jc      loadSector

        jmp     1000h:0000

times 512 - ($ - $$) - 2 db 0

        dw 0AA55h
prog.s

Code: Select all

       mov     ax, cs
        mov     ds, ax

        mov     si, helloWorld
        jmp     printString

        jmp     $

printString:
        mov     ah, 00Eh
        mov     bh, 000h
        mov     bl, 007h

.nextChar:
        lodsb

        cmp     al, 0
        je      .doneString

        int     10h
        jmp     .nextChar

.doneString
        ret

helloWorld db 'Hello, World', 13, 10, 0

Re:ORG and Segment descriptors

Posted: Tue Sep 07, 2004 6:01 pm
by Dreamsmith
chris wrote:What I want to know is why I don't need the ORG directive in prog.s.
Because you're setting the segment registers to the appropriate values. ORG is only needed for code that executes from a fixed, absolute address. If you setup and use segments properly, code can be loaded to and run from any address. (Well, in real mode, only addresses evenly divisible by 16.)
chris wrote:Also, do I really need to set the segment registers to CS? If so, how come?
Yes, because your code appears to be written targetting what we used to call the "tiny" memory model back in the DOS days. Code doesn't have to be written that way, but since your code is, you do need to set the segment registers appropriately.

Re:ORG and Segment descriptors

Posted: Tue Sep 07, 2004 6:07 pm
by chris
Hi Dreamsmith,

Thank you for replying.
Dreamsmith wrote: Because you're setting the segment registers to the appropriate values. ORG is only needed for code that executes from a fixed, absolute address. If you setup and use segments properly, code can be loaded to and run from any address. (Well, in real mode, only addresses evenly divisible by 16.)
Can I omit setting the registers in the bootloader or program (ie. Does the bootloader need them set to function, and does the program need to set them again to work?).
Yes, because your code appears to be written targetting what we used to call the "tiny" memory model back in the DOS days. Code doesn't have to be written that way, but since your code is, you do need to set the segment registers appropriately.
Could you explain a bit further? How can I write it differently?

Re:ORG and Segment descriptors

Posted: Tue Sep 07, 2004 6:57 pm
by Dreamsmith
chris wrote:Can I omit setting the registers in the bootloader or program (ie. Does the bootloader need them set to function, and does the program need to set them again to work?).
Answering last question first: Yes, your program needs to set them again, because it requires them to point somewhere different than your bootloader. Note that your bootloader initialized them to zero, whereas your program is setting them to 0x1000. Since your program assumes its segment registers will be identical, and pointing to the start of the code (ala the DOS tiny memory model), either it needs to set them that way, or your bootloader needs to set them up for the program before calling it.
Could you explain a bit further? How can I write it differently?
Change your assumptions. How would you prefer your memory to be set up? Would you prefer a flat memory model? If so, set the segment registers to zero at bootup and don't fiddle with them again. If you do this, then you'll have to put an ORG statement in prog.s, and make sure it's loaded in the appropriate place and executed from there, make sure the address doesn't conflict with anything else, etc. Would you prefer something like the DOS small model, where your code and data lie in different segments (allowing you to use more memory than in tiny)? Then make that change.

How you would write it differently would involve first deciding what you want to do different. Then do it. What exactly you do will depend entirely on what you decide.