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 / gcd-input.asm
1 .model small
2 ;greatest common divisor, with input from terminal
3 data segment
4 tempStr db " "
5 prompt db "num?$"
6 promptr db "RES $"
7 m dw 0
8 n dw 0
10 data ends
11 ;#macro-commands-start
12 ; print a string on screen
13 print_str macro s
14 push ax
15 push dx
16 mov dx, offset s
17 mov ah, 09
18 int 21h
19 pop dx
20 pop ax
21 endm
23 ; new line
24 print_new_line macro
25 push ax
26 push bx
27 push dx
28 mov ah,03
29 mov bh,0
30 int 10h
31 inc dh
32 mov dl,0
33 mov ah,02
34 int 10h
35 pop dx
36 pop bx
37 pop ax
38 endm
40 ; write a single char
41 print_char macro c
42 push ax
43 push dx
44 mov ah, 02
45 mov dl, c
46 int 21h
47 pop dx
48 pop ax
49 endm
51 ; finish the execution
52 end_execution macro
53 mov ax, 4c02h
54 int 21h
55 endm
57 ; Konvertovanje broja u string
58 inttostr macro num1 str1
59 push ax
60 push bx
61 push cx
62 push dx
63 push si
64 mov ax, num1
65 mov dl, '$'
66 push dx
67 mov si, 10
68 petlja2:
69 mov dx, 0
70 div si
71 add dx, 48
72 push dx
73 cmp ax, 0
74 jne petlja2
76 mov bx, offset str1
77 petlja2a:
78 pop dx
79 mov [bx], dl
80 inc bx
81 cmp dl, '$'
82 jne petlja2a
83 pop si
84 pop dx
85 pop cx
86 pop bx
87 pop ax
88 endm
90 print_num macro num
91 inttostr num,tempStr
92 print_str tempStr
93 endm
94 read_num macro num
95 read_str tempStr,6
96 strtoint tempStr,num
97 endm
99 ;read into a buffer, inc the special 0 and 1 bytes
100 ;fixes it into a $ terminated string
101 read_str macro strbuff, strlen
102 LOCAL copystr
103 push ax
104 push bx
105 push cx
106 push dx
107 push si
108 mov bp, sp
109 mov dx, offset strbuff
110 mov bx, dx
111 mov ax, strlen
112 mov byte [bx] ,al
113 mov ah, 0Ah
114 int 21h
115 mov si, dx
116 mov cl, [si+1]
117 mov ch, 0
118 copystr:
119 mov al, [si+2]
120 mov [si], al
121 inc si
122 loop copystr
123 mov [si], '$'
124 pop si
125 pop dx
126 pop cx
127 pop bx
128 pop ax
129 endm
131 ;ascii to actual number
132 strtoint macro inStr,outNum
133 LOCAL mainloop,end
134 push ax
135 push bx
136 push cx
137 push dx
138 push si
139 mov bp, sp
140 mov bx, offset inStr
141 mov ax, 0
142 mov cx, 0
143 mov si, 10
144 mainloop:
145 mov cl, [bx]
146 cmp cl, '$'
147 je end
148 mul si
149 sub cx, 48
150 add ax, cx
151 inc bx
152 jmp mainloop
153 end:
154 mov outNum, ax
155 pop si
156 pop dx
157 pop cx
158 pop bx
159 pop ax
160 endm
161 ;#macro-commands-end
163 .code
165 start:
166 mov ax,@data
167 mov ds,ax
168 print_str prompt
169 read_num m
170 print_new_line
171 print_str prompt
172 read_num n
173 print_new_line
175 mov ax,n
176 mov bx,m
178 compare:
179 cmp ax,bx
180 je exit ;exit since they're equal
181 ja greater
182 sub bx,ax
183 jmp compare
185 greater:
186 sub ax,bx
187 jmp compare
189 exit:
190 ;exit out of the program
191 mov n,ax
192 print_str promptr
193 print_num n
194 ; print out a result
195 end_execution
197 .stack
198 end start
Svarog.pmf.uns.ac.rs/gitweb maintanance Doni Pracner