Changes

Jump to: navigation, search

Programming:Precalculated square

947 bytes added, 19:00, 6 August 2006
Initial page
This article describes an algorithm which precalculates the square from 0*0 to 255*255 and the routine to get the correct square value from the table.

== Initialisation ==

Precalculate the square table.
<pre>
INITSQ LD DE, 1 ;1st odd number
LD HL, 0 ;HL = 1st square number
LD B, H ;counter = 256
LD IX, SQTAB ;startaddress of the square table
SQLOOP LD (IX), L ;Lowbyte to table
INC IX
LD (IX), H ;Highbyte to table
INC IX
ADD HL, DE ;add odd number
INC DE ;next odd number
INC DE
DJNZ SQLOOP ;256 times
RET
</pre>

== Get square from the table ==

'''Input:''' A = ''Factor''

'''Output:''' DE = ''A*A''

<pre>
GETSQ LD L, A
LD H, 0 ;HL = factor
ADD HL, HL ;* 2
LD DE, SQTAB ;+ startaddress of the table
ADD HL, DE ;= tableaddress
LD E, (HL) ;E = Lowbyte of the result
INC HL
LD D, (HL) ;D = Highbyte of the result
RET
</pre>

== Table definition ==

<pre>
SQTAB DS 512 ;space for the table
</pre>
1,165
edits