-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcode.asm
370 lines (346 loc) · 7.04 KB
/
code.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
.MODEL SMALL
.STACK 1000H
.DATA
CR EQU 0DH
LF EQU 0AH
w DW 10 DUP (0000H)
.Code
main PROC
MOV AX, @DATA
MOV DS, AX
PUSH BP
MOV BP, SP
PUSH BX
SUB SP, 20
PUSH 0
POP BX ;array index
SHL BX, 1
MOV AX, w[BX] ;global array element
PUSH AX ;array element
PUSH BX ;array index
PUSH 2
POP AX ; 2 poped from stack
NEG AX
PUSH AX ; -2 pushed to stack
POP AX ;- 2 popped
POP BX ;index of the array element popped
MOV w[BX], AX ; assigning value of - 2 to w[BX]
POP AX ;Popping out w
PUSH 0
POP BX ;array index
SHL BX, 1
NEG BX
ADD BX, -4
PUSH BP
ADD BP, BX
MOV BX, BP
MOV AX, [BP]
POP BP
PUSH AX ;array element
PUSH BX ;array index
PUSH 0
POP BX ;array index
SHL BX, 1
MOV AX, w[BX] ;global array element
PUSH AX ;array element
PUSH BX ;array index
POP BX ;Pop array index from stack
POP AX ;w popped
POP BX ;index of the array element popped
PUSH BP ;storing BP
MOV BP, BX
MOV [BP], AX ; assigning value of w to x
POP BP
POP AX ;Popping out x
PUSH [BP+-2]
PUSH 0
POP BX ;array index
SHL BX, 1
NEG BX
ADD BX, -4
PUSH BP
ADD BP, BX
MOV BX, BP
MOV AX, [BP]
POP BP
PUSH AX ;array element
PUSH BX ;array index
POP BX ;Pop array index from stack
POP AX ;x popped
MOV [BP+-2], AX ; assigning value of x to i
POP AX ;Popping out i
PUSH [BP+-2]
CALL print_output
PUSH 1
POP BX ;array index
SHL BX, 1
NEG BX
ADD BX, -4
PUSH BP
ADD BP, BX
MOV BX, BP
MOV AX, [BP]
POP BP
PUSH AX ;array element
PUSH BX ;array index
PUSH 0
POP BX ;array index
SHL BX, 1
MOV AX, w[BX] ;global array element
PUSH AX ;array element
PUSH BX ;array index
POP BX ;Pop array index from stack
MOV AX, [BX] ;set AX to the value of w[BX]
INC AX ;Increment AX
MOV w[BX], AX ;set w[BX] to AX
POP AX ;w++ popped
POP BX ;index of the array element popped
PUSH BP ;storing BP
MOV BP, BX
MOV [BP], AX ; assigning value of w++ to x
POP BP
POP AX ;Popping out x
PUSH [BP+-2]
PUSH 1
POP BX ;array index
SHL BX, 1
NEG BX
ADD BX, -4
PUSH BP
ADD BP, BX
MOV BX, BP
MOV AX, [BP]
POP BP
PUSH AX ;array element
PUSH BX ;array index
POP BX ;Pop array index from stack
POP AX ;x popped
MOV [BP+-2], AX ; assigning value of x to i
POP AX ;Popping out i
PUSH [BP+-2]
CALL print_output
PUSH [BP+-2]
PUSH 0
POP BX ;array index
SHL BX, 1
MOV AX, w[BX] ;global array element
PUSH AX ;array element
PUSH BX ;array index
POP BX ;Pop array index from stack
POP AX ;w popped
MOV [BP+-2], AX ; assigning value of w to i
POP AX ;Popping out i
PUSH [BP+-2]
CALL print_output
PUSH [BP+-2]
PUSH [BP+-2]
PUSH 0
POP BX ; 0 poped from stack
POP AX ; i poped from stack
ADD AX, BX
PUSH AX ; i + 0 pushed to stack
POP AX ;i popped
MOV [BP+-2], AX ; assigning value of i to i
POP AX ;Popping out i
PUSH [BP+-2]
PUSH [BP+-2]
PUSH 0
POP BX ; 0 poped from stack
POP AX ; i poped from stack
SUB AX, BX
PUSH AX ; i - 0 pushed to stack
POP AX ;i popped
MOV [BP+-2], AX ; assigning value of i to i
POP AX ;Popping out i
PUSH [BP+-2]
PUSH [BP+-2]
PUSH 1
POP BX ; 1 poped from stack
POP AX ; i poped from stack
IMUL BX
PUSH AX ; i * 1 pushed to stack
POP AX ;i * 1 popped
MOV [BP+-2], AX ; assigning value of i * 1 to i
POP AX ;Popping out i
PUSH [BP+-2]
CALL print_output
PUSH [BP+-2]
PUSH 0
;i > 0
POP BX ;0 popped
POP AX ;i popped
CMP AX, BX ;i > 0
JG L_1 ;i > 0
PUSH 0 ;i > 0 is false
JMP L_2 ;i > 0 is false
L_1:
PUSH 1 ;i > 0 is true
L_2:
PUSH [BP+-2]
PUSH 10
;i < 10
POP BX ;10 popped
POP AX ;i popped
CMP AX, BX ;i < 10
JL L_3 ;i < 10
PUSH 0 ;i < 10 is false
JMP L_4 ;i < 10 is false
L_3:
PUSH 1 ;i < 10 is true
L_4:
;i > 0 && i < 10
POP BX ;i < 10 popped
POP AX ;i > 0 popped
CMP AX, 0 ;i > 0
JNE L_5 ;i > 0 is true
PUSH 0 ;i > 0 is false
JMP L_7 ;i > 0 is false
L_5:
CMP BX, 0 ;i < 10
JNE L_6 ;i < 10 is true
PUSH 0 ;i < 10 is false
JMP L_7 ;i < 10 is false
L_6:
PUSH 1 ;i > 0 AND i < 10 is true
L_7:
PUSH [BP+-2]
PUSH 0
;i < 0
POP BX ;0 popped
POP AX ;i popped
CMP AX, BX ;i < 0
JL L_8 ;i < 0
PUSH 0 ;i < 0 is false
JMP L_9 ;i < 0 is false
L_8:
PUSH 1 ;i < 0 is true
L_9:
PUSH [BP+-2]
PUSH 10
POP AX ; 10 poped from stack
NEG AX
PUSH AX ; -10 pushed to stack
;i > - 10
POP BX ;- 10 popped
POP AX ;i popped
CMP AX, BX ;i > - 10
JG L_10 ;i > - 10
PUSH 0 ;i > - 10 is false
JMP L_11 ;i > - 10 is false
L_10:
PUSH 1 ;i > - 10 is true
L_11:
;i < 0 && i > - 10
POP BX ;i > - 10 popped
POP AX ;i < 0 popped
CMP AX, 0 ;i < 0
JNE L_12 ;i < 0 is true
PUSH 0 ;i < 0 is false
JMP L_14 ;i < 0 is false
L_12:
CMP BX, 0 ;i > - 10
JNE L_13 ;i > - 10 is true
PUSH 0 ;i > - 10 is false
JMP L_14 ;i > - 10 is false
L_13:
PUSH 1 ;i < 0 AND i > - 10 is true
L_14:
;(i > 0) || (i < 0)
POP BX ;(i < 0) popped
POP AX ;(i > 0) popped
CMP AX, 0 ;(i > 0)
JNE L_15 ;(i > 0) is true
CMP BX, 0 ;(i < 0)
JNE L_15 ;(i < 0) is true
PUSH 0 ;(i > 0) OR (i < 0) is false
JMP L_16 ;(i > 0) OR (i < 0) is false
L_15:
PUSH 1 ;(i > 0) OR (i < 0) is true
L_16:
POP AX
CMP AX, 0
JE L_18
JMP L_17
L_17:
PUSH [BP+-2]
PUSH 100
POP AX ;100 popped
MOV [BP+-2], AX ; assigning value of 100 to i
POP AX ;Popping out i
JMP L_19
L_18:
PUSH [BP+-2]
PUSH 200
POP AX ;200 popped
MOV [BP+-2], AX ; assigning value of 200 to i
POP AX ;Popping out i
L_19:
PUSH [BP+-2]
CALL print_output
PUSH 0
MOV AX,4CH
INT 21H
main ENDP
NEW_LINE PROC
; PRINTS A NEW LINE
PUSH AX
PUSH DX
MOV AH, 2
MOV DL, cr
INT 21H
MOV AH, 2
MOV DL, lf
INT 21H
POP DX
POP AX
RET
NEW_LINE ENDP
PRINT_OUTPUT PROC NEAR
PUSH BP ;Saving BP
MOV BP, SP ;BP points to the top of the stack
MOV BX, [BP+4] ;The number to be printed
;if(BX < -1) then the number is positive
CMP BX, 0
JGE POSITIVE
;else, the number is negative
MOV AH, 2
MOV DL, '-' ;Print a '-' sign
INT 21H
NEG BX ;make BX positive
POSITIVE:
MOV AX, BX
MOV CX, 0 ;Initialize character count
PUSH_WHILE:
XOR DX, DX ;clear DX
MOV BX, 10 ;BX has the divisor //// AX has the dividend
DIV BX
;quotient is in AX and remainder is in DX
PUSH DX ;Division by 10 will have a remainder less than 8 bits
INC CX ;CX++
;if(AX == 0) then break the loop
CMP AX, 0
JE END_PUSH_WHILE
;else continue
JMP PUSH_WHILE
END_PUSH_WHILE:
MOV AH, 2
POP_WHILE:
POP DX ;Division by 10 will have a remaainder less than 8 bits
ADD DL, '0'
INT 21H ;So DL will have the desired character
DEC CX ;CX--
;if(CX <= 0) then end loop
CMP CX, 0
JLE END_POP_WHILE
;else continue
JMP POP_WHILE
END_POP_WHILE:
;Print newline
MOV DL, 0DH
INT 21H
MOV DL, 0AH
INT 21H
POP BP ; Restore BP
RET 2
PRINT_OUTPUT ENDP
END main