I am very new to assembly language, and fairly new to C. I have looked at an example that creates a calls a function from the c code and the assembly code has a function that does the calculation and return the value (This is an assignment) C code:
#include <stdio.h>
int Func(int);
int main()
{
int Arg;
Arg = 5;
printf("Value returned is %d when %d sent\n",Func(Arg), Arg);
}
Assembly Code:
.global Func
Func: save %sp,-800, %sp
add %i0, -45 , %l0
mov %l0, %i0
ret
restore
It takes the value from the C code, adds the value to the number in the assembly code, and outputs the new number. I understand this instance for the most part. Our assignment (Modifying the code): "Write a C source file that calls Func1 with 2 parameters A and B, and and assembly source file which contains two methods, Func1 and Func2. Have Func1 call Func2 as though it were Func2(Q). Func2 should double its input argument and send that doubled value back to Func1. Func1 should return to the C main the value 2*A + 2*B." I have attempted this, and came out with this solution (Please forgive me I am new to this as of today)
#include <stdio.h>
int Func1(int, int);
void Func2(int, int);
int main()
{
int Arg1 = 20;
int Arg2 = 4;
printf("Value returned is %d ",Func1(Arg1,Arg2));
}
Assembly:
.global Func1
Func1: save %sp,-800, %sp
mov %l0, %i0
mov %l1, %i1
call Func2
nop
ret
restore
Func2: save %sp,-800, %sp
umul %i0, 2 , %l0
umul %i1, 2 , %l1
call Func1
nop
It is not working, and I'm not surprised one bit. I'm sure there are many things wrong with this code, but a thorough explanation of what is going on here or what I am doing wrong would really help.