-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathARITHMET.ASM
286 lines (286 loc) · 7.92 KB
/
ARITHMET.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
;
;
; ARITHMETIC.ASM -- Basic arithmetic functions, coded in assembly language.
; By AK at 29. 3. 1989
; Added neg and bit-fiddling functions not, and, or, xor, shl, shr, sar
; at 3-Jan-1991 (by AK)
; Plus sxt (sign extend) at 6-jan-1991.
;
; This module belongs to St. Vitus' Lisp which is the Lisp Interpreter
; for the MS-DOS machines. Following text applies to this module and to
; all other modules in this package unless otherwise noted:
;
; Copyright (C) 1991 Antti J. Karttunen
;
; This program is free software; you can redistribute it and/or modify
; it under the terms of the GNU General Public License as published by
; the Free Software Foundation; either version 1, or (at your option)
; any later version.
;
; This program is distributed in the hope that it will be useful,
; but WITHOUT ANY WARRANTY; without even the implied warranty of
; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
; GNU General Public License (in file GPL.TXT) for more details.
;
; You should have received a copy of the GNU General Public License
; along with this program; if not, write to the Free Software
; Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
;
;
include mask-asm.h
;
codeseg segment para public 'code'
dataseg segment para public 'data'
quot_string db "/"
db 0
rem_string db "%"
db 0
dataseg ends
assume cs:codeseg,ds:dataseg,es:dataseg,ss:dataseg
;
;
; TOB add1(x)
; TOB x;
;
public add1_
add1_ proc near
mov bx,sp
mov dx,ss:[bx+4] ; Get high word
mov ax,ss:[bx+2] ; Get low word
inc ax ; and increment.
ret
add1_ endp
;
;
; TOB sub1(x)
; TOB x;
;
public sub1_
sub1_ proc near
mov bx,sp
mov dx,ss:[bx+4] ; Get high word
mov ax,ss:[bx+2] ; Get low word
dec ax ; and decrement it.
ret
sub1_ endp
;
;
; TOB plus(x,y)
; TOB x,y;
;
public plus_
plus_ proc near
mov bx,sp
mov dx,ss:[bx+4] ; Get high word of first arg
mov ax,ss:[bx+2] ; Get low word of first arg
add ax,ss:[bx+6] ; And add low word of second arg to it.
ret
plus_ endp
;
;
;
; TOB difference(x,y)
; TOB x,y;
;
public difference_
difference_ proc near
mov bx,sp
mov dx,ss:[bx+4] ; Get high word of first arg
mov ax,ss:[bx+2] ; Get low word of first arg
sub ax,ss:[bx+6] ; And subtract low word of second arg from it.
ret
difference_ endp
;
;
;
; TOB times(x,y)
; TOB x,y;
;
public times_
times_ proc near
mov bx,sp
mov ax,ss:[bx+2] ; Get low word of first arg.
mul ss:[bx+6] ; And multiply it with low word of second arg.
mov dx,ss:[bx+4] ; Get highword (i.e. tagbits) of first arg.
ret
times_ endp
;
;
;
; TOB quotient(x,y)
; TOB x,y;
;
public quotient_
quotient_ proc near
mov bx,sp
mov ax,ss:[bx+2] ; Get low word of first arg. (Dividend)
xor dx,dx ; Zero DX because div divides DX:AX
mov cx,ss:[bx+6] ; Get low word of second arg (divisor)
jcxz quot_ertzu
div cx ; Divide DX:AX with CX (low word of 2nd arg).
mov dx,ss:[bx+4] ; Get highword (i.e. tagbits) of first arg.
ret
quot_ertzu: ; _inv2arg("/",2,x,y);
push ss:[bx+8]
push ss:[bx+6]
push ss:[bx+4]
push ss:[bx+2]
pushi 2
push ds
pushadr quot_string
call _inv2arg_
quotient_ endp
;
;
;
; TOB remainder(x,y)
; TOB x,y;
;
public remainder_
remainder_ proc near
mov bx,sp
mov ax,ss:[bx+2] ; Get low word of first arg. (Dividend)
xor dx,dx ; Zero DX because div divides DX:AX
mov cx,ss:[bx+6] ; Get low word of second arg (divisor)
jcxz rem_ertzu
div cx ; Divide DX:AX with CX (low word of 2nd arg).
mov ax,dx ; Remainder will be placed to DX.
mov dx,ss:[bx+4] ; Get highword (i.e. tagbits) of first arg.
ret
rem_ertzu: ; _inv2arg("/",2,x,y);
push ss:[bx+8]
push ss:[bx+6]
push ss:[bx+4]
push ss:[bx+2]
pushi 2
push ds
pushadr rem_string
call _inv2arg_
remainder_ endp
;
;
;
; TOB _neg(x)
; TOB x;
;
public _neg_
_neg_ proc near
mov bx,sp
mov dx,ss:[bx+4] ; Get high word
mov ax,ss:[bx+2] ; Get low word
neg ax ; and negate it.
ret
_neg_ endp
;
;
; TOB _sxt(x)
; TOB x;
;
public _sxt_
_sxt_ proc near
mov bx,sp
mov dx,ss:[bx+4] ; Get high word
mov ax,ss:[bx+2] ; Get low word,
cbw ; sign extend low byte of it. sxt(AL) -> AX
ret
_sxt_ endp
;
;
; TOB _not(x)
; TOB x;
;
public _not_
_not_ proc near
mov bx,sp
mov dx,ss:[bx+4] ; Get high word
mov ax,ss:[bx+2] ; Get low word
not ax ; and complement it.
ret
_not_ endp
;
;
; TOB _and(x,y)
; TOB x,y;
;
public _and_
_and_ proc near
mov bx,sp
mov dx,ss:[bx+4] ; Get high word of first argument. (= type)
mov ax,ss:[bx+2] ; Get low word of first argument.
and ax,ss:[bx+6] ; and it with a low word of second arg.
ret
_and_ endp
;
;
; TOB _or(x,y)
; TOB x,y;
;
public _or_
_or_ proc near
mov bx,sp
mov dx,ss:[bx+4] ; Get high word of first argument. (= type)
mov ax,ss:[bx+2] ; Get low word of first argument.
or ax,ss:[bx+6] ; or it with a low word of second arg.
ret
_or_ endp
;
;
; TOB _xor(x,y)
; TOB x,y;
;
public _xor_
_xor_ proc near
mov bx,sp
mov dx,ss:[bx+4] ; Get high word of first argument. (= type)
mov ax,ss:[bx+2] ; Get low word of first argument.
xor ax,ss:[bx+6] ; xor it with a low word of second arg.
ret
_xor_ endp
;
;
; TOB _shl(x,y) /* Shift Left, logical & arithmetic */
; TOB x,y;
;
public _shl_
_shl_ proc near
mov bx,sp
mov dx,ss:[bx+4] ; Get high word of first argument. (= type)
mov ax,ss:[bx+2] ; Get low word of first argument.
mov cx,ss:[bx+6] ; Get shift count from low word of second arg.
shl ax,cl ; Shift AX left.
ret
_shl_ endp
;
;
; TOB _shr(x,y) /* Shift Right, logical */
; TOB x,y;
;
public _shr_
_shr_ proc near
mov bx,sp
mov dx,ss:[bx+4] ; Get high word of first argument. (= type)
mov ax,ss:[bx+2] ; Get low word of first argument.
mov cx,ss:[bx+6] ; Get shift count from low word of second arg.
shr ax,cl ; Shift AX right.
ret
_shr_ endp
;
;
; TOB _sar(x,y) /* Shift right, arithmetic */
; TOB x,y;
;
public _sar_
_sar_ proc near
mov bx,sp
mov dx,ss:[bx+4] ; Get high word of first argument. (= type)
mov ax,ss:[bx+2] ; Get low word of first argument.
mov cx,ss:[bx+6] ; Get shift count from low word of second arg.
sar ax,cl ; Shift AX right. (arithmetically).
ret
_sar_ endp
;
;
extrn _inv1arg_:near
extrn _inv2arg_:near
codeseg ends
END