tepples wrote:
You can't fit a 16-bit number in one byte, but you can fit it in two. Here's how to do 16 bit addition, using the example of 508 + 8 = 516 ($01FC + $0008 = $0204):
Code:
position_hi = $0701
position_lo = $0700
; set the position to $01FC
lda #$01
sta position_hi
lda #$fc
sta position_lo
; now add eight ($0008) to this position
clc
lda #$08 ; add the low byte first
adc position_lo
sta position_lo
lda #$00
adc position_hi
sta position_hi
When you add two 8-bit numbers and the result is more than 256, the CPU subtracts 256 and turns on the carry flag. The carry flag tells the CPU to add one extra the next time it adds anything, just as you'd carry the 1 when adding multi-digit base 10 numbers in first grade. Most of the time, an addition will start with the carry off; that's what the
clc does. (Clearing a flag means turning it off; setting means turning it on.)
Um... so are we susposed to declare low values before high values?? Why is position_hi before position_lo?
I'm under some trouble with this code...
Code:
;6502 Simulator
.START $1000
.ORG $0000
.DB "ABC"
position_lo .DS 1
position_medium .ds 1
position_medihi .DS 1
position_hi .ds 1
oX .ds 4
.ORG $1000
; set the position to $01FC
lda #$01
sta position_hi
LDA #$01
sta position_medihi
LDA #$00
sta position_medium
LDA #$02
STA position_lo
LDa #$01
STa oX+3
LDa #$01
STa oX+2
lda #$00
sta oX+1
lda #$02
STA oX+0
; now subtract eight (#$0008) from this position
sec
lda position_lo ; subtract from the low byte first
sbc #$08
sta position_lo
LDA position_medium
SBC #$00
STA position_medium
LDA position_medihi
SBC #$00
sta position_medihi
LDA position_hi
sbc #$00
STA position_hi
; now subtract eight (#$0008) from this position
sec
lda oX+0 ; subtract from the low byte first
sbc #$08
sta oX+0
LDA oX+1
SBC #$00
STA oX+1
LDA oX+2
SBC #$00
sta oX+2
LDA oX+3
sbc #$00
STA oX+3
Ok well that is my code I've explored in the 6502 Simulator from tokumaru. The only thing is that at the top of it I had to reverse this code.
position_hi .DS 1
position_medihi .ds 1
position_medium .DS 1
position_lo .ds 1I started with that because tepples made his code up there very tricky... I think...
because he started with
Code:
position_hi = $0701
position_lo = $0700
and that has been confusing me. Should
position_lo be above
position_hi?
It's ok, cause I feel that
position_lo is above
position_hi. I want to help others not to have the confusion that I had. : )