0% found this document useful (0 votes)
101 views

MCT-222 Embedded Systems: Lecture 5:parameter Passing, Aapcs Pointers, Arrays

The document discusses parameter passing in subroutines. It describes using registers to pass the first four parameters according to the ARM Application Binary Interface (AAPCS). Beyond four parameters, the stack is used. Examples show parameters passed in registers only, stack only, and a combination of registers and stack. The AAPCS standardizes parameter passing to improve efficiency and management of resources like registers and memory.

Uploaded by

Usman Tariq
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)
101 views

MCT-222 Embedded Systems: Lecture 5:parameter Passing, Aapcs Pointers, Arrays

The document discusses parameter passing in subroutines. It describes using registers to pass the first four parameters according to the ARM Application Binary Interface (AAPCS). Beyond four parameters, the stack is used. Examples show parameters passed in registers only, stack only, and a combination of registers and stack. The AAPCS standardizes parameter passing to improve efficiency and management of resources like registers and memory.

Uploaded by

Usman Tariq
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/ 22

MCT-222 Embedded Systems

Lecture 5:Parameter passing,


AAPCS
Pointers, Arrays

5-1
Registers to pass parameters
High level program Subroutine
1) Sets Registers to
contain inputs
2) Calls subroutine
3) Sees the inputs in
registers
4) Performs the action
of the subroutine
5) Places the outputs
in registers
6) Registers contain
outputs

5-2
Stack to pass parameters
High level program Subroutine
1) Pushes inputs on
the Stack
2) Calls subroutine
3) Sees the inputs on
stack (pops)
4) Performs the action of
the subroutine
5) Pushes outputs on the
stack
6) Stack contain
outputs (pop)
7) Balance stack
5-3
Procedure call standard

❑ How to pass parameters (input) to subroutine is not


just a matter of
❖ Performance (e.g. program execution speed)
But also a matter of efficient
❖ Management of resources (registers and memory)

❑ Need to standardize
❖ How parameters are passed to subroutine
❖ Where can parameters be stored
❖ How registers and memory is managed
❖ Where to store output of subroutine
5-4
Assembly Parameter Passing
❑ Parameters passed using registers/stack
❑ Parameter passing need not be done using only
registers or only stack
❖Some inputs could come in registers and
some on stack and outputs could be returned
in registers and some on the stack
❑ Calling Convention
❖Application Binary Interface (ABI)
❖ARM: use registers for first 4 parameters,
use stack beyond, return using R0
❑ Keep in mind that you may want your assembly
subroutine to someday be callable from C or
callable from someone else’s software
5-5
ARM Arch. Procedure Call Standard
(AAPCS)
❑ ABI standard for all ARM architectures
❑ Use registers R0, R1, R2, and R3 to pass the
first four input parameters (in order) into any
function, C or assembly.
❑ We place the return parameter in Register R0.
❑ Functions can freely modify registers R0–R3
and R12. If a function needs to use R4 through
R11, it is necessary to push their current
register values onto the stack, use the register,
and then pop the old value off the stack before
returning.
❑ Stack push/pop an even number of registers

5-6
Parameter-Passing: Registers
Caller Callee
;--call a subroutine that ;---------Exp-----------
;uses registers for parameter passing ; Input: R0 and R1 have inputs XX an YY
MOV R0,#7 ; Output: R2 has the result XX raised to YY
MOV R1,#3 ; Comments: R1 and R2 and non-negative
BL Exp ; Destroys input R1
;; R2 becomes 7^3 = 343 (0x157) XX RN 0
YY RN 1
Call by value Pow
Exp
RN 2

ADDS XX,#0
BEQ Zero
ADDS YY,#0 ; check if YY is zero
BEQ One
MOV pow, #1 ; Initial product is 1
❑ Suggest changes More MUL pow,XX ; multiply product with XX
SUBS YY,#1 ; Decrement YY
to make it AAPCS BNE More
B Retn ; Done, so return
Zero MOV pow,#0 ; XX is 0 so result is 0
B Retn
One MOV pow,#1 ; YY is 0 so result is 1
Retn BX LR
5-7
Return by value
Parameter-Passing: Stack
Caller Callee
;-------- call a subroutine that ;---------Max5-----------
; Input: 5 signed numbers pushed on the stack
; uses stack for parameter passing
; Output: put only the maximum number on the stack
MOV R0,#12 ; Comments: The input numbers are removed from stack
MOV R1,#5 numM RN 1 ; current number
MOV R2,#22 max RN 2 ; maximum so far
MOV R3,#7 count RN 0 ; how many elements
Max5
MOV R4,#18
POP {max} ; get top element (top of stack)
PUSH {R0-R4} ; store it in max
; Stack has 12,5,22,7 and 18 MOV count,#4 ; 4 more to go
; (with 12 on top) Again POP {numM} ; get next element
BL Max5 CMP numM,max
BLT Next
; Call Max5 to find the maximum
MOV max, numM ; new numM is the max
;of the five numbers Next SUBS count,#1 ; one more checked
POP {R5} BNE Again
;; R5 has the max element (22) PUSH {max} ; found max so push it on stack
BX LR

Call by value Return by value

❑ Flexible style, but not AAPCS


5-8
Parameter-Passing: Stack & Regs
Caller Callee
;---------MinMax-----------
;------call a subroutine that uses
; Input: N numbers reg+stack; N passed in R0
;both stack and registers for ; Output: Return in R0 the min and R1 the max
;parameter passing ; Comments: The input numbers are removed from stack
MOV R0,#6 ; R0 elem count numMM RN 3; hold the current number in numMM
MOV R1,#-14 max RN 1 ; hold maximum so far in max
min RN 2
MOV R2,#5
N RN 0 ; how many elements
MOV R3,#32 MinMax
MOV R4,#-7 PUSH {R1-R3} ; put all elements on stack
MOV R5,#0 CMP N,#0 ; if N is zero nothing to do
BEQ DoneMM
MOV R6,#-5
POP {min} ; pop top and set it
PUSH {R4-R6} ; rest on stack
MOV max,min ; as the current min and max
; R0 has element count loop SUBS N,#1 ; decrement and check
; R1-R3 have first 3 elements; BEQ DoneMM
; remaining on Stack POP {numMM}
CMP numMM,max
BL MinMax
BLT Chkmin
;; R0 has -14 and R1 has 32 MOV max,numMM ; new num is the max
;; upon return Chkmin CMP numMM,min
BGT NextMM
MOV min,numMM ; new num is the min
❑ Not AAPCS NextMM B loop
DoneMM MOV R0,min ; R0 has min
BX LR
5-9
Parameter-Passing: Stack & Regs
Caller Callee
;---------MinMax-----------
;------call a subroutine that uses
; Input: N numbers reg+stack; N passed in R0
;both stack and registers for ; Output: Return in R0 the min and R1 the max
;parameter passing ; Comments: The input numbers are removed from stack
MOV R0,#6 ; R0 elem count numMM RN 3; hold the current number in numMM
MOV R1,#-14 max RN 1 ; hold maximum so far in max
min RN 2
MOV R2,#5
N RN 0 ; how many elements
MOV R3,#32 MinMax
MOV R4,#-7 PUSH {R1-R3} ; put all elements on stack
MOV R5,#0 CMP N,#0 ; if N is zero nothing to do
BEQ DoneMM
MOV R6,#-5
POP {min} ; pop top and set it
PUSH {R4-R6} ; rest on stack
MOV max,min ; as the current min and max
; R0 has element count loop SUBS N,#1 ; decrement and check
; R1-R3 have first 3 elements; BEQ DoneMM
; remaining on Stack POP {numMM}
CMP numMM,max
BL MinMax
BLT Chkmin
;; R0 has -14 and R1 has 32 MOV max,numMM ; new num is the max
;; upon return Chkmin CMP numMM,min
BGT NextMM
MOV min,numMM ; new num is the min
❑ Not AAPCS NextMM B loop
DoneMM MOV R0,min ; R0 has min
BX LR
5-10
Pointers
❑Pointers are addresses that refer to objects
❖Objects may be data, functions or other pointers
❖If a register or memory location contains an
address we say it points into memory

❑Use of indexed addressing mode

5-11
Data Structures
❑Pointers are the basis for data structures

{
Organization of data
Abstract data type
Functions to facilitate access

5-12
Arrays
❑ Random access
❑ Sequential access
❑ An array
o equal precision and
o allows random access.
❑ The precision is the size of each element. (n)
❑ The length is the number of elements (fixed or
variable).
❑ The origin is the index of the first element.
❖ zero-origin indexing.

Data in consecutive memory


int32_t Buffer[100] { Access: Buffer[i]
5-13
Array Declaration
❑ In assembly ❑ Length of the array?
AREA Data ❖ One simple mechanism saves
A SPACE 400
the length of the array as the
AREA |.text|
first element
Prime DCW 1,2,3,5,7,11,13
In C:
const int8_t
❑ In C Data[5]={4,0x05,0x06,0x0A,0x09};
uint32_t A[100]; const int16_t
const uint16_t Prime[] = Powers[6]={5,1,10,100,1000,10000};
{1,2,3,5,7,11,13}; const int32_t Filter[5]=
{4,0xAABBCCDD,0x00,0xFFFFFFFF,-0x01}

In assembly:
Data DCB 4,0x05,0x06,0x0A,0x09
Powers DCW 5,1,10,100,1000,10000
Split declaration on Filter DCD 4,0xAABBCCDD, 0x00
multiple lines if needed DCD 0xFFFFFFFF,-0x01

5-14
… Arrays
❑ Length of the Array:
❖ Alternative mechanism stores a special
termination code (sentinel) as the last
element.

5-15
Array Access
❑ In general, let n be the ❑ In C
precision of a zero-origin
indexed array in
d = Prime[4];
elements.
❖ n=1 (8-bit elements) ❑ In assembly
❖ n=2 (16-bit elements)
LDR R0,=Prime
❖ n=4 (32-bit elements)
❑ If I is the index and LDRH R1,[R0,#8]
❑ Base is the base address
of the array,
❑ then the address of the 16-bit unsigned
element at I is

Base+n*I

5-16
Array Access
❑Indexed access ❑Pointer access
❖In C ❖In C
uint32_t i; uint16_t *p;
sum = 0; sum = 0;
for(i=0; i<7; i++) { for(p = Prime;
sum += Prime[i]; p != &Prime[7]; p++){
} sum += *p;
uint16_t }
❖In assembly
LDR R0,=Prime ❖In assembly
MOV R1,#0 ; ofs = 0 LDR R0,=Prime ; p = Prime
MOV R3,#0 ; sum = 0 ADD R1,R0,#14 ; &Prime[7]
MOV R3,#0 ; sum = 0
lp LDRH R2,[R0,R1] ;Prime[i]
lp LDRH R2,[R0] ; *p
ADD R3,R3,R2 ; sum
ADD R3,R3,R2 ; sum += ..
ADD R1,R1,#2 ; ofs += 2
ADD R0,R0,#2 ; p++
CMP R1,#14 ; ofs <= 14?
CMP R0,R1
BLO lp
BNE lp 5-17
Example
❑ Statement: Design an exponential function, y = 10x, with a 32-bit output.
❑ Solution: Since the output is less than 4,294,967,295, the input must be
between 0 and 9. One simple solution is to employ a constant word array, as
shown below. Each element is 32 bits. In assembly, we define a word constant
using DCD, making sure in exists in ROM.

const uint32_t Powers[10] =


{1,10,100,1000,10000,100000,1000000,10000000,100000000,1000000000};

5-18
…Solution
C Assembly

const uint32_t Powers[10] AREA |.text|,CODE,READONLY,ALIGN=2


={1,10,100,1000,10000, Powers DCD 1, 10, 100, 1000, 10000
100000,1000000,10000000, DCD 100000, 1000000, 10000000
100000000,1000000000}; DCD 100000000, 1000000000
;------power----
uint32_t power(uint32_t x){
return Powers[x]; ; Input: R0=x
} ; Output: R0=10^x
power LSL R0, R0, #2 ; x = x*4
LDR R1, =Powers ; R1= &Powers
LDR R0, [R1, R0] ; y=Powers[x]
BX LR

Base + Offset

5-19
Strings
Problem: Write software to output an ASCII string to an
output device.
Solution: Because the length of the string may be too long
to place all the ASCII characters into the registers at the
same time, call by reference parameter passing will be
used. With call by reference, a pointer to the string will
be passed. The function OutString, will output the string
data to the output device.

The function OutChar will be developed later. For now all


we need to know is that it outputs a single ASCII
character to the serial port.

5-20
…Solution

Hello DCB "Hello world\n\r",0 const uint8_t Hello[]= "Hello world\n\r";

;Input: R0 points to string void UART_OutString(uint8_t *pt){


UART_OutString while(*pt){
PUSH {R4, LR}
UART_OutChar(*pt);
MOV R4, R0
loop LDRB R0, [R4] pt++;
CMP R0, #0 ;done? }
BEQ done ;0 sentinel }
BL UART_OutChar ;print void main(void){
ADD R4, #1 ;next UART_Init();
B loop
done POP {R4, PC} UART_OutString("Hello World");
while(1){};
Start }
LDR R0,=Hello
BL UART_OutString
Call by reference

Think about how this is insecure. What might


happen if a malicious program called this?
5-21
Thank You

Q&A

Slides credit: Jonathan Volvano (http://users.ece.utexas.edu/~valvano/)

5-22

You might also like