flat assembler
Message board for the users of flat assembler.

Index > OS Construction > DOS, Com Files, and org 100h?

Author
Thread Post new topic Reply to topic
rhyno_dagreat



Joined: 31 Jul 2006
Posts: 487
Location: Maryland, Unol Daleithiau
rhyno_dagreat 05 Dec 2007, 04:34
I'm not sure where this would go, because it's used by DOS, but I would like to incorporate it into a 16-bit OS.

I've wondered what's the significance of org 100h in COM files, and how/what uses it and what is it used for?

The most I can get out of it is that the data segment offset (maybe code segment offset also??) is set at 100h, but why?

Regards,

Rhyno DaGreat
Post 05 Dec 2007, 04:34
View user's profile Send private message Reply with quote
edfed



Joined: 20 Feb 2006
Posts: 4324
Location: Now
edfed 05 Dec 2007, 05:01
org 100h is for the Program Segment Prefix
this header is 256 bytes long and is created at load time.

.com org 100h is not only for dos, it's just a directive to create .com files, but can be used for other purposes.

to load a .com file at boot, it's exactlly like for other binaries, don't make a jmp seg:100h but a call seg:100h to permit the return to bootloader.
Post 05 Dec 2007, 05:01
View user's profile Send private message Visit poster's website Reply with quote
sinsi



Joined: 10 Aug 2007
Posts: 786
Location: Adelaide
sinsi 05 Dec 2007, 05:12
A DOS .com file
- is loaded in a 64k segment at an offset of 0100 (i.e. it's a binary image with an origin of 0100) and allows no relocations or more than 1 segment (usually).
- CS=DS=ES=SS, with SP=FFFE and IP=0100
- at 0000-00FF is the PSP, which DOS fills in before the jump to SEG:0100

If this is in 'OS Construction' then forget about .com files...
Post 05 Dec 2007, 05:12
View user's profile Send private message Reply with quote
rhyno_dagreat



Joined: 31 Jul 2006
Posts: 487
Location: Maryland, Unol Daleithiau
rhyno_dagreat 05 Dec 2007, 05:29
Okay, but can you give me more info on how ot create the Program Segment Prefix and use it? Or do I not need to create it and just call that data segment at offset 100h?
Post 05 Dec 2007, 05:29
View user's profile Send private message Reply with quote
edfed



Joined: 20 Feb 2006
Posts: 4324
Location: Now
edfed 05 Dec 2007, 05:31
look at this
Post 05 Dec 2007, 05:31
View user's profile Send private message Visit poster's website Reply with quote
rhyno_dagreat



Joined: 31 Jul 2006
Posts: 487
Location: Maryland, Unol Daleithiau
rhyno_dagreat 05 Dec 2007, 05:32
edfed - Thanks!

sinsi - why?
Post 05 Dec 2007, 05:32
View user's profile Send private message Reply with quote
sinsi



Joined: 10 Aug 2007
Posts: 786
Location: Adelaide
sinsi 05 Dec 2007, 05:45
rhyno_dagreat wrote:
sinsi - why?


.com files were a hangover from CP/M and are crap...
but if you want to support them, then I understand why.

Here's the PSP from ralf:
Quote:

Format of Program Segment Prefix (PSP):
Offset Size Description (Table 01378)
00h 2 BYTEs INT 20 instruction for CP/M CALL 0 program termination
the CDh 20h here is often used as a signature for a valid PSP
02h WORD segment of first byte beyond memory allocated to program
04h BYTE (DOS) unused filler
(OS/2) count of fake DOS version returns
05h BYTE CP/M CALL 5 service request (FAR CALL to absolute 000C0h)
BUG: (DOS 2+ DEBUG) PSPs created by DEBUG point at 000BEh
06h WORD CP/M compatibility--size of first segment for .COM files
08h 2 BYTEs remainder of FAR JMP at 05h
0Ah DWORD stored INT 22 termination address
0Eh DWORD stored INT 23 control-Break handler address
12h DWORD DOS 1.1+ stored INT 24 critical error handler address
16h WORD segment of parent PSP
18h 20 BYTEs DOS 2+ Job File Table, one byte per file handle, FFh = closed
2Ch WORD DOS 2+ segment of environment for process (see #01379)
2Eh DWORD DOS 2+ process's SS:SP on entry to last INT 21 call
32h WORD DOS 3+ number of entries in JFT (default 20)
34h DWORD DOS 3+ pointer to JFT (default PSP:0018h)
38h DWORD DOS 3+ pointer to previous PSP (default FFFFFFFFh in 3.x)
used by SHARE in DOS 3.3
3Ch BYTE DOS 4+ (DBCS) interim console flag (see AX=6301h)
Novell DOS 7 DBCS interim flag as set with AX=6301h
(possibly also used by Far East MS-DOS 3.2-3.3)
3Dh BYTE (APPEND) TrueName flag (see INT 2F/AX=B711h)
3Eh BYTE (Novell NetWare) flag: next byte initialized if CEh
(OS/2) capabilities flag
3Fh BYTE (Novell NetWare) Novell task number if previous byte is CEh
40h 2 BYTEs DOS 5+ version to return on INT 21/AH=30h
42h WORD (MSWindows3) selector of next PSP (PDB) in linked list
Windows keeps a linked list of Windows programs only
44h WORD (MSWindows3) "PDB_Partition"
46h WORD (MSWindows3) "PDB_NextPDB"
48h BYTE (MSWindows3) bit 0 set if non-Windows application (WINOLDAP)
49h BYTE unused by DOS versions <= 6.00
4Ch WORD (MSWindows3) "PDB_EntryStack"
4Eh 2 BYTEs unused by DOS versions <= 6.00
50h 3 BYTEs DOS 2+ service request (INT 21/RETF instructions)
53h 2 BYTEs unused in DOS versions <= 6.00
55h 7 BYTEs unused in DOS versions <= 6.00; can be used to make first FCB
into an extended FCB
5Ch 16 BYTEs first default FCB, filled in from first commandline argument
overwrites second FCB if opened
6Ch 16 BYTEs second default FCB, filled in from second commandline argument
overwrites beginning of commandline if opened
7Ch 4 BYTEs unused
80h 128 BYTEs commandline / default DTA
command tail is BYTE for length of tail, N BYTEs for the tail,
followed by a BYTE containing 0Dh

DOS has a function (26h) to create a PSP as well.
Post 05 Dec 2007, 05:45
View user's profile Send private message Reply with quote
Dex4u



Joined: 08 Feb 2005
Posts: 1601
Location: web
Dex4u 05 Dec 2007, 16:07
This may help you, it's how to load a com file in your OS:
Code:
ImageLoadSeg               equ     60h    

Code:
        mov     ax, ImageLoadSeg      mov     es, ax;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; Setup and Run COM program ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;     cli     ; for stack adjustments       mov     ax, es        sub     ax, 10h  ; "org 100h" stuff Smile      mov     es, ax        mov     ds, ax        mov     ss, ax        xor     sp, sp        push     es   push    word 100h     mov     dl, [cs:bsDriveNumber] ; let program know boot drive      sti   retf    

It is set out as follows:
COM program defaults: cs = ds = es = ss = 50h, sp = 0, ip = 100h
Post 05 Dec 2007, 16:07
View user's profile Send private message Reply with quote
rhyno_dagreat



Joined: 31 Jul 2006
Posts: 487
Location: Maryland, Unol Daleithiau
rhyno_dagreat 06 Dec 2007, 00:00
Dex - I was looking through the MiniDOS source, trying to figure all that out, and now it just hit me that you're subtracting 10h from 60h to get 50h which is what everything's being set to.

Thanks.
So you're setting your ES, DS, SS segments to 50h, and the stack pointer to zero, and when you push something onto the stack and call "RET", it's returning to the current value on the stack, right? So the IP is returning to 100h. But how do you make sure that the code segment is 50h? Is that why you're pushing ES? like, first it returns 100h into the IP, and then 50h into CS? Or do you have to load it via a mov instruction?

Sorry for my ignorance, but I'm getting most of what you're telling me.
Post 06 Dec 2007, 00:00
View user's profile Send private message Reply with quote
edfed



Joined: 20 Feb 2006
Posts: 4324
Location: Now
edfed 06 Dec 2007, 16:20
Code:
far_pointer: 
.segment dw ?
.offset dw 100h
    

why don't you load the .com file at [far_pointer] instead of all this?
and call far [far_pointer]?

excuse me for errors, i don't know exactlly how to call a far memory pointer. Sad
Post 06 Dec 2007, 16:20
View user's profile Send private message Visit poster's website Reply with quote
Dex4u



Joined: 08 Feb 2005
Posts: 1601
Location: web
Dex4u 06 Dec 2007, 18:48
The push ES makes the CS 50h and the push 100h makes the IP 100h along with the retf.
If you make the simplest com file example
Code:
org 100hret    

And opened it in a hex editor you would see something like this:
Code:
0000:0000 c3    

Now as you can see its got no header and the first byte is the ret, so com files are loaded to a address then a 100h is sub off the address, then the IP is set to CS:IP, so in the case CS is 50h and IP is 100h (remember the 50h is real (500h).
So everthing in the PSP is put there by dos, in the above case we are just load com file in our OS so we leave the 256bytes blank.

If you do a retf (realmode) it moves the words at top of stack to IP and CS and increments the SP by 4
This is differant than a far call.

Note: You could load the same com file to differant address and like most coders, i am very rusty on coding in real mode.
Also some set SP to 0xfffe, (remember it stores data downwards)
Post 06 Dec 2007, 18:48
View user's profile Send private message Reply with quote
edfed



Joined: 20 Feb 2006
Posts: 4324
Location: Now
edfed 06 Dec 2007, 20:07
if i port ORG 100h, i make some modifications, not really dangerous.
to exit a org 100h code, simply retf.
or if org 100h for PM, then ret, cause of the first layer is for OS, and is at 0, or else. this layer, the PSP for MSDOS, can be what you want, 256 bytes for the link OS-APPLICATION.
like a boot sector.
this header can become a command line, byte 80h of psp is one exemple.
then, the .com program can end with ret, this ret returns to the PSP, the PSP return to the OS.
no?
Code:
push es
push 100h ;0h if "com loader" in PSP.
;where do you remember the caller?
retf
...
;come from os
mov [farc.segment],ax
mov [farc.offset],100h
call far [farc]
;goto os
...
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;far function, imported or connected
org 100h
........
retf    ;just remember to change the exit mechanism before compile!
;ret   ;for test or dosbox
;mov ax,3 ;or old dos
;int 10h  ;
;int 20h  ;
;mov ax,exitcode ;for newer dos
;int 21h 
;etc etc ...
    
Post 06 Dec 2007, 20:07
View user's profile Send private message Visit poster's website Reply with quote
rhyno_dagreat



Joined: 31 Jul 2006
Posts: 487
Location: Maryland, Unol Daleithiau
rhyno_dagreat 06 Dec 2007, 20:19
Thanks! This has helped tremendously.
Post 06 Dec 2007, 20:19
View user's profile Send private message Reply with quote
edfed



Joined: 20 Feb 2006
Posts: 4324
Location: Now
edfed 07 Dec 2007, 06:25
very simple .com bootloader, tested with my 3Dengine ,without mouse.
Code:
        org 7C00h
        mov ax,cs
        mov ss,ax
        mov ds,ax
        mov [bootdrive],dl
        mov ax,27fh
        mov cx,2
        mov dh,0
        push word 1010h
        pop es
        mov bx,0
        int 13h
        cmp [bootdrive],0
        jl @f
        mov dx,3f2h
        mov al,0
        out dx,al
@@:
        jmp 1000h:100h
times 505-($-$$) db 0   ;here need of the file system entry, extended to other sectors
bootdrive db ?          ;current bios drive, file system load the boot sector of with drive?.
nextsectorentry dd 0    ;
dw 0aa55h                      

;your org 100h code here
    


simply put this code before the org 100h directive of .com programs, compile, and write the full .bin from boot sector to end of file

my bootwriter or any image writer can do that, rawwrite, your own bootwriter, ...

and then boot from this drive.

it works.
so, you can quietlly test your Real Mode interface from DOS or compatible.
Post 07 Dec 2007, 06:25
View user's profile Send private message Visit poster's website Reply with quote
rhyno_dagreat



Joined: 31 Jul 2006
Posts: 487
Location: Maryland, Unol Daleithiau
rhyno_dagreat 07 Dec 2007, 17:00
edfed wrote:
very simple .com bootloader, tested with my 3Dengine ,without mouse.
Code:
        org 7C00h
        mov ax,cs
        mov ss,ax
        mov ds,ax
        mov [bootdrive],dl
        mov ax,27fh
        mov cx,2
        mov dh,0
        push word 1010h
        pop es
        mov bx,0
        int 13h
        cmp [bootdrive],0
        jl @f
        mov dx,3f2h
        mov al,0
        out dx,al
@@:
        jmp 1000h:100h
times 505-($-$$) db 0   ;here need of the file system entry, extended to other sectors
bootdrive db ?          ;current bios drive, file system load the boot sector of with drive?.
nextsectorentry dd 0    ;
dw 0aa55h                      

;your org 100h code here
    


simply put this code before the org 100h directive of .com programs, compile, and write the full .bin from boot sector to end of file

my bootwriter or any image writer can do that, rawwrite, your own bootwriter, ...

and then boot from this drive.

it works.
so, you can quietlly test your Real Mode interface from DOS or compatible.


Thanks edfed! This is interesting.
Post 07 Dec 2007, 17:00
View user's profile Send private message Reply with quote
edfed



Joined: 20 Feb 2006
Posts: 4324
Location: Now
edfed 07 Dec 2007, 23:21
i've made an error, before to jmmp to .com code, we must set ds and ss to be at .com segment
Post 07 Dec 2007, 23:21
View user's profile Send private message Visit poster's website Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2023, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.