Title: CHAPTER 5 MIXING C AND ASSEMBLY
1CHAPTER 5MIXING C AND ASSEMBLY
2The Four Fields of a Line of Code in Assembly
Language
Operation Field
Â
 L1 MOV EAX,RESULT2 load selected table
element   Â
Label Field
Operand Fields
Comment Field
3Use of in NASM Assembler
ORG 1234h xyzzy DD 5678h the address of this
word is 1234 (hex) ... MOV EAX,xyzzy loads
5678 (hex) into register EAX MOV EAX,xyzzy
loads 1234 (hex) into register EAX
4Two Passes of an Assembler
... ... ...
... MOV AL,X2 ... X DB 5,7,3 ...
Assembler Pass 1
Assembler Pass 2
... ... ...
A0
x2
1B27
1B27
3F3C
A0
05
07
05
07
3F3A
3F3A
5Instruction Sequencing
6Code Generated by Compiler for Break and End of
Loop
 for () top_of_for ... ... ... if
(...) break JMP end_of_for ...
... JMP top_of_for end_of_for ... Â
7Commonly-Used Conditional Jump Instructions
8Conditional Jump Preceded by a CMP Instruction
 while (x lt 1000) top_of_while CMP DWORD
x,1000 JNL end_of_while ... ...
JMP top_of_while end_of_while Â
9Compound Conditionals
if (lower_limit lt x x lt upper_limit) y x
if (!(lower_limit lt x x lt upper_limit))
goto L1 y x L1
if (x lt lower_limit x gt upper_limit) goto
L1 y x L1
MOV EAX,x CMP EAX,lower_limit
JL L1 CMP EAX,upper_limit JG L1 MOV y,EAX
L1 ...
if (x lt lower_limit) goto L1 if (x gt
upper_limit) goto L1 y x L1
10Compound Conditionals
if (x lt lower_limit upper_limit lt x) y x
if (x lt lower_limit) goto L1 if (x gt
upper_limit) goto L1 goto L2 L1 y x L2
MOV EAX,x CMP EAX,lower_limit
JL L1 CMP EAX,upper_limit JNG L2 L1 MOV y
,EAX L2 ...
if (x lt lower_limit) goto L1 if (!(x gt
upper_limit)) goto L2 L1 y x L2
11If-Then-Else Statements
 if (x gt y) MOV EAX,x x gt y
? CMP EAX,y x 0 JNG L1 MOV DWOR
D x,0 then x 0 else JMP L2 skip
over else L1 MOV DWORD y,0 else y 0
y 0 L2 ...
12Building a Loop With the JECXZ and LOOP
Instructions
MOV ECX,iteration_count JECXZ loop_exit
jump if ECX is zero. top_of_loop ... ltRegister
ECX N, N-1, ... 1gt ... LOOP top_of_loop
decrement ECX jump if NZ loop_exit
13Building a Loop With an Increasing Loop Index
XOR ECX,ECX Set ECX to 0 top_of_loop ...
ltRegister ECX 0, 1, ... N-1gt ... INC ECX
Add 1 to ECX CMP ECX,iteration_count ECX
lt count? JB top_of_loop Stop if not.
14Application of the Repeated String Instructions
15Interfacing to C
16Register Usage Conventions
17Function Call and Return
- CALL instruction used by caller to invoke the
function - Pushes the return address onto the stack.
- RET instruction used in function to return to
caller. - Pops the return address off the stack.
18No Parameters and No Return Value.
19No Parameters and 8-bit Return Value.
20Parameter Passing
- Parameters are pushed onto stack prior to CALL.
- gcc pushes parameters in reverse order.
- 8/16-bit parameters are extended to 32-bits
- Caller removes parameters after function returns.
21Passing Parameters to a C Function
22Passing an 8-bit Unsigned Integer
23Passing an 8-bit Signed Integer
24Passing a 64-bit Integer
25Retrieving Parameters
PUSH DWORD _data Push 2nd parameter
MOV EAX,03BCh Push 1st parameter PUSH EAX
onto the stack. CALL _Byte2Port Call the
function
Stack immediately after the CALL
26Retrieving Parameters
- Cant use POP instructions to access parameters.
- Parameters expect to be removed from the stack
later by the caller. - RET instruction expects return address to be on
top of the stack. - Need a way to access parameters without actually
removing them from the stack!
27Retrieving Parameters
_Byte2Port MOV DX,ESP4 Copy 1st parameter
to DX (the I/O port adrs). MOV AL,ESP8 Copy
2nd parameter to AL (discard bits
31-8). OUT DX,AL Write the data to the I/O
port. RET Return to caller.
_Byte2Port PUSH EBP Preserve current contents
of BP on stack MOV EBP,ESP Establish a
reference point in the stack MOV DX,EBP8
Copy 1st parameter to DX (the I/O port
address) MOV AL,EBP12 Copy 2nd parameter to
AL (discard bits 15-8) OUT DX,AL Write the
data to the I/O port POP EBP Restore old
contents of BP from stack RET Return to caller
28Everything is Pass By Value
Emulating pass-by-reference in C
29Temporary Variables
- Use automatic allocation
- Temporaries rarely need persistence
- Allocate temporaries on the stack
- Guarantees that function is reentrant
- Only available space is beyond top of stack.
- Must be allocated before it can be used (stack
pointer must be adjusted and later restored when
temporaries are no longer needed).
30_Swap PUSH EBP Preserve original EBP
contents MOV EBP,ESP Establish stack frame
reference in EBP SUB ESP,4 Allocate temporary
in automatic memory
MOV ESP,EBP Release the temporary automatic
int POP EBP Restore original EBP RET
Return from this function
31_Swap PUSH EBP Preserve original EBP
contents MOV EBP,ESP Establish stack frame
reference in EBP SUB ESP,4 Allocate a
temporary in automatic memory MOV ECX,EBP8
temp p1 (1) Get 1st parameter
(p1) MOV EAX,ECX (2) Use it to get p1
into EAX MOV EBP-4,EAX (3) Then store EAX
into temp. MOV ECX,EBP12 p1 p2 (1)
Get 2nd parameter (p2) MOV EAX,ECX (2)
Use it to get p2 into EAX MOV ECX,EBP8
(3) Get 1st parameter (p1) again MOV ECX,EAX
(4) Use it to store EAX into
p1 MOV EAX,EBP-4 p2 temp (1) Get the
temp into EAX MOV ECX,EBP12 (2) Get 2nd
parameter (p2) again MOV ECX,EAX (3) Use
it to store EAX into p2 MOV ESP,EBP Release
the temporary int POP EBP Restore original
EBP RET Return from this function
32Optimized Implementation of the Swap Function in
Assembly
_Swap MOV ECX,ESP4 Copy parameter p1 to
ECX MOV EDX,ESP8 Copy parameter p2 to
EDX MOV EAX,ECX Copy p1 into
EAX XCHG EAX,EDX Exchange EAX with
p2 MOV ECX,EAX Copy EAX into p1 RET
Return from this function