Вы находитесь на странице: 1из 32

UNIT – II

MIXING C AND ASSEMBLY

UNIT II - C AND ASSEMBLY


The Four Fields of a Line of
Code in Assembly Language
Operation
Field

 
L1: MOV EAX,[RESULT+2] ; load selected table
element
 
 
Label Operand Comment
  Field Fields Field

UNIT II - C AND ASSEMBLY


Use 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
UNIT II - C AND ASSEMBLY
Two Passes of an Assembler
... ... ...
MOV AL,[X+2]
1B27 A0 &x+2 1B27 A0 3F3C
... ... ...
Assembler Pass 1

Assembler Pass 2
X DB 5,7,3 3F3A 05 07 … 3F3A 05 07 …
... ... ...

Symbol Table

X 3F3A

UNIT II - C AND ASSEMBLY


Instruction Sequencing

UNIT II - C AND ASSEMBLY


Code 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: ...
 

UNIT II - C AND ASSEMBLY


Commonly-Used Conditional
Jump Instructions
Compar Mnemonic(s
Jump if . . . Determined by . . .
e )
JE (JZ) Equal (Zero) ZF==1
equality
JNE (JNZ) Not Equal (Not Zero) ZF==0
JB (JNAE) Below (Not Above or Equal) CF==1
unsigne JBE (JNA) Below or Equal (Not Above) CF==1 || ZF==1
d JAE (JNB) Above or Equal (Not Below) CF==0
JA (JNBE) Above (Not Below or Equal) CF==0 && ZF==0
Less than (Not Greater than or
JL (JNGE) SF!=OF
Equal)
Less than or Equal (Not Greater
JLE (JNG) SF!=OF || ZF==1
signed than)
Greater than or Equal (Not Less
JGE (JNL) SF==OF
than)
Greater than (Not Less than or SF==OF &&
JG (JNLE)
Equal) ZF==0

UNIT II - C AND ASSEMBLY


Conditional Jump Preceded by a
CMP Instruction

 
while (x < 1000) top_of_while: CMP DWORD [x],1000
{ JNL end_of_while
... ...
} JMP top_of_while
end_of_while:
 

UNIT II - C AND ASSEMBLY


Compound Conditionals
if (lower_limit <= x && x <= upper_limit) y = x ;

if (!(lower_limit <= x && x <= upper_limit)) goto


L1
y=x;
L1:
if (x < lower_limit || x > upper_limit) goto L1
y=x;
L1: MOV EAX,[x]
CMP EAX,
if (x < lower_limit) goto L1[lower_limit] JL
if (x > upper_limit) goto L1 L1
CMP EAX,
y =x; [upper_limit] JG
L1: L1
UNIT II - C AND ASSEMBLY
MOV [y],EAX
Compound Conditionals
if (x < lower_limit || upper_limit < x) y = x ;

if (x < lower_limit) goto MOV EAX,[x]


L1 CMP EAX,
[lower_limit]
if (x > upper_limit) goto JL L1
L1 CMP EAX,
goto L2 ; [upper_limit]
L1: y = x ; JNG L2
L2: L1: MOV [y],EAX
if (x < lower_limit) goto L1 L2: ...
if (!(x > upper_limit)) goto L2
L1: y = x ;
L2:
UNIT II - C AND ASSEMBLY
If-Then-Else Statements

  if (x > y) MOV EAX,[x] ;x>y?


{ CMP EAX,[y]
x=0; JNG L1
} MOV DWORD [x],0 ; then: x = 0 ;
else JMP L2 ; skip over
else
{ L1: MOV DWORD [y],0 ; else: y = 0 ;
y=0; L2: ...
}

UNIT II - C AND ASSEMBLY


Building a Loop With the JECXZ
and LOOP Instructions
MOV ECX,[iteration_count]
JECXZ loop_exit ; jump if ECX is zero.
top_of_loop:
...
<Register ECX: N, N-1, ... 1>
...
LOOP top_of_loop ; decrement ECX & jump if
NZ
loop_exit:

UNIT II - C AND ASSEMBLY


Building a Loop With an
Increasing Loop Index
XOR ECX,ECX ; Set ECX to 0
top_of_loop:
...
<Register ECX: 0, 1, ... N-1>
...
INC ECX ; Add 1 to ECX
CMP ECX,[iteration_count] ; ECX < count?
JB top_of_loop ; Stop if not.

UNIT II - C AND ASSEMBLY


Application of the Repeated
String Instructions
Initialize Compare
Scan Memory Copy Memory
Memory Memory
       
MOV ECX, MOV ECX, MOV ECX, MOV ECX,
[bytes] [bytes] [bytes] [bytes]
MOV AL,[value] MOV AL,[value] MOV ESI, MOV ESI,[sadrs]
MOV EDI, MOV EDI, [sadrs] MOV EDI,
[dadrs] [dadrs] MOV EDI, [dadrs]
CLD CLD [dadrs] CLD
REP STOSB REP SCASB CLD REP CMPSB
JE found REP MOVSB JE identical

UNIT II - C AND ASSEMBLY


Interfacing to C

UNIT II - C AND ASSEMBLY


Register Usage Conventions
Register(s) Usage in C functions

Functions return all pointers and integer values up to 32‑bits


EAX
in this register.

EDX and Functions return 64‑bit values (long long ints) in this register
EAX pair. (Note: EDX holds bits 63-32, EAX holds bits 31-0).
Used to access: (1) The arguments that were passed to a
EBP function when it was called, and (2) any automatic variables
allocated by the function.
EBX, ESI, These registers must be preserved by functions written in
EDI, EBP, assembly language. Any of these registers that the function
DS, ES, modifies should be pushed on entry to the function and
and SS. popped on exit.
EAX, ECX,
"Scratch" registers. These registers may be used without
EDX, FS
preserving their current content.
and GS
DS, ES, Used to reference data. If modified by a function, the current
SS, EBP, contents of these
UNIT IIregisters must be preserved on entry and
- C AND ASSEMBLY
and ESP restored on return.
Function 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.

UNIT II - C AND ASSEMBLY


No Parameters and No Return Value.

C
void Disable_Ints(void) ;
prototype:
Example
Disable_Ints() ;
usage:
Generated
CALL _Disable_Ints
code:
NASM
_Disable_Ints:
source
CLI ; Disables interrupt system
code for
RET ; Return from function
the
function:

UNIT II - C AND ASSEMBLY


No Parameters and 8-bit Return Value.
C prototype: BYTE8 LPT1_Status(void) ;
Example
status = LPT1_Status() ;
usage:
Generated CALL _LPT1_Status ; returns status in EAX
code: MOV [_status],AL

_LPT1_Status:
MOV DX,03BDh ; Load DX w/hex I/O adr
NASM
XOR EAX,EAX ; Pre-Zero rest of EAX
source code
IN AL,DX ; Get status byte from
for the
port.
function:
RET ; Return from function.

UNIT II - C AND ASSEMBLY


Parameter 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.

UNIT II - C AND ASSEMBLY


Passing Parameters to a C Function
Function call
w/parameters: Byte2Port(0x3BC, data) ;

 
PUSH DWORD [_data] ; Push 2nd
param
MOV EAX,03BCh ; Push 1st param
Code generated by PUSH EAX
the compiler: CALL _Byte2Port ; Call the
function.
ADD ESP,8 ; Remove params

UNIT II - C AND ASSEMBLY


Passing an 8‑bit Unsigned
Integer
C Assembly

   
unsigned char data ; MOVZX EAX,[_data]
... PUSH EAX
Do_Something(data) ; CALL _Do_Something
... ADD ESP,4 

UNIT II - C AND ASSEMBLY


Passing an 8‑bit Signed Integer
C Assembly

   
signed char data ; MOVSX EAX,[_data]
... PUSH EAX
Do_Something(data) ; CALL _Do_Something
... ADD ESP,4 

UNIT II - C AND ASSEMBLY


Passing a 64‑bit Integer

C Assembly

   
/* signed or unsigned */ PUSH DWORD [_data+4]
long long data ; PUSH DWORD [_data]
... CALL _Do_Something
Do_Something(data) ; ADD ESP,8 
...

UNIT II - C AND ASSEMBLY


Retrieving 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


Address Contents Description
[ESP+8] _data The 2nd function parameter (data to write to I/O port)
[ESP+4] 03BCh The 1st function parameter (an I/O port address)
Return
[ESP] Pushed onto stack by the CALL instruction
Address

UNIT II - C AND ASSEMBLY


Retrieving Parameters
• Can’t 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!
UNIT II - C AND ASSEMBLY
Retrieving Parameters
_Byte2Port:
MOV DX,[ESP+4] ; Copy 1st parameter to DX (the I/O port
adrs).
MOV AL,[ESP+8] ; 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,[EBP+8]; Copy 1st parameter to DX (the I/O port
address)
MOV AL,[EBP+12] ; Copy 2nd parameter to AL (discard bits
15-8)
OUT DX,AL ; Write
UNIT IIthe data
- C AND to the I/O port
ASSEMBLY
POP EBP ; Restore old contents of BP from stack
Everything is Pass By Value
Function definition Function invocation

void Swap(int *p1, int *p2) int x = 4 ;


{ int y = 7 ;
int temp = *p1 ; …
*p1 = *p2 ; Swap(&x, &y) ;
*p2 = temp ; …
}

Emulating pass-by-reference in C

UNIT II - C AND ASSEMBLY


Temporary 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).

UNIT II - C AND ASSEMBLY


_Swap: PUSH EBP ; Preserve original EBP contents
MOV EBP,ESP ; Establish stack frame reference in EBP
SUB ESP,4 ; Allocate temporary in automatic memory
•••
Content
Address Description
s
••• Stack space currently in use by calling context.
[EBP+12] p2
Function parameters pushed on the stack by the caller.
[EBP+8] p1
Return
[EBP+4] Return address pushed by the CALL and popped by the RET.
address
original Original EBP preserved by PUSH EBP and restored by POP
[EBP]
EBP EBP.
Temporary int with automatic memory allocation. (Top of
[EBP-4] temp
stack)
• • • • • • Unused stack space (Interrupts push return address here)
MOV ESP,EBP ; Release the temporary automatic int
POP EBP ; Restore original EBP
RET ; Return from this function
UNIT II - C AND ASSEMBLY
_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,[EBP+8] ; 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,[EBP+12] ; *p1 = *p2: (1) Get 2nd parameter (p2)


MOV EAX,[ECX] ; (2) Use it to get *p2 into EAX
MOV ECX,[EBP+8] ; (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,[EBP+12] ; (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
UNIT II - C AND ASSEMBLY
Optimized Implementation of the
Swap Function in Assembly
_Swap:
MOV ECX,[ESP+4] ; Copy parameter p1 to ECX
MOV EDX,[ESP+8] ; 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

UNIT II - C AND ASSEMBLY

Вам также может понравиться