-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtools.asm
More file actions
1149 lines (886 loc) · 18.9 KB
/
Copy pathtools.asm
File metadata and controls
1149 lines (886 loc) · 18.9 KB
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
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
; =================== UTILITIES ===================== ;
; this file provides utility macros ;
; and procedures for assembly 8086 programs. ;
; # Usage: ;
; - put this line under CODESEG in your program: ;
; include "tools.asm" ;
; made by Nir Yona ;
; =================================================== ;
DATASEG
TOOLS_newline db 10, 13, '$'
TOOLS_buffer db ?, ?, 100 dup(?), '$'
; (search: colors list color list clrs list)
; from https://yassinebridi.github.io/asm-docs/8086_bios_and_dos_interrupts.html#attrib
BLACK equ 0000b
BLUE equ 0001b
GREEN equ 0010b
CYAN equ 0011b
RED equ 0100b
MAGENTA equ 0101b
BROWN equ 0110b
LIGHT_GRAY equ 0111b
DARK_GRAY equ 1000b
LIGHT_BLUE equ 1001b
LIGHT_GREEN equ 1010b
LIGHT_CYAN equ 1011b
LIGHT_RED equ 1100b
LIGHT_MAGENTA equ 1101b
YELLOW equ 1110b
WHITE equ 1111b
; more colors at https://imgur.com/a/Bwg8pVC
SCREEN_HEIGHT equ 200
SCREEN_WIDTH equ 320
CODESEG
; same as pusha
macro pushh
push ax bx cx dx di si es
endm
; same as popa
macro popp
pop es si di dx cx bx ax
endm
macro pstart
push bp
mov bp, sp
pushh
endm
macro pend retCount
popp
pop bp
ifnb <retCount>
ret retCount
endif
ret
endm
; ========================================
; Description: Print a number.
; Input : (By order) [1] num.
; Output : Number on screen.
; Example:
; push num
; call ShowDecimal
; ========================================
proc ShowDecimal
pstart
; ------------
; Stack State:
; | [bp + 4] |
; | num |
; ------------
mov ax, [bp + 4]
; check if negative
test ax, 08000h
jz PositiveAx
; negative!
; put '-' (minus) on the screen
push ax
mov dl,'-'
mov ah,2
int 21h
pop ax
neg ax ; make it positive
PositiveAx:
mov cx,0 ; will count how many time we did push
mov bx,10 ; the divider
put_mode_to_stack:
xor dx,dx
div bx
add dl,30h
; dl is the current LSB digit
; we cant push only dl so we push all dx
push dx
inc cx
cmp ax,9 ; check if it is the last time to div
jg put_mode_to_stack
cmp ax,0
jz pop_next ; jump if ax was totally 0
add al,30h
mov dl, al
mov ah, 2h
int 21h ; show first digit MSB
pop_next:
pop ax ; remove all rest LIFO (reverse) (MSB to LSB)
mov dl, al
mov ah, 2h
int 21h ; show all rest digits
loop pop_next
pend 2
endp ShowDecimal
; ===============================================
; [MACRO] PRINTV
; Desc: Print a string and replace $ with the
; given variable
; Usage: PRINTV <msg>, <v1>
; Exmpl: PRINTV "num is $", ax
; ===============================================
macro printv msg, v1
LOCAL skip_dcl, msg_dcl, msg_len, iter_msg, print_var, next, done
pushh
JMP skip_dcl
msg_dcl db msg
msg_len = $-msg_dcl
skip_dcl:
mov di, v1
xor si, si
iter_msg:
cmp si, msg_len
je done
mov al, [cs:msg_dcl+si]
cmp al, '$'
je print_var
;print_char
mov ah, 2
mov dl, al
int 21h
jmp next
print_var:
push di
call ShowDecimal
next:
inc si
jmp iter_msg
done:
popp
endm
; ===============================
; Description: Print a newline.
; Output : Newline on the screen.
; Example:
; call Newline
; ===============================
proc Newline
push ax dx
mov dx, offset TOOLS_newline
mov ah, 09h
int 21h
pop dx ax
ret
endp Newline
; =========================================
; Description: Set to video mode (320x200).
; Output : Video mode.
; Example:
; call videomode
; =========================================
proc videoMode
push ax
mov al, 13h
mov ah, 0
int 10h
mov ax, 1 ; show cursor
int 33h
pop ax
ret
endp videoMode
; macro: set to video mode (320x200)
macro video
call videomode
endm
; macro: set to text mode (80x25)
macro text
push ax
mov ax, 0003h
int 10h
pop ax
endm
proc textMode
push ax
mov al, 03h
mov ah, 0
int 10h
pop ax
ret
endp textMode
; this macro prints a string that is given as a parameter, example:
; PRINT 'hello world!'
; NOTE: adds newline
macro println msg
LOCAL skip_dcl, msg_dcl
push ax dx ds
JMP skip_dcl
msg_dcl db msg, 10, 13, '$'
skip_dcl:
; make codeseg the default segment so
; we can access it via offset <varname>
mov ax, cs
mov ds, ax
mov dx, offset msg_dcl
mov ah, 09h
int 21h
; rollback
pop ds dx ax
endm
; this macro prints a string that is given as a parameter, example:
; PRINT 'hello world!'
macro print msg
LOCAL skip_dcl, msg_dcl
push ax dx ds
JMP skip_dcl
msg_dcl db msg, '$'
skip_dcl:
; make codeseg the default segment so
; we can access it via offset <varname>
mov ax, cs
mov ds, ax
mov dx, offset msg_dcl
mov ah, 09h
int 21h
; rollback
pop ds dx ax
endm
macro WaitKey
mov ah, 0
int 16h
endm
; this macro prints the variable whose offset is in si with given length (byte length)
; msg db "hello"
; push si
; mov si, offset msg
; PRINT 5
; pop si
; OUTPUT: "hello" (adds a newline)
macro printvl len
local iter_ofst
push ax bx dx si
xor bx, bx
mov bl, len
xor bx, bx
iter_ofst:
mov dl, [si + bx]
mov ah, 2
int 21h
inc bx
cmp bl, len
jl iter_ofst
mov dx, offset TOOLS_newline
mov ah, 09h
int 21h
pop si dx bx ax
endm
; prints a string with given color
; ARGUMENTS: color , message
; list of color attributes / colors attributes (clrs) are available on DSEG
macro printc color, msg
LOCAL skip_dcl, msg_dcl, msg_len, iter_msg
push ax bx cx dx
JMP skip_dcl
msg_dcl db msg
msg_len dw $-msg_dcl
skip_dcl:
; colors
; use this code for compatibility with dos/cmd prompt full screen mode:
mov ax, 1003h
mov bx, 0 ; disable blinking.
int 10h
mov al, ' ' ; placeholder
mov bl, color
mov cx, [cs:msg_len]
mov ah, 09h
int 10h
xor bx, bx
iter_msg:
mov ah, 02h
mov dl, [cs:msg_dcl + bx]
int 21h
inc bx
cmp bx, [cs:msg_len]
jl iter_msg
; rollback
pop dx cx bx ax
endm
; same as `printc` except it also puts a newline at the end
; ARGUMENTS: color (search "clrs" for list), message
macro printcl color, msg
printc color, msg
call newline
endm
; prints a character with given color number of times
; ARGUMENTS: color (search "clrs" for list), times to print, char
macro putcch color, times, char
push ax bx cx
; colors
; use this code for compatibility with dos/cmd prompt full screen mode:
mov ax, 1003h
mov bx, 0 ; disable blinking.
int 10h
mov al, char
mov bl, color
mov cx, times
mov ah, 09h
int 10h
; rollback
pop cx bx ax
endm
; moves the cursor back number of colors
; ARGUMENTS: columns to go back.
; macro curback columns
; push ax cx dx
; ; get cursor position
; mov ah, 03h
; int 10h ; column in DL
; print "before:"
; xor cx, cx
; mov cl, dl
; push cx
; call ShowDecimal
; sub dl, columns
; print "after:"
; xor cx, cx
; mov cl, dl
; push cx
; call ShowDecimal
; ; set cursor positon (DL, DH)
; mov ah, 02h
; int 10h
; pop dx cx ax
; endm
; =================================================================
; Descrpt: Gets input from keyboard.
; Input : (By order) [1] inputMaxLen, [2] dataVar, [3] dataLenVar.
; Output : Output on screen.
; =================================================================
proc KeyboardInput
push bp
mov bp, sp
; --------------------------------------
; Stack State:
; | bp + 4 | bp + 6 | bp + 8 |
; | dataLenVar | dataVar | inputMaxLen |
; --------------------------------------
mov bx, [bp + 8]
inc bl
mov [offset TOOLS_buffer], bl
mov dx, offset TOOLS_buffer
mov ah, 0ah
int 21h
mov bx, [bp + 4]
xor ax, ax
mov al, [TOOLS_buffer + 1]
mov [bx], ax
xor bx, bx
@@save_buffer:
mov al, [TOOLS_buffer + bx + 2]
mov si, [bp + 6]
mov [si + bx], al
inc bx
cmp bl, [TOOLS_buffer + 1]
jl @@save_buffer
call newline
pop bp
ret 6
endp KeyboardInput
; draws black in coord
macro draw x,y
push ax bx cx dx si
mov ah, 0Ch
mov al, 0CH
mov cx, x
mov dx, y
int 10h
pop si dx cx bx ax
endm
; draws black in coord
macro drawc x,y, c
push ax bx cx dx si
mov ah, 0Ch
mov al, c
mov cx, x
mov dx, y
int 10h
pop si dx cx bx ax
endm
; =============================================================================
; Description: Open a file in read mode, if it doesnt exist filehandle=-1.
; Input : (By order) [1] offset filename, [2] offset filehandle.
; Output : Updated filehandle, if error/file does not exist then filehandle=-1.
; Example:
; push offset filename
; push offset filehandle
; call OpenRead
; =============================================================================
proc OpenRead
push bp
mov bp, sp
pushh
; ---------------------------------------
; Stack State:
; | [bp + 4] | [bp + 6] |
; | offset filehandle | offset filename |
; ---------------------------------------
mov si, [bp+4]
mov di, [bp+6]
; try to open
mov ah, 03Dh ; open file
mov al, 0 ; read mode
mov dx, [bp+6]
int 21h ; filehandle in AX
jc @@open_error
; no error
mov bx, [bp + 4]
mov [bx], ax ; update filehandle
jmp @@end
@@open_error:
printv "cant open file in read mode (code: $)",ax
println ''
mov bx, [bp + 4]
mov [word bx], -1 ; update filehandle
@@end:
popp
pop bp
ret 4
endp OpenRead
; -----------------------
; opens a file for write, and if doesnt exist creates
; USAGE:
; push offset filename
; push offset filehandle
; call OpenWrite
; ------------------------
proc OpenWrite
push bp
mov bp, sp
pushh
; SS
; [bp+4] offset handle
; [bp+6] offset fname
; try to open
mov ah, 3Dh
mov al, 1
mov dx, [bp + 6]
int 21h
jc @@may_not_exist
; exists
mov bx, [bp + 4]
mov [bx], ax
jmp @@end
@@may_not_exist:
cmp ax, 2
jne @@open_error
; create file
xor cx, cx
mov dx, [bp + 6]
mov ah, 3Ch
int 21h
jc @@open_error
mov bx, [bp + 4]
mov [bx], ax
jmp @@end
@@open_error:
println "cant open file for write"
mov bx, [bp + 4]
mov [word bx], -1
@@end:
popp
pop bp
ret 4
endp OpenWrite
; -----------------------
; closes a file
; USAGE:
; push [filehandle]
; call CloseFile
; ------------------------
proc CloseFile
push bp
mov bp, sp
push ax bx cx dx si
; SS:
; [bp+4]: file handle
mov bx, [bp+4]
cmp bx, -1 ; invalid handle
je @@end
mov ah, 3Eh
int 21h
@@end:
pop si dx cx bx ax
pop bp
ret 2
endp CloseFile
macro pixel color, x, y
push ax bx cx dx si
mov cx, x
mov dx, y
mov ah, 0Ch
mov al, color
int 10h
pop si dx cx bx ax
endm
; checks for a keypress and if clicked CF=1 otherwise CF=0
macro zkey key
local done, no
pushh
; check keys in buffer
mov ah, 1
int 16h
jz no
; read keys in buffer
xor ah, ah
int 16h
; is requested key
cmp al, key
jne no
; yes, CF=1
stc
jmp done
; no, CF=0
no:
clc
done:
popp
endm
; this macro accepts a timer var
; and sets its value to current time
macro resetTimer timer
pushh
mov ah, 0
int 1Ah
mov [offset timer], dx
popp
endm
; this macro accepts a timer var, ticks cooldown and callback proc
; and calls the callback if ticks cooldown time has passed :-))))
macro checkTimer timer, ticks, callback
local endmac, set_carry
pushh
mov ah, 0
int 1Ah
sub dx, [offset timer]
cmp dx, ticks
popp
jge set_carry
jmp endmac
set_carry:
resetTimer timer
call callback
endmac:
endm
; macro: sets video mode background color to input
macro bgset color
pushh
; apply stosb instr to the video mode area
mov ax, 0A000h
mov es, ax
xor di, di ; start from 0
cld ; and go forward
mov al, color
mov cx, 320*200
rep stosb
popp
endm
; macro: sets text mode background color to input
macro bgtextset color
pushh
; apply stosb instr to the video mode area
mov ax, 0B800h
mov es, ax
xor di, di ; start from 0
cld ; and go forward
mov ah, color
mov al, '?'
mov cx, 80*25
rep stosw
popp
endm
; MACRO (TEXT MODE)
; put a character with color at x,y coords
; ARGUMENTS: char, color, x, y
macro char chr, color, x, y
pushh
; store cursor position
mov ah, 03h
int 10h
push dx
xor bh, bh ; always page 0
; set cursor position
mov ah, 02h
mov dl, x
mov dh, y
int 10h
; write char with attr
mov ah, 09h
mov al, chr
mov bl, color
mov cx, 1
int 10h
; recall cursor position
mov ah, 02h
pop dx
int 10h
popp
endm
; calls given procedure if je
macro calleq proc
local endmac
jne endmac
call proc
endmac:
endm
macro calleq_then proc, thenJmp
local endmac
jnc endmac
call proc
jmp thenJmp
endmac:
endm
; calls first param proc if je else second param
macro calleqelse procif, procelse
local endmac, noteq
jne noteq
call procif
jmp endmac
noteq:
call procelse
endmac:
endm
; retrives video mode fixed cursor position in CX=x DX=y, BX=click type
macro cursor
mov ax, 3
int 33h
shr cx, 1
endm
; LOGIC: alsoif
; used after a cmp for je
; if je in cmp AND if je in alsoif macro then
macro alsoif a, b
local endmac
jne endmac
cmp a, b
jne endmac
endmac:
endm
; checks if number is in range of two numbers
; if it is then JE else JNE
macro inrange min, cur, max
local bad, endmm
cmp cur, min
jl bad
cmp cur, max
jg bad
cmp ax, ax
jmp endmm
bad:
push ax bx
mov ax, 4
mov bx, 2
cmp ax, bx
pop bx ax
endmm:
endm
; gets a pressed key (if pressed CF=1 and key in AL else CF=0)
macro getkey
local nokey, e
mov ah, 01h
int 16h
jz nokey
mov ah, 0
int 16h
stc
jmp e
nokey:
clc
e:
endm
; calls a given procedure if "jc"
macro callc proc
local no_carry
jnc no_carry
call proc
no_carry:
endm
; clear video mode screen
proc clearvideo
pushh
; hide cursor
mov ax, 2
int 33h
; apply stosb instr to the video mode area
mov ax, 0A000h
mov es, ax
xor di, di ; start from 0
cld ; and go forward
mov al, BLACK
mov cx, 320*200
rep stosb
; put cursor to 0,0
xor dx, dx
xor bx, bx
mov ah, 02h
int 10h
; show cursor
mov ax, 1
int 33h
popp
ret
endp clearvideo
; prints a byte value as decimal
macro printb n
push ax
xor ah, ah
mov al, n
push ax
call showdecimal
pop ax
endm
macro btnrange X, Y, minX, addX, minY, addY
local endi
inrange minX, X, minX+addX
jne endi
inrange minY, Y, minY+addY
endi:
endm
; print a string and replace $ with two variables by order
; EXAMPLE: set ax, 10; set bx, 20; printvv "($,$)", ax, bx
; will do print "(" + call showdecimal(ax) + print "," + call showdecimal(bx) + print ")"
macro printvv msg, v1, v2
LOCAL skip_dcl, msg_dcl, msg_len, iter_msg, print_var, next, done, pv
pushh
JMP skip_dcl
msg_dcl db msg
msg_len = $-msg_dcl
skip_dcl:
push v2
push v1
xor di, di ; var counter
xor si, si
iter_msg:
cmp si, msg_len
je done
mov al, [cs:msg_dcl+si]
cmp al, '$'
je print_var
;print_char
mov ah, 2
mov dl, al
int 21h
jmp next
print_var:
inrange 0, di, 1
je pv
jmp next ; if there are more than two $s then ignore
pv:
call ShowDecimal ; uses item in stack
inc di
jmp next
next:
inc si
jmp iter_msg
done:
popp
endm
; print a string and replace $ with three variables by order
; EXAMPLE: set ax, 10; set bx, 20; set cx, 30; printvvv "($,$,$)", ax, bx, cx
; will do print "(" + call showdecimal(ax) + print "," + call showdecimal(bx) + print "," + call showdecimal(cx) + print ")"