c and assembly
Post on 06-Apr-2018
231 Views
Preview:
TRANSCRIPT
8/3/2019 c and Assembly
http://slidepdf.com/reader/full/c-and-assembly 1/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
1Software Technology For Embedded Systems
8/3/2019 c and Assembly
http://slidepdf.com/reader/full/c-and-assembly 2/41
INTRO
Assembly language means, programming with low level inst. of CPU
Tedious & error-prone, so try to avoid it
Assembly level programming is necessary where1. 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
2Software Technology For Embedded Systems
8/3/2019 c and Assembly
http://slidepdf.com/reader/full/c-and-assembly 3/41
PROGRAMMING IN ASSEMBLY
Each line of source code in assembly language contains a singlemachine language instruction
Each line is divided into four fields
Assembler converts source code into binary 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
L1: MOV EAX,[RESULT+2] ; load selected element
3Software Technology For Embedded Systems
8/3/2019 c and Assembly
http://slidepdf.com/reader/full/c-and-assembly 4/41
In NASM expression enclosed in bracket means operand is contentof memory location whose address given by expression
If bracket are removed the operand is address itself
ORG 1234h
Xyzzy: DW 5678h ; The address of this word is 1234h
………..
MOV AX,[xyzzy] ; Loads 5678h into register AX
………..
MOV AX, xyzzy ; Loads 1234 into register AX
PROGRAMMING IN ASSEMBLY
4Software Technology For Embedded Systems
8/3/2019 c and Assembly
http://slidepdf.com/reader/full/c-and-assembly 5/41
• 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
PROGRAMMING IN ASSEMBLY
5Software Technology For Embedded Systems
8/3/2019 c and Assembly
http://slidepdf.com/reader/full/c-and-assembly 6/41
TWO PASSES OF A ASSEMBLER
PROGRAMMING IN ASSEMBLY
6Software Technology For Embedded Systems
8/3/2019 c and Assembly
http://slidepdf.com/reader/full/c-and-assembly 7/41
Function written in assembly and called from “C” program must
follow these conventions
REGISTER USAGE CONVENTIONS
7Software Technology For Embedded Systems
8/3/2019 c and Assembly
http://slidepdf.com/reader/full/c-and-assembly 8/41
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
TYPICAL USE OF ADDRESSING OPTIONS
8Software Technology For Embedded Systems
8/3/2019 c and Assembly
http://slidepdf.com/reader/full/c-and-assembly 9/41
ACCESSING DATA WHOSE ADDRESS IS A CONSTANT
If the object is scalar like int, char or long then the addressexpression 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
TYPICAL USE OF ADDRESSING OPTIONS
9Software Technology For Embedded Systems
8/3/2019 c and Assembly
http://slidepdf.com/reader/full/c-and-assembly 10/41
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 isloaded with subscript and scale factor component of the addressexpression is set to be number of bytes per array element
TYPICAL USE OF ADDRESSING OPTIONS
10Software Technology For Embedded Systems
8/3/2019 c and Assembly
http://slidepdf.com/reader/full/c-and-assembly 11/41
ACCESSING DATA WHOSE ADDRESS IS A VARIABLE
The base component of a address expression is provided by registerand may be used to hold an address that is computed duringprogram 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 thepointer
TYPICAL USE OF ADDRESSING OPTIONS
11Software Technology For Embedded Systems
8/3/2019 c and Assembly
http://slidepdf.com/reader/full/c-and-assembly 12/41
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 registerand the subscript into an index register
TYPICAL USE OF ADDRESSING OPTIONS
12Software Technology For Embedded Systems
8/3/2019 c and Assembly
http://slidepdf.com/reader/full/c-and-assembly 13/41
Processor executes a straight line sequence of instruction taken out
of successive memory locations
But sometime it occasionally depart from its sequence bytransferring 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
INSTRUCTION SEQUENCING
13Software Technology For Embedded Systems
8/3/2019 c and Assembly
http://slidepdf.com/reader/full/c-and-assembly 14/41
INSTRUCTION SEQUENCING
14Software Technology For Embedded Systems
8/3/2019 c and Assembly
http://slidepdf.com/reader/full/c-and-assembly 15/41
Conditional jump is most commonly used immediately after CMP
instruction
CMP computes the difference between its two operands and recordthe characteristics of that difference in flags
The subsequent conditional jump instruction examine the value offlags to determine whether to jump or not
Conditional jump are used to evaluate the boolean expressions thatare part of every if, while, do-while and for statement
INSTRUCTION SEQUENCING
15Software Technology For Embedded Systems
8/3/2019 c and Assembly
http://slidepdf.com/reader/full/c-and-assembly 16/41
COMPOUND CONDITIONALS
Combining two or more instruction in a single boolean expressioncreates a compound conditionals and little trickier to translate
Let us consider a range test in which compare a value against alower and upper limit:
Easy to convert from C to assembly if the condition is converted intological OR.
Replace the condition by logical inverse (!)
Change the if statement to skip over the assignment when condition is true
INSTRUCTION SEQUENCING
16Software Technology For Embedded Systems
8/3/2019 c and Assembly
http://slidepdf.com/reader/full/c-and-assembly 17/41
COMPOUND CONDITIONALS
Use Demorgan’s 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:
INSTRUCTION SEQUENCING
17Software Technology For Embedded Systems
8/3/2019 c and Assembly
http://slidepdf.com/reader/full/c-and-assembly 18/41
COMPOUND CONDITIONALS
Let us consider a different situation, we want to execute assignmentwhen “x” is outside the range
Eliminate one of the goto by inverting the last comparison
Translated assembly code for range test is:
INSTRUCTION SEQUENCING
18Software Technology For Embedded Systems
8/3/2019 c and Assembly
http://slidepdf.com/reader/full/c-and-assembly 19/41
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 theelse clause (y=0); so that only one of the two statement will beexecuted
INSTRUCTION SEQUENCING
19Software Technology For Embedded Systems
8/3/2019 c and Assembly
http://slidepdf.com/reader/full/c-and-assembly 20/41
BUILDING LOOPS
For loops, while loops and do-while loops are easily constructed byusing conditional jump instructions
But some instructions are especially designed for coding loops
INSTRUCTION SEQUENCING
20Software Technology For Embedded Systems
8/3/2019 c and Assembly
http://slidepdf.com/reader/full/c-and-assembly 21/41
BUILDING LOOPS
Loop instruction combines decrement register ECX as a loop counterwith JECXZ conditional jump at the top in case the iteration count iszero
Sometimes loop index is needed that increments from 0 to N-1 in suchcase use different approach
INSTRUCTION SEQUENCING
21Software Technology For Embedded Systems
8/3/2019 c and Assembly
http://slidepdf.com/reader/full/c-and-assembly 22/41
FASTER LOOPS WITH STRING INSTRUCTIONS
Some loop operation can be implemented using string instuction forspeed. 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 knownas the source and destination
There operands are string of bytes, word, double word
During execution ESI holds the offset of the source operand, EDI holdsoffset of the destination operand
INSTRUCTION SEQUENCING
22Software Technology For Embedded Systems
8/3/2019 c and Assembly
http://slidepdf.com/reader/full/c-and-assembly 23/41
FASTER LOOPS WITH STRING INSTRUCTIONS
INSTRUCTION SEQUENCING
23Software Technology For Embedded Systems
8/3/2019 c and Assembly
http://slidepdf.com/reader/full/c-and-assembly 24/41
FASTER LOOPS WITH STRING INSTRUCTIONS
Each string instruction cause either ESI OR EDI to be adjusted bynumber 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 bytesprocessed
If DF value set to “1”, the registers are decremented
INSTRUCTION SEQUENCING
24Software Technology For Embedded Systems
8/3/2019 c and Assembly
http://slidepdf.com/reader/full/c-and-assembly 25/41
FASTER LOOPS WITH STRING INSTRUCTIONS
String instructions are so useful when they are proceeded by “prefixbyte” 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 bezero
INSTRUCTION SEQUENCING
25Software Technology For Embedded Systems
8/3/2019 c and Assembly
http://slidepdf.com/reader/full/c-and-assembly 26/41
Two intel instruction used to call & return from procedures
When function with no parameter value and no return value is called inC, the compiler generates a single CALL instruction
Example of such a function disables the interrupt system as shownbelow
Similarly for enabling interrupt simply replace CLI by STI instruction
PROCEDURE CALL & RETURN
26Software Technology For Embedded Systems
8/3/2019 c and Assembly
http://slidepdf.com/reader/full/c-and-assembly 27/41
A function with no parameter that returns 8-bit byte containing thecurrent value of the LPT1 printer status port of the IBM pc
Here the compiler generates a MOV instruction to save the resultreturned from the CALL in the variable name “status”
PROCEDURE CALL & RETURN
27Software Technology For Embedded Systems
8/3/2019 c and Assembly
http://slidepdf.com/reader/full/c-and-assembly 28/41
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 leftto right order
To make the stack balanced, parameters are removed from the stack
PARAMETER PASSING
28Software Technology For Embedded Systems
8/3/2019 c and Assembly
http://slidepdf.com/reader/full/c-and-assembly 29/41
0x4087
0x4088
0x4089
0x4090
0x4091
0x4092
0x4093
0x4094
0x4095
0x4096 byte 4
byte 3
byte 2
byte 1
CB30 byte 4
byte 3
byte 2
byte 1
PARAMETER PASSING
POP instruction can be used for removingdata from stack (here 2 pop inst. needed)
If contents are not important we can useADD ESP, 8 instruction to remove data
29Software Technology For Embedded Systems
8/3/2019 c and Assembly
http://slidepdf.com/reader/full/c-and-assembly 30/41
Pointers & integers upto 32-bit are passed as parameter by pushingthem 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 into32-bit representation
If it is a signed or negative the extended bits are set to 1 otherwise thisbit is set to zero
When a 64-bit is passed to a function it must be pushed into two 32-bitwords
PARAMETER PASSING
30Software Technology For Embedded Systems
8/3/2019 c and Assembly
http://slidepdf.com/reader/full/c-and-assembly 31/41
PARAMETER PASSING
31Software Technology For Embedded Systems
8/3/2019 c and Assembly
http://slidepdf.com/reader/full/c-and-assembly 32/41
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. nextdata to be removed
RETRIEVING PARAMETER
32Software Technology For Embedded Systems
8/3/2019 c and Assembly
http://slidepdf.com/reader/full/c-and-assembly 33/41
How to access the parameters without removing them from stack?
Parameters could be accessed relative to the current address in astack 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
RETRIEVING PARAMETER
33Software Technology For Embedded Systems
8/3/2019 c and Assembly
http://slidepdf.com/reader/full/c-and-assembly 34/41
The function must be subtracted a constant from ESP to allocate amemory for a any temporary variable it needs
Alternative approach is to copy ESP into another register that doesn’t have to change during execution of the function
The obvious choice is EBP, but EBP is one of those whose functionmust be preserved
RETRIEVING PARAMETER
34Software Technology For Embedded Systems
8/3/2019 c and Assembly
http://slidepdf.com/reader/full/c-and-assembly 35/41
In C all function parameters are passed by value
To achieve equivalent of pass-by –reference, declare the functionsformal parameter as :pointer to” and use “address of” operator in thecall
Then the function can modify an object defined in the calling contextvia an indirect reference using the pointer
For example a function to exchange the content of two integers
EVERYTHING IS PASS BY VALUE
35Software Technology For Embedded Systems
8/3/2019 c and Assembly
http://slidepdf.com/reader/full/c-and-assembly 36/41
Two good reasons why it is important to use stack space for temporaryvariable instead of fixed locations: Memory conservation
reentrancy
To protect the temporary variables, we assign a unused space of stackand 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 isallotted by simply subtracting its size from stack pointer, whichprevents interrupt routine from over writing this part of the stack
TEMPORARY VARIABLES
36Software Technology For Embedded Systems
8/3/2019 c and Assembly
http://slidepdf.com/reader/full/c-and-assembly 37/41
TEMPORARY VARIABLES
37Software Technology For Embedded Systems
8/3/2019 c and Assembly
http://slidepdf.com/reader/full/c-and-assembly 38/41
TEMPORARY VARIABLES
38Software Technology For Embedded Systems
Just before returning from swap function temporary is released bysimply 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
8/3/2019 c and Assembly
http://slidepdf.com/reader/full/c-and-assembly 39/41
The ENTER instruction can be used to replace the first threeinstruction at the entry to a function and LEAVE instruction replace the
just two before the RET.
TEMPORARY VARIABLES
39Software Technology For Embedded Systems
8/3/2019 c and Assembly
http://slidepdf.com/reader/full/c-and-assembly 40/41
The two operands of the ENTER instruction must be immediateconstants
First is a 16-bit constant containing number of bytes stack spaceneeded for temporary variable
Second is an 8-bit constant used with high level language that supportnested procedure
For writing assembly programming keep second operand as zero
The LEAVE instruction has no operands
ENTER instruction requires 4bytes of code space and LEAVEinstruction requires 1-byte of code space
TEMPORARY VARIABLES
40Software Technology For Embedded Systems
8/3/2019 c and Assembly
http://slidepdf.com/reader/full/c-and-assembly 41/41
Further optimized swap routine shown below
XCHG instruction is used to eliminate a MOV instruction and preloadedpointers p1,p2 rather than loading each time
Stack pointer (ESP) is directly used to access function parameterrather than copying ESP into EBP and using latter
This technique is appropriate only when function can be return withoutusing an instruction that modifies ESP
TEMPORARY VARIABLES
top related