In: Computer Science
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.
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