gitweb on Svarog

projekti pod git sistemom za održavanje verzija -- projects under the git version control system
version 0.81
[asm2wsl.git] / samples-with-macros / rek-gcd-input.asm
1 .model small
2 ASSUME DS:data
3 ;recursive version of GCD, input from terminal
4 data segment
5 strNZD db " "
6 tempStr db " "
7 prompt db "num?$"
8 promptr db "RES $"
9 m dw 0
10 n dw 0
11 data ends
12 ;#macro-commands-start
13 ; print a string on screen
14 print_str macro s
15 push ax
16 push dx
17 mov dx, offset s
18 mov ah, 09
19 int 21h
20 pop dx
21 pop ax
22 endm
24 ; new line
25 print_new_line macro
26 push ax
27 push bx
28 push dx
29 mov ah,03
30 mov bh,0
31 int 10h
32 inc dh
33 mov dl,0
34 mov ah,02
35 int 10h
36 pop dx
37 pop bx
38 pop ax
39 endm
41 ; write a single char
42 print_char macro c
43 push ax
44 push dx
45 mov ah, 02
46 mov dl, c
47 int 21h
48 pop dx
49 pop ax
50 endm
52 ; finish the execution
53 end_execution macro
54 mov ax, 4c02h
55 int 21h
56 endm
58 ; Konvertovanje broja u string
59 inttostr macro num1 str1
60 push ax
61 push bx
62 push cx
63 push dx
64 push si
65 mov ax, num1
66 mov dl, '$'
67 push dx
68 mov si, 10
69 petlja2:
70 mov dx, 0
71 div si
72 add dx, 48
73 push dx
74 cmp ax, 0
75 jne petlja2
77 mov bx, offset str1
78 petlja2a:
79 pop dx
80 mov [bx], dl
81 inc bx
82 cmp dl, '$'
83 jne petlja2a
84 pop si
85 pop dx
86 pop cx
87 pop bx
88 pop ax
89 endm
91 print_num macro num
92 inttostr num,strNZD
93 print_str strNZD
94 endm
96 read_num macro num
97 read_str tempStr,6
98 strtoint tempStr,num
99 endm
101 ;read into a buffer, inc the special 0 and 1 bytes
102 ;fixes it into a $ terminated string
103 read_str macro strbuff, strlen
104 LOCAL copystr
105 push ax
106 push bx
107 push cx
108 push dx
109 push si
110 mov bp, sp
111 mov dx, offset strbuff
112 mov bx, dx
113 mov ax, strlen
114 mov byte [bx] ,al
115 mov ah, 0Ah
116 int 21h
117 mov si, dx
118 mov cl, [si+1]
119 mov ch, 0
120 copystr:
121 mov al, [si+2]
122 mov [si], al
123 inc si
124 loop copystr
125 mov [si], '$'
126 pop si
127 pop dx
128 pop cx
129 pop bx
130 pop ax
131 endm
133 ;ascii to actual number
134 strtoint macro inStr,outNum
135 LOCAL mainloop,end
136 push ax
137 push bx
138 push cx
139 push dx
140 push si
141 mov bp, sp
142 mov bx, offset inStr
143 mov ax, 0
144 mov cx, 0
145 mov si, 10
146 mainloop:
147 mov cl, [bx]
148 cmp cl, '$'
149 je end
150 mul si
151 sub cx, 48
152 add ax, cx
153 inc bx
154 jmp mainloop
155 end:
156 mov outNum, ax
157 pop si
158 pop dx
159 pop cx
160 pop bx
161 pop ax
162 endm
163 ;#macro-commands-end
165 .code
167 start:
168 mov ax,@data
169 mov ds,ax
170 print_str prompt
171 read_num m
172 print_new_line
173 print_str prompt
174 read_num n
175 print_new_line
177 push n
178 push m
179 call gcd
180 pop n
181 print_str promptr
182 print_num n
183 ; print out a result
184 end_execution
187 ;volatile procedure, ruins ax,bx,cx
188 gcd proc
189 ;;#extra-start
190 pop cx ;ret adress
191 ;;#extra-end
192 ;get params
193 pop ax
194 pop bx
195 ;;#extra-start
196 push cx ;ret for later
197 ;;#extra-end
198 cmp ax,bx
199 je endequal
200 ja greatera
201 ;ensure ax is greater
202 xchg ax,bx
203 greatera:
204 sub ax,bx
205 push bx
206 push ax
207 call gcd
208 pop ax;result
210 endequal:
211 ;;#extra-start
212 pop cx
213 ;;#extra-end
214 push ax; result
215 ;;#extra-start
216 push cx;needed before ret
217 ;;#extra-end
218 ret
219 gcd endp
221 .stack
222 end start
Svarog.pmf.uns.ac.rs/gitweb maintanance Doni Pracner