我想将一个 16 位数除以二。我对该问题的解决方案如下
lda $17 ;set high byte
ldx $32 ;set low byte
divide:
PHA ;push A to stack
TXA ;X > A
LSR ;divide low byte by 2
TAX ;A > X
PLA ;pull A from stack
LSR ;divide high byte by 2
BCC + ;C=0, skip
PHA ;while C=1
TXA ;add $80 to the lsb
ADC #$80
TAX
PLA
+
+printDecimal $0400+120
所有PHA/PLA
的诡计都是因为我的printDecimal
宏从 A 读取 MSB,从 X 读取 LSB。
当我在网上查看替代方案时,我发现 4 指令替代我简陋的除法例程。但我不明白。
div2:
LDA counter_hi ;Load the MSB
ASL ;Copy the sign bit into C
ROR counter_hi ;And back into the MSB
ROR counter_lo ;Rotate the LSB as normal
LDA counter_hi
LDX counter_lo
+printDecimal $0400+40
这是如何工作的?