0% found this document useful (0 votes)
13 views16 pages

Chapter 10 C and Assembly

Uploaded by

Sana Ullah
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views16 pages

Chapter 10 C and Assembly

Uploaded by

Sana Ullah
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 16

Embedded Systems with ARM Cortex-M Microcontrollers in

Assembly Language and C

Chapter 10
Mixing C and Assembly

Dr. Yifeng Zhu


Electrical and Computer Engineering
University of Maine

Spring 2018

1
Basic Data Types in C
Data Data Size
Alignment Data Range
Type (bits)
bool 8 byte 0 or 1. Bits 1-7 are ignored
char 8 byte -128 - 127(signed) or 0 - 255(unsigned)
int 32 word -2,147,483,648 - 2,147,483,647(signed)
or
0 - 4,294,967,296(unsigned)
short int 16 halfword -32,768 - 32,767(signed) or 0 -
65,536(unsigned)
long int 32 word same as int
long 64 word -9,223,372,036,854,775,808 -
long 9,223,372,036,854,775,807(signed) or
0-
18,446,744,073,709,551,616(unsigned)
float 32 word +/- 1.4023x10-45 to 3.4028x10+38, always
signed
double 64 word +/- 4.9406x10-324 to 1.7977x10308, always
signed
long 96 word very large range
2
double
Load and Store Data

unsigned char LDRB/STRB


unsigned short LDRH/STRH
unsigned int LDR/STR
char LDRSB/STRSB
short LDRSH/STRSH

3
8 bits

address + 23

Struct address + 22
address + 21
struct Position address + 20
{ address + 19
char x; address + 18
char y; address + 17
char x; address + 16
int time;
address + 15
short scale;
address + 14
} array[10];
address + 13
address + 12
address + 11
address + 10
address + 9
address + 8
address + 7
address + 6
address + 5
address + 4
address + 3
address + 2
address + 1
4
address
8 bits

address + 23 0x00
Struct address + 22
address + 21
0x00
padding

scale
struct Position address + 20
{ address + 19
char x; address + 18
char y; time array[1]
address + 17
char x; address + 16
int time;
address + 15 0x00 padding
short scale;
address + 14 x
} array[10];
address + 13 y
address + 12 x
address + 11 0x00
padding
struct Position { address + 10 0x00
char x; address + 9
scale
char y; address + 8
char x; address + 7
char address + 6
time array[0]
padding_1[1]; address + 5
int time; address + 4
short scale; address + 3 0x00 padding
char address + 2 x
padding_2[2];
address + 1 y
5 } array[10];
address x
8 bits

address + 23

Struct address + 22
address + 21
address + 20

__packed struct Position address + 19


{ address + 18
char x; address + 17
char y; address + 16
char x; address + 15
int time; address + 14
short scale; address + 13
} array[10];
address + 12
address + 11
address + 10
address + 9
address + 8
address + 7
address + 6
address + 5
address + 4
address + 3
address + 2
address + 1
6
address
8 bits
Struct
address + 17
scale
__packed struct Position address + 16
{ address + 15
char x; address + 14
char y; time
address + 13 array[1]
char x;
int time; address + 12
short scale; address + 11 x
} array[10]; address + 10 y
address + 9 x
address + 8
scale
address + 7
address + 6
address + 5
time
address + 4 array[0]
address + 3
address + 2 x
address + 1 y
7 address x
Unaligned Accesses
8 bits
Memory
Bank 0 Bank 1 Bank 2 Bank 3 Address
4x + 20
4x + 16
4x + 12
4x + 8
2nd Access 0x78 4x + 4
1st Access 0x12 0x34 0x56 4x

8 8 8 8

32

Processor

Two memory accesses are required to fetch the unaligned word!

8
Struct
struct Position {
__packed struct Position
char x;
{
char y;
char x;
char x;
char y;
char
char x;
padding_1[1];
int time;
int time;
short scale;
short scale;
} array[10];
char
padding_2[2]; Optimized for space
}Optimized for speed
array[10];
(default)

9
Static Variables
C Code C Code
int foo(); int foo();

int main(void) { int main(void) {


int y; int y;
y = foo(); // y = ? y = foo(); // y = ?
y = foo(); // y = ? y = foo(); // y = ?
y = foo(); // y = ? y = foo(); // y = ?
while(1); while(1);
} }

int foo() { int foo() {


int x = 5; static int x = 5;
x = x + 1; x = x + 1;
return(x) return(x)
} }

10
Static Variables
C Code Assembly Code
int foo(); AREA static_demo, CODE
EXPORT __main
ALIGN
int main(void) { ENTRY
int y; __main PROC
y = foo(); // y = 6 BL foo ; r0 = 6
y = foo(); // y = 6 BL foo ; r0 = 6
y = foo(); // y = 6 BL foo ; r0 = 6
while(1); stop B stop
} ENDP

int foo() { foo PROC


int x = 5; // local variable MOV r0,#5
x = x + 1; ADD r0,r0,#1
return(x) BX LR
} ENDP
END

11
Static Variables
C Code Assembly Code
int foo(); AREA static_demo, CODE
EXPORT __main
ALIGN
int main(void) { ENTRY
int y; __main PROC
y = foo(); // y = 6 BL foo ; r0 = 5
y = foo(); // y = 7 BL foo ; r0 = 6
y = foo(); // y = 8 BL foo ; r0 = 7
while(1); stop B stop
} ENDP

int foo() { foo PROC


// x is initialized only once LDR r1,=x
static int x = 5; // a static LDR r0,[r1]
variable ADD r0,r0,#1
x = x + 1; STR r0,[r1]
return(x) BX LR
} ENDP

AREA myData, DATA


ALIGN
x DCD 5
END
12
Example of C Calling Assembly

C Program (main.c) Assembly Program (strlen.s)


AREA stringLength, CODE
char str[25] = "Hello!"; EXPORT strlen ; make strlen
visible
extern void strlen(char* ALIGN
s); strlen PROC
PUSH {r4, lr} ; preserve r4 and
int main(void){ lr
int i; MOV r4, #0 ; initialize
i = strlen(str); length
while(1); loop LDRB r1, [r0, r4] ; r0 = string
} address
CBZ r1, exit ; branch if zero
ADD r4, r4, #1 ; length++
B loop ; do it again
exit MOV r0, r4 ; place result in
r0
POP {r4, pc} ; exit
ENDP

13
Example of Accessing C Variables
C Program (main.c) Assembly Program (count.s)
int counter; AREA count, CODE
IMPORT counter
extern int getValue(); ALIGN
extern void setValue(int c); setValue PROC
EXPORT setValue
void increment(); LDR r1, =counter
STR r0, [r1]
int main(void) { BX lr
int c = 0; ENDP
setValue(1); getValue PROC
increment(); EXPORT getValue
c = getValue(); LDR r1, =counter
while(1); LDR r0, [r1]
} BX lr
ENDP
void increment(){ increment PROC
counter += 2; EXPORT increment [WEAK]
} LDR r1, =counter
LDR r0, [r1]
ADD r0, r0, #1
STR r0, [r1]
BX lr
ENDP
END
14
Example of Assembly Calling C

Assembly Program (main.s) C Program (strlen.c)


AREA my_strlen, CODE
EXPORT __main
IMPORT strlen int strlen(char *s){
ALIGN int i = 0;
ENTRY
while( s[i] != '\0')
__main PROC i++;
LDR r0, =str
BL strlen return i;
stop B stop }
ENDP

AREA myData, DATA


ALIGN
Str DCB "12345678",0
END

15
Example of Accessing Data
Defined in Assembly
Assembly Program C Program
AREA main, CODE
EXPORT __main
IMPORT getValue extern int counter;
IMPORT increment
IMPORT setValue int getValue() {
ALIGN return counter;
ENTRY }

__main MOVS r2,#0 void increment() {


MOVS r0,#1 counter++;
BL setValue }
BL increment
BL getValue void setValue(int c) {
MOV r2,r0 counter = c;
stop B stop }

AREA myData, DATA


EXPORT counter
counter DCD 0
END

16

You might also like