COA Class3

57
William Stallings Computer Organization and Architecture 8 th Edition Chapter 10 & 11 Instruction Sets T1 10.1-10.5, 11.1-11.4 R1- 2.2 (Memory Locations & Addresses), 2.5 (Addressing Modes)

description

COA Notes

Transcript of COA Class3

Page 1: COA Class3

William Stallings Computer Organization and Architecture8th Edition

Chapter 10 & 11Instruction Sets

T1 10.1-10.5, 11.1-11.4R1- 2.2 (Memory Locations & Addresses), 2.5 (Addressing Modes)

Page 2: COA Class3

Instruction Set • 10.1 Machine Instruction Characteristics• 10.2 Types of Operands• 10.3 Intel x86 Data Types• 10.4 Types of Operations• 10.5 Intel x86 Operation Types

• 11.1 Addressing• 11.2 x86 Addressing Modes• 11.3 Instruction Formats• 11.4 x86 Instruction Formats• Summary

Page 3: COA Class3

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.

Page 4: COA Class3

What is an Instruction Set?• The complete collection of instructions

that are understood by a CPU• Machine Code• Binary• Usually represented by assembly codes

Page 5: COA Class3

Elements of an Instruction• Operation code (Op code)

—Do this• Source Operand reference

—To this• Result Operand reference

—Put the answer here• Next Instruction Reference

—When you have done that, do this...

Page 6: COA Class3

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

• Operands can also be represented in this way—ADD A,B

Simple Instruction Format

Page 7: COA Class3

Instruction Types• Data processing• Data storage (main memory)• Data movement (I/O)• Program flow control

Page 8: COA Class3

3-, 2-, 1-, & 0-Address

• The classification is based on arithmetic instructions that have no. of operands and one result

• 3-address instruction specifies memory addresses for both operands and the result R Op1 op Op2

• A 2-address instruction overwrites one operand in memory with the result Op2 Op1 op Op2

• A 1-address instruction has a processor, called the accumulator register, to hold one operand & the result Acc Acc op Op1

• A 0-address + uses a CPU register stack to hold both operands and the result TOS TOS op SOS

• The 4-address instruction, hardly ever seen, also allows the address of the next instruction to specified explicitly

Page 9: COA Class3

3-Address Instruction Format

• 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

Memory

Op1Addr:Op2Addr:

Op1

Programcounter

Op2

ResAddr:

NextiAddr:

Bits: 8 24 24Instruction format

24

Res

Nexti

CPU

Where to findnext instruction

24

add, Res, Op1, Op2 (Res Op2 + Op1)

add ResAddr Op1Addr Op2AddrWhich

operationWhere toput result Where to find operands

Page 10: COA Class3

2-Address Instruction Format• 2 addresses

—One address doubles as operand and result—a = a + b—Reduces length of instruction—Requires some extra work

– Temporary storage to hold some results

Memory

Op1Addr:

Op2Addr:

Op1

Programcounter

Op2,Res

NextiNextiAddr:

Bits: 8 24 24Instruction format

CPU

Where to findnext instruction

24

add Op2, Op1 (Op2 Op2 + Op1)

add Op2Addr Op1AddrWhich

operation

Where toput result

Where to find operands

Page 11: COA Class3

1-Address Instruction Format

• 1 address—Implicit second address—Usually a register

(accumulator)—Common on early machines

Need instructions to load and store operands:LDA OpAddrSTA OpAddr

Memory

Op1Addr: Op1

NextiProgramcounter

Accumulator

NextiAddr:

Bits: 8 24Instruction format

CPU

Where to findnext instruction

24

add Op1 (Acc Acc + Op1)

add Op1AddrWhich

operationWhere to find

operand1

Where to find operand2, and

where to put result

Page 12: COA Class3

0-Address or Stack Instruction Format

• 0 (zero) addresses—All addresses implicit—Uses a stack— Computer must have a 1-address instruction to push and pop operands to and from

the stack

Memory

Op1Addr:

TOSSOSetc.

Op1

Programcounter

NextiAddr: Nexti

Bits:

Format

Format

8 24

CPU

Where to findnext instruction

Stack

24

push Op1 (TOS Op1)

Instruction formats

add (TOS TOS + SOS)

push Op1AddrOperation

Bits: 8

addWhich operation

Result

Where to find operands, and where to put result

(on the stack)

e.g. push a push b add pop cc = a + b

Page 13: COA Class3

Expression Evaluation for 3-, 2-, 1-, and 0-Address Machines

• Number of instructions & number of addresses both vary

• Discuss as examples: size of code in each case

3 - a d d r e s s 2 - a d d r e s s 1 - a d d r e s s S t a c kadd a, b, cmpy a, a, dsub a, a, e

load a, badd a, cmpy a, dsub a, e

load badd cmpy dsub estore a

push bpush caddpush dmpypush esubpop a

Evaluate a = (b+c)*d - e

Page 14: COA Class3

Problems• Write 0-, 1-, 2- and 3- address machine

programs to compute X = (A+B*C) / (D-E*F).

• Write 0-, 1-, 2- and 3- address machine programs to compute ROOT = √ ((b2 -4ac)/2a)0 1 2 30start: PUSH A PUSH 2 MUL PUSH C PUSH A PUSH 4 MUL MUL PUSH B PUSH B MUL SUB DIV POP SQRT(ROOT) End

1Start: LOAD AMUL #2STORE T1LOAD #4MUL AMUL CSTORE T2LOAD BMUL BSUB T2DIV SQRTSTORE ROOTEnd

2Start: MUL B, BMOV T1, AMUL A, #4MUL A, CSUB B, AMUL T1, #2DIV B, T1SQRT B, BMOV ROOT, B End 

3Start: MUL T1, B, BMUL T2, A, CMUL T3, T2, #4 MUL T4, A, #2SUB T5, T1, T3DIV T6, T5, T4SQRT ROOT, T6End

Page 15: COA Class3

How Many Addresses• More addresses

—More complex (powerful?) instructions—More registers

– Inter-register operations are quicker—Fewer instructions per program

• Fewer addresses—Less complex (powerful?) instructions—More instructions per program—Faster fetch/execution of instructions

Page 16: COA Class3

Design Decisions• Operation repertoire

—How many ops?—What can they do?—How complex are they?

• Data types• Instruction formats

—Length of op code field—Number of addresses

• Registers—Number of CPU registers available—Which operations can be performed on which registers?

• Addressing modes (later…)

• RISC v CISC

Page 17: COA Class3

Types of Operand• Addresses• Numbers

—Integer/floating point• Characters

—ASCII etc.• Logical Data

—Bits or flags• (Aside: Is there any difference between numbers and

characters? Ask a C programmer!)

Page 18: COA Class3

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 even-

numbered address• Data accessed across 32 bit bus in units of

double word read at addresses divisible by 4

• Little endian

Page 19: COA Class3

SIMD Data Types• Integer types

— Interpreted as bit field or integer

• Packed byte and packed byte integer— Bytes packed into 64-bit quadword or 128-bit double

quadword• Packed word and packed word integer

— 16-bit words packed into 64-bit quadword or 128-bit double quadword

• Packed doubleword and packed doubleword integer— 32-bit doublewords packed into 64-bit quadword or 128-bit

double quadword• Packed quadword and packed qaudword integer

— Two 64-bit quadwords packed into 128-bit double quadword

• Packed single-precision floating-point and packed double-precision floating-point— Four 32-bit floating-point or two 64-bit floating-point values

packed into a 128-bit double quadword

Page 20: COA Class3

x86 Numeric Data Formats

Page 21: COA Class3

Types of Operation• Data Transfer• Arithmetic• Logical• Conversion• I/O• System Control• Transfer of Control

Page 22: COA Class3

Data Transfer• Specify

—Source—Destination—Amount of data

• May be different instructions for different movements—e.g. IBM 370

• Or one instruction and different addresses—e.g. VAX

Page 23: COA Class3

Arithmetic• Add, Subtract, Multiply, Divide• Signed Integer• Floating point ?• May include

—Increment (a++)—Decrement (a--)—Negate (-a)

Page 24: COA Class3

Shift and Rotate Operations

Page 25: COA Class3

Logical• Bitwise operations• AND, OR, NOT

Page 26: COA Class3

Input/Output• May be specific instructions• May be done using data movement

instructions (memory mapped)• May be done by a separate controller

(DMA)

Page 27: COA Class3

Systems Control• Privileged instructions• CPU needs to be in specific state

—Ring 0 on 80386+—Kernel mode

• For operating systems use

Page 28: COA Class3

Transfer of Control• Branch

—e.g. branch to x if result is zero• Skip

—e.g. increment and skip if zero—ISZ Register1—Branch xxxx—ADD A

• Subroutine call—c.f. interrupt call

Page 29: COA Class3

Branch Instruction

Page 30: COA Class3

Nested Procedure Calls

Use of Stack

Page 31: COA Class3

Stack Frame Growth Using Sample Procedures P and Q

Page 32: COA Class3

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

• i.e. read top down or bottom up?

Address Value (1) Value (2)184 12 78185 34 56186 56 34187 78 12

Page 33: COA Class3

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

Page 34: COA Class3

Example of C Data Structure

Page 35: COA Class3

Standard…What 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

Page 36: COA Class3

Addressing Modes• Immediate• Direct• Indirect• Register• Register Indirect• Displacement (Indexed) • Stack

Page 37: COA Class3

Immediate Addressing• Operand is part of instruction• Operand = address field• e.g. ADD 5

—Add 5 to contents of accumulator—5 is operand

• No memory reference to fetch data• Fast• Limited range

OperandOpcode

Instruction

Page 38: COA Class3

Direct Addressing• Address field contains address

of operand• 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

• Single memory reference to access data

• No additional calculations to work out effective address

• Limited address space

Address AOpcode

Instruction

Memory

Operand

Page 39: COA Class3

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

Page 40: COA Class3

Indirect Addressing Diagram

Address AOpcode

Instruction

Memory

Operand

Pointer to operand• Large address space • 2n where n = word length• May be nested, multilevel,

cascaded—e.g. EA = (((A)))

– Draw the diagram yourself• Multiple memory accesses

to find operand• Hence slower

Page 41: COA Class3

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

Page 42: COA Class3

Register Addressing Diagram

Register Address ROpcode

Instruction

Registers

Operand

• 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;• c.f. Direct addressing

Page 43: COA Class3

Register Indirect Addressing

Register Address ROpcode

Instruction

Memory

OperandPointer to Operand

Registers

• C.f. indirect addressing

• EA = (R)• Operand is in

memory pointed to by contents of register R

• Large address space (2n)

• One fewer memory access than indirect addressing

Page 44: COA Class3

Displacement Addressing

Register ROpcode

InstructionMemory

OperandPointer to Operand

Registers

Address A

+

• EA = A + (R)• Address field hold two values

—A = base value—R = register that holds displacement—or vice versa

Page 45: COA Class3

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

Page 46: COA Class3

Base-Register Addressing• A holds displacement• R holds pointer to base address• R may be explicit or implicit• e.g. segment registers in 80x86

Page 47: COA Class3

Indexed Addressing• A = base• R = displacement• EA = A + R• Good for accessing arrays

—EA = A + R—R++

Page 48: COA Class3

Combinations• Postindex• EA = (A) + (R)

• Preindex• EA = (A+(R))

• (Draw the diagrams)

Page 49: COA Class3

Stack Addressing• Operand is (implicitly) on top of stack• e.g.

—ADD Pop top two items from stackand add

Page 50: COA Class3

x86 Addressing Modes• Virtual or effective address is offset into segment

—Starting address plus offset gives linear address—This goes through page translation if paging enabled

• 12 addressing modes available—Immediate—Register operand—Displacement—Base—Base with displacement—Scaled index with displacement—Base with index and displacement—Base scaled index with displacement—Relative

Page 51: COA Class3

x86 Addressing Mode Calculation

Page 52: COA Class3

Logical to Physical Address CalculationThe 8086 microprocessor has 20-bit address lines. All the registers in 8086 are 16-bits in length. Hence to obtain 20-bit addresses from the available 16-bit registers, all 8086 memory addresses are computed by summing the contents of a segment register and a effective memory address.1. Given that the EA of a datum is 2359 H and the DS = 490B H, what is the physical address of the datum?

DS: 490B0 HEA: 2359 H Physical add. 4B409

2. If a physical branch address is 5A230 H when (CS) = 5200 H, what will be PA if the (CS) are changed to 7800 H.

CS: 52 0 0 EA: XXXX Physical add. 5A2 3 0 H

Hence EA = Physical add - (Segment address displaced by 4-bits) EA = 5A230 - 52000 = 8230 H If the CS is changed to 7800 H the Physical address will be 78000 + 8230 = 80230

Page 53: COA Class3

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

Page 54: COA Class3

Instruction Length• Affected by and affects:

—Memory size—Memory organization—Bus structure—CPU complexity—CPU speed

• Trade off between powerful instruction repertoire and saving space

Page 55: COA Class3

Allocation of Bits• Number of addressing modes• Number of operands• Register versus memory• Number of register sets• Address range• Address granularity

Page 56: COA Class3

x86 Instruction Format

Page 57: COA Class3

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