除非绝对必须,否则您真的不想考虑操纵单个位。
6502 是一台 8 位机器,所以一切都必须在 8 位边界上进行操作。如果您需要的位数少于“2 的偶数次幂”,则应使用逻辑AND
. 它比尝试以另一种方式AFAIK“保存”四位更有效。
例子:
;Our first variable. Holds a 12-bit value but we allocate 16 bits
; to it because this is an 8-bit architecture
VAR_A_16_LO EQU $80
VAR_A_16_HI EQU $81
;Our second variable. Same thing as above.
VAR_B_16_LO EQU $82
VAR_B_16_HI EQU $83
;Our variable where we store the result.
VAR_C_16_LO EQU $84
VAR_C_16_HI EQU $85
;Ok, lets add some 12-bit numbers
;But first, we have to make sure the variables have 12-bit
; quantities. This means killing the top 4 bits of the high
; bytes of the variables. If this is known to be the case this
; can be skipped.
LDA VAR_A_16_HI
AND #%00001111 ;This logical AND operation will set the upper
; four bits to 0 and keep the lower four bits.
;Of the high byte only.
STA VAR_A_16_HI
LDA VAR_B_16_HI
AND #%00001111
STA VAR_B_16_HI
;
; Now, we can add them as though they were two 16-bit numbers.
CLC
LDA VAR_A_16_LO
ADC VAR_B_16_LO
STA VAR_C_16_LO
LDA VAR_A_16_HI
ADC VAR_B_16_HI
STA VAR_C_16_HI
;
; And cut off the top 12 bits of the result just in case of
;overflow.
LDA VAR_C_16_HI
AND #%00001111
STA VAR_C_16_HI