[旋風(fēng)][6502匯編][16位除法]

;[Division][16_16_16]
;FlameCyclone 20181120
Dividend_L = $00 ;被除數(shù)低位
Dividend_H = Dividend_L + 1 ;被除數(shù)高位
Divisor_L = $02 ;除數(shù)低位
Divisor_H = Divisor_L + 1 ;除數(shù)高位
Quotient_L = $04 ;最終商低位
Quotient_H = Quotient_L + 1 ;最終商高位
Multiple_L = $06 ;位倍數(shù)低位
Multiple_H = $07 ;位倍數(shù)高位
;65535/1耗時(shí)1562
;65535/255耗時(shí)467
;65535/65535耗時(shí)120
;Division
?.ORG $8000
?
?LDA #255
?STA Dividend_L
?LDA #255
?STA Dividend_H
?LDA #255
?STA Divisor_L
?LDA #255
?STA Divisor_H
?JSR Division
?RTS
Division: ;除法計(jì)算
?LDA #$01
?STA Multiple_L
?LDX #$00
?STX Multiple_H
?STX Quotient_L
?STX Quotient_H
?
?LDA Divisor_L
?BNE Multiple_Set
?LDA Divisor_H
?BEQ Division_End
Multiple_Set:
?LDA Divisor_H
?BMI Compare
?BNE Align_Bits
?LDA Divisor_L
?STA Divisor_H
?STX Divisor_L
?STX Multiple_L
?INC Multiple_H
?LDX #$08
?LDA Divisor_H
?BMI Compare
Align_Bits: ;被除數(shù)與除數(shù)左端對(duì)齊
?INX
?ASL Multiple_L
?ROL Multiple_H
?ASL Divisor_L
?ROL Divisor_H
?BPL Align_Bits
Compare: ;被除數(shù)與除數(shù)比較
?LDA Dividend_H
?CMP Divisor_H
?BCC MoveBits_Right
?BNE Subtraction_L1
?LDA Dividend_L
?CMP Divisor_L
?BCS Subtraction
MoveBits_Right:
?LSR Divisor_H
?ROR Divisor_L
?LSR Multiple_H
?ROR Multiple_L
?JMP Compare
?
Subtraction: ;被除數(shù)-除數(shù)
?LDA Dividend_H
Subtraction_L1:
?SBC Divisor_H
?STA Dividend_H
?LDA Dividend_L
?SBC Divisor_L
?STA Dividend_L
Accumulation:
?LDA Quotient_L ;商 = 商 | 位倍數(shù)
?ORA Multiple_L
?STA Quotient_L
?LDA Quotient_H
?ORA Multiple_H
?STA Quotient_H
?DEX
?BMI Division_End
?LDA Dividend_L
?BNE MoveBits_Right
?LDA Dividend_H
?BNE MoveBits_Right
Division_End: ;乘法計(jì)算結(jié)束
?RTS