Problem with parity check inside of LC3 Assembly function

74 Views Asked by At

I am trying to implement a basic LC3 assembly program which calculates the sum of all odd numbers stored between x3200 and x32FF inclusive, however, something is going on in the loop that sums ALL numbers between the two addresses rather than just the odd ones

.ORIG x3000

    ; initialize registers
    LD R0, HEX_ZERO
    LD R1, HEX_FF
    LDI R2, HEX_START ; loads content of address into R2
    LD R3, HEX_START ; address of content
    LD R4, HEX_ONE   ; mask for checking oddness

    ; loop through hex values and sum odd numbers
    LOOP    
            ; check if value at current address is odd
            AND R5, R2, R4
            BRz SKIP
            ; add value at current address to sum
            LDR R6, R3, #0
            ADD R0, R0, R6
        SKIP    
            ; increment array address
            ADD R3, R3, #1

            ; decrement loop counter
            ADD R1, R1, #-1
            BRp LOOP

    ; store sum at x3300
    STI R0, HEX_STORE

    ; halt program
    HALT

HEX_START   .FILL x3200    ; memory location of first hex value
HEX_STORE   .FILL x3300    ; memory location to store sum
HEX_FF      .FILL #256     ; loop counter value
HEX_ONE     .FILL #1       ; mask for checking oddness
HEX_ZERO    .FILL #0       ; sum initial value

; R0 is to store sum, starts with 0
; R1 is the loop counter, decrements from 256
; R2 is to hold the value at a memory location starting at x3200
; R4 is the mask for checking oddness
; finally, R0 will store the sum at the address x3300 (hex_store)
; R3 is the address to be incremented

.END

I looked over my parity check portion, but it seems correct.

; check if value at current address is odd
            AND R5, R2, R4
            BRz SKIP
0

There are 0 best solutions below