; Computes f(R0), storing the result in R4 .ORIG x5000 ST R1, saver1 ST R2, saver2 ST R3, saver3 ST R5, saver5 ST R6, saver6 ST R7, retaddr LD R6, poly LDR R5, R6, #0 ; R5 = Degree ADD R6, R6, #1 LDR R1, R6, #0 ; R1(output) = first term loop2 ADD R5, R5, #-1 ; Degree -= 1 BRn done ; If degree <= 0: end loop JSR mult ; Output *= X AND R1, R2, R2 ADD R6, R6, #1 LDR R2, R6, #0 ADD R1, R1, R2 ; Add next term to output BR loop2 done AND R4, R1, R1 LD R1, saver1 LD R2, saver2 LD R3, saver3 LD R5, saver5 LD R6, saver6 LD R7, retaddr RET poly .FILL x4003 saver1 .BLKW #1 saver2 .BLKW #1 saver3 .BLKW #1 saver5 .BLKW #1 saver6 .BLKW #1 retaddr .BLKW #1 ; Multiplies R0 and R1 (in constant rather than linear time), placing the ; result in R2 (clobbers R1-R4) mult AND R4, R4, #0 ; Set negative flag to false ST R7, saver7 ST R0, saver0 AND R0, R0, R0 BRzp posA ; If R0 is negative: set it to it's absolute value NOT R0, R0 ; and flip the negative flag ADD R0, R0, #1 NOT R4, R4 posA AND R1, R1, R1 BRzp posB ; Do the same for R1 NOT R1, R1 ADD R1, R1, #1 NOT R4, R4 posB AND R3, R3, #0 ADD R3, R3 #1 ; Set the mask to 1 AND R2, R2, #0 ; Clear the output loop AND R7, R0, R3 BRz skip ADD R2, R2, R1 ; If R0[mask] == 1: add R1 to output skip ADD R1, R1, R1 ; Bitshift the mask and R1 left by one ADD R3, R3, R3 BRnp loop ; Repeat for each bit AND R4, R4, R4 BRz pos ; If the flag is enabled, negate the output NOT R2, R2 ADD R2, R2, #1 pos LD R0, saver0 LD R7, saver7 RET saver0 .BLKW #1 saver7 .BLKW #1 .END