Chapter 10 C and Assembly
Chapter 10 C and Assembly
Chapter 10
Mixing C and Assembly
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
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
8 8 8 8
32
Processor
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();
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
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
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
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 }
16