Professional Documents
Culture Documents
Carmi merimovich
Processor architecture concepts
Data Representation, Review Boolean Operations, Review Processor architecture concepts
Instruction Execution—Naive
Program Loading
Execution Environment
Registers
The registers are storage place inside the CPU. Their effective
access time is 0.
32-bit registers:
EAX EBP
EBX ESP
ECX ESI
EDX EDI
EFLAGS EIP
31 − 24 23 − 16 15 − 8 7 − 0
EAX
AX
AH AL
31 − 24 23 − 16 15 − 8 7 − 0
EBX
BX
BH BL
31 − 24 23 − 16 15 − 8 7 − 0
ECX
CX
CH CL
31 − 24 23 − 16 15 − 8 7 − 0
EDX
DX
DH DL
31 − 24 23 − 16 15 − 8 7 − 0
ESI
SI
EDI
DI
EBP
BP
ESP
SP
Physical Memory
Paging Unit o Page Table
36-bit Physical Address
Paging Unit
Integer Constants
Integer Expressions
Character Constants
String Constants
Reserved Words
Identifiers
Directives
Instructions
Labels
A label is an identifier.
A label is used as a place marker, for code or data.
Assuming Var is a label marking address 20 then the following
two instructions do the same thing:
Code example:
Instruction Mnemonic
Operands
stc ; Set CF
inc ax ; Add 1 to ax
sub count,bx ; Subtract bx from count
Comment
.386
.model flat
extern _ExitProcess@4:Near
.code ;Code area
_main:
push 0 ;Black box. Always terminate
call _ExitProcess@4 ;program with this sequence.
?>=<
89:;
.INCC ?>=<
89:;
.LIB? GFED
@ABC
.DLL
CC ?? @@
CC ?? @@
CC ?? @@
C!! ?? @@
@
@ABC
GFED
.ASM //Assembler // GFED
@ABC
.OBJ //Linker // GFED
@ABC
.EXE //Loader // Memory
CC @@
CC @@
CC @@
CC @@
@ABC
GFED
!! @ABC
GFED
@
.LST .MAP
Assembling
.LST I
extern _ExitProcess@4:Near
00000000 .code ;Code area
00000000 _main:
00000000 6A 00 push 0 ;Black box. Terminate
00000002 E8 00000000 E call _ExitProcess@4 ; program with this
00000007 ; sequence.
FLAT . . . . . . . . . . . . . . GROUP
.LST II
_DATA . . . . . . . . . . . . . 32 Bit 00000000 DWORD Public ’DATA’
_TEXT . . . . . . . . . . . . . 32 Bit 00000007 DWORD Public ’CODE’
Symbols:
0 Warnings
0 Errors
Linking
.MAP I
FirstProgram
Timestamp is 3f93b8b8 (Mon Oct 20 12:28:08 2003)
Preferred load address is 00400000
VS.NET suggestion
Data Definition
General form:
BYTE, SBYTE
Examples:
v1 BYTE 0
BYTE 255
v2 BYTE ’A’
SBYTE -128
var3 SBYTE 127
BYTE ?
VAR DB -128
DB 255
BYTE: Strings
WORD, SWORD
Examples:
v1 WORD 0
WORD 65535
v2 SWORD -32768
SWORD 32767
vec WORD 20 DUP(0)
var3 WORD ?
array SWORD 10 DUP (?)
VAR DW -32768
DW 65535
DWORD, SDWORD
DWORD 1,2,3,4,5
generates the memory map
Offset Value
0000 1
0004 2
0008 3
000C 4
0010 5
Little Endian
Offset Value
Bh 06h
Ah 1Fh
9h 22h Word at 1h contains 5C 42h.
8h CDh Word at 2h contains 725Ch.
7h 30h Word at Ah contains 061Fh.
6h D4h
5h 9Bh Doubleword at 4h contains 30D49B8Fh.
4h 8Fh Doubleword at 5h contains CD30D49Bh.
3h 72h QuaDWORD at 3h contains 1F 22CD30D49B8F 72h.
2h 5Ch
1h 42h
0h 8Dh
.386
.model flat
extern _ExitProcess@4:Near
.data
var1 DWORD 2000H
var2 DWORD 1000H
var3 DWORD 3000h
result DWORD ?
_main:
mov eax,var1 ;EAX=2000h
sub eax,var2 ;EAX=1000h
add eax,var3 ;EAX=4000h
mov result,eax
push 0
call _ExitProcess@4
Symbolic Constants
= Directive
Example:
Count = 500
.data
Vector dd Count dup (?)
.code
mov ax, Count ;Loop counter
.
.
.
If there is a need for a vector of size, say, 600, then it is
enough to change the first statement to
Count = 600
Example:
Count = 4
mov ax,count ;ax=4
Count = 10
mov bx,count ;bx=10
Count = Count*5
mov cx,count ;cx=50
The changing of Count has nothing to do with the execution
of the program.
$ Operator
EQU Directive
General form:
identifier EQU expression ;Very much like ‘=’
identifier EQU identifier ;Very much like ‘=’
identifier EQU <text> ;Text is substituted for
;identifier, ‘as is’.
The EQU directives resembles the ‘=’ directive: It generates
symbolic constant.
Symbolic constant generated with EQU can not be redefined.
Example:
Avogadro EQU <6.23E23>
Hello EQU <"Hello!">
Real4 Avogadro
Byte Hello
Lecture 2(32—81) 72/448
Defining Data Simplest Memory Addressing Mode Basic Data Transfer
Example:
Count = 5 ;
e1 equ Count ;e1 will evaluate to 5
e2 equ <Count> ;e2 will evaluate to ‘Count’
Count = 10
The code
.data
var byte 0
.code
mov al,var
translates into
mov al,[1000h]
assuming var is located at address 1000h.
The notation [number] is called direct memory access.
We use labels to access variables since it is very cumbersome
to count offsets all the time.
Notational Conventions
Operand Description
r8 One of the 8-bit registers: AL, AH, BL, BH, CL, CH, DL, DH.
r16 One of the 16-bit registers: AX, BX, CX, DX, SI, DI, SP, BP.
r32 One of the 32-bit registers: EAX, EBX, ECX, EDX, ESI, EDI, ESP, EBP.
reg Anyone of the general purpose registers.
imm8 8-bit immediate value (i.e., constant).
imm16 16-bit immediate value (i.e., constant).
imm32 32-bit immediate value (i.e., constant).
imm 8, 16, or 32-bit immediate value (i.e., constant).
r/m8 8-bit register or byte in memory.
r/m16 16-bit register or word in memory.
r/m32 32-bit register or doubleword in memory.
r/m a byte, word or doubleword in register or memory.
rel A relative address, 8, 16, or 32-bit displacement
MOV Instruction
Basic format:
MOV destination, source.
Operation: destination ← source.
General variants:
MOV reg,reg ;Both operands must
MOV mem,reg ;be of the same size
MOV reg,mem
MOV mem,imm
MOV reg,imm
Note there is no memory/memory MOV.
MOV Example
.data
v1 word ?
v2 word ?
.code
mov ax,v1
mov v2,ax
The problem:
.data
I sword -1
.code
mov ebx,0
mov bx,I ;ebx=0000FFFFh=65535
Zero Extension
Sign Extension