@@ -56,9 +56,8 @@ org 100h
56
56
; n := n - 1;
57
57
; end;
58
58
59
- ; register c is n. hl is a[]. b is a constant 1, a is a constant 0
59
+ ; register c is n. hl is a[]. b is a constant 1
60
60
61
- mvi a , 0
62
61
mvi b , 1
63
62
mvi c , DIGITS - 1
64
63
lxi h , array + DIGITS - 1
@@ -67,7 +66,6 @@ org 100h
67
66
mov m , b
68
67
dcx h
69
68
dcr c
70
- cmp c
71
69
jnz ainit
72
70
73
71
; a[ 1 ] := 2;
@@ -102,7 +100,7 @@ org 100h
102
100
mov m , a ; a[ n ] := x MOD n;
103
101
104
102
dcx h ; make hl point to a[ n - 1 ]
105
- mov e , m ; put a[ n - 1 ] in de
103
+ mov e , m ; put a[ n - 1 ] in de
106
104
mvi d , 0
107
105
mov h , d ; put the typically smaller mul argument (10) in hl for better performance
108
106
mvi l , 10
@@ -111,8 +109,6 @@ org 100h
111
109
dad d ; add the two parts together. x (aka hl) := (mul result + div result)
112
110
113
111
dcr c ; n := n - 1;
114
- xra a
115
- cmp c
116
112
jnz innerloop ; if n isn't 0 then loop again
117
113
118
114
call PrintNumber ; show the next digit(s) of e
@@ -219,47 +215,31 @@ PUTHL: ; print the signed 16-bit number in HL
219
215
; unsigned multiply de by hl, result in hl
220
216
umul:
221
217
mov a , l
222
- cpi 0
223
- jnz umul $ notzero
224
- mov a , h
225
- cpi 0
226
- jnz umul $ notzero
227
- ret
228
- umul $ notzero:
229
- push b ! push d
230
- push h
231
- pop b
218
+ ora h
219
+ rz
220
+ push b
221
+ mov b , h
222
+ mov c , l
232
223
lxi h , 0
233
- shld mulTmp
234
224
umul $ loop:
235
225
dad d
236
- jnc umul $ done
237
- push h
238
- lhld mulTmp
239
- inx h
240
- shld mulTmp
241
- pop h
242
- umul $ done:
243
226
dcx b
244
227
mov a , b
245
228
ora c
246
229
jnz umul $ loop
247
- pop d ! pop b
230
+ pop b
248
231
ret
249
232
250
- ; unsigned divide de by hl, result in hl. remainder in divRem
233
+ ; unsigned divide de by hl, result in hl. remainder in divRem. divide by 0 not checked.
251
234
udiv:
252
- xra a
253
- cmp e
254
- jnz udiv $ notzero
255
- cmp d
235
+ mov a , e
236
+ ora d
256
237
jnz udiv $ notzero
257
238
lxi h , 0
258
239
shld divRem ; 0 mod x is 0
259
240
ret
260
-
261
241
udiv $ notzero:
262
- push b ! push d
242
+ push b
263
243
xchg
264
244
lxi b , 0
265
245
udiv $ loop:
@@ -277,7 +257,7 @@ udiv:
277
257
shld divRem
278
258
mov l , c
279
259
mov h , b
280
- pop d ! pop b
260
+ pop b
281
261
ret
282
262
283
263
NEGF: db 0 ; Space for negative flag
@@ -286,7 +266,6 @@ NUM: db '$' ; Space for number. cp/m strings end with a
286
266
CRLF: db 13 , 10 , 0
287
267
STRDONE: db 'done.' , 13 , 10 , 0
288
268
289
- MULTMP: dw 0
290
269
DIVREM: dw 0
291
270
ARRAY: ds DIGITS
292
271
@@ -337,8 +316,8 @@ imul:
337
316
call neg $ hl
338
317
call neg $ de
339
318
imul $ notneg:
340
- push h
341
- pop b
319
+ mov b , h
320
+ mov c , l
342
321
lxi h , 0
343
322
shld mulTmp
344
323
imul $ loop:
0 commit comments