MCT-222 Embedded Systems: Lecture 5:parameter Passing, Aapcs Pointers, Arrays
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
❑ 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
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.
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.
5-18
…Solution
C Assembly
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.
5-20
…Solution
Q&A
5-22