In: Computer Science
Translate the following C program to Pep/9 assembly language.
#include <stdio.h.>
int main() {
int numitms,j,data,sum;
scanf("%d", &numitms);
sum=0;
for (j=1;j<=numitms;j++) {
scanf("%d", &data);
sum+=data;
}
printf("sum: %d\n",sum);
return0;
}
I got an answer with an error. Please debug the answer or have a new correct answer (don't copy and paste others' answer)
main: SUBSP 2,i
DECI numItms,i
DECI j,j
DECI data,d
DECI sum,s
LDWA numItms,i
sum: .EQUATE 0
LDWA 1,i
STWA j,j
for: CPWA numItms, j
BRGE endFor
STRO msg,d
DECO j,s
LDBA '\n',i
STBA charOut,d
LDWA j,s ;j++)
ADDA 1,i
STWA j,s
BR for
endFor: ADDSP 2,i ; STOP
msg: .ASCII "Sum = \x00"
.END
main:
push
{r11, lr}
mov r11, sp
sub sp, sp, #16
ldr r0, .LCPI0_0
.LPC0_0:
add r0, pc, r0
sub r1, r11, #4
bl __isoc99_scanf
movw
r1, #0
str r1, [sp]
movw
r1, #1
str r1, [sp, #8]
.LBB0_1:
@ =>This Inner Loop Header: Depth=1
ldr r0, [sp, #8]
ldr r1, [r11, #-4]
cmp r0, r1
bgt .LBB0_4
ldr r0, .LCPI0_2
.LPC0_2:
add r0, pc, r0
add r1, sp, #4
bl __isoc99_scanf
ldr r1, [sp, #4]
ldr r2, [sp]
add r1, r2, r1
str r1, [sp]
ldr r0, [sp, #8]
add r0, r0, #1
str r0, [sp, #8]
b .LBB0_1
.LBB0_4:
ldr r0, .LCPI0_1
.LPC0_1:
add r0, pc, r0
ldr r1, [sp]
bl printf
mov sp, r11
pop {r11, pc}
.LCPI0_0:
.long
.L.str-(.LPC0_0+8)
.LCPI0_1:
.long
.L.str.1-(.LPC0_1+8)
.LCPI0_2:
.long
.L.str-(.LPC0_2+8)
.L.str:
.asciz "%d"
.L.str.1:
.asciz "sum: %d\n"