EE 319K�Introduction to Embedded Systems
Lecture 5: AAPCS, Arrays
Disclaimer: not all previous code has been AAPCS compliant
Bard, Erez, Gerstlauer, Valvano, Yerraballi, Telang, Cuevas, Tiwari
5-1
Agenda
Bard, Erez, Gerstlauer, Valvano, Yerraballi, Telang, Cuevas, Tiwari
5-2
Subroutines Review
Fun1
; body of Fun1
BX LR
Fun2 PUSH {R4,LR}
; body of Fun2
BL Fun1
POP {R4,LR}
BX LR
Fun3 PUSH {R4,LR}
; body of Fun3
BL Fun2
POP {R4,LR}
BX LR
Start
Loop
BL Fun3
B Loop
void Fun1(void){
// body of Fun1
}
void Fun2(void){
// body of Fun2
Fun1();
}
void Fun3(void){
// body of Fun3
Fun2();
}
int main(void){
while(1){
Fun3();
}
}
Draw stack
5-3
TExaS Scope Logic Analyzer
; TExaS_Init sets bus clock at 80 MHz, interrupts, ADC1, TIMER3, TIMER5, and UART0
MOV R0,#0 ;0 for TExaS oscilloscope, 1 for PORTE logic analyzer, 2 for Lab3 grader, 3 for none
BL TExaS_Init ;enables interrupts, prints the pin selections based on EID1 EID2
; TExaS_Init sets bus clock at 80 MHz, interrupts, ADC1, TIMER3, TIMER5, and UART0
MOV R0,#1 ;0 for TExaS oscilloscope, 1 for PORTE logic analyzer, 2 for Lab3 grader, 3 for none
BL TExaS_Init ;enables interrupts, prints the pin selections based on EID1 EID2
5-4
ARM Architecture Procedure Call Standard (AAPCS)
Bard, Erez, Gerstlauer, Valvano, Yerraballi, Telang, Cuevas, Tiwari
5-5
AAPCS
;***** Calculate subroutine *********************
;Perform this mathematical function
;Let x be a 32-bit signed input
;If x is less -8, return 0
;If x is greater than +8, return 0
; otherwise return (-5*x*x)+(7*x)+400
;Inputs: R0 is input x
;Output: R0 the result of the calculate
;You are free to use R0,R1,R2,R3, and/or R12
;If you use R4 to R11, save/restore them on stack
EXPORT Calculate
Calculate
; put your code here
MOV R0,#42 ; remove this line
BX LR
5-6
AAPCS
;***** Calculate subroutine *********************
;Perform this mathematical function
;Let x be a 32-bit signed input
;If x is less -8, return 0
;If x is greater than +8, return 0
; otherwise return (-5*x*x)+(7*x)+400
;Inputs: R0 is input x
;Output: R0 the result of the calculate
;You are free to use R0,R1,R2,R3, and/or R12
;If you use R4 to R11, save/restore them on stack
EXPORT Calculate
Calculate
; what is in R0 at the start of the function?
MOV R0,#42 ; remove this line
BX LR
5-7
AAPCS
;***** Calculate subroutine *********************
;Perform this mathematical function
;Let x be a 32-bit signed input
;If x is less -8, return 0
;If x is greater than +8, return 0
; otherwise return (-5*x*x)+(7*x)+400
;Inputs: R0 is input x
;Output: R0 the result of the calculate
;You are free to use R0,R1,R2,R3, and/or R12
;If you use R4 to R11, save/restore them on stack
EXPORT Calculate
Calculate
; what is in R0?
MOV R0,#42 ; what should be in R0 at end?
BX LR
5-8
AAPCS
;***** Calculate subroutine *********************
;Perform this mathematical function
;Let x be a 32-bit signed input
;If x is less -8, return 0
;If x is greater than +8, return 0
; otherwise return (-5*x*x)+(7*x)+400
;Inputs: R0 is input x
;Output: R0 the result of the calculate
;You are free to use R0,R1,R2,R3, and/or R12
;If you use R4 to R11, save/restore them on stack
EXPORT Calculate
Calculate
; what is in R0? What if I want to use R1,R2,R3 or R12?
MOV R0,#42 ; what is in R0 at end?
BX LR
5-9
AAPCS
;***** Calculate subroutine *********************
;Perform this mathematical function
;Let x be a 32-bit signed input
;If x is less -8, return 0
;If x is greater than +8, return 0
; otherwise return (-5*x*x)+(7*x)+400
;Inputs: R0 is input x
;Output: R0 the result of the calculate
;You are free to use R0,R1,R2,R3, and/or R12
;If you use R4 to R11, save/restore them on stack
EXPORT Calculate
Calculate
; What if I want to use R4,R5,R6,R7?
PUSH {R4-R7}
// stuff
POP {R4-R7}
; R0 has return result
BX LR
5-10
AAPCS
;***** Calculate subroutine *********************
;Perform this mathematical function
;Let x be a 32-bit signed input
;If x is less -8, return 0
;If x is greater than +8, return 0
; otherwise return (-5*x*x)+(7*x)+400
;Inputs: R0 is input x
;Output: R0 the result of the calculate
;You are free to use R0,R1,R2,R3, and/or R12
;If you use R4 to R11, save/restore them on stack
EXPORT Calculate
Calculate
; What if I want push just R4?
PUSH {R4,R5}
// stuff
POP {R4,R5}
; R0 has return result
BX LR
5-11
Arrays
Data in consecutive memory
Access: Buffer[i]
{
int32_t Buffer[100]
Bard, Erez, Gerstlauer, Valvano, Yerraballi, Telang, Janapa Reddi, Tiwari
5-12
Data Structures
Organization of data
Functions to facilitate access
{
Abstract data type
Bard, Erez, Gerstlauer, Valvano, Yerraballi, Telang, Cuevas, Tiwari
5-13
… Arrays
Bard, Erez, Gerstlauer, Valvano, Yerraballi, Telang, Cuevas, Tiwari
Base is address of first element
Length is the number of elements
n is precision, number of bytes in each element (1,2,4)
I is the index is 0 to Length-1
Address = Base+n*I
5-14
Array Declaration
AREA Data
A SPACE 400
AREA |.text|
Prime DCW 1,2,3,5,7,11,13
uint32_t A[100];
const uint16_t Prime[] = {1,2,3,5,7,11,13};
Bard, Erez, Gerstlauer, Valvano, Yerraballi, Telang, Cuevas, Tiwari
DCB is 8-bit byte, DCW is 16-bit halfword, DCD is 32-bit word
5-15
Array Access
Base+n*I
d = Prime[4];
LDR R0,=Prime
LDRH R1,[R0,#8]
Bard, Erez, Gerstlauer, Valvano, Yerraballi, Telang, Cuevas, Tiwari
16-bit unsigned
5-16
Indexed Array Access
const uint16_t Prime[] = {1,2,3,5,7,11,13};
uint32_t Sum7Primes(void){
uint32_t sum = 0;
uint32_t i = 0;
do{� sum += Prime[i];
i++;
}while(i<7);
return sum;
}
j RN 1
sum RN 0
Sum7Primes
LDR R3,=Prime
MOV sum,#0 ;sum = 0
MOV j,#0 ;ofs = 0
lp LDRH R2,[R3,j] ;Prime[i]
ADD sum,sum,R2 ; sum
ADD j,j,#2 ; ofs += 2
CMP j,#14 ; ofs <= 14?
BLO lp
BX LR
;j is 2*i
Bard, Erez, Gerstlauer, Valvano, Yerraballi, Telang, Cuevas, Tiwari
Register name
5-17
AAPCS Summary
Bard, Erez, Gerstlauer, Valvano, Yerraballi, Telang, Cuevas, Tiwari
5-18