Subversion Repositories SvarDOS

Rev

Rev 465 | Rev 517 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 465 Rev 490
1
;
1
;
2
; rmod - resident module of the SvarCOM command interpreter
2
; rmod - resident module of the SvarCOM command interpreter
3
;
3
;
4
; Copyright (C) 2021 Mateusz Viste
4
; Copyright (C) 2021 Mateusz Viste
5
; MIT license
5
; MIT license
6
;
6
;
7
; this is installed in memory by the transient part of SvarCOM. it has only
7
; this is installed in memory by the transient part of SvarCOM. it has only
8
; two jobs: providing a resident buffer for command history, environment, etc
8
; two jobs: providing a resident buffer for command history, environment, etc
9
; and respawning COMMAND.COM whenever necessary.
9
; and respawning COMMAND.COM whenever necessary.
10
 
10
 
11
CPU 8086
11
CPU 8086
12
org 0x100
12
org 0x100
13
 
13
 
14
PSP_ENVSEG equ 0x2C
14
PSP_ENVSEG equ 0x2C
15
 
15
 
16
section .text    ; all goes into code segment
16
section .text    ; all goes into code segment
17
 
17
 
18
                 ; offset
18
                 ; offset
19
SIG1 dw 0x1983   ;  +0
19
SIG1 dw 0x1983   ;  +0
20
SIG2 dw 0x1985   ;  +2
20
SIG2 dw 0x1985   ;  +2
21
SIG3 dw 0x2017   ;  +4
21
SIG3 dw 0x2017   ;  +4
22
SIG4 dw 0x2019   ;  +6
22
SIG4 dw 0x2019   ;  +6  this acts also as a guardval to detect stack overflows
23
 
23
 
-
 
24
; DOS int 21h functions that I use require at least 40 bytes of stack under
-
 
25
; DOS-C (FreeDOS) kernel, so here I reserve 64 bytes juste to be sure
-
 
26
STACKBUF db "XXX  SVARCOM RMOD BY MATEUSZ VISTE  XXXXXXXXXXXXXXXXXXXXXXXXXXXX"
24
FFU_UNUSED dw 0  ;  +8
27
STACKPTR dw 0
25
 
28
 
26
; exit code of last application
29
; exit code of last application
27
LEXCODE  dw 0    ; +0Ah
30
LEXCODE  dw 0    ; +4Ah
28
 
31
 
29
; offset of the COMSPEC variable in the environment block, 0 means "use
32
; offset of the COMSPEC variable in the environment block, 0 means "use
30
; boot drive". this value is patched by the transient part of COMMAND.COM
33
; boot drive". this value is patched by the transient part of COMMAND.COM
31
COMSPECPTR dw 0  ; +0Ch
34
COMSPECPTR dw 0  ; +4Ch
32
 
35
 
33
; fallback COMSPEC string used if no COMPSEC is present in the environment
36
; fallback COMSPEC string used if no COMPSEC is present in the environment
34
; drive. drive is patched by the transient part of COMMAND.COM
37
; drive. drive is patched by the transient part of COMMAND.COM
35
COMSPECBOOT db "@:\COMMAND.COM", 0 ; +0Eh
38
COMSPECBOOT db "@:\COMMAND.COM", 0 ; +4Eh
36
 
39
 
37
; ExecParamRec used by INT 21h, AX=4b00 (load and execute program), 14 bytes:
40
; ExecParamRec used by INT 21h, AX=4b00 (load and execute program), 14 bytes:
38
;  offset  size  content
41
;  offset  size  content
39
;     +0     2   segment of environment for child (0 = current)
42
;     +0     2   segment of environment for child (0 = current)
40
;     +2     4   address of command line to place at PSP:0080
43
;     +2     4   address of command line to place at PSP:0080
41
;     +6     4   address of an FCB to be placed at PSP:005c
44
;     +6     4   address of an FCB to be placed at PSP:005c
42
;    +0Ah    4   address of an FCB to be placed at PSP:006c
45
;    +0Ah    4   address of an FCB to be placed at PSP:006c
43
EXEC_PARAM_REC db 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0   ; +1Dh
46
EXEC_PARAM_REC db 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0   ; +5Dh
44
 
47
 
45
; Program to execute, preset by SvarCOM (128 bytes, ASCIIZ)  ; +2Bh
48
; Program to execute, preset by SvarCOM (128 bytes, ASCIIZ)  ; +6Bh
46
EXECPROG dd 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
49
EXECPROG dd 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
47
 
50
 
48
skipsig:         ; +ABh
51
skipsig:         ; +EBh
49
 
52
 
50
; set up CS=DS=SS and point SP to my private stack buffer
53
; set up CS=DS=SS and point SP to my private stack buffer
51
mov ax, cs
54
mov ax, cs
52
mov ds, ax
55
mov ds, ax
53
mov es, ax
56
mov es, ax
54
mov ss, ax
57
mov ss, ax
55
mov sp, STACKPTR
58
mov sp, STACKPTR
56
 
59
 
57
; should I executed command.com or a pre-set application?
60
; should I executed command.com or a pre-set application?
58
or [EXECPROG], byte 0
61
or [EXECPROG], byte 0
59
jz EXEC_COMMAND_COM
62
jz EXEC_COMMAND_COM
60
 
63
 
61
; TODO: perhaps I should call the DOS SetPSP function here? But if I do, the
64
; TODO: perhaps I should call the DOS SetPSP function here? But if I do, the
62
;       int 21h, ah=50h call freezes...
65
;       int 21h, ah=50h call freezes...
63
;mov ah, 0x50           ; DOS 2+ -- Set PSP
66
;mov ah, 0x50           ; DOS 2+ -- Set PSP
64
;mov bx, cs
67
;mov bx, cs
65
;int 0x21
68
;int 0x21
66
 
69
 
67
; exec an application preset (by SvarCOM) in the ExecParamRec
70
; exec an application preset (by SvarCOM) in the ExecParamRec
68
mov ax, 0x4B00         ; DOS 2+ - load & execute program
71
mov ax, 0x4B00         ; DOS 2+ - load & execute program
69
mov dx, EXECPROG       ; DS:DX  - ASCIZ program name (preset at PSP[already)
72
mov dx, EXECPROG       ; DS:DX  - ASCIZ program name (preset at PSP[already)
70
mov bx, EXEC_PARAM_REC ; ES:BX  - parameter block pointer
73
mov bx, EXEC_PARAM_REC ; ES:BX  - parameter block pointer
71
int 0x21
74
int 0x21
72
mov [cs:EXECPROG], byte 0 ; do not run app again (+DS might have been changed)
75
mov [cs:EXECPROG], byte 0 ; do not run app again (+DS might have been changed)
73
 
76
 
74
jmp short skipsig      ; enforce valid ds/ss/etc (can be lost after int 21,4b)
77
jmp short skipsig      ; enforce valid ds/ss/etc (can be lost after int 21,4b)
75
 
78
 
76
EXEC_COMMAND_COM:
79
EXEC_COMMAND_COM:
77
 
80
 
78
; collect the exit code of previous application
81
; collect the exit code of previous application
79
mov ah, 0x4D
82
mov ah, 0x4D
80
int 0x21
83
int 0x21
81
xor ah, ah          ; clear out termination status, I only want the exit code
84
xor ah, ah          ; clear out termination status, I only want the exit code
82
mov [LEXCODE], ax
85
mov [LEXCODE], ax
83
 
86
 
84
; zero out the exec param block (14 bytes)
87
; zero out the exec param block (14 bytes)
85
mov al, 0              ; byte to write
88
mov al, 0              ; byte to write
86
mov cx, 14             ; how many times
89
mov cx, 14             ; how many times
87
mov di, EXEC_PARAM_REC ; ES:DI = destination
90
mov di, EXEC_PARAM_REC ; ES:DI = destination
88
cld                    ; stosb must move forward
91
cld                    ; stosb must move forward
89
rep stosb              ; repeat cx times
92
rep stosb              ; repeat cx times
90
 
93
 
91
; preset the default COMSPEC pointer to ES:DX (ES is already set to DS)
94
; preset the default COMSPEC pointer to ES:DX (ES is already set to DS)
92
mov dx, COMSPECBOOT
95
mov dx, COMSPECBOOT
93
 
96
 
94
; do I have a valid COMSPEC?
97
; do I have a valid COMSPEC?
95
or [COMSPECPTR], word 0
98
or [COMSPECPTR], word 0
96
jz USEDEFAULTCOMSPEC
99
jz USEDEFAULTCOMSPEC
97
; set ES:DX to actual COMSPEC (in env segment)
100
; set ES:DX to actual COMSPEC (in env segment)
98
mov es, [PSP_ENVSEG]
101
mov es, [PSP_ENVSEG]
99
mov dx, [COMSPECPTR]
102
mov dx, [COMSPECPTR]
100
USEDEFAULTCOMSPEC:
103
USEDEFAULTCOMSPEC:
101
 
104
 
102
; prepare the exec param block
105
; prepare the exec param block
103
mov ax, [PSP_ENVSEG]
106
mov ax, [PSP_ENVSEG]
104
mov [EXEC_PARAM_REC], ax
107
mov [EXEC_PARAM_REC], ax
105
mov [EXEC_PARAM_REC+2], word CMDTAIL
108
mov [EXEC_PARAM_REC+2], word CMDTAIL
106
mov [EXEC_PARAM_REC+4], cs
109
mov [EXEC_PARAM_REC+4], cs
107
 
110
 
108
; execute command.com
111
; execute command.com
109
mov ax, 0x4B00         ; DOS 2+ - load & execute program
112
mov ax, 0x4B00         ; DOS 2+ - load & execute program
110
push es                ;
113
push es                ;
111
pop ds                 ;
114
pop ds                 ;
112
;mov dx, COMSPEC       ; DS:DX  - ASCIZ program name (preset already)
115
;mov dx, COMSPEC       ; DS:DX  - ASCIZ program name (preset already)
113
push cs
116
push cs
114
pop es
117
pop es
115
mov bx, EXEC_PARAM_REC ; ES:BX  - parameter block pointer
118
mov bx, EXEC_PARAM_REC ; ES:BX  - parameter block pointer
116
int 0x21
119
int 0x21
117
 
120
 
118
; if all went well, jump back to start
121
; if all went well, jump back to start
119
jnc skipsig
122
jnc skipsig
120
 
123
 
121
; restore DS=CS
124
; restore DS=CS
122
mov bx, cs
125
mov bx, cs
123
mov ds, bx
126
mov ds, bx
124
 
127
 
125
; update error string so it contains the error number
128
; update error string so it contains the error number
126
add al, '0'
129
add al, '0'
127
mov [ERRLOAD + 4], al
130
mov [ERRLOAD + 4], al
128
 
131
 
129
; display error message
132
; display error message
130
mov ah, 0x09
133
mov ah, 0x09
131
mov dx, ERRLOAD
134
mov dx, ERRLOAD
132
int 0x21
135
int 0x21
133
 
136
 
134
; wait for keypress
137
; wait for keypress
135
mov ah, 0x08
138
mov ah, 0x08
136
int 0x21
139
int 0x21
137
 
140
 
138
; back to program start
141
; back to program start
139
jmp skipsig
142
jmp skipsig
140
 
143
 
141
; command.com tail arguments, in PSP format: length byte followed by args and
144
; command.com tail arguments, in PSP format: length byte followed by args and
142
; terminated with \r) - a single 0x0A byte is passed so SvarCOM knows it is
145
; terminated with \r) - a single 0x0A byte is passed so SvarCOM knows it is
143
; called as respawn (as opposed to being invoked as a normal application)
146
; called as respawn (as opposed to being invoked as a normal application)
144
; this allows multiple copies of SvarCOM to stack upon each other.
147
; this allows multiple copies of SvarCOM to stack upon each other.
145
CMDTAIL db 0x01, 0x0A, 0x0D
148
CMDTAIL db 0x01, 0x0A, 0x0D
146
 
149
 
147
ERRLOAD db "ERR x, FAILED TO LOAD COMMAND.COM", 13, 10, '$'
150
ERRLOAD db "ERR x, FAILED TO LOAD COMMAND.COM", 13, 10, '$'
148
 
-
 
149
; DOS int 21h functions that I use require at least 32 bytes of stack, here I
-
 
150
; allocate 64 bytes to be sure
-
 
151
STACKBUF db "0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF"
-
 
152
STACKPTR db "xx"
-
 
153
 
151