Date : Sun, 05 Jun 2005 14:30:15 +0100
From : jgh@... (Jonathan Graham Harston)
Subject: Re: Assembly Language Square Root
"F. Haroon" <haroonnet2002@...> wrote:
> This program gives me a lot of garble when testing...
Works here:
----8<----
>LIST
10PROCasm:PROCtest:END
20:
30DEFPROCasm
40DIM mc% &100
50in=&70:out=&72
60FOR P=0 TO 1:P%=mc%:[OPT P*2
70.sqr :\ On entry, !in=input value
80LDY #1:STY out+0:DEY:STY out+1 :\ Initialise out to first subtrand
90.sqr_loop :\ Repeatedly subtract increasing
100SEC :\ odd numbers until in<0
110LDA in+0:TAX:SBC out+0:STA in+0 :\ in=in-subtrand, remainder in X
120LDA in+1:SBC out+1:STA in+1
130BCC sqr_done :\ in<0, all done
140INY :\
150LDA out+0:ADC #1:STA out+0 :\ step +2 to next odd number
160BCC sqr_loop :\ no overflow, subtract again
170INC out+1:BNE sqr_loop :\ INC high byte and subtract again
180.sqr_done
190STY out+0:STX out+1 :\ out?0=root, out?1=remainder
200RTS
210]NEXT:ENDPROC
220:
230DEFPROCtest
240FOR A%=1 TO 65535:!in=A%:CALL sqr:PRINTA%,out?0,out?1:NEXT
250ENDPROC
>RUN
1 1 0
2 1 1
3 1 2
4 2 0
5 2 1
6 2 2
7 2 3
8 2 4
9 3 0
10 3 1
11 3 2
12 3 3
13 3 4
14 3 5
15 3 6
16 4 0
17 4 1
18 4 2
19 4 3
20 4 4
21 4 5
22 4 6
23 4 7
24 4 8
25 5 0
26 5 1
27 5 2
28 5 3
29 5 4
30 5 5
31 5 6
32 5 7
33 5 8
34 5 9
35 5 10
36 6 0
37 6 1
38 6 2
39 6 3
40 6 4
41 6 5
42 6 6
43 6 7
44 6 8
45 6 9
46 6 10
47 6 11
48 6 12
49 7 0
50 7 1
51 7 2
52 7 3
53 7 4
54 7 5
55 7 6
56 7 7
57 7 8
58 7 9
59 7 10
60 7 11
61 7 12
62 7 13
63 7 14
64 8 0
65 8 1
66 8 2
67 8 3
68 8 4
69 8 5
70 8 6
71 8 7
72 8 8
73 8 9
74 8 10
75 8 11
76 8 12
77 8 13
78 8 14
79 8 15
80 8 16
81 9 0
82 9 1
83 9 2
84 9 3
85 9 4
86 9 5
87 9 6
88 9 7
89 9 8
90 9 9
91 9 10
92 9 11
93 9 12
94 9 13
95 9 14
96 9 15
Escape at line 240
>*SPOOL
--
J.G.Harston - jgh@... - mdfs.net/User/JGH
HADFS System Resources - http://mdfs.net/Software/HADFS