Documente Academic
Documente Profesional
Documente Cultură
Instruction Set
10.1 10.2 10.3 10.4 10.5 Machine Instruction Characteristics Types of Operands Intel x86 Data Types Types of Operations Intel x86 Operation Types
11.1 Addressing 11.2 x86 Addressing Modes 11.3 Instruction Formats 11.4 x86 Instruction Formats Summary
Objectives
To program a computer in machine or assembly language one must know about CPU registers, memory structure, supported data types, data access methods, operations supported by the ALU, and elements of the instructions & its size etc. The treatment is more generic but 8086 ISA can be use as an example.
What is an Instruction Set? The complete collection of instructions that are understood by a CPU Machine Code Binary Usually represented by assembly codes
Instruction Representation In machine code each instruction has a unique bit pattern For human consumption (well, programmers anyway) a symbolic representation is used
e.g. ADD, SUB, LOAD
Instruction Types Data processing Data storage (main memory) Data movement (I/O) Program flow control
3 addresses
Operand 1, Operand 2, Result a = b + c; May be a forth next instruction (usually implicit) Not common Needs very long words to hold everything
Ne xtiAd d r:
Ne xti
P ro g ra m c o u n te r
24
its :
24 R e s Ad d r Wh e re to p u t re s u lt
24 O p 2 Ad d r
a dd Wh ic h o p e ra tio n
Wh e re to fin d op e ra n d s
One address doubles as Op2Addr: Op2,Re s operand and result Ne xtiAddr: Ne xti a = a + b Reduces length of instruction Requires some extra work
Temporary storage to hold some results
24
Wh e re to fin d o p e ra n d 2 , a n d wh e re to p u t re s u lt
Ac c u m u la to r Ne xtiAd d r: Ne xti P ro g ra m c o u n te r 24
1 address
Implicit second address Usually a register (accumulator) Common on early machines
Need instructions to load and store operands: In s tru c tio n fo rm a t LDA OpAddr Bits : 8 24 STA OpAddr
a dd O p 1 Ad d r Wh ic h Wh e re to fin d o p e ra tio n o p e ra n d 1
F o rm a t p u s h Op e ra tio n
Ne xtiAd d r:
Ne xti
a dd Wh ic h o p e ra tio n
0 (zero) addresses
All addresses implicit Uses a stack
Wh e re to fin d o p e ra n d s , a n d wh e re to p u t re s u lt (o n th e s ta c k)
Computer must have a 1-address instruction to push and pop operands to and from the stack
Number of instructions & number of addresses both vary Discuss as examples: size of code in each case
Problems
Write 0-, 1-, 2- and 3- address machine programs to compute X = (A+B*C) / (DE*F). Write 0-, 1-, 2- and 3- address machine programs to compute ROOT = ((b2 4ac)/2a)
0 0start: PUSH A PUSH 2 MUL PUSH C PUSH A PUSH 4 MUL MUL PUSH B PUSH B MUL SUB DIV POP SQRT(ROOT) End 1 1Start: LOAD A MUL #2 STORE T1 LOAD #4 MUL A MUL C STORE T2 LOAD B MUL B SUB T2 DIV SQRT STORE ROOT End 2 2Start: MUL B, B MOV T1, A MUL A, #4 MUL A, C SUB B, A MUL T1, #2 DIV B, T1 SQRT B, B MOV ROOT, B End 3 3Start: MUL T1, B, B MUL T2, A, C MUL T3, T2, #4 MUL T4, A, #2 SUB T5, T1, T3 DIV T6, T5, T4 SQRT ROOT, T6 End
Fewer addresses
Less complex (powerful?) instructions More instructions per program Faster fetch/execution of instructions
Design Decisions
Operation repertoire
How many ops? What can they do? How complex are they?
Registers
Number of CPU registers available Which operations can be performed on which registers?
Characters
ASCII etc.
Logical Data
Bits or flags
(Aside: Is there any difference between numbers and characters? Ask a C programmer!)
x86 Data Types 8 bit Byte 16 bit word 32 bit double word 64 bit quad word 128 bit double quadword Addressing is by 8 bit unit Words do not need to align at evennumbered address Data accessed across 32 bit bus in units of double word read at addresses divisible by 4 Little endian
Packed doubleword and packed doubleword integer Packed quadword and packed qaudword integer
Types of Operation Data Transfer Arithmetic Logical Conversion I/O System Control Transfer of Control
Arithmetic Add, Subtract, Multiply, Divide Signed Integer Floating point ? May include
Increment (a++) Decrement (a--) Negate (-a)
Input/Output May be specific instructions May be done using data movement instructions (memory mapped) May be done by a separate controller (DMA)
Skip
e.g. increment and skip if zero ISZ Register1 Branch xxxx ADD A
Subroutine call
c.f. interrupt call
Branch Instruction
Use of Stack
Byte Order (A portion of chips?) What order do we read numbers that occupy more than one byte e.g. (numbers in hex to make it easy to read) 12345678 can be stored in 4x8bit locations as follows
Address 184
185 186 187
Value (1) 12
34 56 78
Value (2) 78
56 34 12
Byte Order Names The problem is called Endian The system on the left has the least significant byte in the lowest address This is called big-endian The system on the right has the least significant byte in the highest address This is called little-endian
StandardWhat Standard?
Pentium (x86), VAX are little-endian IBM 370, Moterola 680x0 (Mac), and most RISC are big-endian Internet is big-endian
Makes writing Internet programs on PC more awkward! WinSock provides htoi and itoh (Host to Internet & Internet to Host) functions to convert Alternative View of Memory Map
Addressing Modes Immediate Direct Indirect Register Register Indirect Displacement (Indexed) Stack
Immediate Addressing
Operand is part of instruction Operand = address field e.g. ADD 5
Add 5 to contents of accumulator 5 is operand
Direct Addressing
Address field contains address of operand Opcode Effective address (EA) = address field (A) e.g. ADD A
Add contents of cell A to accumulator Look in memory at address A for operand
Instruction Address A
Memory
Operand
Single memory reference to access data No additional calculations to work out effective address Limited address space
Indirect Addressing Memory cell pointed to by address field contains the address of (pointer to) the operand EA = (A)
Look in A, find address (A) and look there for operand e.g. ADD (A) Add contents of cell pointed to by contents of A to accumulator
Large address space 2n where n = word length May be nested, multilevel, cascaded
e.g. EA = (((A)))
Draw the diagram yourself
Pointer to operand
Operand
Register Addressing Operand is held in register named in address filed EA = R Limited number of registers Very small address field needed
Shorter instructions Faster instruction fetch
No memory access Very fast execution Very limited address space Multiple registers helps performance
Requires good assembly programming or compiler writing N.B. C programming
register int a;
Operand
Memory
Operand
Displacement Addressing
EA = A + (R) Address field hold two values
A = base value R = register that holds displacement or vice versa Instruction
Memory
Registers
Pointer to Operand
Operand
Relative Addressing A version of displacement addressing R = Program counter, PC EA = A + (PC) i.e. get operand from A cells from current location pointed to by PC c.f locality of reference & cache usage
Base-Register Addressing A holds displacement R holds pointer to base address R may be explicit or implicit e.g. segment registers in 80x86
Instruction Formats
Layout of bits in an instruction Includes opcode Includes (implicit or explicit) operand(s) Usually more than one instruction format in an instruction set
Instruction Length
Affected by and affects:
Memory size Memory organization Bus structure CPU complexity CPU speed
Allocation of Bits
Number of addressing modes Number of operands Register versus memory Number of register sets Address range Address granularity
Summary
Machine Instruction Characteristics Types of Operands Intel x86 Data Types Types of Operations Intel x86 Operation Types Addressing x86 Addressing Modes Instruction Formats x86 Instruction Formats