Difference between revisions of "Programming:Precalculated square"
From CPCWiki - THE Amstrad CPC encyclopedia!
(Initial page) |
(No difference)
|
Revision as of 14:00, 6 August 2006
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.
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
Get square from the table
Input: A = Factor
Output: DE = A*A
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
Table definition
SQTAB DS 512 ;space for the table