This repository has been archived by the owner on Jun 13, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmm.asm
649 lines (536 loc) · 7.51 KB
/
mm.asm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
; mm.s: mini-monitor program for x86
[BITS 16]
TTY equ 0x03F8
; Start-up code
; mov cx,di is temporary and will eventually be removed
[GLOBAL _start]
_start:
cli
mov ax,cs
mov ds,ax
; Initialize serial device (console)
mov bx,0
mov cx,TTYINITCOUNT
init_loop:
mov al,[bx+ttyinitdata]
rol bx,1
mov dx,[bx+ttyinitport]
ror bx,1
inc bx
out dx,al
loop init_loop
; Display start up message
mov di,info1
mov bx,startupmsg
jmp puts
; Information
info1:
mov di,info2
mov bx,bootdate
jmp puts
info2:
mov ax,0xFFFF
mov es,ax
mov bp,5
mov di,info3
info3:
cmp bp,15
jz info4
mov ah,[es:bp]
inc bp
cmp ah,0x20
jb info4
cmp ah,0x7F
jae info4
jmp putc
info4:
mov di,info5
mov bx,codeseg
jmp puts
info5:
mov di,prompt
mov ah,4
mov bx,cs
jmp puthex
prompt:
mov word [echo],0xFFFF ; Turn-on echo
mov di,req
mov bx,prompttext
jmp puts
; Get user request
req:
mov di,req_test
jmp getc
req_test:
cmp ah,'?'
jz help
or ah,0x20
cmp ah,'d'
jz req_44
cmp ah,'f'
jz req_40
cmp ah,'g'
jz go
cmp ah,'i'
jz req_40
cmp ah,'j'
jz req_40
cmp ah,'o'
jz req_42
cmp ah,'m'
jz req_44
mov word [echo],0x0000 ; Turn-off echo
cmp ah,'s'
jz req_40
jmp prompt
; Help screen
help:
mov bp,addontbl
mov di,help_rtn1
mov bx,helptext
jmp puts
help_rtn1:
mov cx,[bp]
cmp cx,0xFFFF
jz prompt
add bp,2
mov di,help_rtn2
mov bx,stub1
jmp puts
help_rtn2:
mov di,help_rtn3
mov ah,4
mov bx,cx
jmp puthex
help_rtn3:
mov di,help_rtn4
mov bx,stub2
jmp puts
help_rtn4:
mov di,help_rtn1
mov bx,bp
add bp,30
jmp puts
req_40:
mov es,ax
mov di,req_40b
mov cx,4
jmp gethex
req_40b:
mov ax,bx
mov cx,es
cmp ch,'i'
jz req_i
cmp ch,'j'
jz req_j
; Memory fill (f or s!)
req_fs:
mov es,ax
mov bp,0
req_fs_nl:
mov di,req_fs_rtn1
mov ah,0x0A
jmp putc
req_fs_rtn1:
mov di,req_fs_rtn2
mov bx,es
mov ah,4
jmp puthex
req_fs_rtn2:
mov di,req_fs_rtn3
mov ah,0x3A
jmp putc
req_fs_rtn3:
mov di,req_fs_rtn4
mov bx,bp
mov ah,4
jmp puthex
req_fs_rtn4:
mov di,req_fs_rtn5
mov cx,2
jmp gethex
req_fs_rtn5:
mov [es:bp],bl
inc bp
test bp,0x000F
jnz req_fs_rtn4
jmp req_fs_nl
; Input from port
req_i:
mov dx,ax
in al,dx
mov bh,al
mov di,req_i_rtn
mov ah,0x0A
jmp putc
req_i_rtn:
mov di,prompt
mov ah,2
jmp puthex
; Intra-segment jump
req_j:
mov di,req_j_rtn
mov ah,0x0A
jmp putc
req_j_rtn:
mov di,prompt
jmp bx
req_42:
mov es,ax
mov di,req_42b
mov cx,4
jmp gethex
req_42b:
mov bp,bx
mov di,req_42c
mov cx,2
jmp gethex
req_42c:
mov ax,bp
mov cx,es
; Output to port
req_o:
mov dx,ax
mov ax,bx
out dx,al
jmp prompt
req_44:
mov es,ax
mov di,req_44b
mov cx,4
jmp gethex
req_44b:
mov bp,bx
mov di,req_44c
mov cx,4
jmp gethex
req_44c:
mov ax,bp
mov cx,es
cmp ch,0x6D
jz req_m
; Dump range of memory
req_d:
mov cl,4
ror bx,cl
test bh,0xF0
jz req_d_noinc
and bh,0x0F
inc bx
req_d_noinc:
mov es,ax
mov bp,bx
mov si,0
mov cl,0
req_d_nl:
test bp,bp
jz req_d_checksum
mov di,req_d_rtn1
mov ah,0x0A
jmp putc
req_d_rtn1:
mov di,req_d_rtn2
mov bx,es
mov ah,4
jmp puthex
req_d_rtn2:
mov di,req_d_rtn3
mov ah,0x3A
jmp putc
req_d_rtn3:
mov di,req_d_rtn4
mov bx,si
mov ah,4
jmp puthex
req_d_rtn4:
mov di,req_d_rtn5
mov ah,0x20
jmp putc
req_d_rtn5:
mov bh,[es:si]
inc si
add cl,bh
mov di,req_d_rtn6
mov ah,2
jmp puthex
req_d_rtn6:
test si,0x000F
jnz req_d_rtn4
sub si,0x10
mov di,req_d_rtn7
mov ah,0x20
jmp putc
req_d_rtn7:
mov di,req_d_rtn8
req_d_cloop:
mov ah,[es:si]
inc si
cmp ah,0x20
jb req_d_badc
cmp ah,0x7F
jae req_d_badc
jmp req_d_okc
req_d_badc:
mov ah,0x2E
req_d_okc:
jmp putc
req_d_rtn8:
test si,0x000F
jnz req_d_cloop
dec bp
jmp req_d_nl
req_d_checksum:
mov di,req_d_rtn9
mov bx,checksum
jmp puts
req_d_rtn9:
mov di,prompt
mov bh,cl
mov ah,2
jmp puthex
; Modify memory location
req_m:
mov di,req_m_rtn1
mov ah,0x0A
jmp putc
req_m_rtn1:
mov ds,bp
mov ah,[bx]
mov cx,cs
mov ds,cx
mov es,bx
mov di,req_m_rtn2
mov bh,ah
mov ah,2
jmp puthex
req_m_rtn2:
mov di,req_m_rtn3
mov cx,2
jmp gethex
req_m_rtn3:
mov ah,bl
mov ds,bp
mov bx,es
mov [bx],ah
mov ax,cs
mov ds,ax
jmp prompt
; Go! (jmp 0x1010:0x0100)
go:
jmp 0x1010:0x0100
; puthex: Prints a number of hex digits to the console
;
; DI = Return address
; AH = Number of digits
; BX = Value to display, "left justified"
; Alters: AX, BX and DX
puthex:
cmp word [echo],0
jz puthex_no_echo
mov dx,TTY+LSR
puthex_wait:
in al,dx
test al,0x20
jz puthex_wait
mov al,cl
mov cl,4
rol bx,cl
mov cl,al
mov dx,bx
and bx,0x000F
mov al,[bx+hextable]
mov bx,dx
mov dx,TTY
out dx,al
dec ah
jnz puthex
puthex_no_echo:
jmp di
; puts: Sends a null-terminated string to the console
;
; DI = Return address
; BX = Pointer to string
; Alters: AX, BX, DX, DI and SI
puts:
mov si,di
puts_loop:
mov ah,[bx]
test ah,ah
jz puts_exit
inc bx
mov di,puts_loop
jmp putc
puts_exit:
jmp si
; putc: Sends one character to the console
;
; DI = Return address
; AH = Character
; Alters: AX and DX
putc:
cmp word [echo],0
jz putc_no_echo
cmp ah,0x0A
jnz putc_noendl
mov ah,0x0D
mov dx,TTY+LSR
putc_wait1:
in al,dx
test al,0x20
jz putc_wait1
mov al,ah
mov dx,TTY
out dx,al
mov ah,0x0A
putc_noendl:
mov dx,TTY+LSR
putc_wait2:
in al,dx
test al,0x20
jz putc_wait2
mov al,ah
mov dx,TTY
out dx,al
putc_no_echo:
jmp di
; gethex: retrieves a hexadecimal number from the console
; if invalid input is detected, the prompt is displayed again
;
; DI = Return address
; BX = Value retrieved, "right justified"
; CX = Number of digits
; Alters: AX, BX, CX, DX, DI and SI
gethex:
mov si,di
mov di,gethex_loop
mov ah,0x20
jmp putc
gethex_loop:
mov di,gethex_rtn
jmp getc
gethex_rtn:
test ah,0x40
jz gethex_num
and ah,0x5F
cmp ah,0x41
jb prompt
cmp ah,0x46
ja prompt
sub ah,0x37
jmp gethex_gen
gethex_num:
sub ah,0x30
cmp ah,9
ja prompt
gethex_gen:
mov di,cx
mov cl,4
shl bx,cl
mov cx,di
mov al,ah
mov ah,0
or bx,ax
loop gethex_loop
jmp si
; getc: retrieves one character from the console
; if echo is non-zero, the character is echoed
;
; DI = Return address
; AH = Character retrieved
; Alters: AX and DX
getc:
mov dx,TTY+LSR
getc_wait1:
in al,dx
test al,0x01
jz getc_wait1
mov dx,TTY
in al,dx
mov ah,al
cmp word [echo],0
jz getc_no_echo
mov dx,TTY+LSR
getc_wait2:
in al,dx
test al,0x20
jz getc_wait2
mov al,ah
mov dx,TTY
out dx,al
getc_no_echo:
jmp di
testmsg:
mov bx,testmsgtext
jmp puts
int0:
mov ax,0
div ax
DLLB equ 0
IER equ 1
DLHB equ 1
IIR equ 2
FCR equ 2
LCR equ 3
MCR equ 4
LSR equ 5
MSR equ 6
SCRATCH equ 7
TTYINITCOUNT equ 7
ttyinitdata:
DB 0x00 ; Disable interrupts
DB 0x00
DB 0x80 ; Set baud and framing parameters
DB 0x01 ; 115200
;DB 0x0C ; 9600
;DB 0x30 ; 2400
DB 0x00
DB 0x03 ; 8N1
DB 0xE7 ; Enable FIFOs (if any)
ttyinitport:
DW TTY+LCR
DW TTY+IER
DW TTY+LCR
DW TTY+DLLB
DW TTY+DLHB
DW TTY+LCR
DW TTY+FCR
echo:
DW 1
startupmsg:
DB `\nx86 ROM Mini-monitor`
DB `\nCopyright (C) 2001`
DB `\nEnter "?" for help`, 0x00
bootdate:
DB `\nBoot ROM date = `, 0x00
codeseg:
DB `\nDS = CS = `, 0x00
prompttext:
DB `\n>`, 0x00
checksum:
DB `\nChecksum = `, 0x00
hextable:
DB `0123456789ABCDEF`
testmsgtext:
DB `Testing add-on command functionality. If you`
DB `\ncan read this, add-on commands are working.`, 0x00
helptext:
DB `\nInternal commands:`
DB `\nd <seg> <count> - dump range of memory`
DB `\nf <seg> - fill range of memory`
DB `\ng - go! (jmp 0x1010:0x0100)`
DB `\ni <port> - read byte from I/O port`
DB `\nj <offset> - intra-segment jump`
DB `\nm <seg> <offset> - modify memory location`
DB `\no <port> <byte> - write byte to I/O port`
DB `\ns <seg> - "silent" fill (same as f without echo)`
DB `\nAdd-on commands:`, 0x00
stub1:
DB `\nj `, 0x00
stub2:
DB ` - `, 0x00
addontbl:
DW testmsg
DB `print test message `, 0x00
DW int0
DB `int 0 (divide by zero) `, 0x00
DW 0xFFFF