-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathex5.asm
More file actions
204 lines (177 loc) · 2.86 KB
/
ex5.asm
File metadata and controls
204 lines (177 loc) · 2.86 KB
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
data segment
;numerr db 'no operand for operator',0dh,0ah,'$'
;synerr db 'unmatched parenthese',0dh,0ah,'$'
;oprerr db 'wrong operator',0dh,0ah,'$'
unknownerr db 'unknown input',0dh,0ah,'$'
buffer db 1024 dup(?)
data ends
stack segment para stack 'stack'
dw 64 dup(0)
stack ends
code segment
assume cs:code,ss:stack,ds:data
main proc far
start:
mov ax,data
mov ds,ax
xor si,si
xor ax,ax
call readExpr
dec si
xor bx,bx
xor cx,cx
call calStk
;pop ax
xor di,di
tranto10:
mov dx,0
mov bx,10
inc di
div bx;(dx)(ax)/10
push dx;dx为模,ax为商
mov dx,0
cmp ax,0
jz print
jmp tranto10
print:
;cmp di,0
;jz printloop
pop ax
mov dl,al
add dl,48
mov ah,02h
int 21h
dec di
cmp di,0
jne print
jmp quit
main endp
calStk proc
push bx
push cx
cmp buffer[si],'-'
je eval
cmp buffer[si],'+'
je eval
xor ah,ah
mov al,buffer[si]
dec si
jmp calquit
eval:
mov cl,buffer[si]
dec si
call calStk
mov bx,ax
call calStk
cmp cl,'+'
je plus
sub ax,bx
jmp calquit
plus:
add ax,bx
calquit:
pop cx
pop bx
;push ax
ret
calStk endp
readExpr proc
push bx
push dx
xor bx,bx
xor dx,dx
read:
call getChar
compare:
cmp al,13
je check
cmp al,')'
je check
cmp al,'('
je newExpr
cmp al,'+'
je opr
cmp al,'-'
je opr
cmp al,'0'
jl unknown
cmp al,'9'
jg unknown
sub al,'0'
jmp readNum
readNum:
mov cl,al
mov al,bl
xor ah,ah
mov ch,10
mul ch
mov bl,al
add bl,cl
mov dl,1
call getChar
jmp compare
opr:
test dl,dl
jz noNum
xor dl,dl
mov buffer[si],bl
inc si
xor bl,bl
noNum:
test bh,bh
jz noOpr
mov buffer[si],bh
inc si
noOpr:
mov bh,al
jmp read
newExpr:
call readExpr
cmp al,')'
jne synErr
test bh,bh
jz read
mov buffer[si],bh
inc si
xor bx,bx
jmp read
numErr:
jmp quit
synErr:
jmp quit
oprErr:
jmp quit
unknown:
push dx
push ax
lea dx,unknownerr
mov ah,09h
int 21h
pop ax
pop dx
jmp quit
check:
test dl,dl
jz finish
mov buffer[si],bl
inc si
test bh,bh
jz finish
mov buffer[si],bh
inc si
finish:
pop dx
pop bx
ret
readExpr endp
getChar proc
mov ah,1
int 21h
ret
getChar endp
quit:
mov ah,4ch
int 21h
ret
code ends
end start