Translate the following C++ program to assembly. Upload the .pep file here. This
ID: 3556490 • Letter: T
Question
Translate the following C++ program to assembly. Upload the .pep file here. This program uses a recursive function to add a list of 4 integers stored in an array. The C++ source code is attached.
// Listing 2.25.cpp
// A recursive function that returns the sum
// of the first n numbers in an array
#include <iostream>
using namespace std;
int list[4];
int sum (int a[], int n){
// Returns the sum of the elements of a between a[0] and a[n]
if (n == 0){
return a[0];
}
else{
return a[n] + sum(a, n-1);
}
}
int main() {
cout << "Enter four integers: ";
cin >> list[0] >> list[1] >> list[2] >> list[3];
cout << "Their sum is: " << sum(list,3) << endl;
return 0;
}
Explanation / Answer
GNU GCC C++ to Assembly Language conversion.. [NOTE - No .pep file]
Ltext0:
.local _ZStL8__ioinit
.comm _ZStL8__ioinit,1,1
.globl list
.bss
.align 16
list:
.zero 16
.text
.globl _Z3sumPii
_Z3sumPii:
.LFB971:
.cfi_startproc
pushq %rbp
.cfi_def_cfa_offset 16
.cfi_offset 6, -16
movq %rsp, %rbp
.cfi_def_cfa_register 6
pushq %rbx
subq $24, %rsp
.cfi_offset 3, -24
movq %rdi, -24(%rbp)
movl %esi, -28(%rbp)
cmpl $0, -28(%rbp)
jne .L2
movq -24(%rbp), %rax
movl (%rax), %eax
jmp .L3
.L2:
movl -28(%rbp), %eax
cltq
leaq 0(,%rax,4), %rdx
movq -24(%rbp), %rax
addq %rdx, %rax
movl (%rax), %ebx
movl -28(%rbp), %eax
leal -1(%rax), %edx
movq -24(%rbp), %rax
movl %edx, %esi
movq %rax, %rdi
call _Z3sumPii
addl %ebx, %eax
.L3:
addq $24, %rsp
popq %rbx
popq %rbp
.cfi_def_cfa 7, 8
ret
.cfi_endproc
.LFE971:
.section .rodata
.LC0:
.string "Enter four integers: "
.LC1:
.string "Their sum is: "
.text
.globl main
main:
.LFB972:
.cfi_startproc
pushq %rbp
.cfi_def_cfa_offset 16
.cfi_offset 6, -16
movq %rsp, %rbp
.cfi_def_cfa_register 6
pushq %rbx
subq $8, %rsp
.cfi_offset 3, -24
movl $.LC0, %esi
movl $_ZSt4cout, %edi
call _ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc
movl $list, %esi
movl $_ZSt3cin, %edi
call _ZNSirsERi
movl $list+4, %esi
movq %rax, %rdi
call _ZNSirsERi
movl $list+8, %esi
movq %rax, %rdi
call _ZNSirsERi
movl $list+12, %esi
movq %rax, %rdi
call _ZNSirsERi
movl $3, %esi
movl $list, %edi
call _Z3sumPii
movl %eax, %ebx
movl $.LC1, %esi
movl $_ZSt4cout, %edi
call _ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc
movl %ebx, %esi
movq %rax, %rdi
call _ZNSolsEi
movl $_ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_, %esi
movq %rax, %rdi
call _ZNSolsEPFRSoS_E
movl $0, %eax
addq $8, %rsp
popq %rbx
popq %rbp
.cfi_def_cfa 7, 8
ret
.cfi_endproc
.LFE972:
_Z41__static_initialization_and_destruction_0ii:
.LFB981:
movl $_ZStL8__ioinit, %edi
call _ZNSt8ios_base4InitC1Ev
movl $__dso_handle, %edx
movl $_ZStL8__ioinit, %esi
movl $_ZNSt8ios_base4InitD1Ev, %edi
call __cxa_atexit
.cfi_startproc
pushq %rbp
.cfi_def_cfa_offset 16
.cfi_offset 6, -16
movq %rsp, %rbp
.cfi_def_cfa_register 6
subq $16, %rsp
movl %edi, -4(%rbp)
movl %esi, -8(%rbp)
cmpl $1, -4(%rbp)
jne .L6
cmpl $65535, -8(%rbp)
jne .L6
.L6:
leave
.cfi_def_cfa 7, 8
ret
.cfi_endproc
.LFE981:
_GLOBAL__sub_I_list:
.LFB982:
.cfi_startproc
pushq %rbp
.cfi_def_cfa_offset 16
.cfi_offset 6, -16
movq %rsp, %rbp
.cfi_def_cfa_register 6
movl $65535, %esi
movl $1, %edi
call _Z41__static_initialization_and_destruction_0ii
popq %rbp
.cfi_def_cfa 7, 8
ret
.cfi_endproc
.LFE982:
.section .init_array,"aw"
.align 8
.quad _GLOBAL__sub_I_list
.text
.Letext0:
Please Rate
Thank You
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.