Sunum Vize
Sunum Vize
Registers
• The micro processor as a programmable device
• Internal programming model
• How its memory space is addressed
The Microprocessor and Its •
•
Basic Architecture
Outline
Before a program is written, the internal configuration of the µp
Architecture must be known.
Program-visible architecture of the 8086 through Core 2 will be
detailed. Program visible architecture, e.g. registers .
The adressing mode of this family of µps are described for
both the real and protected modes.
Real Mode Addressing:
Real Mode Memory: 00000H-FFFFFH (the first 1MB of main memory).It
is present on all versions of the microprocessor.
Protected Mode memory is available to 80286-Core 2.(80286, 16MB;
80386-Pentium, 4G; Pentium pro-core2 4G-64GB.
Programmer invisible registers to control and operate the protected
memory system.
Some registers are general purpose regs., some registers are special purpose regs.
Addressing Modes
Introduction
• Efficient software development for the µps
familarity with addressig modes.
• Tha data adressing modes: register,
immediate , direct, register indirect, base-plus-
index, register relative,and base relative-plus-
index in the 8086-80286. The 80386 and
above also include a scaled-index mode. The
program memory addressing : relative, direct,
indirect .
• MOV inst. for data addressing modes: It
transfers data between registers or between
register and memory.
• Program memory addressing : jump
• Operation of the stack memory: Push and pop
Data Addressing Modes
• Cover the data addressing modes using the mov
instruction.
• Data movement instructions move data (bytes, words
and doublewords) between registers and between
registers and memory.
MOV AX , BX
• Copies the word content of the BX into AX.
MOV opcode (operation code)
BX source, AX destination (operands).
The source never changes but the destination changes.
• Only the movs (strings) instruction can have both
operands in memory.
• Most data transfer instructions do not change the
EFLAGS register.
DATA ADDRESSING MODES
Register addressing
Note: MOV really COPIES data from the source to destination register.
In the 80386 through the p4 the immediate data can be a double word
data.
Use b for binary, q for octal and nothing for decimal.
ASCII data requires a set of apostrophes:mov eax, ‘A’ ;Moves ASCII
value 0x41H into eax. Immediate addressing examples:
MOV EAX, 12230000H MOV EAX,100B
MOV BL, 44 MOV RCX, 100H
MOV CL, 11001010B MOV ESI, 12
MOV AL, ‘A’
MOV ECX, 0
MOV SI, 0
MOV CH,100
Direct addressing: Direct addressing
Transfers data between memory location and al, • MOV AL, DATA1 ;Copies a byte from DATA1.
ax and eax. – 0000 DATA1 DB 10H
Usually encoded in 3 bytes, sometimes 4: MOV AL, DATA1
• MOV AL, [1234h] ; DS=1000H (effective • MOV DATA2, AX;Copies a word to DATA2.
addres=1000x10H+1234H=11234H • MOV HOME, EAX;
• MOV ES:[2000H],AL
• MOV RAX, WATER ; Copies 8 bytes from memory
location
;WATER into RAX.
Base Relative-Plus-Index
addressing: • Figure 3.12 : MOV AX, [BX+SI+100H]
Effective address computed as: DS=1000H
seg_base *10h+ base + index + constant.
Some EXAMPLES:
MOV LIST[EBP+ESI+4], DH ;Stack
segment copy.
MOV DH, [EBX+EDI+20H] ;Data segment
copy.
MOV AX, FILE[EBX+EDI] ;Constant is
FILE.
MOV EAX, FILE[EBX+ECX+2] ;32-bit
addressing:
Designed to be used as a mechanism to Scaled-Index addressing:
address a two-dimensional array.
(Suppose that a file of many records
exists in a memory. Disp. Addresses the • It is unique to the 80386-Core2
file , the base reg. addresses a record, • Effective address computed as:
the index reg. Addresses an element of seg_base*10h + base + constant*index.
the record. )
• The scaling factor (constant) can 1x,2x,4x,8x
• Some examples:
mov eax, [ebx+4*ecx] ;Data segment DWORD
copy.
mov [eax+2*edi-100H], cx; copies cx into the DS
mem. Loc. addressed by eax+e*edi-100H
mov eax, array[4*ecx]; copies the DW content of
DS mem. loc. addressed by array+4*ecx
Direct
• Absolute jump address is stored in the
instruction following the opcode.
• If a program jumps to memory location 10000h
for the next instruction, the adress 10000h is
stored following the opcode in the memory. • A direct jump is often called a far jump
because it can jump to any memory
location.
Indirect Program Memory
Relative Program Memory • Addressing
Jump location is specified by a register.
Addressing • There are three forms:
Register:
ax, bx, cx, dx, sp, bp, di or si
• Relative means “relative to the IP”. Any register can be used: eax, ebx, ecx, edx, esp, ebp, edi or esi.
• A displacement is added to the IP register. (80386 and above)
• This constant is encoded into the instruction itself, as above. JMP BX; if bx=1000H the microp. Jumps to 1000H offset in the code
• Intrasegment jumps: segment.
• Short jumps use a 1-byte signed displacement. (+127, -128) Register Indirect:
• Near jumps use a 2-byte signed displacement. (+,- 32Kbytes) Any relative register [bp], [bx], [di], or [si]
• In the 80386 and above, the displacement can also be a 32-bit Register Relative:
value (+,- 2 Gbytes) Any relative register with a displacement
• The assembler usually computes the displacement and selects the • Some examples
appropriate form. JMP EAX ; Jump within the code seg. Jump to (CS*10h+eax)
JMP [EBX] ; Jump to the current code segment location addressed by
10000 eb ebx
10001 02 jmp [2] JMP [EDİ+2]
10002 JMP TABLE[bx] ; Jump table. It jumps to the code segment’ memory
location addressed by the content of the data segment memory
10003 location addressed by Table plus bx.
10004 JMP RDI (64 bit mode)
• push and pop instructions are used to manipulate it.(8086-286 word never bytes,
80386 word , doubleword))
call and ret also refer to the stack implicitly.
• There are six forms of the push and pop instructions.
Register, memory , immediate, segment register, flags, and all registers. The PUsH-
POP immediate and PUshA-POPa are not available in 8086/8088.
• Two registers maintain the stack, esp and ss.
Intel Assembly
Arithmetic Operations:
Decrement subtraction:
z It subtracts a 1 from a register or the contents of a
Register subtraction: memory location.
z Example: This example subtracts the 16-bit contents of z The decrement indirect memory data instructions require
registers cx and dx from the contents of register bx. byte ptr, word ptr or dword ptr.( e.g. dec byte ptr [si] )
sub bx,cx
z Examples
Dec bh; bh=bh-1
sub bx, dx Dec cx ; cx=cx-1
Immediate Subtraction: The micropr. allows Dec r14;
Dec edx;
immediate operands for subtraction. Dec byte ptr [di] ; data segment
z Example: Subtract a 44h from a 22h. Dec word ptr [bp]; stack segment
mov ch, 22h Dec dword ptr[ebx]
Dec qword ptr[rsi] ; dec numb
sub ch, 44h ; c=deh
Z=0
C=1
A=1
s=1
P=1
O=0
Subtraction-with-borrow
z It functions as a regular subtraction, 32-bit Subtraction with 16-bit
except that the carry flag subtracts from
the difference. It is for subtractions that are registers
wider than16 bits in (8086-80286 )or 32
bits in 80386 and above .
sbb ah, al ; ah=ah-al-carry
sbb ax, bx
sbb cl,2; cl=cl-2-carry
sbb byte ptr [di],3
sbb ecx, ebx ;Subs registers - borrow.
Sbb rbp,8
Sbb al, [ebx+ecx]
Sbb [di], al
SUB AX,DI
Sbb di, [BP+2] SBB BX, SI
with the RAX register. z None of the flags change predictably for a division
z Division by zero and overflow generate errors.Overflow occurs when
a small number divides a large dividend.
z The result is written to the RDX and RAX
z Examples:
registers with high-order bits in the RDX div cl; ah|al=ax/cl, unsigned quotient (bölüm) in al, remainder in
ah. (8-bit division)
register. idiv cx ;dx|ax=(dx|ax)/cx (16-bit signed division. dx-ax 32-bit dividend.
quotient in ax, remainder in dx)
z Signed and unsigned numbers can be div byte pointer [bp](8-bit unsigned division)
multiplied
z MUL RCX, IMUL RDI, MUL QWORD
PTR[RSI]
LOGIC INSRUCTIONS
Allow bits to be set, cleared and complemented.
TEST: Operates like the AND but doesn't affect the destination. It affect
the flag register. It uses the same addressing modes as the AND Shift and rotate instructions manipulate binary
instruction. numbers at the binary bit level . They are used
Sets the Z flag to the complement of the bit being tested: to shift or rotate any memory data or register.
test al, 4;Tests bit 2 in al – 00000100. Bit=1 z=0 They are used control I/o devices.
jz LABEL ;Jump to LABEL if bit 2 is zero.
jz (jump if zero) and jnz (jump if not zero) Shift:
TEST DL, DH, DL ANDED WİTH DH z Moves numbers to the left or right within a
TEST CX, BX; CX ANDED WİTH BX register or memory location.
TEST EDX,ECX
TEST RDX,R15 z They also perform simple arithmetic such as
TEST AH, 4 multiplication and divison.
TEST EAX,256
NOT (logical one's complement)
NEG (arithmetic two's complement - sign of number inverted)
not ebx,
neg TEMP
Neg ax
Not byte ptr [bx]
Not rax
Neg ecx
z Not and neg can use any addressing mode except segment register
addressing!!!
z The arithmetic and logical left shifts are z Logical shifts function with unsigned numbers, and arithmetic shifts
function with signed numbers. Logical shifts multiply or divide
identical. unsigned numbers. Arithmetic shifts multiply or divide signed
numbers.
z The arithmetic and logical right shifts are z Example shift instructions:
different. (Logical shifts insert 0, arithmetic SHL EAX, 1 ; eax is logically shifted left 1 bit pos.
right shifts insert sign bit.) SHR BX, 12;
SHL AX,1
SHR ECX, 10
SAL DATA1, CL; CL HOLDS THE SHIFT COUNT
SAR SI, 2
SAL DATA1,CL
SHL RAX, CL
z Updates cf,of,pf,zf and sf, and leaves af undefined
z Example
Shl dx,14
Or
Mov cl,14
Shl dx, cl
z Example 5.31
; multiply ax by 10 Rotate:
SHL AX,1 z Rotates bits from one end to the other or
through the carry flag.
MOV BX,AX
z Commonly used to operate on numbers
SHL AX, 2 wider than 32-bits in the 386-p4 or wider
ADD AX, BX than 16 bits in the 8086-80286.
z A rotate count can be immediate or
;multiply ax by 5 located in register CL.
MOV BX, AX z Figure 5.10 page page 185
SHL AX,2
ADD AX,BX
ROL SI, 14 ;si rotated left 14 places.
RCL BL, 6; bl rotates left through carry 6 places.
ROL ECX, 18
ROL RDX, 40
RCR BL, CL; bl rotated right cl places through carry.
RCL DX, 1 ;Rotate carry bit from previous rcl in dx.
ROR WORD PTR [BP], 2; word contents of the memory location addressed by bp
in the stack segment rotated right 2 places.
z Affects only cf and of
z Conditional Jumps:
z EB Disp z Let the microproc. make an execution decision based on
z E9 Displow DispHigh some prescribed condition. If the condition is satisfied
proccesor makes the jump
z EA Iplow IpHigh Cslow CsHigh z Test flag bits S, Z, C, P and O.
z The general form
Jx short_label
where x a modifier.The target (short_label) must be no
more than -128 or 127 bytes away from the instr.
z Cont. transfer inst.s occupy 2 bytes in memory. The First
byte holds opcode and the second one holds the relative
displacement.
z For unsigned numbers: z For either signed or unsigned:
ja ;Jump if above (Z=0 and C=0) jne ;Jump if != (Z=0)
jbe ;Jump if below or equal (Z=1 or C=1) je or jz ;Jump if ==; or jump if zero (Z=1)
z For signed numbers jc ;Jump if carry set (C=1)
jl ;Jump if < (S not=O)
jge ;Jump if >= (S=O) Test cx instead of flags:
jcxz ;Jump if cx==0
jecxz ;Jump if ecx==0
ǡ
Ǥ Ǥ
à Ǥ
ǡ Ǥ
Ǥ à Ǥ
ǣ͛ Ǥ ǡ D
͚͛Ǥ Ǥ ͞Ǥ͠ ͚͙͚
ǡ ǣ
ǣ͝ Ǥ
à
Ǥ à Ǥ
Ǥ