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.
For 10 points extra credit, make theArray a local array in main.
Submit source code.
.section __TEXT, __text, regular, pure_instructions
.macosx_version_min 10, 12
.globl _main
.align 4, 0x90
_main: ## @main
.cfi_startproc
## BB#0:
pushq %rbp
Ltmp0:
.cfi_def_cfa_offset 16
Ltmp1:
.cfi_offset %rbp, -16
movq %rsp, %rbp
Ltmp2:
.cfi_def_cfa_register %rbp
subq $16, %rsp
leaq L_.str(%rip), %rdi
leaq _s(%rip), %rsi
movl $2000, -4(%rbp) ## imm = 0x7D0
movl $17, -8(%rbp)
movl -4(%rbp), %eax
addl -8(%rbp), %eax
movl %eax, %edx
movb $0, %al
callq _printf
xorl %edx, %edx
movl %eax, -12(%rbp) ## 4-byte Spill
movl %edx, %eax
addq $16, %rsp
popq %rbp
retq
.cfi_endproc
.section __DATA, __data
.globl _s ## @s
_s:
.asciz "GeeksforGeeks"
.section __TEXT, __cstring, cstring_literals
L_.str: ## @.str
.asciz "%s %d \n"
.subsections_via_symbols
Get Answers For Free
Most questions answered within 1 hours.