Question

In: Computer Science

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++)

     cout << theArray[k] << endl;

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

   return 0;

}

Solutions

Expert Solution

Assembly Code for the given Program

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

Assembly Code in Binary

std::ostream::operator<<(std::ostream& (*)(std::ostream&))@plt:

jmp    QWORD PTR [rip+0x2fca]        # 404030 <std::ostream::operator<<(std::ostream& (*)(std::ostream&))@GLIBCXX_3.4>

push   0x3

jmp    401020 <.plt>

std::ios_base::Init::Init()@plt:

jmp    QWORD PTR [rip+0x2fc2]        # 404038 <std::ios_base::Init::Init()@GLIBCXX_3.4>

push   0x4

jmp    401020 <.plt>

std::ostream::operator<<(int)@plt:

jmp    QWORD PTR [rip+0x2fba]        # 404040 <std::ostream::operator<<(int)@GLIBCXX_3.4>

push   0x5

jmp    401020 <.plt>

std::ios_base::Init::~Init()@plt:

jmp    QWORD PTR [rip+0x2fb2]        # 404048 <std::ios_base::Init::~Init()@GLIBCXX_3.4>

push   0x6

jmp    401020 <.plt>

sumPos(int*, int, int&):

push   rbp

mov    rbp,rsp

mov    QWORD PTR [rbp-0x18],rdi

mov    DWORD PTR [rbp-0x1c],esi

mov    QWORD PTR [rbp-0x28],rdx

mov    rax,QWORD PTR [rbp-0x28]

mov    DWORD PTR [rax],0x0

mov    DWORD PTR [rbp-0x4],0x0

mov    eax,DWORD PTR [rbp-0x4]

cmp    eax,DWORD PTR [rbp-0x1c]

jge    4011ee <sumPos(int*, int, int&)+0x6c>

mov    eax,DWORD PTR [rbp-0x4]

cdqe   

lea    rdx,[rax*4+0x0]

mov    rax,QWORD PTR [rbp-0x18]

add    rax,rdx

mov    eax,DWORD PTR [rax]

test   eax,eax

jle    4011e8 <sumPos(int*, int, int&)+0x66>

mov    rax,QWORD PTR [rbp-0x28]

mov    edx,DWORD PTR [rax]

mov    eax,DWORD PTR [rbp-0x4]

cdqe   

lea    rcx,[rax*4+0x0]

mov    rax,QWORD PTR [rbp-0x18]

add    rax,rcx

mov    eax,DWORD PTR [rax]

add    edx,eax

mov    rax,QWORD PTR [rbp-0x28]

mov    DWORD PTR [rax],edx

add    DWORD PTR [rbp-0x4],0x1

jmp    4011a2 <sumPos(int*, int, int&)+0x20>

nop

pop    rbp

ret    

main:

push   rbp

mov    rbp,rsp

sub    rsp,0x10

lea    rax,[rbp-0x8]

mov    rdx,rax

mov    esi,0x6

mov    edi,0x404060

call   401182 <sumPos(int*, int, int&)>

mov    DWORD PTR [rbp-0x4],0x0

cmp    DWORD PTR [rbp-0x4],0x5

jg     401247 <main+0x56>

mov    eax,DWORD PTR [rbp-0x4]

cdqe   

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

mov    esi,eax

mov    edi,0x404080

call   401080 <std::ostream::operator<<(int)@plt>

mov    esi,0x401030

mov    rdi,rax

call   401060 <std::ostream::operator<<(std::ostream& (*)(std::ostream&))@plt>

add    DWORD PTR [rbp-0x4],0x1

jmp    401216 <main+0x25>

mov    esi,0x402005

mov    edi,0x404080

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

mov    rdx,rax

mov    eax,DWORD PTR [rbp-0x8]

mov    esi,eax

mov    rdi,rdx

call   401080 <std::ostream::operator<<(int)@plt>

mov    esi,0x401030

mov    rdi,rax

call   401060 <std::ostream::operator<<(std::ostream& (*)(std::ostream&))@plt>

mov    eax,0x0

leave  

ret    

_GLOBAL__sub_I_theArray:

push   rbp

mov    rbp,rsp

mov    esi,0xffff

mov    edi,0x1

call   40127a <__static_initialization_and_destruction_0(int, int)>

pop    rbp

ret    

nop    DWORD PTR [rax]


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 <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...
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 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...
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 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. 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; }
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...
Assembly Language Programming Construct an assembly language program fragment equivalent to the following C/C++ statement: if...
Assembly Language Programming Construct an assembly language program fragment equivalent to the following C/C++ statement: if (M <= N + 3 && (C == ‘N’ || C == ‘n’)) C = ‘0’; else C = ‘1’; Assume that M and N are 32-bit signed integer variables, and C is an 8-bit ASCII character variable. All variables are stored in memory, and all general-purpose registers are available for use.
A C++ question: I want to indent the code of this C++ program: #include<iostream> #include<cstring> using...
A C++ question: I want to indent the code of this C++ program: #include<iostream> #include<cstring> using namespace std; int lastIndexOf(char *s, char target) { int n=strlen(s); for(int i=n-1;i>=0;i--) { if(s[i]==target) { return i; } } return -1; } void reverse(char *s) { int n=strlen(s); int i=0,j=n-1; while(i<=j) { char temp=s[i]; s[i]=s[j]; s[j]=temp; i++; j--; } return; } int replace(char *s, char target, char replacementChar) { int len=strlen(s); int total=0; for(int i=0;i<len;i++) { if(s[i]==target) { s[i]=replacementChar; total+=1; } } return total;...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT