2022-08-03 18:41:06 +00:00
|
|
|
#
|
|
|
|
! $Source$
|
|
|
|
! $State$
|
|
|
|
! $Revision$
|
|
|
|
|
|
|
|
! Declare segments (the order is important).
|
|
|
|
|
|
|
|
.sect .text
|
|
|
|
.sect .rom
|
|
|
|
.sect .data
|
|
|
|
.sect .bss
|
|
|
|
|
|
|
|
.sect .text
|
|
|
|
#define STACK_BUFFER 128 /* number of bytes to leave for stack */
|
|
|
|
|
2022-08-10 14:14:12 +00:00
|
|
|
! g 18b to break at the retf before coming here
|
2022-08-03 18:41:06 +00:00
|
|
|
begtext:
|
2022-08-10 14:14:12 +00:00
|
|
|
! On entry, the stub has cs and ds pointing at the 32-bit
|
|
|
|
! segment, but ss is still pointing at the 16-bit segment.
|
2022-08-08 20:03:12 +00:00
|
|
|
!
|
2022-08-10 14:14:12 +00:00
|
|
|
! ax: pmode code segment of stub
|
2022-08-17 20:34:06 +00:00
|
|
|
! bx: pmode data segment of stub
|
|
|
|
! cx: rmode segment of stub
|
2022-08-23 14:35:43 +00:00
|
|
|
! dx: pointer to realloc routine (in stub)
|
2022-08-17 20:34:06 +00:00
|
|
|
! si: pointer to interrupt routine (in stub)
|
|
|
|
! di: pointer to transfer buffer (in stub)
|
2022-08-23 14:35:43 +00:00
|
|
|
! bp: rmode PSP segment
|
2022-08-10 14:14:12 +00:00
|
|
|
|
|
|
|
! Resize the segment to include the BSS.
|
|
|
|
|
2022-08-23 14:35:43 +00:00
|
|
|
o16 mov (realloc_ptr+4), ax
|
|
|
|
mov (realloc_ptr+0), edx
|
|
|
|
o16 mov (pmode_cs), ax
|
|
|
|
o16 mov (pmode_ds), bx
|
|
|
|
o16 mov (rmode), cx
|
|
|
|
o16 mov (interrupt_ptr+4), ax
|
|
|
|
mov (interrupt_ptr+0), esi
|
|
|
|
o16 mov (transfer_buffer_ptr), di
|
2022-08-17 20:34:06 +00:00
|
|
|
|
2022-08-17 22:24:08 +00:00
|
|
|
mov eax, __end
|
2022-08-23 14:35:43 +00:00
|
|
|
callf (realloc_ptr)
|
2022-08-08 20:03:12 +00:00
|
|
|
|
|
|
|
! Clear BSS.
|
2022-08-10 14:14:12 +00:00
|
|
|
|
|
|
|
mov edi, begbss
|
2022-08-24 10:17:15 +00:00
|
|
|
mov ecx, endbss+3
|
2022-08-10 14:14:12 +00:00
|
|
|
sub ecx, edi
|
2022-08-24 10:17:15 +00:00
|
|
|
shr ecx, 2
|
2022-08-10 14:14:12 +00:00
|
|
|
xor eax, eax
|
2022-08-08 20:03:12 +00:00
|
|
|
cld
|
2022-08-24 10:17:15 +00:00
|
|
|
rep stos
|
2022-08-08 20:03:12 +00:00
|
|
|
|
2022-08-10 14:14:12 +00:00
|
|
|
! It's now safe to switch stacks.
|
|
|
|
|
|
|
|
cli
|
|
|
|
mov eax, ds
|
|
|
|
mov ss, eax
|
|
|
|
mov sp, .stack
|
|
|
|
sti
|
|
|
|
|
|
|
|
! Create a handle for low 1MB access.
|
|
|
|
|
|
|
|
o16 mov ax, 0x0000
|
|
|
|
o16 mov cx, 1
|
|
|
|
int 0x31 ! allocate LDT
|
2022-08-24 10:17:15 +00:00
|
|
|
o16 mov (.doshandle), ax
|
2022-08-10 14:14:12 +00:00
|
|
|
mov es, ax
|
2022-08-24 10:17:15 +00:00
|
|
|
xchg ebx, eax
|
2022-08-10 14:14:12 +00:00
|
|
|
|
|
|
|
xor ecx, ecx
|
|
|
|
xor edx, edx
|
|
|
|
o16 mov ax, 0x0007
|
|
|
|
int 0x31 ! set base address to 0
|
2022-08-23 14:35:43 +00:00
|
|
|
o16 dec dx
|
|
|
|
o16 mov cx, 0x000f
|
2022-08-10 14:14:12 +00:00
|
|
|
o16 mov ax, 0x0008
|
|
|
|
int 0x31 ! set limit to 1MB
|
|
|
|
|
|
|
|
mov cx, ds
|
|
|
|
and cx, 3
|
|
|
|
shl cx, 5
|
|
|
|
or cx, 0xc093 ! 32-bit, big, data, r/w, expand-up
|
|
|
|
mov ax, 0x0009
|
|
|
|
int 0x31 ! set descriptor access rights
|
|
|
|
|
|
|
|
! Locate the PSP.
|
|
|
|
|
2022-08-23 14:35:43 +00:00
|
|
|
movzx esi, bp
|
2022-08-11 21:36:23 +00:00
|
|
|
shl esi, 4 ! convert to linear address
|
|
|
|
|
|
|
|
! Copy the whole thing into 32-bit memory.
|
|
|
|
|
|
|
|
mov ecx, 0x100/4
|
2022-08-26 17:29:54 +00:00
|
|
|
mov edi, .psp
|
2022-08-11 21:36:23 +00:00
|
|
|
cld
|
|
|
|
1:
|
|
|
|
eseg lods
|
|
|
|
mov (edi), eax
|
|
|
|
add edi, 4
|
|
|
|
loop 1b
|
|
|
|
|
|
|
|
! Find the environment.
|
|
|
|
|
2022-08-26 17:29:54 +00:00
|
|
|
mov es, (.psp + 0x002C) ! converted to pmode segment
|
2022-08-11 21:36:23 +00:00
|
|
|
|
|
|
|
! Count the size of the environment variable block.
|
|
|
|
|
|
|
|
xorb al, al ! byte to test for
|
|
|
|
xor edi, edi
|
|
|
|
xor edx, edx
|
|
|
|
cld
|
|
|
|
mov ecx, -1
|
2022-08-08 20:03:12 +00:00
|
|
|
scasb
|
2022-08-11 21:36:23 +00:00
|
|
|
jz 2f ! empty environment
|
|
|
|
1:
|
2022-08-08 20:03:12 +00:00
|
|
|
repnz scasb
|
2022-08-11 21:36:23 +00:00
|
|
|
inc edx
|
|
|
|
scasb
|
|
|
|
jnz 1b
|
|
|
|
2:
|
|
|
|
add edi, 2 ! skip the mysterious word
|
|
|
|
repnz scasb ! program name
|
|
|
|
|
|
|
|
! Copy the whole environment block onto the stack.
|
|
|
|
|
|
|
|
mov esi, edi
|
|
|
|
add esi, 3
|
|
|
|
and esi, ~3 ! round up
|
|
|
|
jz empty_environment
|
|
|
|
mov ecx, esi
|
|
|
|
shr ecx, 2 ! number of dwords
|
2022-08-08 20:03:12 +00:00
|
|
|
std
|
2022-08-11 21:36:23 +00:00
|
|
|
sub esi, 4
|
|
|
|
1:
|
|
|
|
eseg lods
|
|
|
|
push eax
|
|
|
|
loop 1b
|
|
|
|
empty_environment:
|
|
|
|
mov ecx, esp ! environment data
|
2022-08-08 20:03:12 +00:00
|
|
|
|
|
|
|
! Reset DF and es properly.
|
2022-08-11 21:36:23 +00:00
|
|
|
|
2022-08-08 20:03:12 +00:00
|
|
|
cld
|
|
|
|
push ss
|
|
|
|
pop es
|
|
|
|
|
|
|
|
! Reserve space for argc and the argv and envp pointers on the
|
|
|
|
! stack. These will be passed to __m_a_i_n later.
|
2022-08-11 21:36:23 +00:00
|
|
|
|
|
|
|
sub esp, 3*4
|
|
|
|
mov eax, esp
|
2022-08-08 20:03:12 +00:00
|
|
|
|
|
|
|
! Build up argc, argv[], and envp[].
|
2022-08-11 21:36:23 +00:00
|
|
|
push eax ! output buffer for argc, argv, envp
|
|
|
|
push 3 ! MS-DOS version
|
|
|
|
push ecx ! env. string data
|
|
|
|
push edx ! count of env. vars.
|
2022-08-26 17:29:54 +00:00
|
|
|
push .psp+0x80 ! raw command line
|
2022-08-08 20:03:12 +00:00
|
|
|
call __sys_initmain
|
2022-08-11 21:36:23 +00:00
|
|
|
add esp, 5*4
|
2022-08-08 20:03:12 +00:00
|
|
|
|
|
|
|
! Bail out if something went wrong.
|
2022-08-17 22:24:08 +00:00
|
|
|
test eax, eax
|
2022-08-08 20:03:12 +00:00
|
|
|
jnz no_room
|
|
|
|
|
|
|
|
! argc, argv, and envp are now at the stack top. Now go.
|
|
|
|
call __m_a_i_n
|
2022-08-18 19:21:33 +00:00
|
|
|
add sp, 3*4
|
2022-08-08 20:03:12 +00:00
|
|
|
push ax
|
|
|
|
call _exit
|
2022-08-03 18:41:06 +00:00
|
|
|
|
|
|
|
no_room:
|
2022-08-08 20:03:12 +00:00
|
|
|
mov dx, no_room_msg
|
2022-08-10 14:14:12 +00:00
|
|
|
!call dos_msg
|
2022-08-08 20:03:12 +00:00
|
|
|
movb al, -1
|
|
|
|
jmp al_exit
|
2022-08-03 18:41:06 +00:00
|
|
|
|
2022-08-08 20:03:12 +00:00
|
|
|
! Exit.
|
2022-08-03 18:41:06 +00:00
|
|
|
.define __exit
|
|
|
|
.extern __exit
|
|
|
|
.define EXIT
|
|
|
|
.extern EXIT
|
|
|
|
__exit:
|
|
|
|
EXIT:
|
2022-08-08 20:03:12 +00:00
|
|
|
pop bx
|
|
|
|
pop ax
|
2022-08-03 18:41:06 +00:00
|
|
|
al_exit:
|
2022-08-08 20:03:12 +00:00
|
|
|
movb ah, 0x4c
|
|
|
|
int 0x21
|
2022-08-03 18:41:06 +00:00
|
|
|
|
2022-08-17 20:34:06 +00:00
|
|
|
! These must be in the code segment due to bootstrap issues.
|
|
|
|
realloc_ptr: .space 6 ! far
|
|
|
|
interrupt_ptr: .space 6 ! far
|
|
|
|
transfer_buffer_ptr: .space 2 ! near
|
|
|
|
rmode: .space 2
|
|
|
|
pmode_cs: .space 2
|
|
|
|
pmode_ds: .space 2
|
|
|
|
|
2022-08-17 22:24:08 +00:00
|
|
|
.define realloc_ptr
|
2022-08-17 20:34:06 +00:00
|
|
|
.define interrupt_ptr
|
|
|
|
.define rmode
|
|
|
|
.define pmode_cs
|
|
|
|
.define pmode_ds
|
|
|
|
.define transfer_buffer_ptr
|
2022-08-10 14:14:12 +00:00
|
|
|
|
2022-08-03 18:41:06 +00:00
|
|
|
! Define symbols at the beginning of our various segments, so that we can find
|
|
|
|
! them. (Except .text, which has already been done.)
|
|
|
|
|
|
|
|
.define begtext, begdata, begbss
|
|
|
|
.sect .data; begdata:
|
|
|
|
.sect .rom; begrom:
|
|
|
|
.sect .bss; begbss:
|
|
|
|
|
|
|
|
.sect .rom
|
|
|
|
|
|
|
|
! Some text messages.
|
|
|
|
bad_sys_msg: .ascii 'Bad DOS$'
|
|
|
|
no_room_msg: .ascii 'No room$'
|
|
|
|
|
|
|
|
! Some magic data. All EM systems need these.
|
|
|
|
|
|
|
|
.define .trppc, .ignmask, _errno
|
|
|
|
.comm .trppc, 4
|
|
|
|
.comm .ignmask, 4
|
|
|
|
.comm _errno, 4
|
2022-08-08 20:03:12 +00:00
|
|
|
|
2022-08-10 14:14:12 +00:00
|
|
|
.comm .doshandle, 2
|
2022-08-26 17:29:54 +00:00
|
|
|
.comm .psp, 256
|
2022-08-10 14:14:12 +00:00
|
|
|
|
|
|
|
.sect .bss
|
2022-08-17 20:34:06 +00:00
|
|
|
.space 32*1024
|
2022-08-10 14:14:12 +00:00
|
|
|
.stack:
|
|
|
|
|
2022-08-08 20:03:12 +00:00
|
|
|
! vim: ts=4 sw=4 et ft=asm
|
|
|
|
|