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