[go: up one dir, main page]

0% found this document useful (0 votes)
47 views27 pages

LEGv8 - Section 3 - Procedure

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
47 views27 pages

LEGv8 - Section 3 - Procedure

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 27

Computer Organization and Architecture

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

1. Caller puts parameters in registers for the procedure


1. Specify a return address
Procedure Instructions
• Procedure call: jump and link
BL ProcedureLabel
– Address of following instruction put in X30 (LR)
• Actually PC + 4 (32 bit instruction)
– Jumps to target address
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 (BR, branch register)
Steps in Executing a Procedure
X0 (a)
Op1
X1 (b)

X2 (results) Op2

Op3
X30(LR) RAddr
BR LR
XZR Computer

6. Return control to Caller


Procedure Instructions
• Procedure call: jump and link
BL ProcedureLabel
– BL: Branch and Link Register
– Address of following instruction put in X30 (LR)
– Jumps to target address
• Procedure return: jump register
BR LR
– BR: Branch Register
– Copies LR to program counter
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)
}

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: AddProcedure: ADD X0, X0, X1 // c = c+d


ADD X5, X0, XZR // Copy c into result register
Int add(int c, int d){ BR LR // Branch to caller address (PC +4 )
c=c+d
return c
}
Spill and Restore Registers
The caller/callee have to save some registers before execution
1. Save variable to memory from register
2. Finish executing procedure
3. Restore value of variable from memory to Previous location

Values are store in a Stack


Stack Pointer (X28): Register containing memory location to
store the values
Spilling to stack
• To spill registers (X10, X9, X19) on to
the stack
Spilling to stack
• To spill registers (X10, X9, X19) on to
the stack
• Address of stack is save in X28 (SP)
Spilling to stack
• To spill registers (X10, X9, X19) on to
the stack
• Address of stack is save in X28 (SP)
• Goes from High to low for historic
reasons
Spilling to stack
• To spill registers (X10, X9, X19) on to
the stack
• Address of stack is save in X28 (SP)
LEGv8 Code:
Make room for three items
Spilling to stack
• To spill registers (X10, X9, X19) on to
the stack
• Address of stack is save in X28 (SP)
LEGv8 Code:
// 𝑀𝑎𝑘𝑒 𝑟𝑜𝑜𝑚 𝑓𝑜𝑟 𝑡ℎ𝑟𝑒𝑒 𝑖𝑡𝑒𝑚𝑠
𝑆𝑈𝐵𝐼 𝑆𝑃, 𝑆𝑃, #24
Spilling to stack
• To spill registers (X10, X9, X19) on to
the stack
• Address of stack is save in X28 (SP)
LEGv8 Code:
𝑆𝑈𝐵𝐼 𝑆𝑃, 𝑆𝑃, #24 // 𝑀𝑎𝑘𝑒 𝑟𝑜𝑜𝑚 𝑓𝑜𝑟 𝑡ℎ𝑟𝑒𝑒 𝑖𝑡𝑒𝑚𝑠
𝑆𝑇𝑈𝑅 𝑋10, 𝑆𝑃, #16 //𝑆𝑝𝑖𝑙𝑙 𝑋10
(PUSH)
Spilling to stack
• To spill registers (X10, X9, X19) on to
the stack
• Address of stack is save in X28 (SP)
LEGv8 Code:
𝑆𝑈𝐵𝐼 𝑆𝑃, 𝑆𝑃, #24 // 𝑀𝑎𝑘𝑒 𝑟𝑜𝑜𝑚 𝑓𝑜𝑟 𝑡ℎ𝑟𝑒𝑒 𝑖𝑡𝑒𝑚𝑠
𝑆𝑇𝑈𝑅 𝑋10, 𝑆𝑃, #16 //𝑆𝑝𝑖𝑙𝑙 𝑋10
𝑆𝑇𝑈𝑅 𝑋9, 𝑆𝑃, #8 //𝑆𝑝𝑖𝑙𝑙 𝑋9
𝑆𝑇𝑈𝑅 𝑋19, 𝑆𝑃, #0 //𝑆𝑝𝑖𝑙𝑙 𝑋19
Restore from Stack
• To spill registers (X10, X9, X19) on to
the stack
• Address of stack is save in X28 (SP)
LEGv8 Code:
𝐿𝐷𝑈𝑅 𝑋19, 𝑆𝑃, #0 //𝑆𝑝𝑖𝑙𝑙 𝑋19 (POP)
𝐿𝐷𝑈𝑅 𝑋9, 𝑆𝑃, #8 //𝑆𝑝𝑖𝑙𝑙 𝑋9
𝐿𝐷𝑈𝑅 𝑋10, 𝑆𝑃, #16 //𝑆𝑝𝑖𝑙𝑙 𝑋10
𝐴𝐷𝐷𝐼 𝑆𝑃, 𝑆𝑃, #24 // 𝑀𝑎𝑘𝑒 𝑟𝑜𝑜𝑚 𝑓𝑜𝑟 𝑡ℎ𝑟𝑒𝑒 𝑖𝑡𝑒𝑚𝑠
Example
int main(){ Assume that the variable a, b and result correspond to registers X9,
X10, X5.
result = add(a, b) Assume that the main needs to spill the registers containing the values
.. a and b before making a call to the add function.
And finally restore them once the add procedure is executed.
} What is the LEGv8 code?

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

• X19 to X28: saved registers


– If used, the callee saves and restores them
Example
int main(){ Assume that the variable a, b and result correspond to registers X9,
X10, X5.
result = add(a, b) Assume that the main needs to spill the registers containing the values
.. a and b before making a call to the add function, and finally restore
them once the add procedure is executed.
} In addition assume that the caller needs to use the saved register X19
What is the LEGv8 code?

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 // 𝑅𝑒𝑠𝑒𝑡 𝑆𝑃
}

You might also like