Lecture 1c
Lecture 1c
Opcode R1 R2
IBM 360 instruction set (II)
• RX (register to/from indexed storage)
– 32 bits
– Address of memory operand was:
contents of base and index registers plus
12-bit displacement D
Opcode R1 X2 B2 D2
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 plus
12-bit displacement D
Opcode I B D
IBM 360 instruction set (IV)
• SS ( storage to storage)
– 48 bits
– Two memory operands
• First addresses of fields of length L
Opcode L B1 D1 B2 D2
IBM 360 instruction set (V)
• S ( storage )
– 32 bits with a 16-bit opcode
– Mostly for privileged instructions
Opcode B D
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)
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
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
displacement
vs
• 24 bits for a full address
THE MIPS INSTRUCTION SET
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.
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
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
Bit ordering
• All examples assume that byte-ordering is
little-endian:
– Bits are numbered from right to left
31 0
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
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
100……101
• 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
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
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)
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;
R format instructions (II)
• More arithmetic instructions
– addu $s1, $s2, $s3
– subu $s1, $s2, $s3
– Unsigned versions of add and sub
?????????????????xy??????????
• slr $t0, $s0,15
0000000000??????????????????xy
16 MSB 16 LSB
Loading 32-bit constants (I)
• Works in combination with ori
16 MSB 16 LSB
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
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
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
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
Allows to jump to
anywhere in memory
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
•…
Comparison with IBM 360 IS
IBM 360 MIPS
• Variable length • Fixed-size instructions
instructions
• Sixteen GP registers • Thirty-two GP registers
• RR format has two • R format has three
register operands register operands
• RS format • An option of I format
• SI format • No, but the equivalent of
a non-existing RI format
• SS format • No equivalent
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
The stack
Stack
Stack pointer sp
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
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
Simple procedure call (II)
• At call time
– addi $sp, $sp, -32 # eight times four bytes
sw $s7, 28($sp)
sw $s6, 24($sp) Reality Check:
sw $s5, 20($sp) We will only save the
sw $s4, 16($sp)
registers that will be
sw $s3, 12($sp)
sw $s2, 8($sp)
reused by the callee
sw $s1, 4($sp)
sw $s0, 0($sp
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
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
Nested procedures (II)
• All saved registers are restored when the
procedure returns and the stack is shrunk
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: …
The assembler (II)
– Pseudo-instructions
• To reserve memory locations for data
• To create user-friendly specialized versions
of very general instructions
– bzero $s0, address
for
– beq $s0, $0, address
A review question
Which MIPS instructions involve
– Three explicit operands?
– Two explicit operands?
– One explicit operand?
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
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)
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)
OTHER EXAMPLES
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
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
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)
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)
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
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
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
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
Source Machine
code Compiler
code
Source
Compiler Bytecode
code