1 of 18

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

Friday 3/4 7-8:30pm

Location: Canvas

http://users.ece.utexas.edu/~valvano/Volume1/exams.htm

5-1

2 of 18

Agenda

  • Recap
    • Switch and LED interfacing
    • PWM
    • Software delay
    • PUSH POP
    • Conditional branching in assembly

  • Outline
    • TExaS Logic Analyzer
    • Arm Architecture Procedure Call Standard
    • Functions, parameter passing
    • Indexed Addressing and Pointers
    • Data Structures: Arrays in assembly

Bard, Erez, Gerstlauer, Valvano, Yerraballi, Telang, Cuevas, Tiwari

5-2

3 of 18

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

4 of 18

TExaS Scope Logic Analyzer

  • Modify Lab3 starter to toggle a pin
    • Lec4b slide 9
  • Scope,
    • connect PD2 to pin
    • Run TExasDisplay in scope mode

  • Logic analyzer
    • Run TExasDisplay in logic analyzer mode

; 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

5 of 18

ARM Architecture Procedure Call Standard (AAPCS)

  • Allows your code to work with others
    • Compilers generate AAPCS compliant code
      • Labs 7,8,9,10 mix C and assembly
      • Exam 1 mixes your assembly with grader C
    • Input parameters in R0,R1,R2,R3
    • If more than 4, rest go on stack
      • Exam 1 will not have more than 4 parameters
    • Output parameter in R0 if needed
    • You can freely use R0-R3,R12
    • If you use R4-R11, then save/restore on stack
    • Push/pop an even number of registers
  • Not an AAPCS rule but…
    • Save LR if one function needs to call another

Bard, Erez, Gerstlauer, Valvano, Yerraballi, Telang, Cuevas, Tiwari

5-5

6 of 18

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

7 of 18

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

8 of 18

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

9 of 18

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

10 of 18

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

11 of 18

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

12 of 18

Arrays

  • Random access
  • Sequential access
  • An array
      • equal precision and
      • 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

Access: Buffer[i]

{

int32_t Buffer[100]

Bard, Erez, Gerstlauer, Valvano, Yerraballi, Telang, Janapa Reddi, Tiwari

5-12

13 of 18

Data Structures

  • Pointers are the basis for data structures

Organization of data

Functions to facilitate access

{

Abstract data type

Bard, Erez, Gerstlauer, Valvano, Yerraballi, Telang, Cuevas, Tiwari

5-13

14 of 18

… Arrays

  • Length of the Array:
    • Alternative mechanism stores a special termination code as the last element.

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

15 of 18

Array Declaration

  • In assembly

AREA Data

A SPACE 400

AREA |.text|

Prime DCW 1,2,3,5,7,11,13

  • In C

uint32_t A[100];

const uint16_t Prime[] = {1,2,3,5,7,11,13};

  • Length of the array?
    • Fixed length
    • Length of the array is the first element
    • Length is a separate parameter
    • Termination code

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

16 of 18

Array Access

  • In general, let n be the precision of a zero-origin indexed array in elements.
    • n=1 (8-bit elements)
    • n=2 (16-bit elements)
    • n=4 (32-bit elements)
  • If I is the index and
  • Base is the base address of the array,
  • then the address of the element at I is

Base+n*I

  • In C

d = Prime[4];

  • In assembly

LDR R0,=Prime

LDRH R1,[R0,#8]

Bard, Erez, Gerstlauer, Valvano, Yerraballi, Telang, Cuevas, Tiwari

16-bit unsigned

5-16

17 of 18

Indexed Array Access

  • Indexed access
    • In C

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;

}

    • In assembly

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

18 of 18

AAPCS Summary

  • Allows your code to work with others
    • Input parameters in R0,R1,R2,R3
      • Call by value is a number
      • Call by reference is a pointer
    • If more than 4, rest go on stack
    • Output parameter in R0 if needed
    • You can freely use R0-R3,R12
    • If you use R4-R11, then save/restore on stack
    • Push/pop an even number of registers

Bard, Erez, Gerstlauer, Valvano, Yerraballi, Telang, Cuevas, Tiwari

5-18