In: Computer Science
2.32 Translate function f into LEGv8 assembly language. If you need to use registers X10 through X27, use the lower-numbered registers first. Assume the function declaration for g is “int g(int a, int b)”. The code for function f is as follows:
int f(int a, int b, int c, int d){
return g(g(a,b),c+d);
}
2.34 Right before your function f from Exercise 2.32 returns, what do we know about contents of registers X5, X29, X30, and SP? Keep in mind that we know what the entire function f looks like, but for function g we only know its declaration.
Transalation of above given code in LEGv8 (asm low level machine code(language))
For 64 bit system we can user register form X0 to X30
For 32 bit system we can user register form W0 to W30
int f(int a, int b, int c, int d) {
return g(g(a,b),c+d);
}
int g(int a, int b, int c, int d) { // initializing as int type output data type
return a+b;
}
-------- LEGv8 ----- g function -- sample return type of g -- replacing with other scenario can change the ouput of function f
g:
SUBI SP,SP,#24 // Save X10, X9, X19 on stack
MOV X10, SP,#10 // Moving stack for a to register X10
MOV X11 SP,#20 // Moving stack for b to register X11
ADD X10, X11, X12 // X12 = X11 + X12
ret X12 // returning register value in X12
---------------------------------------------------------------
-------- LEGv8 ----- f function
f :
MOV X13, SP,#10 // Moving stack for a to register X10
MOV X15 SP,#20
LDR X13, X15, =g, X16 // X16 = g( X13, X14 ); calling g with argument
LDR X16, X17, =g, X18 // X18 = g( X16, X17 );
ret X18 // return X18 as output.
---- end ---