Question

In: Computer Science

Translate the following C program to Pep/9 assembly language. #include <stdio.h.> int main() { int numitms,j,data,sum;...

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

Solutions

Expert Solution

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"


Related Solutions

Translate the following C program to PEP/9 assembly language. #include <stdio.h> Int main (){ int number;...
Translate the following C program to PEP/9 assembly language. #include <stdio.h> Int main (){ int number; Scanf (“%d”, & number); if (number % 2 ==0) { printf (“Even\n”); } else { printf(“Odd\n”); } Return 0; }
Take the following program and translate it into PEP/9 assembly language: #include using namespace std; int...
Take the following program and translate it into PEP/9 assembly language: #include using namespace std; int fib(int n) { int temp; if (n <= 0) return 0; else if (n <= 2) return 1; else { temp = fib(n – 1); return temp + fib(n-2); } } int main() { int num; cout << "Which fibonacci number? "; cin >> num; cout << fib(num) << endl; return 0; } You must use equates to access the stack and follow the...
Take the following C++ program and translate it into Pep/9 assembly language #include using namespace std;...
Take the following C++ program and translate it into Pep/9 assembly language #include using namespace std; int age; char first, last; int main() {    cin >> age;    cin >> first >> last;    cout << "Your age " << age << endl;    cout << "Initials " << first << last << endl;    if (age >= 30)        cout << “Cannot trust\n”;    return 0; }
Take the following C++ program and translate it into PEP/9 assembly language #include <iostream> using namespace...
Take the following C++ program and translate it into PEP/9 assembly language #include <iostream> using namespace std; int num; char letter; int main() {    cin >> num;    cin >> letter;    cout << "You inputted " << num << endl;    cout << "Option " << letter << endl;    if (letter == '*')       cout << "Multiplied by 2 " << num*2 << endl;    return 0; }
Take the following program from C++ and translate it into Pep/9 assembly language: #include <iostream> using...
Take the following program from C++ and translate it into Pep/9 assembly language: #include <iostream> using namespace std; int theArray[] = { 5, 11, -29, 45, 9, -1}; void sumPos(int ary[], int len, int &sum) {    sum = 0;    for (int i = 0; i < len; i++)            if (ary[i] > 0)                sum = sum + ary[i]; } int main() {    int total;    sumPos(theArray, 6, total);    for (int k=0; k < 6; k++)...
Take the following program and translate it into PEP/9 assembly language: #include <iostream> using namespace std;...
Take the following program and translate it into PEP/9 assembly language: #include <iostream> using namespace std; int theArray[] = { 5, 11, -29, 45, 9, -1}; void sumPos(int ary[], int len, int &sum) {    sum = 0;    for (int i = 0; i < len; i++)            if (ary[i] > 0)                sum = sum + ary[i]; } int main() {    int total;    sumPos(theArray, 6, total);    for (int k=0; k < 6; k++)      cout...
Translate the following C++ program to Pep/9 assembly language. const char chConst = '+'; char ch1;...
Translate the following C++ program to Pep/9 assembly language. const char chConst = '+'; char ch1; char ch2; int main() { cin.get(ch1); cin.get(ch2);    cout << ch1 << chConst << ch2;    return 0; }
Please convert the following C program into the RISC-V assembly code 1) #include <stdio.h> int main()...
Please convert the following C program into the RISC-V assembly code 1) #include <stdio.h> int main() { int i = 2, j = 2 + i, k; k = i * j; printf("%d\n", k + j); return 0; } 2) #include <stdio.h> int main() { int i = 2, j = 2 + i, k = j / 2; if (k == 1) { printf("%d\n", j) k = k * 2; if ( k == j) { printf("%d\n|, j); }...
Take the following C++ program and translate it into assembly language( pep9 ) #include using namespace...
Take the following C++ program and translate it into assembly language( pep9 ) #include using namespace std; char ch; int main() {    cin >> ch;    cout << "You inputted " << ch << endl;    ch++;    cout << "Next character is " << ch << endl; if (ch <= ‘Z’)         cout << “Could be luppercase\n”;    return 0; }
*Answer in C program* #include <stdio.h> int main() {      FILE *fp1;      char c;     ...
*Answer in C program* #include <stdio.h> int main() {      FILE *fp1;      char c;      fp1= fopen ("C:\\myfiles\\newfile.txt", "r");      while(1)      {         c = fgetc(fp1);         if(c==EOF)             break;         else             printf("%c", c);      }      fclose(fp1);      return 0; } In the program above which statement is functioning for opening a file Write the syntax for opening a file What mode that being used in the program. Give the example from the program Referring to...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT