LEGv8 - Section 3 - Procedure
LEGv8 - Section 3 - Procedure
Lecture – 14
Oct 5th , 2022
LEGv8 Assembly Language
1. Arithmetic and Immediate instructions
2. Load/Store Instructions
1. Accessing operands in memory
3. Decision Making
1. Branch on zero/not zero (==, ! =)
2. Condition flags, branches (<, >, >=, ==, etc.)
4. Procedures
1. Branch and link (BL), Branch register (BR)
2. Spilling registers
Procedures
Procedure Calling
• Steps required
1. Place parameters in registers X0 to X7
2. Transfer control to procedure
3. Acquire storage for procedure
4. Perform procedure’s operations
5. Place result in register for caller
6. Return to place of call (address in X30)
Procedure Calling
• Steps required
1. Place parameters in registers X0 to X7
2. Transfer control to procedure (BL, branch and link)
3. Acquire storage for procedure
4. Perform procedure’s operations
5. Place result in register for caller
6. Return to place of call (address in X30)
Steps in Executing a Procedure
X0 (a)
Op1
X1 (b)
X2 Op2
…
Op3
BL ProcedureLabel X30(LR) RAddr
XZR Computer
X2 (results) Op2
…
Op3
X30(LR) RAddr
BR LR
XZR Computer
Procedure:
Int add(int c, int d){
c=c+d
return c
}
What Registers used?
• X0 – X7: procedure arguments/results
• X30 (LR): link register (return address)
– Also called as program counter (PC)
Example
Assume that the variable a, b and result correspond to registers X9,
int main(){ X10, X5. What is the LEGv8 code
result = add(a, b)
ADD X0, X9, XZR //Move a to X0
} ADD X1, X10, XZR // Move b to x1
BL AddProcedure //Call the procedure and link the return address
Procedure:
Int add(int c, int d){
c=c+d
return c
}
Assume that the variable a, b and result correspond to registers X9,
X10, X5.
Assume that the main procedure needs to spill the registers containing
the values a and b before making a call to the add function.
int main(){ And finally restore them once the add procedure is executed.
What is the LEGv8 code?
result = add(a, b)
ADD X0, X9, XZR //Move a to X0
} ADD X1, X10, XZR // Move b to x1
𝑺𝑼𝑩𝑰 𝑺𝑷, 𝑺𝑷, #𝟏𝟔 // 𝑴𝒂𝒌𝒆 𝒓𝒐𝒐𝒎 𝒇𝒐𝒓 𝒕𝒘𝒐 𝒊𝒕𝒆𝒎𝒔
𝑺𝑻𝑼𝑹 𝑿𝟗, 𝑺𝑷, #𝟖 //𝑺𝒑𝒊𝒍𝒍 𝑿𝟏𝟎
Procedure: 𝑺𝑻𝑼𝑹 𝑿𝟏𝟎, 𝑺𝑷, #𝟎 //𝑺𝒑𝒊𝒍𝒍 𝑿9
BL AddProcedure //Call the procedure and link the return address
Int add(int c, int d){
c=c+d AddProcedure: ADD X0, X0, X1 // c = c+d
ADD X5, X0, XZR // Copy c into result register
return c BR XLR // Branch to caller address (PC +4 )
} 𝑳𝑫𝑼𝑹 𝑿𝟏𝟎, 𝑺𝑷, #𝟎 //𝑹𝒆𝒔𝒕𝒐𝒓𝒆 𝑿10
𝑳𝑫𝑼𝑹 𝑿𝟗, 𝑺𝑷, #𝟖 //𝑹𝒆𝒔𝒕𝒐𝒓𝒆 𝑿𝟗
𝑨𝑫𝑫𝑰 𝑺𝑷, 𝑺𝑷, #𝟏𝟔 // 𝑹𝒆𝒔𝒆𝒕 𝑺𝑷
Registers to be Saved
• X9 to X17: temporary registers
– The caller has to save them if needed for latter
– Not preserved by the callee
Procedure:
Int add(int c, int d){
c=c+d
return c
}
ADD X0, X9, XZR //Move a to X0
ADD X1, X10, XZR // Move b to x1
𝑆𝑈𝐵𝐼 𝑆𝑃, 𝑆𝑃, #16 // 𝑀𝑎𝑘𝑒 𝑟𝑜𝑜𝑚 𝑓𝑜𝑟 𝑡𝑤𝑜 𝑖𝑡𝑒𝑚𝑠
𝑆𝑇𝑈𝑅 𝑋9, 𝑆𝑃, #8 //𝑆𝑝𝑖𝑙𝑙 𝑋10
𝑆𝑇𝑈𝑅 𝑋10, 𝑆𝑃, #0 //𝑆𝑝𝑖𝑙𝑙 𝑋9
int main(){ BL AddProcedure //Call the procedure and link the return address
result = add(a, b)
AddProcedure: ADD X0, X0, X1 // c = c+d
} 𝑺𝑼𝑩𝑰 𝑺𝑷, 𝑺𝑷, #8 // 𝑴𝒂𝒌𝒆 𝒓𝒐𝒐𝒎 𝒇𝒐𝒓 𝒐𝒏𝒆 𝒊𝒕𝒆𝒎
𝑺𝑻𝑼𝑹 𝑿𝟏𝟗 𝑺𝑷, #𝟎 //𝑺𝒑𝒊𝒍𝒍 𝑿19
ADD X5, X0, XZR // Copy c into result register
Procedure: 𝑳𝑫𝑼𝑹 𝑿𝟏𝟗, 𝑺𝑷, #𝟎 //𝑹𝒆𝒔𝒕𝒐𝒓𝒆 𝑿𝟏𝟗
𝑨𝑫𝑫𝑰 𝑺𝑷, 𝑺𝑷, #8 // 𝑹𝒆𝒔𝒆𝒕 𝑺𝑷
Int add(int c, int d){ BR XLR // Branch to caller address (PC +4 )
c=c+d 𝐿𝐷𝑈𝑅 𝑋10, 𝑆𝑃, #0 //𝑅𝑒𝑠𝑡𝑜𝑟𝑒 𝑋10
return c 𝐿𝐷𝑈𝑅 𝑋9, 𝑆𝑃, #8 //𝑅𝑒𝑠𝑡𝑜𝑟𝑒 𝑋9
𝐴𝐷𝐷𝐼 𝑆𝑃, 𝑆𝑃, #16 // 𝑅𝑒𝑠𝑒𝑡 𝑆𝑃
}