I am creating an LC3 program to display sum and count of positive and negative numbers but when I run the program in the Simulate program - I look at R5 which is suppose to hold the sum of positive numbers only shows the first +ve number added. My code is below - any suggestions new to using lc3/assembly
.ORIG x3000
and r1, r1,#0 ; clear r1
and r2, r2, #0 ; neg counter amount of negative numbers
and r3, r3, #0 ; positive counter amount of pos numbers
and r4, r4, #0; negative sum
and r5,r5, #0; positive sum
and r6,r6,#0; clear register
LEA R1, DATA
loop
ldr r6, r1, #0
BRn negativecountm ;if negative go to branch to ncount method
BRp positivecountm ;if positive go to branch to pcount method
BRz Escape ;if 0 encountered program done go to escape method
negativecountm
add r2, r2, #1 ; increment count for neg numbers
add r4, r4, r6 ; sum of neg number
add r1, r1,#1 ; increment pointer to point to next number in data
BRp loop ; go back to original loop
positivecountm
add r3, r3, #1 ; increment count for positive num
add r5, r5, r6; sum of +ve num
add r1, r1, #1 ; increment pointer to point to next num in data
BRn loop;
Escape
ST r2, negativecount ;
ST r3, positivecount;
ST r5, positivesum;
ST r4, negativesum;
TRAP x25 ;
DATA .Fill 1244
.Fill -23
.Fill 17
.Fill 6
.Fill -12
.Fill 0
negativecount .BLKW 1
positivecount .BLKW 1
positivesum .BLKW 1
negativesum .BLKW 1
.END
BRp loop ; go back to original loop
[...]
BRn loop;
Both of these branch statements are not quite correct. Remember that the condition codes are set anytime an instruction writes to a register (that is LD,LEA,LDR,LDI,ADD,AND,NOT)
The condition codes are being set when you do
add r1, r1, #1 ; increment pointer to point to next num in data
In the positive number case surely r1 will not contain a negative value after that ADD instruction.
Simply changing your branches to be an unconditional branch would fix it.
So instead of BRn/p simply saying
BR loop
Will do the trick.