Download ELEC3730 Embedded Systems Lecture 11: Mixing C and Assembly

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
ELEC3730 Embedded Systems
Lecture 11: Mixing C and Assembly
• Register Usage
• Calling Functions
• Passing/Retrieving Parameters
• Stack Management
ELEC3730 Callaghan Campus
1
C - Register Usage Conventions
Register(s)
EAX
Usage in C functions
Functions return all pointers and integer values up to 32‑bits in this register.
EDX and EAX
Functions return 64‑bit values (long long ints) in this register pair. (Note:
EDX holds bits 63-32, EAX holds bits 31-0).
EBP
Used to access: (1) The arguments that were passed to a function when it
was called, and (2) any automatic variables allocated by the function.
EBX, ESI,
EDI, EBP,
DS, ES, and
SS.
EAX, ECX,
EDX, FS and
GS
These registers must be preserved by functions written in assembly
language. Any of these registers that the function modifies should be
pushed on entry to the function and popped on exit.
"Scratch" registers. These registers may be used without preserving their
current content.
DS, ES, SS,
Used to reference data. If modified by a function, the current contents of
EBP, and
ESP Callaghan
these registers
ELEC3730
Campus must be preserved on entry and restored on return.
2
C Objects (Variables) - Compiled Assembly
C
static int x=1;
static int y=2;
Intel Assembly
_x
_y
x=y;
DWORD 1
DWORD 2
MOV EAX, DWORD [_y]
MOV DWORD [_x], EAX
C
static int a[100];
static int k=5;
Intel Assembly
_a
_y
a[k]=5;
DWORD 100 DUP(0)
DWORD 5
MOV EAX,[_k]
MOV DWORD [_a+4*EAX], 5
C
Intel Assembly
static int x;
_x
DWORD 0
static int *p;
p=&x;
_p
DWORD 0
MOV DWORD [_p],_x
*p=0;
ELEC3730 Callaghan Campus
MOV EAX,[_p]
MOV DWORD [EAX],0
3
No Parameters and No Return Value.
•
CALL instruction used by caller to invoke the function
•
RET instruction used in function to return to caller.
−
−
Pushes the return address onto the stack.
Pops the return address off the stack.
C prototype:
Example
usage:
Generated
code:
void Disable_Ints(void) ;
Disable_Ints() ;
CALL
NASM _Disable_Ints:
source code
CLI
RET
for the
function:
_Disable_Ints
; Disables interrupt system
; Return from function
ELEC3730 Callaghan Campus
4
No Parameters and 8-bit Return Value.
C prototype:
Example
usage:
Generated
code:
NASM source
code for the
function:
BYTE8
LPT1_Status(void) ;
status = LPT1_Status() ;
CALL
MOV
_LPT1_Status:
MOV
XOR
IN
RET
_LPT1_Status ; returns status in EAX
[_status],AL
DX,03BDh
EAX,EAX
AL,DX
; Load DX w/hex I/O adr
; Pre-Zero rest of EAX
; Get status byte from port.
; Return from function.
ELEC3730 Callaghan Campus
5
Passing Parameters to a C Function
•
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.
Function call
w/parameters:
Code generated by the
compiler:
ELEC3730 Callaghan Campus
Byte2Port(0x3BC, data) ;
PUSH
MOV
PUSH
CALL
ADD
DWORD [_data]
EAX,03BCh
EAX
_Byte2Port
ESP,8
; Push 2nd param
; Push 1st param
; Call the function.
; Remove params
6
Passing an 8‑
8‑bit Integer - Signed Versus Unsigned
C
Assembly
unsigned char data ;
...
Do_Something(data) ;
...
MOVZX
PUSH
CALL
ADD
C
EAX,[_data]
EAX
_Do_Something
ESP,4
Assembly
signed char data ;
...
Do_Something(data) ;
...
MOVSX
PUSH
CALL
ADD
EAX,[_data]
EAX
_Do_Something
ESP,4
ELEC3730 Callaghan Campus
7
Passing a 64‑
64‑ bit Integer - Little Endian Format
C
Assembly
/* signed or unsigned */
long long data ;
...
Do_Something(data) ;
...
PUSH
PUSH
CALL
ADD
DWORD [_data+4]
DWORD [_data]
_Do_Something
ESP,8
ELEC3730 Callaghan Campus
8
Retrieving Parameters
PUSH
MOV
PUSH
CALL
DWORD [_data]
EAX,03BCh
EAX
_Byte2Port
;
;
;
;
Push
Push
onto
Call
2nd parameter
1st parameter
the stack.
the function
Stack immediately after the CALL
Address
[ESP+8]
[ESP+4]
[ESP]
•
Description
The 2nd function parameter (data to write to I/O port)
The 1st function parameter (an I/O port address)
Pushed onto stack by the CALL instruction
Can’t use POP instructions to access parameters.
−
−
•
Contents
_data
03BCh
Return
Address
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!
ELEC3730 Callaghan Campus
9
Retrieving Parameters
_Byte2Port:
MOV
MOV
OUT
RET
DX,[ESP+4]
AL,[ESP+8]
DX,AL
;
;
;
;
Copy 1st parameter to DX (the I/O port adrs).
Copy 2nd parameter to AL (discard bits 31-8).
Write the data to the I/O port.
Return to caller.
Better way - EBP is associated with Stack Segment ESS in forming physical addresses - specifically
designed for retrieving parameters off the stack.
_Byte2Port:
PUSH
MOV
MOV
MOV
OUT
POP
RET
EBP
EBP,ESP
DX,[EBP+8]
AL,[EBP+12]
DX,AL
EBP
; Preserve current contents of BP on stack
; Establish a reference point in the stack
; Copy 1st parameter to DX (the I/O port address)
; Copy 2nd parameter to AL (discard bits 15-8)
; Write the data to the I/O port
; Restore old contents of BP from stack
; Return to caller
ELEC3730 Callaghan Campus
10
Temporary Variables
•
Use automatic allocation:
−
−
−
•
Only available space is beyond top of stack.
−
•
Temporaries rarely need persistence;
Allocate temporaries on the stack;
Guarantees that function is reentrant.
Must be allocated before it can be used (stack pointer must be adjusted and
later restored).
Example - swap two integers:
Function definition
void Swap(int *p1, int *p2)
{
int temp = *p1 ;
*p1 = *p2 ;
*p2 = temp ;
}
Function invocation
int x = 4 ;
int y = 7 ;
…
Swap(&x, &y) ;
…
ELEC3730 Callaghan Campus
11
Temporary Variables - Swap Example
_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
ELEC3730 Callaghan Campus
12
Temporary Variables - Swap Example (Continued)
_Swap: PUSH
MOV
SUB
EBP
EBP,ESP
ESP,4
&p1
; Preserve original EBP contents
; Establish stack frame reference in EBP
; Allocate a temporary in automatic memory
MOV
MOV
MOV
ECX,[EBP+8]
EAX,[ECX]
[EBP-4],EAX
&p2
; temp = *p1:
;
;
(1) Get 1st parameter (p1)
(2) Use it to get *p1 into EAX
(3) Then store EAX into temp.
MOV
MOV
MOV
MOV
ECX,[EBP+12]
EAX,[ECX]
ECX,[EBP+8]
[ECX],EAX
; *p1 = *p2:
;
;
;
(1)
(2)
(3)
(4)
MOV
MOV
MOV
EAX,[EBP-4]
ECX,[EBP+12]
[ECX],EAX
; *p2 = temp:
;
;
(1) Get the temp into EAX
(2) Get 2nd parameter (p2) again
(3) Use it to store EAX into *p2
MOV
POP
RET
ESP,EBP
EBP
; Release the temporary int
; Restore original EBP
; Return from this function
Get
Use
Get
Use
2nd
it
1st
it
parameter (p2)
to get *p2 into EAX
parameter (p1) again
to store EAX into *p1
ELEC3730 Callaghan Campus
13
Swap Example (Continued) - Use of Enter and Leave Instructions
_Swap: ; PUSH EBP
; MOV
EBP,ESP
; SUB
ESP,4
ENTER 4,0
MOV
ECX,[EBP+8]
MOV
EAX,[ECX]
MOV
[EBP-4],EAX
;
;
;
;
;
;
;
Preserve original EBP contents
Establish stack frame reference in EBP
Allocate a temporary in automatic memory
Push EBP, ESP->EBP, ESP<-ESP-4
temp = *p1: (1) Get 1st parameter (p1)
(2) Use it to get *p1 into EAX
(3) Then store EAX into temp.
MOV
MOV
MOV
MOV
ECX,[EBP+12]
EAX,[ECX]
ECX,[EBP+8]
[ECX],EAX
; *p1 = *p2:
;
;
;
MOV
MOV
MOV
LEAVE
; MOV
; POP
RET
EAX,[EBP-4]
ECX,[EBP+12]
[ECX],EAX
;
;
;
;
;
;
;
ESP,EBP
EBP
ELEC3730 Callaghan Campus
(1)
(2)
(3)
(4)
Get
Use
Get
Use
2nd
it
1st
it
parameter (p2)
to get *p2 into EAX
parameter (p1) again
to store EAX into *p1
*p2 = temp:
(1) Get the temp into EAX
(2) Get 2nd parameter (p2) again
(3) Use it to store EAX into *p2
EBP->ESP, POP Stack into EBP
Release the temporary int
Restore original EBP
Return from this function
14