How functions work in low level?

·

2 min read

Whenever we call function there is big hand of low level in this process.First address where this code of function is present is calculated as immediate value or it is already given in general purpose register.

For this blog let us assume that it is already given in a general purpose register

Assume that this function's address is given in register R9 and so instruction is

Call_Register R9;

Following are normal 5 stage steps for calling a function ->

1. Memory address <- [PC]; Read Memory IR <- Memory data PC <- [PC] + 4

=> Fetch the instruction and then increment the pointer

2. Decode the instruction RA <- [R9]

=> Decode the instruction and then put the address of function code in intermediate register RA.

3. PC-Temp <- [PC] PC<-[RA]

=> As we need to return to current address after execution of function we need to store the current address somewhere so we store that address in PC and put the address of function in PC so that we can execute that function.

4. RY <- [PC-Temp] => Store the value of current address in intermediate register RY

5. Link Register<- RY

=> Put the value of current address in Link Register

Why don't we put current address directly into link register in step 3? => Link register is present in register file and register file can be only accessed in step 5 so we need to extent that till step 5 to extend that we put current address into PC-Temp and then in step 4 we put value from PC-Temp to step RY.

What happens after (return) in function ? => Value saved in LINK Register is put into PC .