LDY #$00 ; R = 0 LDX #$07 CLC ; clear bit 16 of M .loop TYA ORA stab-1,X STA THI ; (R ASL 8) | (D ASL 7) LDA MHI BCS .skip0 ; M >= 65536? then T <= M is always true CMP THI BCC .skip1 ; T <= M .skip0 SBC THI STA MHI ; M = M - T TYA ORA stab,x TAY ; R = R OR D .skip1 ASL MLO ROL MHI ; M = M ASL 1 DEX BNE .loop ; bit 0 iteration STY THI LDX MLO LDA MHI BCS .skip2 CPX #$80 SBC THI BCC .skip3 .skip2 TXA SBC #$80 TAX LDA MHI SBC THI STA MHI INY ; R = R OR D (D is 1 here) .skip3 CPX #$80 ROL MHI ; bit -1 iteration and rounding ( + 0.5) LDX #$00 BCS .skip4 CPY MHI BCS .skip5 .skip4 INY ; R = R + 0.5 BNE .skip5 INX .skip5 ; R in X and Y (Y is low-byte) RTS stab: .BYTE $01,$02,$04,$08,$10,$20,$40,$80
; bit -1 iteration BCS .skip4 LDX #$00 CPY MHI BCS .skip5 .skip4 LDX #$80 .skip5 ; R in Y, X contains bit -1 RTS