unit - 2 c and assembly
TRANSCRIPT
8/8/2019 UNIT - 2 C and Assembly
http://slidepdf.com/reader/full/unit-2-c-and-assembly 1/32
UNIT II - C AND ASSEMBLY
UNIT – II
MIXING C AND ASSEMBLY
8/8/2019 UNIT - 2 C and Assembly
http://slidepdf.com/reader/full/unit-2-c-and-assembly 2/32
UNIT II - C AND ASSEMBLY
L1: MOV EAX,[RESULT+2] ; load selected tableelement
The Four Fields of a Line of
Code in Assembly Language
LabelField
LabelField
OperationField
OperationField
OperandFields
OperandFields
CommentField
CommentField
8/8/2019 UNIT - 2 C and Assembly
http://slidepdf.com/reader/full/unit-2-c-and-assembly 3/32
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
8/8/2019 UNIT - 2 C and Assembly
http://slidepdf.com/reader/full/unit-2-c-and-assembly 4/32
UNIT II - C AND ASSEMBLY
Two Passes of an Assembler
...
...
...
A0
05 07
&x+21B27
3F3A
...
...
...
A0
05 07
1B27
3F3A
3F3C
...
MOV AL,[X+2]
...
X DB 5,7,3
...
A s s em b l er P a s
s 1
A s
s em b l er P a s
s 1
A s s em b l er P a s
s 2
A s
s em b l er P a s
s 2 Symbol Table
3F3AX
… …
8/8/2019 UNIT - 2 C and Assembly
http://slidepdf.com/reader/full/unit-2-c-and-assembly 5/32
UNIT II - C AND ASSEMBLY
Instruction Sequencing
8/8/2019 UNIT - 2 C and Assembly
http://slidepdf.com/reader/full/unit-2-c-and-assembly 6/32
UNIT II - C AND ASSEMBLY
for (;;) top_of_for: ...
{... ...if (...) break ; JMP end_of_for... ...
} JMP top_of_forend_of_for: ...
Code Generated by Compiler for
Break and End of Loop
8/8/2019 UNIT - 2 C and Assembly
http://slidepdf.com/reader/full/unit-2-c-and-assembly 7/32
UNIT II - C AND ASSEMBLY
Commonly-Used Conditional
Jump InstructionsCompar
eMnemonic(s
) Jump if . . . Determined by . . .
equality JE (JZ) Equal (Zero) ZF==1
JNE (JNZ) Not Equal (Not Zero) ZF==0
unsigned
JB (JNAE) Below (Not Above or Equal) CF==1 JBE (JNA) Below or Equal (Not Above) CF==1 || ZF==1
JAE (JNB) Above or Equal (Not Below) CF==0
JA (JNBE) Above (Not Below or Equal) CF==0 && ZF==0
signed
JL (JNGE)Less than (Not Greater than orEqual)
SF!=OF
JLE (JNG) Less than or Equal (Not Greaterthan)
SF!=OF || ZF==1
JGE (JNL)Greater than or Equal (Not Lessthan)
SF==OF
JG (JNLE)Greater than (Not Less than orEqual)
SF==OF &&ZF==0
8/8/2019 UNIT - 2 C and Assembly
http://slidepdf.com/reader/full/unit-2-c-and-assembly 8/32
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:
8/8/2019 UNIT - 2 C and Assembly
http://slidepdf.com/reader/full/unit-2-c-and-assembly 9/32
UNIT II - C AND ASSEMBLY
Compound Conditionals
if (lower_limit <= x && x <= upper_limit) y = x ;
if (x < lower_limit) goto L1if (x > upper_limit) goto L1y = x ;
L1:
if (x < lower_limit || x > upper_limit) goto L1y = x ;
L1:
if (!(lower_limit <= x && x <= upper_limit)) gotoL1
y = x ;L1:
MOV EAX,[x]
CMP EAX,[lower_limit] JLL1CMP EAX,
[upper_limit] JGL1MOV [y],EAX
8/8/2019 UNIT - 2 C and Assembly
http://slidepdf.com/reader/full/unit-2-c-and-assembly 10/32
UNIT II - C AND ASSEMBLY
Compound Conditionals
if (x < lower_limit || upper_limit < x) y = x ;
if (x < lower_limit) gotoL1
if (x > upper_limit) gotoL1
goto L2 ;L1: y = x ;L2:
if (x < lower_limit) goto L1if (!(x > upper_limit)) goto L2
L1: y = x ;L2:
MOV EAX,[x]CMP EAX,
[lower_limit] JL L1CMP EAX,
[upper_limit] JNG L2
L1: MOV [y],EAX
L2: ...
8/8/2019 UNIT - 2 C and Assembly
http://slidepdf.com/reader/full/unit-2-c-and-assembly 11/32
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 overelse
{ L1: MOV DWORD [y],0 ; else: y = 0 ;y = 0 ; L2: ...}
8/8/2019 UNIT - 2 C and Assembly
http://slidepdf.com/reader/full/unit-2-c-and-assembly 12/32
UNIT II - C AND ASSEMBLY
Building a Loop With the JECXZ
and LOOP InstructionsMOV 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
NZloop_exit:
8/8/2019 UNIT - 2 C and Assembly
http://slidepdf.com/reader/full/unit-2-c-and-assembly 13/32
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.
8/8/2019 UNIT - 2 C and Assembly
http://slidepdf.com/reader/full/unit-2-c-and-assembly 14/32
UNIT II - C AND ASSEMBLY
Application of the Repeated
String InstructionsInitializeMemory
Scan Memory Copy MemoryCompareMemory
MOV ECX,[bytes]MOV AL,[value]MOV EDI,[dadrs]
CLDREP STOSB
MOV ECX,[bytes]MOV AL,[value]MOV EDI,[dadrs]
CLDREP SCASB
JE found
MOV ECX,[bytes]MOV ESI,[sadrs]MOV EDI,
[dadrs]CLDREP MOVSB
MOV ECX,[bytes]MOV ESI,[sadrs]MOV EDI,[dadrs]
CLDREP CMPSB
JE identical
8/8/2019 UNIT - 2 C and Assembly
http://slidepdf.com/reader/full/unit-2-c-and-assembly 15/32
UNIT II - C AND ASSEMBLY
Interfacing to C
8/8/2019 UNIT - 2 C and Assembly
http://slidepdf.com/reader/full/unit-2-c-and-assembly 16/32
UNIT II - C AND ASSEMBLY
Register Usage ConventionsRegister(s) Usage in C functions
EAXFunctions return all pointers and integer values up to 32‑bitsin this register.
EDX andEAX
Functions return 64‑bit values (long long ints) in this registerpair. (Note: EDX holds bits 63-32, EAX holds bits 31-0).
EBPUsed to access: (1) The arguments that were passed to afunction when it was called, and (2) any automatic variablesallocated by the function.
EBX, ESI,EDI, EBP,DS, ES,
and SS.
These registers must be preserved by functions written inassembly language. Any of these registers that the functionmodifies should be pushed on entry to the function and
popped on exit.EAX, ECX,EDX, FSand GS
"Scratch" registers. These registers may be used withoutpreserving their current content.
DS, ES,
SS, EBP,and ESP
Used to reference data. If modified by a function, the current
contents of these registers must be preserved on entry andrestored on return.
8/8/2019 UNIT - 2 C and Assembly
http://slidepdf.com/reader/full/unit-2-c-and-assembly 17/32
UNIT II - C AND ASSEMBLY
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.
8/8/2019 UNIT - 2 C and Assembly
http://slidepdf.com/reader/full/unit-2-c-and-assembly 18/32
UNIT II - C AND ASSEMBLY
No Parameters and No Return Value.
Cprototype:
void Disable_Ints(void) ;
Exampleusage:
Disable_Ints() ;
Generatedcode:
CALL _Disable_Ints
NASMsource
code forthefunction:
_Disable_Ints:CLI ; Disables interrupt system
RET ; Return from function
8/8/2019 UNIT - 2 C and Assembly
http://slidepdf.com/reader/full/unit-2-c-and-assembly 19/32
UNIT II - C AND ASSEMBLY
No Parameters and 8-bit Return Value.
C prototype: BYTE8 LPT1_Status(void) ;Example
usage:status = LPT1_Status() ;
Generatedcode:
CALL _LPT1_Status; returns status in EAXMOV [_status],AL
NASMsource code
for the
function:
_LPT1_Status:MOV DX,03BDh ; Load DX w/hex I/O adrXOR EAX,EAX ; Pre-Zero rest of EAXIN AL,DX ; Get status byte from
port.RET ; Return from function.
8/8/2019 UNIT - 2 C and Assembly
http://slidepdf.com/reader/full/unit-2-c-and-assembly 20/32
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 functionreturns.
8/8/2019 UNIT - 2 C and Assembly
http://slidepdf.com/reader/full/unit-2-c-and-assembly 21/32
UNIT II - C AND ASSEMBLY
Passing Parameters to a C Function
Function callw/parameters: Byte2Port(0x3BC, data) ;
Code generated by the compiler:
PUSH DWORD [_data] ; Push 2nd paramMOV EAX,03BCh ; Push 1st paramPUSH EAXCALL _Byte2Port ; Call the
function.ADD ESP,8 ; Remove params
8/8/2019 UNIT - 2 C and Assembly
http://slidepdf.com/reader/full/unit-2-c-and-assembly 22/32
UNIT II - C AND ASSEMBLY
Passing an 8 bit‑ Unsigned
IntegerC Assembly
unsigned char data ;...Do_Something(data) ;...
MOVZX EAX,[_data]PUSH EAXCALL _Do_SomethingADD ESP,4
8/8/2019 UNIT - 2 C and Assembly
http://slidepdf.com/reader/full/unit-2-c-and-assembly 23/32
UNIT II - C AND ASSEMBLY
Passing an 8 bit‑ Signed Integer
C Assembly
signed char data ;...Do_Something(data) ;...
MOVSX EAX,[_data]PUSH EAXCALL _Do_SomethingADD ESP,4
8/8/2019 UNIT - 2 C and Assembly
http://slidepdf.com/reader/full/unit-2-c-and-assembly 24/32
UNIT II - C AND ASSEMBLY
Passing a 64 bit Integer‑
C Assembly
/* signed or unsigned */long long data ;...
Do_Something(data) ;...
PUSH DWORD [_data+4]PUSH DWORD [_data]CALL _Do_Something
ADD ESP,8
8/8/2019 UNIT - 2 C and Assembly
http://slidepdf.com/reader/full/unit-2-c-and-assembly 25/32
UNIT II - C AND ASSEMBLY
Retrieving Parameters
Address Contents Description
[ESP+8] _data The 2
nd
function parameter (data to write to I/O port)[ESP+4] 03BCh The 1st function parameter (an I/O port address)
[ESP]Return
AddressPushed onto stack by the CALL instruction
Stack immediately after the CALL
PUSH DWORD [_data] ; Push 2nd parameter
MOV EAX,03BCh ; Push 1st parameter
PUSH EAX ; onto the stack.
CALL _Byte2Port ; Call the function
8/8/2019 UNIT - 2 C and Assembly
http://slidepdf.com/reader/full/unit-2-c-and-assembly 26/32
8/8/2019 UNIT - 2 C and Assembly
http://slidepdf.com/reader/full/unit-2-c-and-assembly 27/32
UNIT II - C AND ASSEMBLY
Retrieving Parameters
_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 portaddress)
MOV AL,[EBP+12] ; Copy 2nd parameter to AL (discard bits15-8)
OUT DX,AL ; Write the data to the I/O portPOP EBP ; Restore old contents of BP from stack
_Byte2Port:MOV DX,[ESP+4] ; Copy 1st parameter to DX (the I/O portadrs).
MOV AL,[ESP+8] ; Copy 2nd parameter to AL (discard bits31-8).
OUT DX,AL ; Write the data to the I/O port.
RET ; Return to caller.
8/8/2019 UNIT - 2 C and Assembly
http://slidepdf.com/reader/full/unit-2-c-and-assembly 28/32
UNIT II - C AND ASSEMBLY
Everything is Pass By Value
Function definition Function invocation
void Swap(int *p1, int *p2)
{int temp = *p1 ;
*p1 = *p2 ;
*p2 = temp ;
}
int x = 4 ;
int y = 7 ;…
Swap(&x, &y) ;
…
Emulating pass-by-reference in C
8/8/2019 UNIT - 2 C and Assembly
http://slidepdf.com/reader/full/unit-2-c-and-assembly 29/32
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 (stackpointer must be adjusted and later restored when
temporaries are no longer needed).
Swap: PUSH EBP ; Preserve original EBP contents
8/8/2019 UNIT - 2 C and Assembly
http://slidepdf.com/reader/full/unit-2-c-and-assembly 30/32
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
• • •
• • •
MOV ESP,EBP ; Release the temporary automatic int
POP EBP ; Restore original EBP
RET ; Return from this function
AddressContent
s Description
• • • Stack space currently in use by calling context.
[EBP+12] p2Function parameters pushed on the stack by the caller.
[EBP+8] p1
[EBP+4]Return
address Return address pushed by the CALL and popped by the RET.
[EBP]original
EBP
Original EBP preserved by PUSH EBP and restored by POP
EBP.
[EBP-4] tempTemporary int with automatic memory allocation. (Top of
stack)
• • • Unused stack space (Interrupts push return address here)
S ap: PUSH EBP ; Preser e original EBP contents
8/8/2019 UNIT - 2 C and Assembly
http://slidepdf.com/reader/full/unit-2-c-and-assembly 31/32
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) againMOV [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
8/8/2019 UNIT - 2 C and Assembly
http://slidepdf.com/reader/full/unit-2-c-and-assembly 32/32
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 EDXMOV 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