In: Computer Science
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;
}
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]