-
Notifications
You must be signed in to change notification settings - Fork 0
/
TOOLS.ASM
executable file
·648 lines (505 loc) · 15.2 KB
/
TOOLS.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
.MODEL SMALL
.DATA
INCLUDE const.inc
allowed_chars DB '0123456789'
DB 'abcdef'
DB 'ABCDEF'
DB MEMORY
DB QUIT
DB CR,BKSP,SPACE
DB DOT,COLON,CHEVRONL,CHEVRONR
DB EXCL
ALLOWED_CHARS_LENGTH EQU ($ - allowed_chars)
.CODE
INCLUDE macros.inc
PUBLIC read_str
PUBLIC wait_for_char
PUBLIC read_char
PUBLIC print_number
PUBLIC print_number_hex
PUBLIC add_32bit
PUBLIC subtract_32bit
PUBLIC cls
PUBLIC print_address
PUBLIC print_io_address
PUBLIC print_hex
PUBLIC print_at_col
PUBLIC div_by_10
PUBLIC alloc_mem
PUBLIC free_mem
; Reads from standard input a string of max si
; length into es:di
;
; destroys cx,ax,di
read_str PROC NEAR
mov cx,si ; ensure max si characters in string
cld ; ensure incrementing di for stosb
read_str_char:
mov ah,08h ; read char into al
int 21h
cmp al,BKSP ; check if the character is backspace
jne read_str_nobksp ; if no handle normal character
cmp cx,si ; else check if not first character
je read_str_char ; if first char just read next char
inc cx ; else fix cx and di
dec di
mov byte ptr es:[di],0 ; clear the corresponding character in buffer
print BKSP ; and print backspace
print SPACE
print BKSP
jmp read_str_char ; then read next char
read_str_nobksp:
print al ; print the char
stosb ; store the char in destination buffer
cmp al,CR ; terminate if char is CR
loopne read_str_char ; or the buffer is depleted
retn
ENDP
; Loops until char declared in al is
; read from keyboard
;
; destroys ah,bx
wait_for_char PROC NEAR
push ax
wait_for:
mov ah,08h ; read char into al
int 21h
mov bx,sp
cmp al,ss:[bx]
jne wait_for
pop ax
retn
ENDP
; Reads only allowed chars
; destroys ax,es,di,dl,cx
;
; returns char in al
read_char PROC NEAR
mov ax,@data ; load data segment to es regiser
mov es,ax
cld ; ensure incrementing di for scasb
read:
mov di,OFFSET allowed_chars ; load allowed chars offset to di
mov cx,ALLOWED_CHARS_LENGTH ; initialize counter
mov ah,07h ; read char into al
int 21h
read_loop:
scasb ; scan for char in al
je found ; if found then return
loop read_loop
print BELL ; print bell on not allowed char
jmp read ; if not allowed read again
found:
retn
ENDP
; Prints in decimal unsigned 32 bit integer number
; from bytes on stack
;
; destroys: ax, bx, cx, dx, si
print_number PROC NEAR
push bp ; set stack frame with 11 bytes for
mov bp,sp ; number string variable
sub sp,11
mov byte ptr [bp - 1],'$' ; put string termination at highest byte
mov si,0
sub sp,5 ; reserve 5 bytes on stack for number and
mov bx,sp ; remainder
mov ax,[bp + 4] ; copy argument for division
mov word ptr ss:[bx + 1],ax
mov ax,[bp + 6]
mov word ptr ss:[bx + 3],ax
calc_digit:
call NEAR PTR div_by_10 ; divide with remainder
mov bx,sp
mov al,ss:[bx] ; convert remainder to ASCII digit
add al,30h
dec si ; decrement digit index in string
mov byte ptr [bp + si - 1],al ; store ASCII digit in string
mov ax,word ptr ss:[bx + 1] ; check if quotient is 0
or ax,word ptr ss:[bx + 3]
jnz calc_digit ; if not repeat division
mov bx,ss ; get stack segment for string print
lea dx,[bp + si - 1] ; get effective offset for string beginning
print_string bx,dx ; and print string
add sp,5 ; restore stack: 5 bytes for quotient and r
add sp,11 ; restore stack: 11 bytes for number string
pop bp
retn
ENDP
; Prints unsigned 32 bit number in hex
; skipping leading zeros
print_number_hex PROC NEAR a:DWORD
LOCAL print_zeros:byte
push bp
mov bp,sp
push ax
push bx
push cx
mov print_zeros,0 ; clear print leading zeros flag
mov cx,4 ; set four nibbles
mov bx,word ptr[a+2] ; load upper word
print_number_hex_upper:
mov ax,bx ; get highest nibble
and ax,0F000h
shiftr ax,12 ; shift to al
or print_zeros,al ; check if leading zero
jz print_number_hex_upper_next ; and skip if true
call NEAR PTR print_nibble ; print nibble
print_number_hex_upper_next:
shiftl bx,4 ; proceed with next nibble
loop print_number_hex_upper
mov cx,4 ; set four nibbles
mov bx,word ptr[a] ; load lower word
print_number_hex_lower:
mov ax,bx
and ax,0F000h
shiftr ax,12
or print_zeros,al ; check if leading zero
jz print_number_hex_lower_next ; and skip if true
call NEAR PTR print_nibble
print_number_hex_lower_next:
shiftl bx,4 ; proceed with next nibble
loop print_number_hex_lower
pop cx ; restore registers
pop bx
pop ax
pop bp
retn
ENDP
; Adds two 32 bit numbers passed on stack.
; Returns result also on stack
add_32bit PROC NEAR a:DWORD, b:DWORD RETURNS c:DWORD
push bp
mov bp,sp
push ax ; preseve ax,bx
push bx
clc
mov ax,word ptr [a]
mov bx,word ptr [a+2]
add ax,word ptr [b] ; add lower bytes of args
adc bx,word ptr [b+2] ; add with carry higher bytes of args
mov word ptr [c],ax ; save result on stack
mov word ptr [c+2],bx
pop bx ; restore ax,bx
pop ax
pop bp
retn
ENDP
; Subtracts two 32 bit number from stack and
; leaves result on stack. Calculates a - b.
; First argument is on the bottom of stack.
subtract_32bit PROC NEAR a:DWORD,b:DWORD RETURNS c:DWORD
push bp
mov bp,sp
push ax
push bx
clc
mov ax,word ptr [a] ; load a argument lower byte
mov bx,word ptr [a+2] ; load a argument upper byte
sub ax,word ptr [b] ; subtract lower bytes of args
sbb bx,word ptr [b+2] ; subtract with borrow higher bytes of args
mov word ptr [c],ax ; save result on stack
mov word ptr [c+2],bx
pop bx ; restores ax,bx
pop ax
pop bp
retn
ENDP
; Clears screen and positions
; the cursor in home position (0,0)
;
; destroys ax,dx,cx,bh
cls PROC NEAR
mov dl,79 ; dl,dh = x,y of right corner of window
mov dh,24
mov cx,0 ; cl,ch = x,y of upper left corner of window
mov bh,07h ; display attribute to use
mov ah,06h ; scroll or initialize window function
mov al,0 ; zero lines to scroll
int 10h ; call ROM BIOS video driver
mov dx,0 ; set cursor position in dx to 0,0
mov bh,0 ; set page 0
mov ah,2 ; select cursor position function
int 10h ; call ROM BIOS video driver
retn
ENDP
; Prints real address in es:di
;
; destroys ax,cl
print_address PROC NEAR
push bp ; save bp
mov bp,sp ; set new stack frame
sub sp,3 ; reserve 3 bytes on stack for real address
; calculation
mov ax,es ; move segment register to ax
shl ax,1 ; rotate MSBit in ax into the highest
rcl byte ptr [bp - 1],1 ; byte on stack frame, 4x times
shl ax,1
rcl byte ptr [bp - 1],1
shl ax,1
rcl byte ptr [bp - 1],1
shl ax,1
rcl byte ptr [bp - 1],1
add ax,di ; sum shifted segment and offset
adc byte ptr [bp - 1],0 ; add carry to highest byte on stack frame
mov word ptr [bp - 3],ax ; save sum (4 address nibbles) on stack frame
mov al,[bp - 1] ; print MSB nibble of real address
call print_nibble ; from highest byte on stack frame
mov cl,12 ; set cl for 12 right shifts
print_nibbles:
mov ax,word ptr [bp - 3] ; set ax to remaining nibbles
shr ax,cl ; shift right to get next nibble in al
call print_nibble
sub cl,4 ; decrement cl by nibble length
jns print_nibbles ; branch till cl becomes negative
add sp,3 ; restore original stack frame
pop bp
retn
ENDP
; Prints 16 bit IO address stored in ax
print_io_address PROC NEAR
push cx
mov cx,ax
mov al,ch
call print_hex
mov al,cl
call print_hex
pop cx
retn
ENDP
; Prints byte in al
;
print_hex PROC NEAR
push cx ; preserve cx
push ax ; preserve ax
push bx ; preserve bx
mov cl,4
shr al,cl ; shr 4 times to get high nibble
call print_nibble ; print high nibble
mov bx,sp
mov ax,ss:[bx+2] ; restore ax without popping from stack
call print_nibble ; print low nibble
pop bx ; restore bx
pop ax ; restore ax
pop cx ; restore cx
retn
ENDP
; Prints charcter in al at column in ah
; int the same row, and then returns to
; the previous column
;
; destroys ah,bh,dx
print_at_col PROC NEAR
push cx ; save cx
push bp ; save current stack frame
mov bp,sp ; set new stack frame
sub sp,1 ; reserve 1 byte on stack for current column
mov bh,00h ; set page 0
push ax ; save column parameter in ah
mov ah,03h ; read current cursor position into dh:dl
int 10h
mov [bp - 1],dl ; save original cursor column
; note: original row still in dh
pop ax ; restore column parameter in ah
mov dl,ah ; set destination column in dl
mov ah,02h ; set cursor position at destination column
int 10h
cmp al,SPACE
jb print_dot
cmp al,7eh
ja print_dot
print al
jmp restore_col
print_dot:
print DOT
restore_col:
mov dl,[bp - 1] ; set column to the original
mov ah,02h ; set cursor position at original column
int 10h
add sp,1 ; destroy local variable
pop bp ; restore stack frame pointer
pop cx ; restore cx
retn
ENDP
; Prints lower nibble in al
;
; destroys al
print_nibble PROC NEAR
and al,0Fh
cmp al,9
jg hex_nibble
add al,30h
print al
ret
hex_nibble:
add al,57h
print al
retn
ENDP
div_by_10 PROC NEAR
push bp ; preserve frame pointer
mov bp,sp
sub sp,8 ; reserve 8 bytes on stack for quotient
mov bx,sp ; and remainder
xor ax,ax ; r = q = 0
mov word ptr ss:[bx],ax ; q
mov word ptr ss:[bx + 2],ax ; q + 2
mov word ptr ss:[bx + 4],ax ; r
mov word ptr ss:[bx + 6],ax ; r + 2
mov cx,8 ; i = 31
div_loop1:
dec cl
shl byte ptr ss:[bx + 4],1 ; r <<= 1
rcl byte ptr ss:[bx + 5],1
rcl byte ptr ss:[bx + 6],1
rcl byte ptr ss:[bx + 7],1
mov ax,1 ; 1 << i
shl ax,cl
mov dl,[bp + 8] ; n & (1 << i)
and dl,al
jz zero1
or byte ptr ss:[bx + 4],1 ; r |= n & (1 << i) > ? 1 : 0
zero1:
mov al,ss:[bx + 4] ; r > d ?
cmp al,10
jge larger1
xor al,al
or al,ss:[bx + 5]
or al,ss:[bx + 6]
or al,ss:[bx + 7]
jz div_loop_end1
larger1:
mov ax,1 ; q |= 1 << i
shl ax,cl
or ss:[bx + 3],al
mov ax,10 ; r = r - 10
sub word ptr ss:[bx + 4],ax
sbb word ptr ss:[bx + 6],0
div_loop_end1:
inc cl
loop div_loop1
mov cx,8
div_loop2:
dec cl
shl byte ptr ss:[bx + 4],1
rcl byte ptr ss:[bx + 5],1
rcl byte ptr ss:[bx + 6],1
rcl byte ptr ss:[bx + 7],1
mov ax,1
shl ax,cl
mov dl,[bp + 7]
and dl,al
jz zero2
or byte ptr ss:[bx + 4],1
zero2:
mov al,ss:[bx + 4]
cmp al,10
jge larger2
xor al,al
or al,ss:[bx + 5]
or al,ss:[bx + 6]
or al,ss:[bx + 7]
jz div_loop_end2
larger2:
mov ax,1 ; q |= 1 << i
shl ax,cl
or ss:[bx + 2],al
mov ax,10 ; r = r - 10
sub word ptr ss:[bx + 4],ax
sbb word ptr ss:[bx + 6],0
div_loop_end2:
inc cl
loop div_loop2
mov cx,8
div_loop3:
dec cl
shl byte ptr ss:[bx + 4],1
rcl byte ptr ss:[bx + 5],1
rcl byte ptr ss:[bx + 6],1
rcl byte ptr ss:[bx + 7],1
mov ax,1
shl ax,cl
mov dl,[bp + 6]
and dl,al
jz zero3
or byte ptr ss:[bx + 4],1
zero3:
mov al,ss:[bx + 4]
cmp al,10
jge larger3
xor al,al
or al,ss:[bx + 5]
or al,ss:[bx + 6]
or al,ss:[bx + 7]
jz div_loop_end3
larger3:
mov ax,1 ; q |= 1 << i
shl ax,cl
or ss:[bx + 1],al
mov ax,10 ; r = r - 10
sub word ptr ss:[bx + 4],ax
sbb word ptr ss:[bx + 6],0
div_loop_end3:
inc cl
loop div_loop3
mov cx,8
div_loop4:
dec cl
shl byte ptr ss:[bx + 4],1
rcl byte ptr ss:[bx + 5],1
rcl byte ptr ss:[bx + 6],1
rcl byte ptr ss:[bx + 7],1
mov ax,1
shl ax,cl
mov dl,[bp + 5]
and dl,al
jz zero4
or byte ptr ss:[bx + 4],1
zero4:
mov al,ss:[bx + 4]
cmp al,10
jge larger4
xor al,al
or al,ss:[bx + 5]
or al,ss:[bx + 6]
or al,ss:[bx + 7]
jz div_loop_end4
larger4:
mov ax,1 ; q |= 1 << i
shl ax,cl
or ss:[bx],al
mov ax,10 ; r = r - 10
sub word ptr ss:[bx + 4],ax
sbb word ptr ss:[bx + 6],0
div_loop_end4:
inc cl
loop div_loop4
mov al,ss:[bx + 4] ; r
mov byte ptr ss:[bp + 4],al ; result r
mov ax,ss:[bx] ; q
mov word ptr ss:[bp + 5],ax ; result q
mov ax,ss:[bx + 2] ; q + 2
mov word ptr ss:[bp + 7],ax ; result q + 2
add sp,8 ; restore previous stack frame
pop bp
retn
ENDP
; Allocates number of paragraphs (16 bytes) specified in bx.
; Returns segment of allocated memory in ax. C=1, bx=max available
; if allocation failed.
;
; destroys ax,may destroy bx
alloc_mem PROC NEAR
mov ah,48h
int 21h
retn
ENDP
; Frees memory at segment specified in es. C=1 if release failed.
;
; destroys ah
free_mem PROC NEAR
mov ah,49h
int 21h
retn
ENDP
END