Question

In: Computer Science

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 << theArray[k] << endl;

cout << "Positive sum is " << total << endl;

   return 0;

}

You must use equates to access the stack and the index register in accessing the array. Remember, the sumPos array does NOT know about the global "theArray" – the address of the array must be passed via the parameter.

ANSWER HAS TO BE IN Pep/9. please.

Solutions

Expert Solution

  • theArray:

    .long 5

    .long 11

    .long -29

    .long 45

    .long 9

    .long -1

    sumPos(int*, int, int&):

    push rbp

    mov rbp, rsp

    mov QWORD PTR [rbp-24], rdi

    mov DWORD PTR [rbp-28], esi

    mov QWORD PTR [rbp-40], rdx

    mov rax, QWORD PTR [rbp-40]

    mov DWORD PTR [rax], 0

    mov DWORD PTR [rbp-4], 0

    .L4:

    mov eax, DWORD PTR [rbp-4]

    cmp eax, DWORD PTR [rbp-28]

    jge .L5

    mov eax, DWORD PTR [rbp-4]

    cdqe

    lea rdx, [0+rax*4]

    mov rax, QWORD PTR [rbp-24]

    add rax, rdx

    mov eax, DWORD PTR [rax]

    test eax, eax

    jle .L3

    mov rax, QWORD PTR [rbp-40]

    mov edx, DWORD PTR [rax]

    mov eax, DWORD PTR [rbp-4]

    cdqe

    lea rcx, [0+rax*4]

    mov rax, QWORD PTR [rbp-24]

    add rax, rcx

    mov eax, DWORD PTR [rax]

    add edx, eax

    mov rax, QWORD PTR [rbp-40]

    mov DWORD PTR [rax], edx

    .L3:

    add DWORD PTR [rbp-4], 1

    jmp .L4

    .L5:

    nop

    pop rbp

    ret

    .LC0:

    .string "Positive sum is "

    main:

    push rbp

    mov rbp, rsp

    sub rsp, 16

    lea rax, [rbp-8]

    mov rdx, rax

    mov esi, 6

    mov edi, OFFSET FLAT:theArray

    call sumPos(int*, int, int&)

    mov DWORD PTR [rbp-4], 0

    .L8:

    cmp DWORD PTR [rbp-4], 5

    jg .L7

    mov eax, DWORD PTR [rbp-4]

    cdqe

    mov eax, DWORD PTR theArray[0+rax*4]

    mov esi, eax

    mov edi, OFFSET FLAT:_ZSt4cout

    call std::basic_ostream<char, std::char_traits<char> >::operator<<(int)

    mov esi, OFFSET FLAT:_ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_

    mov rdi, rax

    call std::basic_ostream<char, std::char_traits<char> >::operator<<(std::basic_ostream<char, std::char_traits<char> >& (*)(std::basic_ostream<char, std::char_traits<char> >&))

    add DWORD PTR [rbp-4], 1

    jmp .L8

    .L7:

    mov esi, OFFSET FLAT:.LC0

    mov edi, OFFSET FLAT:_ZSt4cout

    call std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)

    mov rdx, rax

    mov eax, DWORD PTR [rbp-8]

    mov esi, eax

    mov rdi, rdx

    call std::basic_ostream<char, std::char_traits<char> >::operator<<(int)

    mov esi, OFFSET FLAT:_ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_

    mov rdi, rax

    call std::basic_ostream<char, std::char_traits<char> >::operator<<(std::basic_ostream<char, std::char_traits<char> >& (*)(std::basic_ostream<char, std::char_traits<char> >&))

    mov eax, 0

    leave

    ret

    __static_initialization_and_destruction_0(int, int):

    push rbp

    mov rbp, rsp

    sub rsp, 16

    mov DWORD PTR [rbp-4], edi

    mov DWORD PTR [rbp-8], esi

    cmp DWORD PTR [rbp-4], 1

    jne .L12

    cmp DWORD PTR [rbp-8], 65535

    jne .L12

    mov edi, OFFSET FLAT:_ZStL8__ioinit

    call std::ios_base::Init::Init() [complete object constructor]

    mov edx, OFFSET FLAT:__dso_handle

    mov esi, OFFSET FLAT:_ZStL8__ioinit

    mov edi, OFFSET FLAT:_ZNSt8ios_base4InitD1Ev

    call __cxa_atexit

    .L12:

    nop

    leave

    ret

    _GLOBAL__sub_I_theArray:

    push rbp

    mov rbp, rsp

    mov esi, 65535

    mov edi, 1

    call __static_initialization_and_destruction_0(int, int)

    pop rbp

    ret


Related Solutions

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 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 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 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; }
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; }
--- TURN this Code into Java Language --- #include <iostream> #include <string> using namespace std; //...
--- TURN this Code into Java Language --- #include <iostream> #include <string> using namespace std; // constants const int FINAL_POSITION = 43; const int INITIAL_POSITION = -1; const int NUM_PLAYERS = 2; const string BLUE = "BLUE"; const string GREEN = "GREEN"; const string ORANGE = "ORANGE"; const string PURPLE = "PURPLE"; const string RED = "RED"; const string YELLOW = "YELLOW"; const string COLORS [] = {BLUE, GREEN, ORANGE, PURPLE, RED, YELLOW}; const int NUM_COLORS = 6; // names...
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; }
Starting with the following C++ program: #include <iostream> using namespace std; void main () { unsigned...
Starting with the following C++ program: #include <iostream> using namespace std; void main () { unsigned char c1; unsigned char c2; unsigned char c3; unsigned char c4; unsigned long i1 (0xaabbccee); _asm { } cout.flags (ios::hex); cout << "results are " << (unsigned int) c1 << ", " << (unsigned int) c2 << ", " << (unsigned int) c3 << ", " << (unsigned int) c4 << endl; } Inside the block denoted by the _asm keyword, add code to...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT