This repository has been archived by the owner on Jan 8, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.asm
196 lines (164 loc) · 2.81 KB
/
main.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
macro setCursor x*, y*{
push dx
mov dh,y
mov dl,x
call set_cursor
pop dx
}
macro write_char char*{
mov dl,char
call print_char
}
macro if lhs*, rhs*,function*, label* {
cmp lhs,rhs
function label
}
macro call_if lhs*, rhs*,jump*,function*{
cmp lhs,rhs
jump @f
jmp ex#function
@@:
call function
ex#function = $
}
use16
org 100h
pusha
mov ah, 0x00
mov al, 0x03 ; text mode 80x25 16 colours
int 0x10
popa
main:
call read_key
call search_array
call_if bx,0,je, place_array_element
call_if bx,0,jne,increment_array_element
setCursor 0,0
call print_array
setCursor 0,23
jmp main
call wait_exit
.main_exit:
mov ax,4C00h
int 21h
.vars:
press db 13,10, 'Press any key ... $'
text db " - $"
pointer dw 0
array db 128 dup(0,0)
array_end = $
print_array:
mov bx, array
@@:
if byte[bx],0,je, @f
if bx, array_end, je, @f
;if byte[bx], 0, jne, .exit
write_char byte[bx]
mov ah,0x9
mov dx, text
int 0x21
mov al,byte[bx + 1]
mov ah, 0
;call print_number
call printw
write_char 0x9 ; tab
inc bx
inc bx
jmp @b
@@:
mov bx,0
RET
; Write char to stdout
; <- DL - char to print
print_char:
mov ah, 0x02
int 0x21
RET
; Read char from stdin
; -> AL - char
read_key:
mov ah,1
int 0x21
RET
; Move cursor to position
; <- DH - row
; <- DL - column
set_cursor:
push ax
push dx
mov ah, 2 ; set cursor pos
mov bh, 0 ; video page
int 10h
pop dx
pop ax
RET
; <- AL - char
; <- *BX - array_pointer
increment_array_element:
inc byte[bx+1]
RET
; <- AL - char
; <- *CX - first free pointer
place_array_element:
push bx
mov bx,cx
mov [bx], al
inc byte[bx+1]
pop bx
RET
; <- AL - char
; -> *BX - first found pointer
; if BX == 0 =>
; -> *CX - first free
search_array:
mov bx, array
.start:
if byte[bx],0,je, .save_free
if byte[bx], al, je, .exit
if bx, array_end, je, .pre_exit
inc bx
inc bx
jmp .start
.save_free:
mov cx,bx
.pre_exit:
mov bx,0
.exit:
RET
wait_exit:
mov ah, 0x9
mov dx, press
int 0x21
mov ah, 0x8
int 0x21
RET
; Print HEX number with leading zero's
; <- AX - number to prin
printw:
push ax
shr ax, 8
call .printb
pop ax
push ax
and ax, 0xff
call .printb
pop ax
RET
.printb:
push ax
shr al, 4
call .printasc
pop ax
and al, 0xf
call .printasc
ret
.printasc:
add al, 0x30
cmp al, 0x39
jle .printasc_e
add al, 0x7
.printasc_e:
mov dl, al
mov ah, 0x2
int 0x21
ret