Академический Документы
Профессиональный Документы
Культура Документы
L1: MOV EAX,[RESULT+2] ; load selected table
element
Label Operand Comment
Field Fields Field
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
while (x < 1000) top_of_while: CMP DWORD [x],1000
{ JNL end_of_while
... ...
} JMP top_of_while
end_of_while:
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.
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:
_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.
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
unsigned char data ; MOVZX EAX,[_data]
... PUSH EAX
Do_Something(data) ; CALL _Do_Something
... ADD ESP,4
signed char data ; MOVSX EAX,[_data]
... PUSH EAX
Do_Something(data) ; CALL _Do_Something
... ADD ESP,4
C Assembly
/* signed or unsigned */ PUSH DWORD [_data+4]
long long data ; PUSH DWORD [_data]
... CALL _Do_Something
Do_Something(data) ; ADD ESP,8
...
_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
Emulating pass-by-reference in C
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