0

I am making an LC3 assembly program that calculates x factorial. I have this label called "OUTERLOOP" that is utilized as long as my counter value is not negative. However, the assembler is giving me this error: "32: label OUTERLOOP has already appeared." It does not give me this error for the inner loop. For some reason, it looks like it thinks I'm trying to name another address with OUTERLOOP again when I'm trying to get it to branch back to the OUTERLOOP address. Any ideas?

; This program calculates X! (X factorial). It can calculate
;   different numbers (4!, 6!, etc.) by changing the value of the first memory
;   location at the bottom of the code. It is currently set up to calculate
;   5!. The program does not account for zero or negative numbers as input.

; This program primarily uses registers in the following manner:
; R0 contains 0 (registers contain zero after reset)
; R1 contains multiplication result (6x5 = 30, 30x4 = 120, etc)
; R2 contains -1
; R3 contains counter for outer loop
; R4 contains counter for inner loop
; R5 contains current sum

           .ORIG  x3000

            LD    R1,INPUT            ; R1 contains input number
            LD    R2,xFFFF            ; R2 contains -1
            ADD   R3,R1,R2            ; R3 contains input number -1
            ADD   R3,R3,R2            ; R3 contains input number -2
                                      ;   (initializes outer count)
OUTERLOOP   ADD   R4,R0,R3            ; Copy outer count into inner count

; This loop multiplies via addition (6x5 = 6+6+6+6+6 = 30,
;   30x4 = 30+30+30+30 = 120, etc)
INNERLOOP   ADD   R5,R5,R1            ; Increment sum
            ADD   R4,R4,R2            ; Decrement inner count
            BRzp  INNERLOOP           ; Branch to inner loop if inner count
                                      ;   is positive or zero
            ADD   R1,R0,R5            ; R1 now contains sum result from inner loop
            AND   R5,R5,#0            ; Clear R5 (previous sum) to 0
            ADD   R3,R3,R2            ; Decrement outer count
            BRpz  OUTERLOOP           ; Branch to outer loop if outer count
                                      ;   is positive or zero

            ST   R1,INPUT             ; This address contains X!
            TRAP x25                  ; HALT

INPUT      .FILL  x0005               ; Input for X!, in this case X = 5
           .FILL  x0000
           .FILL  xFFFF               ; 2's complement of 1 (i.e. -1)
           .FILL  x0000               ; At program completion, the result is
                                      ;   stored here
           .END
LTClipp
  • 526
  • 1
  • 4
  • 14

1 Answers1

3

Try changing

BRpz  OUTERLOOP           ; Branch to outer loop if outer count

to

BRzp  OUTERLOOP           ; Branch to outer loop if outer count
Weather Vane
  • 33,872
  • 7
  • 36
  • 56