107
INSTRUCTION SET DESIGN Jehan-François Pâris [email protected]

INSTRUCTION SET DESIGN

  • Upload
    alena

  • View
    86

  • Download
    5

Embed Size (px)

DESCRIPTION

INSTRUCTION SET DESIGN. Jehan-François Pâris [email protected]. Chapter Organization. General Overview Objectives Realizations The MIPS instruction set. Importance. The instruction set of a processor is its interface with the outside world Defined by the hardware - PowerPoint PPT Presentation

Citation preview

Page 1: INSTRUCTION SET DESIGN

INSTRUCTION SET DESIGN

Jehan-François Pâ[email protected]

Page 2: INSTRUCTION SET DESIGN

Chapter Organization

• General Overview– Objectives – Realizations

• The MIPS instruction set

Page 3: INSTRUCTION SET DESIGN

Importance

• The instruction set of a processor is its interface with the outside world– Defined by the hardware– Used by assemblers, compilers and

interpreters• Remained very visible to the users up to the 80’s

– Earlier PC programs were written in assembler

Page 4: INSTRUCTION SET DESIGN

GENERAL OVERVIEW

Page 5: INSTRUCTION SET DESIGN

Common features

• A machine instruction normally consists of– An operation code– One, two or three operand addresses– Various flags

• Some operands can be immediate– Address field contains the value of the

operand instead of its address

Page 6: INSTRUCTION SET DESIGN

Common features

• One or more operands can be in high-speed registers– Dedicated registers:

• Very old solution• Register address can be specified in the

opcode– General purpose registers

Page 7: INSTRUCTION SET DESIGN

Common features

• Memory operand addresses are represented in a compact form– Base + displacement:

• The address is specified by the contents of a base register plus a displacement

–Saves space because displacement is generally small

Page 8: INSTRUCTION SET DESIGN

Objectives

• IS should be– Expressive:

• Powerful instructions– Designed for speed:

• Should be able to run fast and allow extensive prefetching

– Compact:• Faster fetches from disk and from main

memory

Page 9: INSTRUCTION SET DESIGN

Objectives

– User friendly:• Very important when people were expected

to program is assembler –Manufacturers loved that because

• Instruction sets were mostly proprietary:IBM 360/370 was the exception

• Programs could not be ported to a different architecture

Page 10: INSTRUCTION SET DESIGN

The story of Gene Amdahl

• Raised on a farm without electricity until he went to high school

• PhD from U Wisconsin-Madison• Became one of the top architects of the IBM/360

series• Had his next design rejected by IBM• Started his own company (Amdahl ) building big

mainframes and selling them at a much lower cost than comparable IBM machines

Page 11: INSTRUCTION SET DESIGN

How could they do that? (II)

• Amdahl could undersell IBM by focusing on larger "mainframes"

• Andahl's computers were air-cooled while IBM's water-cooled– "[D]ecreased installation costs by $50,000 to

$250,000."• http://www.fundinguniverse.com/company-

histories/amdahl-corporation-history/

Page 12: INSTRUCTION SET DESIGN

How could they do that? (I)

• IBM 360 series was first series of computers with– Very different capacities– Same instruction set

• IBM pricing policy was keeping computer prices proportional to their capacity– Did not reflect proportionally lower

manufacturing cost of high-end machines

Page 13: INSTRUCTION SET DESIGN

The end of the story

• Left Amdahl in 1979 to pursue unsuccessfully several ventures

• Amdahl Computers is now part of Fujitsu and focuses on services

Page 14: INSTRUCTION SET DESIGN

Inherent conflicts

• Expressiveness vs. Speed– CISC instructions were powerful but

microcoded• Compactness vs. Speed

– Many instruction sets had instructions of different length

– Cannot know start of next instruction before decoding the opcode of current one

Page 15: INSTRUCTION SET DESIGN

What is microcode?

• Some machine language below the instruction set– Invisible to the programmer/compiler– Each instruction corresponded to one or

more microinstructions– Some architectures allowed the user to

program new instructions or a whole new instruction set

Page 16: INSTRUCTION SET DESIGN

AN EXAMPLE: IBM 360/370/…

Page 17: INSTRUCTION SET DESIGN

The 360 architecture (I)

• Developed in the 60’s but kept almost unchanged for 30+ years

• Thirty-two bit words and 8-bit bytes• Memory was byte-addressable

– Had 24-bit addresses restricting main memory size to 16 MB (enormous at that time!)

– Later extended to 32 then 64 bits

Page 18: INSTRUCTION SET DESIGN

The 360 architecture (II)

• Instruction set included– 32-bit operations for scientific and engineering

computing (FORTRAN)– Byte-oriented operations for character

processing and decimal arithmetic then judged essential for business applications

• Name of series referred to wide range of applications that could run on the machine

Page 19: INSTRUCTION SET DESIGN

IBM 360 instruction set (I)

• Had multiple instructions formats all with – Mostly 8-bit opcodes– 16 general purpose registers

• RR (register to register)

– 16 bits

Opcode R1 R2

Page 20: INSTRUCTION SET DESIGN

IBM 360 instruction set (II)

• RX (register to/from indexed storage)– 32 bits– Address of memory operand was:

contents of base and index registers plus12-bit displacement D

Opcode R1 X2 D2B2

Page 21: INSTRUCTION SET DESIGN

IBM 360 instruction set (III)

• SI ( storage and immediate)– 32 bits– Has an 8-bit wide immediate field I– Address of memory operand was:

contents of base register B plus12-bit displacement D

Opcode I DB

Page 22: INSTRUCTION SET DESIGN

IBM 360 instruction set (IV)

• SS ( storage to storage)– 48 bits– Two memory operands

• First addresses of fields of length L

Opcode L D1B1 D2B2

Page 23: INSTRUCTION SET DESIGN

IBM 360 instruction set (V)

• S ( storage )– 32 bits with a 16-bit opcode– Mostly for privileged instructions

Opcode DB

Page 24: INSTRUCTION SET DESIGN

Discussion (I)

• Flexible and compact– Multiple instruction sizes

• Must decode current instruction to know start of the next one

• Regular design• Many operations can be RR, RS, RX, SI and SS

(character manipulation and decimal arithmetic)

Page 25: INSTRUCTION SET DESIGN

Discussion (II)

• RX format:– Memory address is indexed by base register

and index register• a[i] can be decomposed into

–Current base register –Offset of a[0] relative to base register– Index i multiplied by size of array

element in index register

Page 26: INSTRUCTION SET DESIGN

Discussion (III)

• Why such a complex addressing format?– Index register was used to access arrays– Base register allowed for a much shorter

address field• 4 bits for base register + 12 bits for

displacementvs

• 24 bits for a full address

Page 27: INSTRUCTION SET DESIGN

THE MIPS INSTRUCTION SET

Page 28: INSTRUCTION SET DESIGN

MIPS (I)

• Originally stood for Microprocessor without Interlocked Pipeline Stages

• First RISC microprocessor• Development started in 1981 under John

Hennessy at Stanford University• Started a company:

MIPS Computer Systems, Inc.

Page 29: INSTRUCTION SET DESIGN

MIPS (II)

• Owned by SGI from 1992 to 1998– Until SGI switched to the Intel Itanium

architecture • Used by used by DEC, NEC, Pyramid

Technology, Siemens Nixdorf, Tandem and others during the late 80s and 90s– Until Intel Pentium took over

• Now primarily used in embedded systems

Page 30: INSTRUCTION SET DESIGN

Overview

• Two versions– MIPS32 with 32-bit addresses (discussed here)– MIPS64 with 64-bit addresses

• Both MIPS architectures have– Thirty-two registers (32 bits on MIPS 32)– A byte-addressable memory– Byte, half-word and word-oriented operations

Page 31: INSTRUCTION SET DESIGN

Bit ordering

• All examples assume that byte-ordering islittle-endian:– Bits are numbered from right to left

31 0

Page 32: INSTRUCTION SET DESIGN

Number representation (I)

• MIPS uses two’s complement representation for negative numbers:– 00….0 represents 0– 00….1 represents 1– 01….1 represents 2n–1 – 1– 10….0 represents – 2n–1 – 11….1 represents – 1

Page 33: INSTRUCTION SET DESIGN

Two-complement representation

• Assume n-bit integers– All positive integers have first bit equal to zero– All negative integers have first bit equal to one

• To negate an integer, we compute its complement to 2n in unsigned arithmetic

Page 34: INSTRUCTION SET DESIGN

Example

• Assume n = 4– 0000, 0001, 0010, 0011, 0100, 0101, 0110 and

0111 represent integers 0 to 7– To find the representation of -3, we do

• 16 -3 = 13, that is, 1101– More generally 1000, 1001, 1010, 1011, 1100,

1101, 1110, 1111 represent negative integers -8 to -1

Page 35: INSTRUCTION SET DESIGN

Another way to look at it (I)

Unsigned Unsigned0 0000 1000 81 0001 1001 92 0010 1010 103 0011 1011 114 0100 1100 125 0101 1101 136 0110 1110 147 0111 1110 15

Page 36: INSTRUCTION SET DESIGN

Another way to look at it (II)

U and S Unsigned Signed0 0000 1000 8 = 16 - 8 - 81 0001 1001 9 = 16 - 7 - 72 0010 1010 10 = 16 - 6 - 63 0011 1011 11 = 16 - 5 - 54 0100 1100 12 = 16 - 4 - 45 0101 1101 13 = 16 - 3 - 36 0110 1110 14 = 16 -2 - 27 0111 1110 15 = 16 -1 - 1

Page 37: INSTRUCTION SET DESIGN

Number representation (II)

• Can create problems when we fetch a byte or half-word into a 32 bit register• If we fetch the 16-bit half-word

• we have two possible outcomes

100……101

100……101all zeroes

100……101all ones

(unsigned)

(signed)

Page 38: INSTRUCTION SET DESIGN

MIPS instruction set

• Designed for speed and prefetching ease• All instructions are 32-bit long

– Five instruction formats• Three basic formats

–R, I and J• Two floating point formats

–FR and FJ

Page 39: INSTRUCTION SET DESIGN

The R format

• Six-bit opcode• R instructions have three operands

• Five bits per register 32 registers– Shamt specifies a shift amount (5 bits)– Funct selects the specific variant of operation

defined in opcode (6 bits)• Many R instructions have an all-zero opcode

functshamtrdopcode rs rt

Page 40: INSTRUCTION SET DESIGN

Register naming conventions

• $s0, $s1, …, $s7 for saved registers– Saved when we do a procedure call

• $t0, …, $t9 for temporary registers– Not saved when you do a procedure call

• $0 is the zero register:– Always contains zeroes

• Other conventions are used for registers used in procedures calls

Page 41: INSTRUCTION SET DESIGN

R format instructions (I)

• Arithmetic instructions– add $sa, $sb, $sc # a = b + c;– sub $sd, $se, $sf # d = e – f;

• Logical instructions– and $sa, $sb, $sc # a = b & c;– or $sd, $se, $sf # d = e | f;– nor $sg, $sh, $si # g = ~(h | i);– xor $sj, $sk, $sl # j = (k&~l)|(~k&l)

Page 42: INSTRUCTION SET DESIGN

Notes

• MIPS logical instructions are bitwise operations– Implement bitwise & and I operations of C

• MIPS has no negation instructions– Use NOR and specify register $0 as one of

the two input registers• $0 is hard-wired to contain zero

– nor $sk, $sl, $0 # k = ~l;

Page 43: INSTRUCTION SET DESIGN

R format instructions (II)

• More arithmetic instructions– addu $s1, $s2, $s3– subu $s1, $s2, $s3– Unsigned versions of add and sub

– Multiply and divide instructions will be covered later

Page 44: INSTRUCTION SET DESIGN

R format instructions (III)

• Shift instructions– sll $s1, $s0, n– slr $s1, $s0, n

• Shift contents of source register $s0 by n bits to the left (sll) or to the right (slr)

• Fill the emptied bits with zero• Store results in destination register $s1

Page 45: INSTRUCTION SET DESIGN

Notes (II)

• A right shift followed by a logical and can be used to extract some specific bits

• If we are interested in bits 14-15 of register $s0– We set up a register $s2 containing 011two

– We do• slr $t0, $s0, 14 # use temporary register $t0• and $s1, $t0, $s2 # answer is in $s1

Page 46: INSTRUCTION SET DESIGN

Notes (III)

• Want to extract bits XY in positions 14 and 15

• slr $t0, $s0,15

• and $s1, $t0, $s2 # $s2 contains mask 011

?????????????????xy??????????

0000000000??????????????????xy

000000000000000000000000000xy

Page 47: INSTRUCTION SET DESIGN

R format instructions (IV)

• Register comparison instructions– slt $t0, $s3, $s4– sltu $t0, $s3, $s4

• Sets register $t0 to 1 if $s3 < $s4and to 0 otherwise– slt does a signed comparison– sltu does an unsigned comparison

Page 48: INSTRUCTION SET DESIGN

R format instructions (IV)

• Jump instructions– jr $s0

• Jump to address contained in register $s0• Since $s0 can contain a 32-bit address, the

jump can go anywhere

– jalr $s0, $s1• Jump to address contained in register $s0

and save address of next instruction in register $s1 (defaults to register 31)

for big jumps

Page 49: INSTRUCTION SET DESIGN

The I format

• Last field can be– a 16-bit displacement:

Address of memory operand is the sum of the contents of register rt and this displacement

– a 16-bit constant:Register rt can then specify a second register operand

opcode rs rt constant/address

Page 50: INSTRUCTION SET DESIGN

Discussion

• I format contains instructions involving– One register and a memory location– Two registers and an immediate value– Two registers and a jump address relative to

the current value of the program counter (PC)• MIPS instruction set uses the same format for

three very different instruction styles – Simplifies decoding hardware

Page 51: INSTRUCTION SET DESIGN

Register and memory instructions

• Transfer data– To a register: load– From a register: store

• No arithmetic instructions as in the IBM/360 IS– All arithmetic and logical operations are either

register to register or immediate to register

Page 52: INSTRUCTION SET DESIGN

Register and memory instructions

• Load instructions– lbu $s1, a($s2)

• Load unsigned byte into register $s1 from memory address obtained by adding the contents of register $s2 to theaddress field a

– lbhu $s1, a($s2)• Load half-word unsigned

Page 53: INSTRUCTION SET DESIGN

Register and memory instructions

• Load instructions (cont’d)– lw $s1, a($s2)

• Load word unsigned– ll $t1, a($s2)

• Load linked: used with store conditional to implement atomic locks

–The famous spinlocksWait for

COSC 4330

Page 54: INSTRUCTION SET DESIGN

Register and memory instructions

• Store instructions – sb $s1, a($s2)

• Store least significant byte– sbh $s1, a($s2)

• Store least significant half-word – sw $s1, a($s2)

• Store word

Page 55: INSTRUCTION SET DESIGN

Register and memory instructions

• Store instructions (cont’d)– sc $t1, a($s2)

• Store conditional–Always follows a load linked–Fails if value in memory changes

since the load linked instruction–Saves contents of $t1 in memory and

sets $t1 to one if store conditional succeeds or to zero otherwise

Wait for COSC 4330

Page 56: INSTRUCTION SET DESIGN

Immediate instructions

• Immediate value is 16-bit wide• addi $1, $2, n

– Store in register $s0 the sum of the contents of register $s1 and the decimal value n

• addiu $1, $2, n– Unsigned version of addi

• andi $1, $2, n• ori $1, $2, n

Page 57: INSTRUCTION SET DESIGN

Three missing instructions

• No subi or subiu– Instead of subtracting an immediate value n

we can add a negative value –n• No load immediate

– Replaced by ori with a zero value– ori $s0, $0, n # load value n into register $s0

↑ Typo in textbook?

Page 58: INSTRUCTION SET DESIGN

Loading 32-bit constants (I)

• lui $s0, n– load upper immediate– Loads the decimal value n into the 16 most

significant bits of register $s0

16 LSB16 MSB

lui $s0, n

Page 59: INSTRUCTION SET DESIGN

Loading 32-bit constants (I)

• Works in combination with ori

16 LSB16 MSB

lui $s0, m ori $s0, #0, n

Page 60: INSTRUCTION SET DESIGN

Other immediate instructions

• Comparison instructions– slti $t0, $s3, n– sltui $t0, $s3, n– Both set register $t0

• To one if $s3 < sign extended value of n• To zero otherwise

– slti does a signed comparison– sltui does an unsigned comparison

Page 61: INSTRUCTION SET DESIGN

Notes

• We should use immediate instructions whenever possible– They are faster and use one register

less than the equivalent R instructions• To isolate bits 14-15 of register $s0, use

– slr $t0, $s0, 14 – and $s1, $t0, 3 # decimal 3 = binary 011

Page 62: INSTRUCTION SET DESIGN

Immediate branch instructions (I)

• All immediate jump and branch instructions use a very specific addressing scheme– Use the program counter as base register

• Makes sense• Both register fields can be used for

conditional branch instructions

Page 63: INSTRUCTION SET DESIGN

Immediate branch instructions (II)

– Immediate field contains a signed number• Can jump ahead or behind the address

indicated by the current value of the PC + 4– Address is multiplied by four before being

added to the value of the PC

New PC = Old PC + 4 + 4×n

Page 64: INSTRUCTION SET DESIGN

Why PC + 4

• Because– By the time the MIPS CPU adds 4×nto the PC

register, it will contain the address of the next instruction

Page 65: INSTRUCTION SET DESIGN

Immediate branch instructions (III)

– Works well because• PC value is a multiple of four as

instructions are properly aligned at addresses that are multiple of 4

• Solution extends the range of the jump from 215 B = 32 KB to 217 B = 128 KB

– can use J or JR for bigger jumps

Page 66: INSTRUCTION SET DESIGN

Immediate branch instructions (IV)

• beq $s0, $1 a– Branch on equal– Jump to address computed by adding 4×a to

the current value of the PC + 4 if the values of registers $s0 and $s1 are equal

• bne $s0, $1 a– Branch on not equal

Page 67: INSTRUCTION SET DESIGN

The J format (I)

• Sole operand is a 26-bit address• Jump instructions

– j a• Unconditionally jump to a new address

– jal a• Unconditionally jump to a new address and

store address of next instruction in $ra

opcode rs rt address

Page 68: INSTRUCTION SET DESIGN

The J format (I)

• Note that jal has an implicit operand– Register $ra (stands for return address)

• Always register 31

• In general, implicit operands– Allow more compact instruction formats– Complicate register management

opcode rs rt address

Page 69: INSTRUCTION SET DESIGN

Computing the new address

• Obtained as follows– Bits 1:0 are zero (address is multiple of 4)– Bits 28:2 come from jump operand (26 bits)– Bits 31:29 come from PC+4

Allows to jump to anywhere in memory

Page 70: INSTRUCTION SET DESIGN

Observations

• The overall philosophy used to design the MIPS instruction set was– Favor simplicity and regularity

• Only three instruction formats– Optimize for the more frequent cases

• Immediate to register instructions– Make good compromises

• …

Page 71: INSTRUCTION SET DESIGN

Comparison with IBM 360 IS

IBM 360• Variable length

instructions• Sixteen GP registers• RR format has two

register operands• RS format• SI format

• SS format

MIPS• Fixed-size instructions

• Thirty-two GP registers• R format has three

register operands• An option of I format• No, but the equivalent of

a non-existing RI format• No equivalent

Page 72: INSTRUCTION SET DESIGN

The stack

• Used to store saved registers• LIFO structure starting at a high address value

and growing downwards• MIPS software reserves register 29 for the stack

pointer ($sp)• We can push registers to the stack and pop

them later

Page 73: INSTRUCTION SET DESIGN

The stack

Stack

Stack pointer sp

Page 74: INSTRUCTION SET DESIGN

Handling procedure calls

• MIPS software uses the following conventions– $a0, … $a3 are the four argument registers

that can be used to pass parameters– $v0 and $v1 are the two value registers that

can be used to return values– $ra is the register containing the

return address

Page 75: INSTRUCTION SET DESIGN

Simple procedure call (I)

• Before starting the new procedure, we must save the registers used by the calling procedures– Not all of them

• Save the eight registers $s0, $s1, …, $s7 • Do not save the ten temporary registers

$t0, …, $t9• Must also restore these registers when we exit

the procedure

Page 76: INSTRUCTION SET DESIGN

Simple procedure call (II)

• At call time – addi $sp, $sp, -32 # eight times four bytes

sw $s7, 28($sp)sw $s6, 24($sp)sw $s5, 20($sp)sw $s4, 16($sp)sw $s3, 12($sp)sw $s2, 8($sp)sw $s1, 4($sp)sw $s0, 0($sp

Reality Check:We will only save theregisters that will bereused by the callee

Page 77: INSTRUCTION SET DESIGN

Simple procedure call (III)

• At return time – lw $s0, 0($sp) # restore the registers $s0 to $s7

lw $s1, 4($sp)lw $s2, 8($sp)lw $s3, 12($sp)lw $s4, 16($sp)lw $s5, 20($sp)lw $s6, 24($sp)lw $s7, 28($sp)addi $sp, $sp, 32 # eight times four bytes

Page 78: INSTRUCTION SET DESIGN

Nested procedures (I)

• Procedures that call other procedures – Including themselves: recursive procedures

• Likely to reuse argument registers and temporary registers

• Caller will save the argument registers and temporary registers it will need after the call

• Callee will save the saved registers of the caller before reusing them

Page 79: INSTRUCTION SET DESIGN

Nested procedures (II)

• All saved registers are restored when the procedure returns and the stack is shrunk

Page 80: INSTRUCTION SET DESIGN

The assembler (I)

• Helps the programmer with– Symbolic names

• Arguments of jump and branch instructions are labels rather than numerical constants

beq $s0, $s1 done….done: …

Page 81: INSTRUCTION SET DESIGN

The assembler (II)

– Pseudo-instructions• To reserve memory locations for data• To create user-friendly specialized versions

of very general instructions–bzero $s0, addressfor–beq $s0, $0, address

Page 82: INSTRUCTION SET DESIGN

A review question

Which MIPS instructions involve– Three explicit operands?– Two explicit operands?– One explicit operand?

Page 83: INSTRUCTION SET DESIGN

Answer (I)

• Three explicit operands:– All R format instructions but jr and jalr– All I format instructions involving an

immediate value but lui – All I format instructions involving a conditional

branch

Page 84: INSTRUCTION SET DESIGN

Answer (II)

• Two explicit operands:– All I format instructions involving a load or a

store– Load upper immediate (lui)– Jump and link to register (jalr)

Page 85: INSTRUCTION SET DESIGN

Answer (III)

• One explicit operand:– Jump register instruction (jr)– Both J format instructions

• jal has an implicit second operand that it uses to store the return address ($ra)

Page 86: INSTRUCTION SET DESIGN

OTHER EXAMPLES

Page 87: INSTRUCTION SET DESIGN

Another RISC IS: ARM (I)

• ARM stands for Advanced RISC Machine• Originally developed as a CPU architecture for a

desktop computer manufactured by Acorn in Britain– Now dominates the embedded system market

• Company has no foundry– It licenses its products to manufacturers

Page 88: INSTRUCTION SET DESIGN

Another RISC IS: ARM (II)

• Used in cell phones, embedded systems, …• Same 32-bit instruction formats• Only 8 registers• Nine addressing modes

– Including autoincrement• Branches use condition codes of arithmetic unit

Page 89: INSTRUCTION SET DESIGN

Another RISC IS: ARM

• All instructions have a 4-bit condition code allowing their conditional execution– Claimed to faster than using a branch

• NO zero register:– Includes instructions like logical not, load

immediate, move (R to R)

Page 90: INSTRUCTION SET DESIGN
Page 91: INSTRUCTION SET DESIGN

The x86 instruction set (I)

• Not the result of a well-thought design process– Evolved over the years

• 8086 was the first x86 processor architecture– Announced in 1978– Sixteen-bit architecture– Assembly language-compatible extension to

Intel 8080 (8-bit microprocessor)

Page 92: INSTRUCTION SET DESIGN

The x86 instruction set (II)

• New instructions were added over the years, mostly by Intel , but also by AMD– Floating-point instructions– Multimedia instructions– More floating-point instructions– Vector computing

• 80836 was the first 32-bit processor– Introduced more interesting instructions

Page 93: INSTRUCTION SET DESIGN

The x86 instruction set (III)

• AMD extended the x86 architecture to 64-bit address space and registers in 2003

• Intel followed AMD’s lead the next year

Page 94: INSTRUCTION SET DESIGN

The x86 instruction set (IV)

• The result is a huge instruction set combining– Old instructions kept to insure backward

compatibility• “Golden handcuff”

– Newer 32-bit instructions that are in use today

Page 95: INSTRUCTION SET DESIGN

A curious tradeoff

• Complexity of x86 instruction set– Complicates the design of x86

microprocessors• More work for Intel and AMD architects

– Effectively prevents other manufacturers to design and sell x86-compatible microprocessors

• A mixed blessing for the duopoly Intel/AMD

Page 96: INSTRUCTION SET DESIGN

Stack-oriented architectures (I)

• A rarity in microprocessor architecture– Bad solution

• Used in many interpreted languages– Java virtual machine, Python– Big exceptions are Dalvik and Lua

• Register-based virtual machines

Page 97: INSTRUCTION SET DESIGN

Interpreted vs. Compiled (I)

• Compiled language

• Machine code is directly executable

Sourcecode Compiler

Machinecode

Page 98: INSTRUCTION SET DESIGN

Interpreted vs. Compiled (II)

• Interpreted language

Compiler BytecodeSourcecode

InterpreterBytecode Results

Page 99: INSTRUCTION SET DESIGN

Stack-oriented architectures (II)

• Named registers replaced by a stack of registers

• Two basic operations:• PUSH <address>

push on stack contents of memory address

• POP <address>pop top of stack and store it at specified memory address

Page 100: INSTRUCTION SET DESIGN

Stack-oriented architecture (III)

• Binary arithmetic and logical operations– Have both operands on the top of the stack– Replace them by the result

• Example:

……AB

……

A+BA+B

Page 101: INSTRUCTION SET DESIGN

Tradeoff

• Advantages– Simple compilers and interpreters– Very compact object code

• Main disadvantage– More memory references

• Cannot save partial results into a register

Page 102: INSTRUCTION SET DESIGN

CONCLUSION

Page 103: INSTRUCTION SET DESIGN

Good design principles (I)

• Simplicity favors regularity– As few instruction formats as possible for

easier decoding• Smaller is faster

– No complicated instructions

Page 104: INSTRUCTION SET DESIGN

Good design principles (II)

• We should make the common case faster– PC-relative addressing for branches

• Good design requires good compromises– Limiting address displacements and

immediate values to 16 bits ensures that all instructions can fit in a 32-bit word

– Weird J format addressing mode

Page 105: INSTRUCTION SET DESIGN

Myths (I)

• Adding more powerful instructions will increase performance– IBM 360 has an instruction saving multiple

registers– DEC VAX has an instruction computing

polynomial terms• These complex instructions limit pipelining

options

Page 106: INSTRUCTION SET DESIGN

Myths (II)

• Writing programs in assembly language produces faster code than that generated by a compiler– Compilers are now better than humans for

register allocations• In addition, programs written in assembly

language cannot be ported to other CPU architectures– Think of Macintosh programs

Page 107: INSTRUCTION SET DESIGN

Myths (III)

• Backward compatibility prevents instruction sets from evolving– Not true for x86 architecture

• Can add new instructions• Write compilers that avoid the older

instructions