Transcript ppt

ECE 291
Lecture 4:
The 80x86 Instruction Set Architecture
Registers-Instructions
Constantine D. Polychronopoulos
Spring 2000
CDP
ECE 291 -- Spring 2000
Instruction Format
• ALL instructions have the following specifiers:
– OPCODE: a field that specifies the operation to be done
– OPERAND(S): one or more fields giving the operands or the location
where the operands can be found
– DESTINATION: a field that specifies the location (register or
memory) where the result of the operation is to be stored
– [Descriptor fields]: Special bit specifiers that allow for different
interpretation of the same field (e.g. register or offset specifier)
• ASSEMBLY INSTRUCTIONS: symbolic (mnemonic) versions of
machine instructions
• MACHINE INSTR. Or BINARY CODE: Binary codes that give the
specific value for each of the above fields
• Assembly program ==> ASSEMBLER (MASM) ==> Machine code
CDP
ECE 291 -- Spring 2000
Instruction Format: x86
• In 80x86 instructions can vary in length from 8-bits (1b) to more
than 100-bits (13b)
• REAL MODE:
– Default instruction size is 16-bits
• 16-bit registers & 16-bit offset fields
• RPOTECTED MODE:
– Default instruction size is 32-bits (x386 and above)
• 32-bit registers & 32-bit offset fields
– D-bit in descriptor specifies real or protected mode:
• D=0: (real-mode) 16-bit instructions, register values and addresses
• D=1: (protected mode) 32-bit instructions, reg. values and addresses
CDP
ECE 291 -- Spring 2000
Addressing Modes
• Immediate: Move an immediate value (in the field itself) to the
destination register or memory location:
– MOV
AX, 7F55H
• Register: Move a byte or word from the source register to the
destination register or memory location:
– MOV
AX, BX
• Direct: Move a byte/word from a memory location to a register or
memory location:
– MOV
CDP
AX, [7777H]
ECE 291 -- Spring 2000
Addressing Modes
• Base-relative or indexed: Move a byte/word between a register
and mem. Location specified by an index (DI or SI) or base
register (BP or BX):
– MOV
AX, [BX]
• Register-relative: Move a byte/word between a register and
mem. Location specified by an index OR base register + offset:
– MOV
AX, [DI + 7777H]
• Base-relative and indexed: Move a byte/word between a register
and mem. Location specified by a base register PLUS an index
register PLUS offset:
– MOV
CDP
AX, [SP + DI + 7777H]
ECE 291 -- Spring 2000
Addressing Modes: Register
Instruction
OP
Comment
Addr. Mode
Memory Cont.
Dest Source
MOV AX, BX
Move to AX the 16-bit value in BX
Register
89 D8
MOV AX, DI
Move to AX the 16-bit value in DI
Register
89F8
MOV AH, AL
Move to AH the 8-bit value in AL
Register
88C4
CDP
ECE 291 -- Spring 2000
Addressing Modes - Immediate
Instruction
OP
Comment
Addr. Mode
Memory Cont.
Dest Source
MOV AH, 12H
Move to AH the byte value 12H
Immediate
B412
MOV AX, 1234H
Move to AX the value 1234H
Immediate
B8 34 12
MOV AX, CONST
Move to AX the constant CONST
Immediate
B8LSB MSB
MOV AX, OFFSET x
Move to AX the address (offset) of
variable x MASM Notation
Immediate
B 8 LSB MSB
CDP
ECE 291 -- Spring 2000
Addressing Modes: Direct & Indexed
Instruction
OP
CDP
Comment
Addr. Mode
Memory Cont.
Dest Source
MOV AX, [1234H]
Move to AX the value at memory
Direct
location 1234H (uses default segment, DS)
A1 34 12
MOV AX, x
Move to AX the value of M[x]
(uses default segment, DS)
MASM Notation
Direct
A1 LSB MSB
MOV x, AX
Move to M[x] the value of AX
(uses default segment, DS)
MASM Notation
Direct
A3 LSB MSB
MOV AX, [DI]
Move to AX the value at M[DI]
(uses default segment, DS)
Indexed
8B 05
MOV [DI], AX
Move to M[DI] the value AX
(uses default segment, DS)
Indexed
89 05
ECE 291 -- Spring 2000
Addressing Modes: Base-relat.
Instruction
OP
CDP
Comment
Addr. Mode
Memory Cont.
Dest Source
MOV AX, [BX]
Move to AX the value M[BX]
(uses default segment, DS)
Base-relative
8B 07
MOV [BX], AX
Move to M[BX] the value AX
(uses default segment, DS)
Base-relative
89 07
MOV AX, [BP]
Move to AX the value of M[BP]
(uses stack segment, SS)
Base-relative
8B 46
MOV [BP], AX
Move to M[BP] the value of AX
(uses stack segment, SS)
ECE 291 -- Spring 2000
Base-relative
89 46
Addressing Modes: Base-relat./Direct/Indexed-Direct
Instruction
OP
Comment
Addr. Mode
Memory Contents
Dest Source
MOV AX, offs[BX] Move to AX the value M[offs+BX] Base-relative
(uses default segment, DS)
Direct
8B 87 LSB MSB
MOV offs[BX], AX
89 87 LSB MSB
Move to M[offs+BX] the value AX Base-relative
(uses default segment, DS)
Direct
MOV AX, [BX+DI] Move to AX the value M[BX+DI]
(uses default segment, DS)
Base-relative
Direct
8B 01
MOV [BX+DI], AX Move to M[BX+DI] the value
AX (uses default segment, DS)
Base-relative
Indexed
89 01
MOV AX, [BX+DI+1234H]
Move to AX the value pointed to by Base-relative 8B 81 34 12
M[BX+DI+1234H]
Indexed Direct
(uses default segment, DS)
CDP
ECE 291 -- Spring 2000
Memory Model: Real & Protected Modes
• Due to downward compatibility with previous generations all x86
processors support real address mode which allows direct
addressing of only 1Mb of memory (20 bits) - recall that:
– Memory address = segment register + offset
• where segment reg. Is 16-bits left-shifted by 4 bits - hence a 20bit address.
• Protected mode allows extended memory of 4Gb or even
64Gb: An SDT (segment description table) is used to get the
starting address of memory segment to be addressed. The
original DS or CS register is used as an index into SDT whose
entry points to actual memory segment. Offset is added to latter
to form address:
– Memory addr. = SDT[segment register] + offset
CDP
ECE 291 -- Spring 2000
Memory Model (Cont.)
• ALL memory is allocated and managed in units of 64Kb segments
• Segments are used to organize different partitions of memory for
different objects (with different access restrictions):
–
–
–
–
user code & user data
user stack area
system code and data
memory-mapped I/O devices and other peripherals
• The segment starting address must first be loaded to DS or CS before
any access to that segment via x86 mem. Instructions.
• Before x386 only real memory addressing was available. But
protected mode was introduced starting with the 32-bit architectures.
• Default is always real mode for all x86 processors.
CDP
ECE 291 -- Spring 2000
Instruction Format
• ALL instructions have the following specifiers:
– OPCODE: a field that specifies the operation to be done
– OPERAND(S): one or more fields giving the operands or the location
where the operands can be found
– DESTINATION: a field that specifies the location (register or
memory) where the result of the operation is to be stored
– [Descriptor fields]: Special bit specifiers that allow for different
interpretation of the same field (e.g. register or offset specifier)
• ASSEMBLY INSTRUCTIONS: symbolic (mnemonic) versions of
machine instructions
• MACHINE INSTR. Or BINARY CODE: Binary codes that give the
specific value for each of the above fields
• Assembly program ==> ASSEMBLER (MASM) ==> Machine code
CDP
ECE 291 -- Spring 2000
Instruction Format: x86
• In 80x86 instructions can vary in length from 8-bits (1b) to more
than 100-bits (13b)
• REAL MODE:
– Default instruction size is 16-bits
• 16-bit registers & 16-bit offset fields
• RPOTECTED MODE:
– Default instruction size is 32-bits (x386 and above)
• 32-bit registers & 32-bit offset fields
– D-bit in descriptor specifies real or protected mode:
• D=0: (real-mode) 16-bit instructions, register values and addresses
• D=1: (protected mode) 32-bit instructions, reg. values and addresses
CDP
ECE 291 -- Spring 2000
Unconditional Jump (JMP)
• Short jump: 2-byte jump instr. - allows short jumps within
memory locations [-128:+127] from the location following the
jump instr.:
OPCODE
– JMP SHORT Target_Label
DISP.
• Near jump: 3-byte jump instr. - supports jumps within [32K:+32K] bytes from current location:
– JMP Label
OPCODE DISP-low DISP-high
• Far jump: 5-byte instruction allowing jumps anywhere within
4Gb of address space:
– JMP
Label
OPCODE DISP-low DISP-high
CDP
ECE 291 -- Spring 2000
CS low
CS high
Conditional Jumps
• A conditional Jump instruction tests a condition bit (FLAGS) and
sets the IP to a specified address (given in a field of the
instruction). Otherwise, IP is left unchanged and the next
instruction is fetched from IP+1 (in byte addressable mode)
• Hence: A conditional jump is materialized by TWO instructions:
– One that “compares” values or does an arith./logic op and sets bits
of FLAGS accordingly, and
– One that carries out the Jump based on the outcome of the
operation or the bit value of selected FLAGS
• FLAGS used by conditional branches:
–
–
–
–
–
CDP
S (sign)
Z (zero)
C (carry)
P (parity)
O (overflow)
ECE 291 -- Spring 2000
CMP (Comparison)
• The CMP is used to compare two values in signed or unsigned
form and sets one or more of the previous FLAG bits based on
the outcome of the comparison:
• CMP Operand_1 Operand_2
Unsigned Operands
Signed Operands
Z: set if equal
Z: set if equal
C=1 if Op_1 < Op_2 C: no meaning
C=0 if Op_1 >= Op_2
S and O: no meaning
CDP
if (S=0 AND O=1) OR (S=1 AND O=0): Op_1< Op_2
if (S=0 AND O=0)OR (S=1 AND O=1): Op_1>=Op_2
ECE 291 -- Spring 2000
Comparing Signed Integers
• CMP AX, BX
– Sign bit (S) will be set if AX-BX has a 1 in MSB
– Overflow bit (O) is set if AX-BX result is out of range (-215, 215-1)
• JS
Target_label:
– Check S bit and if set then jump to Target_label (i.e. sign bit is 1)
• JL (Jump on less than):
– JL takes the jump if (S XOR O) is 1 (jump is taken even on an
overflow because overflow in a CMP or SUB instruction can
happen only when first operand is a negative number and second
operand is a positive number - hence their SUB becomes an
addition that may overflow)
CDP
ECE 291 -- Spring 2000
Jump Instruction Semantics
• If num_1 & num_2 are unsigned we say num_1 is above num_2
if num_1 > num_2 (otherwise it’s below).
• If num_1 & num_2 are signed, num_1 is greater than num_2 if
num_1 > num_2 (otherwise it’s less).
• Notation of jump instructions:
CDP
– J
= JUMP
– N
= Not
– E
= Equal
– A/B
= Above/below
– G/L
= greater/less
ECE 291 -- Spring 2000
Jump Instructions
Instruction
Description
Condition
JA=JNBE
Jump if above
Jump if not below or equal
Jump if below or equal
Jump if not above
Jump if above or equal
Jump if not below
Jump if no Carry
Jump if below
Jump if not above
Jump if Carry
Jump if equal
Jump if Zero (set)
Jump if not equal
Jump if not Zero
Jump if Sign
C=0 & Z=0
JBE=JNA
JAE=JNB=JNC
JB=JNA=JC
JE=JZ
JNE=JNZ
JS
CDP
ECE 291 -- Spring 2000
C=1 | Z=1
C=0
C=1
Z=1
Z=0
S=1
Jump Instructions (Cont.)
Instruction
Description
Condition
JNS
Jump Not Sign
S=0
JO
Jump if Overflow
O=1
JNO
Jump if No Overflow
O=0
JG=JNLE
Jump if greater
Jump if not less or equal
Jump if greater or equal
Jump if not less
Jump if less
Jump if Not greater or eq.
Jump if less or equal
Jump if not greater
Jump if reg. CX=0
S=0 & Z=0
JGE=JNL
JL=JNGE
JLE=JNG
JCXZ
CDP
ECE 291 -- Spring 2000
S=0
S XOR O
S XOR O | z=1
CX=0
Branch Instructions
CDP
ECE 291 -- Spring 2000
Case Statements
CDP
ECE 291 -- Spring 2000
Repeat/Until & While Looping
CDP
ECE 291 -- Spring 2000
Multiplication in x86
CDP
ECE 291 -- Spring 2000
Multiplication
CDP
ECE 291 -- Spring 2000
Division
CDP
ECE 291 -- Spring 2000
Stack
CDP
ECE 291 -- Spring 2000
Stack Frame Organization
CDP
ECE 291 -- Spring 2000
Stack Frame Layout
CDP
ECE 291 -- Spring 2000