Sie sind auf Seite 1von 41

UNIT-II

C and Assembly
Overview of Embedded C Compilers and Optimization Programming in Assembly Register usage conventions

Typical use of addressing options


Instruction sequencing Procedure call and return Parameter passing Retrieving parameters

Everything is pass by value


Temporary variables
Software Technology For Embedded Systems

INTRO
Assembly language means, programming with low level inst. of CPU Tedious & error-prone, so try to avoid it Assembly level programming is necessary where
1. Where absolute speed is critical 2. To access particular feature of the hardware

Better to program in high level language Becoz less tedious, more reliable and productive Most real time embedded programs are mix of C and assembly Maximum routines are in c, minimum routines are in assembly
Software Technology For Embedded Systems

PROGRAMMING IN ASSEMBLY
Each line of source code in assembly language contains a single machine language instruction Each line is divided into four fields L1: MOV converts source code into binary selected element Assembler EAX,[RESULT+2] ; load representation

Common error of assembly program is misunderstood of operand field Resulting value of operand field must be constant If expression contains name, then it refers to address not a content of the variable
Software Technology For Embedded Systems

PROGRAMMING IN ASSEMBLY
In NASM expression enclosed in bracket means operand is content of memory location whose address given by expression If bracket are removed the operand is address itself

Xyzzy:

ORG 1234h DW 5678h .. MOV AX,[xyzzy] .. MOV AX, xyzzy

; The address of this word is 1234h ; Loads 5678h into register AX ; Loads 1234 into register AX

Software Technology For Embedded Systems

PROGRAMMING IN ASSEMBLY
Translation of assembly language source code into binary object code is two-step process First step # Assembler builds symbol table
# It contains information about programmer defined identifiers

# Such as labels, variable names, address of the object, size of the object

Second step # Assembler use this information to construct the representation of the
individual instructions

Software Technology For Embedded Systems

PROGRAMMING IN ASSEMBLY

TWO PASSES OF A ASSEMBLER

Software Technology For Embedded Systems

REGISTER USAGE CONVENTIONS


Function written in assembly and called from C program must follow these conventions

Software Technology For Embedded Systems

TYPICAL USE OF ADDRESSING OPTIONS


Accessing data in memory because of two situations

First The address of the data is constant


That may be specified by using the identifier that appears in the label field where the data is defined in the program

Statically allotted and declared as global or locally

Second The address of the data is variable

That may be determined at execution time and loaded into the register
Accessed through any pointer or non-statically declared variables only inside a function
Software Technology For Embedded Systems

TYPICAL USE OF ADDRESSING OPTIONS


ACCESSING DATA WHOSE ADDRESS IS A CONSTANT If the object is scalar like int, char or long then the address expression consist of only a displacement

If the statically allotted object is struct or union not only is its address a constant but also the relative offset of each of its members

Software Technology For Embedded Systems

TYPICAL USE OF ADDRESSING OPTIONS


ACCESSING DATA WHOSE ADDRESS IS A CONSTANT When you make a subscripted reference to a statically allotted array, the displacement holds the address of the array, an index register is loaded with subscript and scale factor component of the address expression is set to be number of bytes per array element

Software Technology For Embedded Systems

10

TYPICAL USE OF ADDRESSING OPTIONS


ACCESSING DATA WHOSE ADDRESS IS A VARIABLE The base component of a address expression is provided by register and may be used to hold an address that is computed during program execution

Using a pointer to access a member element of a struct or union, we must add the relative offset of the member to the value of the pointer

Software Technology For Embedded Systems

11

TYPICAL USE OF ADDRESSING OPTIONS


ACCESSING DATA WHOSE ADDRESS IS A VARIABLE Pointer can be subscripted just as if it is were the name of the array The difference is that the base address of the array is now variable rather than a constant requiring that it be loaded into a base register and the subscript into an index register

Software Technology For Embedded Systems

12

INSTRUCTION SEQUENCING
Processor executes a straight line sequence of instruction taken out of successive memory locations

But sometime it occasionally depart from its sequence by transferring control to another part of memory
In C, unconditional jumps are created by break, continue, switch, goto statement and end of the loops. During compilation these codes are converted into jump instructions

Software Technology For Embedded Systems

13

INSTRUCTION SEQUENCING

Software Technology For Embedded Systems

14

INSTRUCTION SEQUENCING
Conditional jump is most commonly used immediately after CMP instruction

CMP computes the difference between its two operands and record the characteristics of that difference in flags
The subsequent conditional jump instruction examine the value of flags to determine whether to jump or not

Conditional jump are used to evaluate the boolean expressions that are part of every if, while, do-while and for statement

Software Technology For Embedded Systems

15

INSTRUCTION SEQUENCING
COMPOUND CONDITIONALS Combining two or more instruction in a single boolean expression creates a compound conditionals and little trickier to translate Let us consider a range test in which compare a value against a lower and upper limit:

Easy to convert from C to assembly if the condition is converted into logical OR.
Replace the condition by logical inverse (!) Change the if statement to skip over the assignment when condition is true

Software Technology For Embedded Systems

16

INSTRUCTION SEQUENCING
COMPOUND CONDITIONALS Use Demorgans law to remove the logical OR. To do so,
Replace all logical AND by OR Replace all logical OR by AND Replace all operands by their inverse

Now the expression is OR-ing the result from the two compares

Translated assembly code for range test is:

Software Technology For Embedded Systems

17

INSTRUCTION SEQUENCING
COMPOUND CONDITIONALS Let us consider a different situation, we want to execute assignment when x is outside the range

Eliminate one of the goto by inverting the last comparison

Translated assembly code for range test is:

Software Technology For Embedded Systems

18

INSTRUCTION SEQUENCING
IF-THEN-ELSE STATEMENTS When else part is added then conditional jump is required JMP immediately follows then clause (x=0); in order to skip over the else clause (y=0); so that only one of the two statement will be executed

Software Technology For Embedded Systems

19

INSTRUCTION SEQUENCING
BUILDING LOOPS For loops, while loops and do-while loops are easily constructed by using conditional jump instructions

But some instructions are especially designed for coding loops

Software Technology For Embedded Systems

20

INSTRUCTION SEQUENCING
BUILDING LOOPS Loop instruction combines decrement register ECX as a loop counter with JECXZ conditional jump at the top in case the iteration count is zero

Sometimes loop index is needed that increments from 0 to N-1 in such case use different approach

Software Technology For Embedded Systems

21

INSTRUCTION SEQUENCING
FASTER LOOPS WITH STRING INSTRUCTIONS Some loop operation can be implemented using string instuction for speed. For example string operation can be used
To initialize the region of memory to constant value Scan a region of memory for particular value Copy one region of memory to another Compare the content of two region of memory

These instuction operates on one or two operands in memory known as the source and destination There operands are string of bytes, word, double word During execution ESI holds the offset of the source operand, EDI holds offset of the destination operand
Software Technology For Embedded Systems

22

INSTRUCTION SEQUENCING
FASTER LOOPS WITH STRING INSTRUCTIONS

Software Technology For Embedded Systems

23

INSTRUCTION SEQUENCING
FASTER LOOPS WITH STRING INSTRUCTIONS Each string instruction cause either ESI OR EDI to be adjusted by number of bytes contained in the operands If direction flag DF is zero, these registers are automatically incremented on each repetition of instructions by the number of bytes processed If DF value set to 1, the registers are decremented

Software Technology For Embedded Systems

24

INSTRUCTION SEQUENCING
FASTER LOOPS WITH STRING INSTRUCTIONS String instructions are so useful when they are proceeded by prefix byte thus provides high speed alternative to the loops

It is important to insert a JECXZ instruction after loading register ECX, to bypass string sequence in case the repetitions count happens to be zero

Software Technology For Embedded Systems

25

PROCEDURE CALL & RETURN


Two intel instruction used to call & return from procedures

When function with no parameter value and no return value is called in C, the compiler generates a single CALL instruction Example of such a function disables the interrupt system as shown below

Similarly for enabling interrupt simply replace CLI by STI instruction Software Technology For Embedded Systems

26

PROCEDURE CALL & RETURN


A function with no parameter that returns 8-bit byte containing the current value of the LPT1 printer status port of the IBM pc Here the compiler generates a MOV instruction to save the result returned from the CALL in the variable name status

Software Technology For Embedded Systems

27

PARAMETER PASSING
Parameter are passed to function in C by pushing them onto stack DJGPP pushes the parameter from right to left It is not same for all compilers, some compiler push parameters in a left to right order To make the stack balanced, parameters are removed from the stack

Software Technology For Embedded Systems

28

PARAMETER PASSING

POP instruction can be used for removing data from stack (here 2 pop inst. needed)

If contents are not important we can use ADD ESP, 8 instruction to remove data

0x4087 0x4088 0x4089 0x4090 0x4091 0x4092 0x4093 0x4094 0x4095 0x4096

C B 3 0

byte 1 byte 2 byte 3 byte 4 byte 1 byte 2 byte 3 byte 4

Software Technology For Embedded Systems

29

PARAMETER PASSING
Pointers & integers upto 32-bit are passed as parameter by pushing them as a single 32-bit double word 64-bit integers are pushed as two 32-bit double words & MSB first When an 8-bit or 16-bit passed to a function it must first converted into 32-bit representation If it is a signed or negative the extended bits are set to 1 otherwise this bit is set to zero When a 64-bit is passed to a function it must be pushed into two 32-bit words

Software Technology For Embedded Systems

30

PARAMETER PASSING

Software Technology For Embedded Systems

31

RETRIEVING PARAMETER
As item are pushed onto the stack the stack grows downwards in memory towards location with numerically smaller address Consider previous example

After executing CALL instruction we can use either POP or ADD ESP, 8 instruction to remove data from stack Instead of that if RET instruction is executed at the end of the function, it expect return address to be placed on the top of the stack i.e. next data to be removed
Software Technology For Embedded Systems

32

RETRIEVING PARAMETER
How to access the parameters without removing them from stack? Parameters could be accessed relative to the current address in a stack pointer ESP Consider previous example

Most complex function requires additional registers EBX, ESI, EDI, EBP, DS, ES, SS
If any function modifies one of the value of these registers its content is preserved in stack & later restored
Software Technology For Embedded Systems

33

RETRIEVING PARAMETER
The function must be subtracted a constant from ESP to allocate a memory for a any temporary variable it needs Alternative approach is to copy ESP into another register that doesnt have to change during execution of the function

The obvious choice is EBP, but EBP is one of those whose function must be preserved

Software Technology For Embedded Systems

34

EVERYTHING IS PASS BY VALUE


In C all function parameters are passed by value To achieve equivalent of pass-byreference, declare the functions formal parameter as :pointer to and use address of operator in the call Then the function can modify an object defined in the calling context via an indirect reference using the pointer For example a function to exchange the content of two integers

Software Technology For Embedded Systems

35

TEMPORARY VARIABLES
Two good reasons why it is important to use stack space for temporary variable instead of fixed locations:
Memory conservation reentrancy

To protect the temporary variables, we assign a unused space of stack and we must adjust the stack pointer Consider the code for non-optimizing compiler swap function Here the stack space for the temporary int required by the function is allotted by simply subtracting its size from stack pointer, which prevents interrupt routine from over writing this part of the stack

Software Technology For Embedded Systems

36

TEMPORARY VARIABLES

Software Technology For Embedded Systems

37

TEMPORARY VARIABLES

Just before returning from swap function temporary is released by simply restoring previous value of the stack pointer But ESP happens to be same value that ESP had before the allocation, so a MOV instruction can be used to copy that value back into ESP
Software Technology For Embedded Systems

38

TEMPORARY VARIABLES
The ENTER instruction can be used to replace the first three instruction at the entry to a function and LEAVE instruction replace the just two before the RET.

Software Technology For Embedded Systems

39

TEMPORARY VARIABLES
The two operands of the ENTER constants instruction must be immediate

First is a 16-bit constant containing number of bytes stack space needed for temporary variable
Second is an 8-bit constant used with high level language that support nested procedure For writing assembly programming keep second operand as zero The LEAVE instruction has no operands ENTER instruction requires 4bytes of code space and LEAVE instruction requires 1-byte of code space
Software Technology For Embedded Systems

40

TEMPORARY VARIABLES
Further optimized swap routine shown below

XCHG instruction is used to eliminate a MOV instruction and preloaded pointers p1,p2 rather than loading each time
Stack pointer (ESP) is directly used to access function parameter rather than copying ESP into EBP and using latter This technique is appropriate only when function can be return without using an instruction that modifies ESP
Software Technology For Embedded Systems

41

Das könnte Ihnen auch gefallen