* added compilers lcc and bcc (linux86)
[mascara-docs.git] / i86 / mtx / mtx / samples / LAB5.timer / ts.s
blob636a3a2b6136a7bfcddc2bc6ab1bb09ae9a9bbb1
1 BOOTSEG = 0x1000
2 .globl begtext, begdata, begbss !! needed by linker
4 ! IMPORTS and EXPORTS
5 .globl _resetVideo,_getc,_putc,_diskr,_setes,_inces
6 .globl _main,_prints
7 .globl _tswitch,_running,_scheduler
9 .globl _int80h,_kcinth
10 .globl _goUmode
11 .globl _get_byte,_put_byte
12 .globl _proc, _procsize
13 .globl _color
14 .globl _lock, _unlock,_restore, _in_byte, _out_byte
15 .globl _inkmode
16 .globl _tinth, _thandler
18 .text !! these tell as:
19 begtext: !! text,data,bss segments
20 .data !! are all the same.
21 begdata:
22 .bss
23 begbss:
24 .text
26 mov ax,cs ! establish segments
27 mov ds,ax ! we know ES,CS=0x1000. Let DS=CS
28 mov ss,ax ! SS = CS ===> all point to 0x1000
29 mov es,ax
31 mov sp,#_proc ! SP -> proc[0].kstack HIGH end
32 add sp,_procsize
34 mov ax,#0x0003
35 int #0x10
37 call _main ! call main[] in C
39 ! if ever return, just hang
40 mov ax, #msg
41 push ax
42 call _prints
43 dead: jmp dead
44 msg: .asciz "BACK TO ASSEMBLY AND HANG\n\r"
46 !*************************************************************
47 ! KCW added functions for MT system
48 !************************************************************
49 _tswitch:
50 push ax
51 push bx
52 push cx
53 push dx
54 push bp
55 push si
56 push di
57 pushf
58 mov bx, _running
59 mov 2[bx], sp
61 find: call _scheduler
63 resume: mov bx, _running
64 mov sp, 2[bx]
65 popf
66 pop di
67 pop si
68 pop bp
69 pop dx
70 pop cx
71 pop bx
72 pop ax
73 ret
75 USS = 4
76 USP = 6
78 ! as86 macro: parameters are ?1 ?2, etc
79 ! as86 -m -l listing src (generates listing with macro expansion)
81 MACRO INTH
82 push ax
83 push bx
84 push cx
85 push dx
86 push bp
87 push si
88 push di
89 push es
90 push ds
92 push cs
93 pop ds
95 inc _inkmode ! enter Kmode : ++inkmode
96 cmp _inkmode,#1 ! if inkmode == 1 ==> interrupt was in Umode
97 jg ?1 ! imode>1 : was in Kmode: bypass saving uss,usp
99 ! was in Umode: save interrupted (SS,SP) into proc
100 mov si,_running ! ready to access proc
101 mov USS[si],ss ! save SS in proc.USS
102 mov USP[si],sp ! save SP in proc.USP
104 ! change DS,ES,SS to Kernel segment
105 mov di,ds ! stupid !!
106 mov es,di ! CS=DS=SS=ES in Kmode
107 mov ss,di
109 mov sp, _running ! sp -> running's kstack[] high end
110 add sp, _procsize
112 ?1: call _?1 ! call handler in C
114 br _ireturn ! return to interrupted point
116 MEND
119 _int80h: INTH kcinth
121 _tinth: INTH thandler
123 !*===========================================================================*
124 !* _ireturn and goUmode() *
125 !*===========================================================================*
126 ! ustack contains flag,ucs,upc, ax,bx,cx,dx,bp,si,di,es,ds
127 ! uSS and uSP are in proc
128 _ireturn:
129 _goUmode:
131 dec _inkmode ! --inkmode
132 cmp _inkmode,#0 ! inkmode==0 means was in Umode
133 jg xkmode
135 ! restore uSS, uSP from running PROC
136 mov si,_running ! si -> proc
137 mov ax,USS[si]
138 mov ss,ax ! restore SS
139 mov sp,USP[si] ! restore SP
140 xkmode:
141 pop ds
142 pop es
143 pop di
144 pop si
145 pop bp
146 pop dx
147 pop cx
148 pop bx
149 pop ax
150 iret
153 !--------------------------------
154 ! resetVideo[] : clear screen, home cursor
155 !--------------------------------
156 _resetVideo:
157 mov ax, #0x0012 ! 640x480 color
158 int 0x10 ! call BIOS to do it
159 mov ax, #0x0200 ! Home the cursor
160 xor bx, bx
161 xor dx, dx
162 int 0x10 ! call BIOS to home cursor
163 ret
165 !---------------------------------------
166 ! int diskr[cyl, head, sector, buf]
167 ! 4 6 8 10
168 !---------------------------------------
169 _diskr:
170 push bp
171 mov bp,sp
173 movb dl, #0x00 ! drive 0=fd0
174 movb dh, 6[bp] ! head
175 movb cl, 8[bp] ! sector
176 incb cl ! inc sector by 1 to suit BIOS
177 movb ch, 4[bp] ! cyl
178 mov ax, #0x0202 ! READ 2 sectors
179 mov bx, 10[bp] ! put buf value in BX ==> addr=[ES,BX]
180 int 0x13 ! call BIOS to read the block
181 jb error ! to error if CarryBit is on [read failed]
183 mov sp,bp
184 pop bp
187 !---------------------------------------------
188 ! char getc[] function: returns a char
189 !---------------------------------------------
190 _getc:
191 xorb ah,ah ! clear ah
192 int 0x16 ! call BIOS to get a char in AX
193 ret
195 !----------------------------------------------
196 ! void putc[char c] function: print a char
197 !----------------------------------------------
198 !_putc:
199 push bp
200 mov bp,sp
202 movb al,4[bp] ! get the char into aL
203 movb ah,#14 ! aH = 14
205 mov bx,_color ! bL = color B Cyan C Red
206 int 0x10 ! call BIOS to display the char
208 mov sp,bp
209 pop bp
213 _setes: push bp
214 mov bp,sp
216 mov ax,4[bp]
217 mov es,ax
219 mov sp,bp
220 pop bp
223 _inces: ! inces[] inc ES by 0x40, or 1K
224 mov ax,es
225 add ax,#0x40
226 mov es,ax
229 !------------------------------
230 ! error & reboot
231 !------------------------------
232 error:
233 mov bx, #bad
234 push bx
235 call _prints
237 int 0x19 ! reboot
238 bad: .asciz "Error!"
241 !*===========================================================================*
242 !* get_byte *
243 !*===========================================================================*
244 ! This routine is used to fetch a byte from anywhere in memory.
245 ! The call is:
246 ! c = get_byte[segment, offset]
247 ! where
248 ! 'segment' is the value to put in es
249 ! 'offset' is the offset from the es value
250 _get_byte:
251 push bp ! save bp
252 mov bp,sp ! we need to access parameters
254 push es ! save es
255 push bx
257 mov es,4[bp] ! load es with segment value
258 mov bx,6[bp] ! load bx with offset from segment
259 seg es ! go get the byte
260 movb al,[bx] ! al = byte
261 xorb ah,ah ! ax = byte
263 pop bx
264 pop es ! restore es
266 mov bp,sp
267 pop bp ! restore bp
268 ret ! return to caller
271 !*===========================================================================*
272 !* put_byte *
273 !*===========================================================================*
274 ! This routine is used to put a word to anywhere in memory.
275 ! The call is:
276 ! put_byte[char,segment,offset]
277 ! where
278 ! char is a byte
279 ! 'segment' is a segment
280 ! 'offset' is the offset from the segment
281 _put_byte:
282 push bp ! save bp
283 mov bp,sp ! we need to access parameters
285 push es ! save es
286 push bx
288 mov es,6[bp] ! load es with segment value
289 mov bx,8[bp] ! load bx with offset from segment
290 movb al,4[bp] ! load byte in aL
291 seg es ! go put the byte to [ES, BX]
292 movb [bx],al ! there it goes
294 pop bx ! restore bx
295 pop es ! restore es
297 mov bp,sp
298 pop bp ! restore bp
299 ret ! return to caller
300 !*===========================================================================*
301 !* old_flag=lock() *
302 !*===========================================================================*
303 ! Disable CPU interrupts.
304 _lock:
305 pushf ! save flags on stack
306 cli ! disable interrupts
307 pop ax ! pop saved flag into ax
308 ret ! return old_flag
311 !*===========================================================================*
312 !* unlock *
313 !*===========================================================================*
314 ! Enable CPU interrupts.
315 _unlock:
316 sti ! enable interrupts
317 ret ! return to caller
319 !*===========================================================================*
320 !* restore(old_flag) *
321 !*===========================================================================*
322 ! Restore enable/disable bit to the value it had before last lock.
323 _restore:
324 push bp
325 mov bp,sp
327 push 4[bp]
328 popf ! restore old_flag
330 mov sp,bp
331 pop bp
332 ret ! return to caller
335 !*===========================================================================*
336 !* in_byte *
337 !*===========================================================================*
338 ! PUBLIC unsigned in_byte[port_t port];
339 ! Read an [unsigned] byte from the i/o port port and return it.
341 _in_byte:
342 push bp
343 mov bp,sp
344 mov dx,4[bp]
345 in ax,dx ! input 1 byte
346 subb ah,ah ! unsign extend
347 pop bp
350 !*===========================================================================*
351 !* out_byte *
352 !*==============================================================
353 ! out_byte[port_t port, int value];
354 ! Write value [cast to a byte] to the I/O port port.
356 _out_byte:
357 push bp
358 mov bp,sp
359 mov dx,4[bp]
360 mov ax,6[bp]
361 outb dx,al ! output 1 byte
362 pop bp