0% found this document useful (0 votes)
14 views33 pages

Chapter - 6

Uploaded by

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

Chapter - 6

Uploaded by

mersha abdisa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd

Procedure and functions

Chapter Six
Outline
• Procedures
• Functions

Procedure
Procedures or subroutines are very important in assembly
language, as the assembly language programs tend to be
large in size.
• Procedures are identified by a name. Following this name,
the body of the procedure is described, which perform a
well-defined job.
• End of the procedure is indicated by a return statement
• Syntax: Following is the syntax to define a procedure:

The procedure is called from another function by using the CALL instruction. The
CALL instruction should have the name of the called procedure as argument as
shown below:
CALL proc_name
Cont…
• The called procedure returns the control to the calling procedure by using
the RET instruction.
• Example: Let us write a very simple procedure named sum that adds the
variables stored in the ECX and EDX register and returns the sum in the
EAX register:
Cont…

When the above code is compiled and executed, it produces following result:
The sum is: 9
Stacks Data Structure:
• A stack is an array-like data structure in the memory in which data can be
stored and removed from a location called the 'top' of the stack. The data
need to be stored is 'pushed' into the stack and data to be retrieved is
'popped' out from the stack. Stack is a LIFO data structure, i.e., the data
stored first is retrieved last.
• Assembly language provides two instructions for stack operations: PUSH
and POP. These instructions have syntaxes like:

The memory space reserved in the stack segment is used for implementing stack.
The registers SS and ESP (or SP) are used for implementing the stack.
Cont…
• The top of the stack, which points to the last data item inserted into the stack is
pointed to by the SS:ESP register, where the SS register points to the beginning of
the stack segment and the SP (or ESP) gives the offset into the stack segment.
• The stack implementation has the following characteristics:
– Only words or doublewords could be saved into the stack, not a byte.
– The stack grows in the reverse direction i.e., toward the lower memory address
– The top of the stack points to the last item inserted in the stack; it points to the
lower byte of the last word inserted
• As we discussed about storing the values of the registers in the stack before using
them for some use; it can be done in following way: .
Example
• The following program displays the entire ASCII character set. The main program
calls a procedure named display, which displays the ASCII character set.

When the above code is compiled and executed, it produces following result:
Writing function in assembly
Approach
• Most of a program is written in a high-level
language (HLL) such as C
– easier to implement
– easier to understand
– easier to maintain

• Only a few functions are written in assembly


– to improve performance, or
– because it’s difficult or impossible to do in a HLL.
Most of code,
Creating a Program
including Library
function main header
files

C
Object
source Compiler
code
code

ASM Execu-
Assembler Object Linker table
source
code Program
code

A few small Library


functions code
Consequence
Assembly functions must be compatible with:
1. How the C compiler invokes the execution of a
function
2. How the C compiler passes parameters to
functions
3. How the C compiler expects to receive results
from functions
4. How the C compiler uses CPU registers
FUNCTION CALL AND RETURN
Simple Call-Return in C

1. Suspend the current sequence


2. Record the return address Branch with Link

3. Transfer control to the function Instruction (BL)


void f1(void)
f1() ;

{
● 4. Execute the function

● ●

return ;
}
5. Use the recorded return
address to go back and Branch Indirect
resume the suspended Instruction (BX)
code.
FUNCTION CALL AND RETURN
ARM instructions used to call and return from functions

Instruction Format Operation


Function Call:
Branch with Link BL label LR  return address,
PC  address of label
Function Return:
Branch Indirect BX LR
PC  LR

LR (aka R14) and PC (aka R15) are two of the 16 registers inside the CPU.
LR is the “Link Register” – used w/function calls to hold the return address
PC is the “Program Counter” – holds the address of the next instruction.
FUNCTION CALL AND RETURN
Simple Call-Return in Assembly

The “Branch with Link” instruction (BL) saves


the address of the instruction immediately
following it (the return address) in the Link
Register (LR).


● Consider: There is only one Link

f1: Register. What if inside function
BL f1 ●
● ● f1 there is a call to another
● ● function?
● BX LR

The “Branch Indirect” instruction


(BX) copies the return address
from LR into PC, thus transferring
control back to where the function
had been called.
FUNCTION CALL AND RETURN
Nested Call-Return in Assembly

Saves the contents


Saves the return of LR on the stack.
address in LR



Modifies the link register
BL f1 f1: PUSH {LR} (LR), writing over f1’s
● ●
return address!
● ●
● ●
BL f2
● ●
● ●
● BX LR
POP {LR}
BX LR

Copies the saved Restores the contents


return address from of LR from the stack.
LR back into PC
FUNCTION CALL AND RETURN
ARM instructions used in functions that call other functions
SP is one of the 16 CPU registers (R13) – 1000 Pushed data
holds the address of the top of the stack. 996 Pushed data
(register R13)
SP 992 992 Top of stack
988
Instruction Format Operation 984 Unused
980 Stack
Space
Push registers SP  SP – 4 × #registers 976
PUSH register list
onto stack Copy registers to mem[SP] 972

(memory)
Pop registers Copy mem[SP] to registers,
POP register list
from stack SP  SP + 4 × #registers

“register list” format: { reg, reg, reg-reg, …}


Optimizing Function Calls

f1: PUSH {LR} f1: PUSH {LR}


● ●
● ●
● ●
BL f2 BL f2
● ●
● ●
● ●
POP {LR} POP {PC}
BX LR
Optimizing Function Calls



BL f1 f1: PUSH {LR}
● ●
● ●
● ●
BL f2

POP {LR} ●
BX LR BX LR




BL f1 f1: ●
● ●
● ●
● B f2


BX LR
ARM PROCEDURE CALL STANDARD
Registers used as input parameters

Register AAPCS Name Usage

R0 A1 1st parameter
R1 A2 2nd parameter
R2 A3 3rd parameter
R3 A4 4th parameter

One register is used for each 8, 16 or 32-bit parameter.


A sequential register pair is used for each 64-bit parameter.
PARAMETER PASSING
void foo(int8_t, int32_t, int64_t) ;

C Function Call Compiler Output

int8_t LDRSB R0,x8 // R0 <-- x8


x8 ; LDR R1,y32 // R1 <-- y32
int32_t y32 ; LDRD R2,R3,z64 // R3.R2 <--
int64_t z64 ; z64
Inside foo, you must use
● BL foo the register copies of the

foo(x8, y32, ● actual arguments.



z64) ; ●

LDR R0,=5 // R0 <-- 5

foo(5, -10, 20) ; LDR R1,=-10 // R1 <-- -10


● LDR R2,=20 // R3.R2 <-- 20

LDR R3,=0
BL foo
ARM PROCEDURE CALL STANDARD
Registers used to return function result

AAPCS
Register Usage
Name

8, 16 or 32-bit result, or the


R0 A1 least-significant half of a 64-bit
result
The most-significant half of a 64-
R1 A2
bit result
PREPARING THE RETURN VALUE
Functions that return an 8, 16 or 32-bit result

Functions must provide a full 32-bit


representation of return value,
even for 8 and 16-bit results.
Review Summary
• Call functions using Branch with Link (BL)
– Saves the return address in Link Register (LR)
– Copies the target address into Program Counter (PC)
• Return from a function using BX LR
• Writing functions that call other functions:
– Using BL to call another function changes LR
– Use PUSH {LR} / POP {LR} to preserve LR
• Pass parameters using registers R0-R3
– 64-bit parameters in consecutive register pair
• Return 8, 16 and 32 bit results using R0
– 64-bit result returned in R1.R0 register pair
Review Summary
void f2(int32_t,
f2(void) ; int32_t) ;
f1: PUSH
. {LR}
int32_t
void f1(void)
f1(void) .
{ .
LDR R0,=1
. LDR
. R1,=2
. BL f2
.
.f2() ; 2) ;
f2(1,
.
.
return 10 ; LDR
. R0,=10
.
BX
POP LR
{PC}
}
PREPARING THE RETURN VALUE
Promoting a return value from 8 to 16 or 32 bits
C Function Call Compiler Output
uint8_t save8, get8(void) ; ●

uint16_tsave16 ; ●
uint32_tsave32 ; BL get8
uint64_tsave64 ; STRB R0,save8 8, 16 and 32-bit func-
● BL get8
● tions must always
STRH R0,save16

save8 = get8() ; BL get8 provide a 32-bit result.
save16 = (uint16_t) get8() ; STR R0,save32

save32 = (uint32_t) get8() ;
BL get8

LDR R1,=0
save64 = (uint64_t) get8() ;
STRD R0,R1,save64
● ●
● ●
● ●

Promotion to 64-bits requires


extending the result after the call
PREPARING THE RETURN VALUE
Functions that return 64-bit result

Functions that return a 64-bit result


leave it in R0 and R1 before returning.
PREPARING THE RETURN VALUE
Functions that return an 8 or 16-bit result
uint8_t u8 ; uint8_t Add1(uint8_t x)
{
// What value is assigned? return x + 1 ;
u8 = Add1(255) ; }

uint16_t u16 ; Add1: ADD R0,R0,1


uint32_t u32 ; BX LR
uint64_t u64 ;

// What value is assigned?


u16 = (uint16_t) Add1(255) ; Add1: ADD R0,R0,1
u32 = (uint32_t) Add1(255) ; UXTB R0,R0
u64 = (uint64_t) Add1(255) ; BX LR
PREPARING THE RETURN VALUE
Functions that return an 8 or 16-bit result

Instruction Operation operand2 options:


UXTB Rd, operand2 Rd Zero extend operand2<7..0>

UXTH Rd, operand2 Rd Zero extend operand2<15..0> 1. Rm (a register)


2. Rm,ROR constant
SXTB Rd, operand2 Rd Sign extend operand2<7..0> (constant=8, 16 or
24)
SXTH R d,
Rd Sign extend operand2<15..0>
operand2
REGISTER USAGE CONVENTIONS
ARM PROCEDURE CALL STANDARD
AAPCS
Register Usage Notes
Name
R0 A1 Argument / result /scratch register 1
R1 A2 Argument / result /scratch register 2
R2 A3 Argument / scratch register 3
R3 A4 Argument / scratch register 4
R4 V1 Variable register 1
R5 V2 Variable register 2
R6 V3 Variable register 3
R7 V4 Variable register 4 Must preserve original
R8 V5 Variable register 5 contents
R9 V6 Variable register 6
R10 V7 Variable register 7
R11 V8 Variable register 8
R12 IP Intra-Procedure-call scratch register
R13 SP Stack Pointer
Reserved,
R14 LR Link Register
Do not use
R15 PC Program Counter
FUNCTION CODING CONVENTIONS
Functions that modify only registers R0 - R3, R12

Using only R0-R3, R12 Using only R0-R3, R12


and no function call and calling another function

f1: ● f2: PUSH {LR}


● ●

● OK to ● OK to modify R0–R3, R12


modify ● Function f3
● R0-R3 and R12 BL f3 might modify
● ● R0 – R3, R12
BX LR ● OK to modify R0–R3,
R12

POP {PC}
FUNCTION CODING CONVENTIONS
Functions that modify for example registers R4 and R5

Using R4 and R5 Using R4 and R5


and no function call and calling another function

f1: PUSH {R4,R5} f2: PUSH {R4,R5,LR}


● ●

●OK to modify ● OK to modify


● R0-R5 ● R0–R5 and R12
and R12 ●
Function f3 might
● BL f3
modify R0-R3 and R12,
POP {R4,R5} ●
but preserves R4-R11
BX LR ● OK to modify
● R0–R5 and R12

POP {R4,R5,PC}
FUNCTION CODING CONVENTIONS
Two functions with parameters, one calling the other
C Version Assembly Version
int32_t f1(int32_t f1: PUSH {R4,LR} //
x) Preserve R4
{ MOV R4,R0 // Keep x safe in R4
return f2(4) + x ; LDR R0,=4 // R0 <-- f2’s arg
} BL f2 // R0 <-- f2(4)
ADD R0,R0,R4 // R0 <-- f2(4) + x
POP {R4,PC} // Restore R4

On entry to function: R0 parameter x R4 some value


After MOV instruction: R0 parameter x R4 parameter x
After LDR instruction: R0 constant 4 R4 parameter x
After the BL instruction: R0 f2 return value R4 parameter x
After the ADD instruction: R0 f1 return value R4 parameter x
After the POP instruction: R0 f1 return value R4 some value

You might also like