Given the following C code below which demonstrates different parameter passing
ID: 3715662 • Letter: G
Question
Given the following C code below which demonstrates different parameter passing methods, implement an equivalent version in ARM assembly. Your ARM assembly code must have the subroutines "pass_by_val", which must use a pass by value mechanism to pass all parameters, and "pass_by_ref", which must use a pass by value for the primitive types and pass by reference for the array.
void main( // Note that size and myArray are local variables inside of main int size 10; int myArray-11, 2, 3, 4, 5, 6, 7, 8, 9, 10); // print myArray pass by val(size, myArray, 2) /I print myArray pass_by ref(size, myArray, 5)i 1/ print myArray // pass_by val must use pass-by-value mechanism for all parameters. / Note that we use a lot of space when an array is passed by value. void pass_by_val(int size, int a[], int n) 7note thatl ls a local variable in the pass_by_val function int i; for (1-0; iExplanation / Answer
ANS:-
PROGRAM:-
//Code is explained in comments
//Strictly speaking we cannot pass by value in arrays because of the following reasons:
/*
Strictly speaking you cannot pass an array by value in C.
void pass_by_val (int* x); /* this is a pointer
void pass_by_val (int x[]); /* this is a pointer
void pass_by_val (int x[10]); /* this is a pointer
*/
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
//#include <time.h>
void pass_by_val(int size,int a,int n){ //Here i passed every value of array in for loop which is pass by value
a=a*n;//changed the value of array here
}
void pass_by_ref(int size,int *a,int n){//here i passed by reference
int i;
for(i=0;i<size;i++){//Modified the values by passing array address as pointer
a[i]=a[i]+n;
}
}
int main(){
int myArray[]={1,2,3,4,5,6,7,8,9,10};
int size=sizeof(myArray)/sizeof(myArray[0]); //calculated size of array dividing by size of each integer
for(int i=0;i<size;i++){
printf("Array Index --- %d ----ArrayValue-%d ",i,myArray[i]); //Printing the array
}
for(int i=0;i<size;i++){
pass_by_val(size,myArray[i],2);//function calling by passing each array member value---call by value
}
printf("---------------After Pass by value ------ ");
for(int i=0;i<size;i++){
printf("Array Index --- %d ----ArrayValue-%d ",i,myArray[i]);//Printing the array
}
pass_by_ref(size,myArray,5);// here i called the function by passing array address &myArray~~myArray,calling by reference -modified the values
printf("---------------After Pass by refereence------ ");
for(int i=0;i<size;i++){
printf("Array Index --- %d ----ArrayValue-%d ",i,myArray[i]);//Printing the array
}
return 0;
}
//Output:
VirtualBox:~/Downloads/C++$ g++ pointer.c -o prog
-VirtualBox:~/Downloads/C++$ ./prog
Array Index --- 0 ----ArrayValue-1
Array Index --- 1 ----ArrayValue-2
Array Index --- 2 ----ArrayValue-3
Array Index --- 3 ----ArrayValue-4
Array Index --- 4 ----ArrayValue-5
Array Index --- 5 ----ArrayValue-6
Array Index --- 6 ----ArrayValue-7
Array Index --- 7 ----ArrayValue-8
Array Index --- 8 ----ArrayValue-9
Array Index --- 9 ----ArrayValue-10
---------------After Pass by value ------
Array Index --- 0 ----ArrayValue-1
Array Index --- 1 ----ArrayValue-2
Array Index --- 2 ----ArrayValue-3
Array Index --- 3 ----ArrayValue-4
Array Index --- 4 ----ArrayValue-5
Array Index --- 5 ----ArrayValue-6
Array Index --- 6 ----ArrayValue-7
Array Index --- 7 ----ArrayValue-8
Array Index --- 8 ----ArrayValue-9
Array Index --- 9 ----ArrayValue-10
---------------After Pass by refereence------
Array Index --- 0 ----ArrayValue-6
Array Index --- 1 ----ArrayValue-7
Array Index --- 2 ----ArrayValue-8
Array Index --- 3 ----ArrayValue-9
Array Index --- 4 ----ArrayValue-10
Array Index --- 5 ----ArrayValue-11
Array Index --- 6 ----ArrayValue-12
Array Index --- 7 ----ArrayValue-13
Array Index --- 8 ----ArrayValue-14
Array Index --- 9 ----ArrayValue-15
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.