Interface Hardware-Software
Aula 6-1
C-Assembler Integration
Prof. Dr. Stefan Michael Blawid
IHS - §6 C/Asm 2
Tópicos
1) Introduction
2) Calling assembly procedures from C 3) Calling C functions from assembly 4) Inline assembly
IHS - §6 C/Asm 3
Tópicos
1) Introduction
2) Calling assembly procedures from C 3) Calling C functions from assembly 4) Inline assembly
IHS - §6 C/Asm 4
Software Layers
The HAL can be implemented in the assembly language recognized by the processor or in specific C code.
IHS - §6 C/Asm 5
Low level SW layer
Depends more on the executing HW: SW that is in direct contact with HW SW that is significantly affected by HW SW that directly influences HW behavior
Yet, many HdS layer parts are nonetheless HW independent Allows controlled access and management of HW devices:
I/O
Process Scheduling Device Boot
Requires code that is more complex to write and debug Should take into account HW details
If programming language is low level, more lines of code are required
IHS - §6 C/Asm 6
Programming Languages Used
The low level SW layer needs not to be written entirely in assembly In fact, most code is written in high level languages
Only specific small parts written in assembly
There are many libraries or routines that grant low level access to high level languages
POSIX (Portable Operating System Interface) — Process Routines
newlib (C library) — Peripheral Access, like stdio, on Embedded Systems
IHS - §6 C/Asm 7
When to Use Assembly?
Full control of which pieces of hardware should be accessed Specific registers
Ensure a routine executes more deterministically
High-level language compilers can modify the programmer's intended execution
Important for real time systems
Specific routines that high level languages do not support Ex: Boot Loader parts
IHS - §6 C/Asm 8
Why Not Use Assembly in All Code?
Low productivity
Too many lines of code to do what few lines of high level code can achieve
Slower and more complex debugging High maintenance cost
Lack of portability
Code specific to the employed instruction set architecture (ISA) Not all low level code is HW dependent
IHS - §6 C/Asm 9
C-Assembly Integration
We can combine C and Assembly in different ways Separate C and Assembly Modules
Integration through routine calls
During the linking of object files the integration is made Inline Assembly Code
Inserting assembly code into a C code
In most C compilers, external labels should start with an underscore character (_)
IHS - §6 C/Asm 10
IHS - §6 C/Asm 11
IHS - §6 C/Asm 12
Tópicos
1) Introduction
2) Calling assembly procedures from C
3) Calling C functions from assembly 4) Inline assembly
IHS - §6 C/Asm 13
Calling Assembly Routines in C
There are conventions for passing C code parameters to assembly and for return values.
Some rules should be followed in assembly code when using
IHS - §6 C/Asm 14
Parameter Passing in C
Stack
(Stack Pointer points to the top of tack, copied to EBP) (Saved return address = near call)
IHS - §6 C/Asm 15
Stack Frame
Stack frame contains local variables of called function Stack frame grows from major to minor addresses
Saves also the beginning of the stack frame of the routine that called the function
By convention, the EBP register keeps the stack frame start address Known as the Frame Pointer (FP)
1. Push EBP 2. Copy ESP into EBP
3. Reduce ESP to carve out space; dynamic storage Function Parameters
Stack Frame back pointer
Space for local variables
IHS - §6 C/Asm 16
What Happens in the Assembly Routine Call
Pushes the frame pointer from the EBP register onto the stack and copies ESP → EBP
Pushes the value of the EIP register onto stack Store variable in main’s frame
Right pusher
Skips stacked EIP and EBP
IHS - §6 C/Asm 17
Conventions for Procedure Return
C functions use certain registers for return, so C code always assumes that assembly routines also use these registers.
8-,16- and 32-bit values are returned in EAX 64-bit values are returned in EDX:EAX
Floating point values are returned in ST(0) Addresses (pointers) are returned in EAX
IHS - §6 C/Asm 18
Rules for Writing the Assembly Code
Assembly code called by C can use any register, but must preserve EBP, EBX, ESI, and EDI
To preserve means to put the register value onto the stack (save to memory) before use
Assembly must use default return registers
It is advisable to start the routine with the enter statement and before the return put the leave statement
IHS - §6 C/Asm 19
The
ENTER Instruction
Make Stack Frame for Procedure Parameters Syntax: enter bytes, level
Stacks previous stack frame pointer (EBP), moves ESP (top of stack) value to EBP, and allocates bytes to store local variables (i.e., subtracts bytes from ESP and updates ESP)
level tells the nesting of the routine, with 0 being the highest level Defines how many stack frames pointers (FPs) should be copied to the new stack frame
IHS - §6 C/Asm 20
IHS - §6 C/Asm 21
The
LEAVE Instruction
High Level Procedure Exit Syntax: leave
Instruction to reverse the actions of enter
Copy EBP to ESP to free allocated space, and then restores old EBP value
As a consequence, restores ESP to the old value before entering routine
IHS - §6 C/Asm 22
IHS - §6 C/Asm 23
IHS - §6 C/Asm 24
IHS - §6 C/Asm 25
Tópicos
1) Introduction
2) Calling assembly procedures from C
3) Calling C functions from assembly
IHS - §6 C/Asm 26
Calling C Functions from Assembly
The same conventions are used for passing C code parameters to assembly and return values.
IHS - §6 C/Asm 27
IHS - §6 C/Asm 28
IHS - §6 C/Asm 29
Tópicos
1) Introduction
2) Calling assembly procedures from C 3) Calling C functions from assembly
IHS - §6 C/Asm 30
Inline Assembly Code
We can also insert assembly code directly into the C code Advisable for short code snippets only
Otherwise the code is very unreadable
IHS - §6 C/Asm 31
Key Differences of the AT&T Syntax
Register naming: The register name must be preceded by %
Source and Destination order: The order of destination and source operands in the instruction is reversed
Operand size: The instruction must specify the size of the operands (byte, word, longword)
IHS - §6 C/Asm 32
Key Differences of the AT&T Syntax (cont.)
Immediate and constant operands: Must be preceded by $ Addressing:
To specify indirect addressing, brackets () are used rather than square brackets []
To use scalar-indexed-base addressing (like in protected mode), you must put (base, index, scale) instead of []
IHS - §6 C/Asm 33
Example: Basic
ASM
Limitations of basic asm: - Global variables
- Compiler does not know about register changes - To access C data, it is safer to use extended asm
IHS - §6 C/Asm 34
Extended Inline Statements
Syntax: asm(assembly code : outputs : inputs : clobber list) Last three components are optional
Extended asm statements must be inside a function Output format: “=op-constraint” (C-expression)
Example: “=r” (sum) specifies that the C variable sum should be mapped to any of the eight general registers r (EAX, EBX, ECX, EDX, ESI, EDI, EBP, ESP)
Some other constraints: a (EAX), m (memory), i (immediate), … Input format: “op-constraint” (C-expression)
Clobber list: List of registers modified by the assembly instructions in the asm statement
IHS - §6 C/Asm 35
Example
sum is identified by %0 value by %1