• Nenhum resultado encontrado

61 aula

N/A
N/A
Protected

Academic year: 2021

Share "61 aula"

Copied!
36
0
0

Texto

(1)

Interface Hardware-Software

Aula 6-1

C-Assembler Integration

Prof. Dr. Stefan Michael Blawid

(2)

IHS - §6 C/Asm 2

Tópicos

1) Introduction

2) Calling assembly procedures from C 3) Calling C functions from assembly 4) Inline assembly

(3)

IHS - §6 C/Asm 3

Tópicos

1) Introduction

2) Calling assembly procedures from C 3) Calling C functions from assembly 4) Inline assembly

(4)

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.

(5)

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

(6)

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

(7)

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

(8)

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

(9)

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 (_)

(10)

IHS - §6 C/Asm 10

(11)

IHS - §6 C/Asm 11

(12)

IHS - §6 C/Asm 12

Tópicos

1) Introduction

2) Calling assembly procedures from C

3) Calling C functions from assembly 4) Inline assembly

(13)

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

(14)

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)

(15)

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

(16)

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

(17)

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

(18)

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

(19)

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

(20)

IHS - §6 C/Asm 20

(21)

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

(22)

IHS - §6 C/Asm 22

(23)

IHS - §6 C/Asm 23

(24)

IHS - §6 C/Asm 24

(25)

IHS - §6 C/Asm 25

Tópicos

1) Introduction

2) Calling assembly procedures from C

3) Calling C functions from assembly

(26)

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.

(27)

IHS - §6 C/Asm 27

(28)

IHS - §6 C/Asm 28

(29)

IHS - §6 C/Asm 29

Tópicos

1) Introduction

2) Calling assembly procedures from C 3) Calling C functions from assembly

(30)

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

(31)

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)

(32)

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 []

(33)

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

(34)

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

(35)

IHS - §6 C/Asm 35

Example

sum is identified by %0 value by %1

(36)

Referências

Documentos relacionados

Este estudo se estruturou em meio a vivência no Estágio Supervisionado III em Gestão Escolar no curso de Pedagogia da Universidade Estadual da Paraíba, na cidade de Guarabira, que

Designam-se por doenças raras aquelas que afetam um pequeno número de pessoas, quando comparado com a população em geral e que têm inerente questões clínicas específicas

Nessa perspectiva, objetivando minimizar as dificuldades na compreensão dos conhecimentos morfofisiológicos relativos ao corpo humano, visto que estes conteúdos muitas vezes

19 precárias de moradia, a alimentação diferente, a má comunicação com funcionários e intérpretes das fazendas e as dívidas adquiridas para a sobrevivência

Nesse cenário, destaca-se o desenvolvimento de modelos que potencializam o intercâmbio de informações a partir da interligação de dados disponíveis em

COMPARAÇÃO ENTRE OS MÉTODOS DE REGULAÇÃO Nas tabelas 26 e 27 são apresentadas, respectivamente, a produção anual m3 e a área de corte anual ha, assim como o retorno líquido e

Quando se compara, o maior valor do deslocamento horizontal que a viga apresenta com o maior valor do deslocamento vertical, gerados pela aplicação do carregamento, é

In Table 2, it may be observed that the parameters of plant height (PH), number of trifoliate leaves (NT), shoot dry matter (SDM), root dry matter (RDM), no- dules dry matter