Lecture 9 Using C
Lecture 9 Using C
using C Language
© 2021 Arm
Learning Objectives
At the end of this lecture, you should be able to:
• Outline the advantages and disadvantages of C and Assembly programming languages.
• Outline the steps in which high level language programs like C are generated and
downloaded onto the hardware.
• Identify the data types supported by in Arm processors.
• Describe how data is stored in RAM.
• Identify how to call a C function from Assembly and call an Assembly function from C
Identify how to write and call an assembly function inside C files.
2 © 2021 Arm
Module Syllabus
3 © 2021 Arm
Building a System on a Chip (SoC)
Software high-level
Application Design (e.g., Game)
application development
Hardware design
Arm Cortex-M0 Peripheral Peripheral Peripheral
AHB
Processor Memory
VGA UART
Peripheral Peripheral
Interrupt
4 © 2021 Arm
C and Assembly Language Review
Language Advantages Disadvantages
Easy to learn Limited or no direct access to core registers and
stack
Portable No direct control over instruction sequence
C generation
Easy handling of complex data structures No direct control over stack usage
Allow direct control to each instruction step and all Take longer time to learn
memory
Assembly Allows direct access to instructions that cannot be Difficult to manage data structure
generated with C
Less portable
5 © 2021 Arm
Typical Program-Generation Flow
• The generation of a program follows a typical development flow.
• Compile > Assemble > Link > Download
• The generated executable file (or program image) is stored in the program memory (normally an on-
chip flash memory), to be fetched by the processor.
Off-line Compilation
C Code
Compile Processor
Assembly Code Fetch
Assemble Instruction
Fetch Decode
Machine Code Libraries
Link Execute
Program Image
Download Data Input Data Output
Processing
Program Memory
.O Files
Object Files (.o) C/C++
C/ C++
Libraries .S Files
.AXF File
Executable Program Mage .LIB file
Code region
Interrupt vectors
0x00000040
SysTick vector 0x0000003C
PendSV vector 0x00000038
Start-up routine
Reserved
SVC vector 0x0000002C
Program code Reserved
Program
Image Hard fault vector 0x0000000C
NMI vector 0x00000008
C library code
Reset vector 0x00000004
Initial MSP value 0x00000000
Vector table
0x00000000
8 © 2021 Arm
Program Image
Code region
Interrupt vectors
0x00000040
SysTick vector 0x0000003C
PendSV vector 0x00000038
Start-up routine
Reserved
SVC vector 0x0000002C
Program code Reserved
Program
Image Hard fault vector 0x0000000C
NMI vector 0x00000008
C library code
Reset vector 0x00000004
Initial MSP value 0x00000000
Vector table
0x00000000
9 © 2021 Arm
Program Image
Code region
Interrupt vectors
0x00000040
SysTick vector 0x0000003C
PendSV vector 0x00000038
Start-up routine
Reserved
SVC vector 0x0000002C
Program code Reserved
Program
Image Hard fault vector 0x0000000C
NMI vector 0x00000008
C library code
Reset vector 0x00000004
Initial MSP value 0x00000000
Vector table
0x00000000
10 © 2021 Arm
Program Image
Code region
Interrupt vectors
0x00000040
SysTick vector 0x0000003C
PendSV vector 0x00000038
Start-up routine
Reserved
SVC vector 0x0000002C
Program code Reserved
Program
Image Hard fault vector 0x0000000C
NMI vector 0x00000008
C library code
Reset vector 0x00000004
Initial MSP value 0x00000000
Vector table
0x00000000
11 © 2021 Arm
Program Image
Code region
Interrupt vectors
0x00000040
SysTick vector 0x0000003C
PendSV vector 0x00000038
Start-up routine
Reserved
SVC vector 0x0000002C
Program code Reserved
Program
Image Hard fault vector 0x0000000C
NMI vector 0x00000008
C library code
Reset vector 0x00000004
Initial MSP value 0x00000000
Vector table
0x00000000
12 © 2021 Arm
Program Image in Global Memory
0xFFFFFFFF
Reserved
0xE0100000
512MB
0xE00FFFFF
Private Peripheral Bus
0xE0000000
0xDFFFFFFF
0xA0000000
0x9FFFFFFF
0x60000000
0x5FFFFFFF
Peripherals 512MB
Mainly used for data memory 0x40000000
0x3FFFFFFF
e.g., on-chip SRAM, SDRAM SRAM 512MB
0x20000000
0x1FFFFFFF
Mainly used for program image Code 512MB
13 © 2021 Arm
Program Data Types
Data type Size Signed range Unsigned range
char, int8_t, uint8_t Byte -128 to 127 0 to 255
short, int16_t, uint16_t Half word -32768 to 32767 0 to 65535
14 © 2021 Arm
Data Qualifiers in C Language
const vs volatile
const volatile
• Never written by program • Can be changed outside of normal program
• Can be put in ROM to save RAM flow: ISR, hardware register
• Compiler must be careful with optimizations
15 © 2021 Arm
How Is Data Stored in RAM
High
Low
16 © 2021 Arm
Example of Data Storage
17 © 2021 Arm
Define Interrupt Vector in C
• The interrupt vector can be defined in either C language or assembly language. For
example, in C:
typedef void(* const ExecFuncPtr)(void) __irq;
ExecFuncPtr exception_table[] = {
(ExecFuncPtr)&Image$$ARM_LIB_STACK$$ZI$$Limit, /* Initial SP */
(ExecFuncPtr)__main, /* Initial PC */
NMIException,
HardFaultException,
MemManageException,
BusFaultException,
UsageFaultException,
0, 0, 0, 0, /* Reserved */
SVCHandler,
DebugMonitor,
0, /* Reserved */
PendSVC,
SysTickHandler
/* Configurable interrupts start here...*/
};
#pragma arm section
18 © 2021 Arm
Define Stack and Heap
• The stack and heap can be defined in either C language (with a linker file) or assembly
language. For example, in C:
/* Set stack and heap parameters */
19 © 2021 Arm
Define Stack and Heap
• Define stack and heap in assembly language:
20 © 2021 Arm
Accessing Peripherals in C
• Define base addresses for peripherals.
#define AHB_VGA_BASE 0x50000000
#define AHB_UART_BASE 0x51000000
#define AHB_TIMER_BASE 0x52000000
#define AHB_GPIO_BASE 0x53000000
#define AHB_7SEG_BASE 0x54000000
#define NVIC_INT_ENABLE 0xE000E100
21 © 2021 Arm
Calling a C Function from Assembly
• When a C function is called from an assembly file, the following areas should be
checked:
• Register R0, R1, R2, R3, R12, and LR could be changed; hence, it is better to save them to the stack.
• The value of SP should be aligned to a double-word address boundary.
• Input parameters have to be stored in the correct registers; for example, registers R0 to R3 can be
used for passing four parameters.
• The return value is usually stored in R0.
22 © 2021 Arm
Calling a C Function from Assembly
UART_Handler PROC
EXPORT UART_Handler // label name in assembly
IMPORT UART_ISR // function name in C
PUSH {R0,R1,R2,LR} // context saving
BL UART_ISR // branch to ISR written in C
POP {R0,R1,R2,PC} // context restoring
ENDP
23 © 2021 Arm
Calling an Assembly Function from C
• When calling an assembly function from C code, the following areas should be checked:
• If registers R4 to R11 need to be changed, they have to be stacked and restored in the assembly
function.
• If another function is called inside the assembly function, the LR register needs to be saved on the
stack and used for return.
• The function return value is normally stored in R0.
24 © 2021 Arm
Calling an Assembly Function from C
• Write a function in assembly:
EXPORT add_asm
add_asm FUNCTION
ADDS R0, R0, R1
ADDS R0, R0, R2
ADDS R0, R0, R3
BX LR ; result is returned in R0
ENDFUNC
25 © 2021 Arm
Embedded Assembly
• The embedded assembler allows a developer to write assembly functions inside C files:
_asm int add_asm( int k1, int k2, int k3, int k4) {
void main {
int x;
x = add_asm (11,22,33,44); // call assembly function
…
}
26 © 2021 Arm
Thank You
Danke
Gracias
谢谢
ありがとう
Asante
Merci
감사합니다
धन्यवाद
Kiitos
شك ًرا
ধন্যবাদ
© 2021 Arm תודה
The Arm trademarks featured in this presentation are registered
trademarks or trademarks of Arm Limited (or its subsidiaries) in
the US and/or elsewhere. All rights reserved. All other marks
featured may be trademarks of their respective owners.
www.arm.com/company/policies/trademarks
© 2021 Arm