Question

I'm having another issue with addition in 6502....

I am attempting to add two n-byte integers to produce an n-byte result. I'm not completely sure if I understand the 6502 chip as much as I should for this project so any feedback on my current code would be extremely helpful.

I know I am supposed to be using INX (increment the x register) and DEY (decrement the y register) but I am unsure of the placement of the opcodes.

Description: Add two n-byte integers using absolute indexed addressing

Adding two n-byte integers using absolute indexed addressing 
The addends start at memory locations $xxxx, $yyyy, answer is at $zzzz
Byte length of the integers is at $AAAA (¢—>256)

START = $0500
              CLC
              ____
loop          LDA      $0400, x 
              ADC      $0410, x
              STA      $0412, x
             ____
             BNE      loop
             BRK

LDA, ADC, and STA are outside the loop (first time using loops in assembly)

EDIT:

    Variables

A1 = $0600
B1 = $0700
B2 = $0800
Z1 = $0900

    [START] = $0500

            CLC             18  
            LDX             AE  
            LDY     A1      AC
loop:       LDA     B1, x   BD      
            ADC     B2, x   7D  
            STA     Z1, x   9D      
            INX     E8  
            DEY             88  
            BNE    loop     D0
Was it helpful?

Solution

;Adding two n-byte integers using absolute indexed addressing 
;The addends start at memory locations $xxxx, $yyyy, answer is at $zzzz
;Byte length of the integers is at $AAAA (¢—>256)

        CLC
        LDX #0        ; start at the beginning
        LDY $AAAA     ; load length into Y
loop:   LDA $xxxx, X  ; load first operand
        ADC $yyyy, x  ; add second operand
        STA $zzzz, x  ; store result
        INX           ; go on to next byte
        DEY           ; count how many are left
        BNE loop      ; if more, do more
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top