Wednesday, 14 May 2008

x^n (mod CORESIZE) in Redcode

The following code implements the binary method to calculate x^n (mod CORESIZE) in Redcode. For calculations with n > 13, the binary method out-performs a simple loop.
power     mov    #1,             _powerr
_powloop mov.b _powern, temp
mod #2, temp
seq.b temp, #0
mul.b _powerx, _powerr
mul.b _powerx, _powerx
div #2, _powern
jmn _powloop, _powern

_powern dat n
_powerx dat x
_powerr dat 1

No comments: