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 / array-sum-input.asm
1 .model small
2 ;make a sum of an array - version with loading values
3 ;predefined array size
5 ;#macro-commands-start
6 end_execution macro
7 ; finish the execution
8 mov ax, 4c02h
9 int 21h
10 endm
12 print_str macro s
13 ; print a s string on screen
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 inttostr macro num1 str1
24 ; convert num1 to str1
25 push ax
26 push bx
27 push cx
28 push dx
29 push si
30 mov ax, num1
31 mov dl, '$'
32 push dx
33 mov si, 10
34 itosloop:
35 mov dx, 0
36 div si
37 add dx, 48
38 push dx
39 cmp ax, 0
40 jne itosloop
42 mov bx, offset str1
43 itosloopa:
44 pop dx
45 mov [bx], dl
46 inc bx
47 cmp dl, '$'
48 jne itosloopa
49 pop si
50 pop dx
51 pop cx
52 pop bx
53 pop ax
54 endm
56 print_num macro num
57 ;convert num, print str
58 inttostr num,tempStr
59 print_str tempStr
60 endm
62 ; new line
63 print_new_line macro
64 push ax
65 push bx
66 push dx
67 mov ah,03
68 mov bh,0
69 int 10h
70 inc dh
71 mov dl,0
72 mov ah,02
73 int 10h
74 pop dx
75 pop bx
76 pop ax
77 endm
79 read_num macro num
80 read_str tempStr,6
81 strtoint tempStr,num
82 endm
84 ;read into a buffer, inc the special 0 and 1 bytes
85 ;fixes it into a $ terminated string
86 read_str macro strbuff, strlen
87 LOCAL copystr
88 push ax
89 push bx
90 push cx
91 push dx
92 push si
93 mov bp, sp
94 mov dx, offset strbuff
95 mov bx, dx
96 mov ax, strlen
97 mov byte [bx] ,al
98 mov ah, 0Ah
99 int 21h
100 mov si, dx
101 mov cl, [si+1]
102 mov ch, 0
103 copystr:
104 mov al, [si+2]
105 mov [si], al
106 inc si
107 loop copystr
108 mov [si], '$'
109 pop si
110 pop dx
111 pop cx
112 pop bx
113 pop ax
114 endm
116 ;ascii to actual number
117 strtoint macro inStr,outNum
118 LOCAL mainloop,end
119 push ax
120 push bx
121 push cx
122 push dx
123 push si
124 mov bp, sp
125 mov bx, offset inStr
126 mov ax, 0
127 mov cx, 0
128 mov si, 10
129 mainloop:
130 mov cl, [bx]
131 cmp cl, '$'
132 je end
133 mul si
134 sub cx, 48
135 add ax, cx
136 inc bx
137 jmp mainloop
138 end:
139 mov outNum, ax
140 pop si
141 pop dx
142 pop cx
143 pop bx
144 pop ax
145 endm
146 ;#macro-commands-end
148 .code
149 start:
150 mov dx, @data
151 mov ds, dx
152 print_str prompt
153 read_num n
154 mov ax, 0
155 mov cx, n
156 cmp maxn, cx
157 jae normal
158 print_str errorN
159 end_execution
160 normal:
161 loadloop:
162 print_str prompt
163 read_num t
164 mov ax,t
165 mov bx,n
166 sub bx,cx
167 mov niz[bx],al
168 loop loadloop
169 print_new_line
170 ; now sum up
171 mov cx, n
172 xor ax,ax
173 mainloop:
174 mov bx, cx
175 add al, niz[bx-1] ; get array memeber, byte
176 ; store sum in al
177 loop mainloop ; end calc if done
178 print_num ax
179 end_execution
181 data segment
182 n dw 7
183 t dw 0
184 maxn dw 20
185 niz db 1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0
186 tempStr db " "
187 prompt db "Number?$"
188 errorN db "Number too large!$"
189 ends
190 end start
Svarog.pmf.uns.ac.rs/gitweb maintanance Doni Pracner