Sap - 2
Sap - 2
Chapter 6 <1>
Chapter 6 :: Topics
• Introduction
• Assembly Language
• Machine Language
• Programming
• Addressing Modes
• Lights, Camera, Action: Compiling,
Assembling, & Loading
• Odds and Ends
Chapter 6 <2>
Introduction
• Jumping up a few levels Application
Software
programs
of abstraction Operating
Systems
device drivers
• Architecture: Architecture
instructions
registers
to implement an Devices
transistors
diodes
architecture in hardware Physics electrons
(covered in Chapter 7)
Chapter 6 <3>
Assembly Language
• Instructions: commands in a computer’s
language
– Assembly language: human-readable format of
instructions
– Machine language: computer-readable format
(1’s and 0’s)
• MIPS architecture:
– Developed by John Hennessy and his colleagues at
Stanford and in the 1980’s.
– Used in many commercial systems, including
Silicon Graphics, Nintendo, and Cisco
Chapter 6 <4>
John Hennessy
• President of Stanford University
• Professor of Electrical Engineering
and Computer Science at Stanford
since 1977
• Coinvented the Reduced
Instruction Set Computer (RISC)
with David Patterson
• Developed the MIPS architecture at
Stanford in 1984 and cofounded
MIPS Computer Systems
• As of 2004, over 300 million MIPS
microprocessors have been sold
Chapter 6 <5>
Architecture Design Principles
Underlying design principles, as articulated by
Hennessy and Patterson:
1.Simplicity favors regularity
2.Make the common case fast
3.Smaller is faster
4.Good design demands good compromises
Chapter 6 <6>
Instructions: Addition
Chapter 6 <7>
Instructions: Subtraction
• Similar to addition - only mnemonic changes
• sub: mnemonic
• b, c: source operands
• a: destination operand
Chapter 6 <8>
Design Principle 1
Simplicity favors regularity
• Consistent instruction format
• Same number of operands (two sources and
one destination)
• Easier to encode and handle in hardware
Chapter 6 <9>
Multiple Instructions
• More complex code is handled by multiple
MIPS instructions.
C Code MIPS assembly code
a = b + c - d; add t, b, c # t = b + c
sub a, t, d # a = t - d
Chapter 6 <10>
Design Principle 2
Make the common case fast
• MIPS includes only simple, commonly used instructions
• Hardware to decode and execute instructions can be
simple, small, and fast
• More complex instructions (that are less common)
performed using multiple simple instructions
• MIPS is a reduced instruction set computer (RISC), with
a small number of simple instructions
• Other architectures, such as Intel’s x86, are complex
instruction set computers (CISC)
Chapter 6 <11>
Operands
• Operand location: physical location in
computer
– Registers
– Memory
– Constants (also called immediates)
Chapter 6 <12>
Operands: Registers
• MIPS has 32 32-bit registers
• Registers are faster than memory
• MIPS called “32-bit architecture” because
it operates on 32-bit data
Chapter 6 <13>
Design Principle 3
Smaller is Faster
• MIPS includes only a small number of
registers
Chapter 6 <14>
MIPS Register Set
Name Register Number Usage
$0 0 the constant value 0
$at 1 assembler temporary
$v0-$v1 2-3 Function return values
$a0-$a3 4-7 Function arguments
$t0-$t7 8-15 temporaries
$s0-$s7 16-23 saved variables
$t8-$t9 24-25 more temporaries
$k0-$k1 26-27 OS temporaries
$gp 28 global pointer
$sp 29 stack pointer
$fp 30 frame pointer
$ra 31 Function return address
Chapter 6 <15>
Operands: Registers
• Registers:
– $ before name
– Example: $0, “register zero”, “dollar zero”
• Registers used for specific purposes:
• $0 always holds the constant value 0.
• the saved registers, $s0-$s7, used to hold
variables
• the temporary registers, $t0 - $t9, used to
hold intermediate values during a larger
computation
• Discuss others later
Chapter 6 <16>
Instructions with Registers
• Revisit add instruction
Chapter 6 <17>
Operands: Memory
• Too much data to fit in only 32 registers
• Store more data in memory
• Memory is large, but slow
• Commonly used variables kept in registers
Chapter 6 <18>
Word-Addressable Memory
• Each 32-bit data word has a unique
address
Word Address Data
00000003 4 0 F 3 0 7 8 8 Word 3
00000002 0 1 E E 2 8 4 2 Word 2
00000001 F 2 F 1 A C 0 7 Word 1
00000000 A B C D E F 7 8 Word 0
Note: MIPS uses byte-addressable memory, which we’ll talk about next.
Chapter 6 <19>
Reading Word-Addressable Memory
• Memory read called load
• Mnemonic: load word (lw)
• Format:
lw $s0, 5($t1)
• Address calculation:
– add base address ($t1) to the offset (5)
– address = ($t1 + 5)
• Result:
– $s0 holds the value at address ($t1 + 5)
00000003 4 0 F 3 0 7 8 8 Word 3
00000002 0 1 E E 2 8 4 2 Word 2
00000001 F 2 F 1 A C 0 7 Word 1
00000000 A B C D E F 7 8 Word 0
Chapter 6 <21>
Writing Word-Addressable Memory
• Memory write are called store
• Mnemonic: store word (sw)
Chapter 6 <22>
Writing Word-Addressable Memory
• Example: Write (store) the value in $t4
into memory address 7
– add the base address ($0) to the offset (0x7)
– address: ($0 + 0x7) = 7
Offset can be written in decimal (default) or hexadecimal
Assembly code
sw $t4, 0x7($0) # write the value in $t4
# to memory word 7
Word Address Data
00000003 4 0 F 3 0 7 8 8 Word 3
00000002 0 1 E E 2 8 4 2 Word 2
00000001 F 2 F 1 A C 0 7 Word 1
00000000 A B C D E F 7 8 Word 0
Chapter 6 <23>
Byte-Addressable Memory
• Each data byte has unique address
• Load/store words or single bytes: load byte (lb) and
store byte (sb)
• 32-bit word = 4 bytes, so word address increments by 4
0000000C 4 0 F 3 0 7 8 8 Word 3
00000008 0 1 E E 2 8 4 2 Word 2
00000004 F 2 F 1 A C 0 7 Word 1
00000000 A B C D E F 7 8 Word 0
width = 4 bytes
Chapter 6 <24>
Reading Byte-Addressable Memory
• The address of a memory word must now
be multiplied by 4. For example,
– the address of memory word 2 is 2 × 4 = 8
– the address of memory word 10 is 10 × 4 = 40
(0x28)
• MIPS is byte-addressed, not word-
addressed
Chapter 6 <25>
Reading Byte-Addressable Memory
• Example: Load a word of data at memory
address 4 into $s3.
• $s3 holds the value 0xF2F1AC07 after
load
MIPS assembly code
lw $s3, 4($0) # read word at address 4 into $s3
0000000C 4 0 F 3 0 7 8 8 Word 3
00000008 0 1 E E 2 8 4 2 Word 2
00000004 F 2 F 1 A C 0 7 Word 1
00000000 A B C D E F 7 8 Word 0
width = 4 bytes
Chapter 6 <26>
Writing Byte-Addressable Memory
• Example: stores the value held in $t7
into memory address 0x2C (44)
0000000C 4 0 F 3 0 7 8 8 Word 3
00000008 0 1 E E 2 8 4 2 Word 2
00000004 F 2 F 1 A C 0 7 Word 1
00000000 A B C D E F 7 8 Word 0
width = 4 bytes
Chapter 6 <27>
Big-Endian & Little-Endian Memory
• How to number bytes within a word?
• Little-endian: byte numbers start at the little (least
significant) end
• Big-endian: byte numbers start at the big (most
significant) end
• Word address is the same for big- or little-endian
Big-Endian Little-Endian
Byte Word Byte
Address Address Address
C D E F C F E D C
8 9 A B 8 B A 9 8
4 5 6 7 4 7 6 5 4
0 1 2 3 0 3 2 1 0
MSB LSB MSB LSB
Chapter 6 <28>
Big-Endian & Little-Endian Memory
• Jonathan Swift’s Gulliver’s Travels: the Little-Endians
broke their eggs on the little end of the egg and the Big-
Endians broke their eggs on the big end
• It doesn’t really matter which addressing type used –
except when the two systems need to share data!
Big-Endian Little-Endian
Byte Word Byte
Address Address Address
C D E F C F E D C
8 9 A B 8 B A 9 8
4 5 6 7 4 7 6 5 4
0 1 2 3 0 3 2 1 0
MSB LSB MSB LSB
Chapter 6 <29>
Big-Endian & Little-Endian Example
• Suppose $t0 initially contains 0x23456789
• After following code runs on big-endian system, what
value is $s0?
• In a little-endian system?
sw $t0, 0($0)
lb $s0, 1($0)
Chapter 6 <30>
Big-Endian & Little-Endian Example
• Suppose $t0 initially contains 0x23456789
• After following code runs on big-endian system, what
value is $s0?
• In a little-endian system?
sw $t0, 0($0)
lb $s0, 1($0)
• Big-endian: 0x00000045
• Little-endian: 0x00000067
Big-Endian Little-Endian
Word
Byte Address 0 1 2 3 Address 3 2 1 0 Byte Address
Data Value 23 45 67 89 0 23 45 67 89 Data Value
MSB LSB MSB LSB
Chapter 6 <31>
Design Principle 4
Good design demands good compromises
• Multiple instruction formats allow flexibility
- add, sub: use 3 register operands
- lw, sw: use 2 register operands and a constant
• Number of instruction formats kept small
- to adhere to design principles 1 and 3
(simplicity favors regularity and smaller is
faster).
Chapter 6 <32>
Operands: Constants/Immediates
• lw and sw use constants or immediates
• immediately available from instruction
• 16-bit two’s complement number
• addi: add immediate
• Subtract immediate (subi) necessary?
Chapter 6 <33>
Machine Language
• Binary representation of instructions
• Computers only understand 1’s and 0’s
• 32-bit instructions
– Simplicity favors regularity: 32-bit data &
instructions
• 3 instruction formats:
– R-Type: register operands
– I-Type: immediate operand
– J-Type: for jumping (discuss later)
Chapter 6 <34>
R-Type
• Register-type
• 3 register operands:
– rs, rt: source registers
– rd: destination register
• Other fields:
– op: the operation code or opcode (0 for R-type instructions)
– funct: the function
with opcode, tells computer what operation to perform
– shamt: the shift amount for shift instructions, otherwise it’s 0
R-Type
op rs rt rd shamt funct
6 bits 5 bits 5 bits 5 bits 5 bits 6 bits
Chapter 6 <35>
R-Type Examples
Assembly Code Field Values
op rs rt rd shamt funct
Machine Code
op rs rt rd shamt funct
I-Type
op rs rt imm
6 bits 5 bits 5 bits 16 bits
Chapter 6 <37>
I-Type Examples
Assembly Code Field Values
op rs rt imm
lw $t2, 32($0) 35 0 10 32
sw $s1, 4($t1) 43 9 17 4
6 bits 5 bits 5 bits 16 bits
Machine Code
Note the differing order of op rs rt imm
registers in assembly and
001000 10001 10000 0000 0000 0000 0101 (0x22300005)
machine codes:
001000 10011 01000 1111 1111 1111 0100 (0x2268FFF4)
addi rt, rs, imm
100011 00000 01010 0000 0000 0010 0000 (0x8C0A0020)
lw rt, imm(rs)
101011 01001 10001 0000 0000 0000 0100 (0xAD310004)
sw rt, imm(rs)
6 bits 5 bits 5 bits 16 bits
Chapter 6 <38>
Machine Language: J-Type
• Jump-type
• 26-bit address operand (addr)
• Used for jump instructions (j)
J-Type
op addr
6 bits 26 bits
Chapter 6 <39>
Review: Instruction Formats
R-Type
op rs rt rd shamt funct
6 bits 5 bits 5 bits 5 bits 5 bits 6 bits
I-Type
op rs rt imm
6 bits 5 bits 5 bits 16 bits
J-Type
op addr
6 bits 26 bits
Chapter 6 <40>
Power of the Stored Program
• 32-bit instructions & data stored in memory
• Sequence of instructions: only difference
between two applications
• To run a new program:
– No rewiring required
– Simply store new program in memory
• Program Execution:
– Processor fetches (reads) instructions from memory
in sequence
– Processor performs the specified operation
Chapter 6 <41>
The Stored Program
Assembly Code Machine Code
lw $t2, 32($0) 0x8C0A0020
add $s0, $s1, $s2 0x02328020
addi $t0, $s3, -12 0x2268FFF4
sub $t0, $t3, $t5 0x016D4022
Stored Program
Address Instructions Program Counter
(PC): keeps track of
0040000C 0 1 6 D 4 0 2 2 current instruction
00400008 2 2 6 8 F F F 4
00400004 0 2 3 2 8 0 2 0
00400000 8 C0 A0 0 2 0 PC
Main Memory
Chapter 6 <42>
Interpreting Machine Code
• Start with opcode: tells how to parse rest
• If opcode all 0’s
– R-type instruction
– Function bits tell operation
• Otherwise
– opcode tells operation
Machine Code Field Values Assembly Code
op rs rt imm op rs rt imm
(0x2237FFF1) 001000 10001 10111 1111 1111 1111 0001 8 17 23 -15 addi $s7, $s1, -15
2 2 3 7 F F F 1
(0x02F34022) 000000 10111 10011 01000 00000 100010 0 23 19 8 0 34 sub $t0, $s7, $s3
0 2 F 3 4 0 2 2
Chapter 6 <43>
Programming
• High-level languages:
– e.g., C, Java, Python
– Written at higher level of abstraction
• Common high-level software constructs:
– if/else statements
– for loops
– while loops
– arrays
– function calls
Chapter 6 <44>
Ada Lovelace, 1815-1852
Chapter 6 <45>
Logical Instructions
• and, or, xor, nor
– and: useful for masking bits
• Masking all but the least significant byte of a value:
0xF234012F AND 0x000000FF = 0x0000002F
– or: useful for combining bit fields
• Combine 0xF2340000 with 0x000012BC:
0xF2340000 OR 0x000012BC = 0xF23412BC
– nor: useful for inverting bits:
• A NOR $0 = NOT A
• andi, ori, xori
– 16-bit immediate is zero-extended (not sign-extended)
– nori not needed
Chapter 6 <46>
Logical Instructions Example 1
Source Registers
$s1 1111 1111 1111 1111 0000 0000 0000 0000
Chapter 6 <47>
Logical Instructions Example 1
Source Registers
$s1 1111 1111 1111 1111 0000 0000 0000 0000
Chapter 6 <48>
Logical Instructions Example 2
Source Values
$s1 0000 0000 0000 0000 0000 0000 1111 1111
Chapter 6 <49>
Logical Instructions Example 2
Source Values
$s1 0000 0000 0000 0000 0000 0000 1111 1111
Chapter 6 <50>
Shift Instructions
• sll: shift left logical
– Example: sll $t0, $t1, 5 # $t0 <= $t1 << 5
• srl: shift right logical
– Example: srl $t0, $t1, 5 # $t0 <= $t1 >> 5
• sra: shift right arithmetic
– Example: sra $t0, $t1, 5 # $t0 <= $t1 >>> 5
Chapter 6 <51>
Variable Shift Instructions
• sllv: shift left logical variable
– Example: sllv $t0, $t1, $t2 # $t0 <= $t1 << $t2
• srlv: shift right logical variable
– Example: srlv $t0, $t1, $t2 # $t0 <= $t1 >> $t2
• srav: shift right arithmetic variable
– Example: srav $t0, $t1, $t2 # $t0 <= $t1 >>> $t2
Chapter 6 <52>
Shift Instructions
Assembly Code Field Values
op rs rt rd shamt funct
Machine Code
op rs rt rd shamt funct
Chapter 6 <53>
Generating Constants
• 16-bit constants using addi:
C Code MIPS assembly code
// int is a 32-bit signed word # $s0 = a
int a = 0x4f3c; addi $s0, $0, 0x4f3c
Chapter 6 <54>
Multiplication, Division
• Special registers: lo, hi
• 32 × 32 multiplication, 64 bit result
– mult $s0, $s1
– Result in {hi, lo}
• 32-bit division, 32-bit quotient, remainder
– div $s0, $s1
– Quotient in lo
– Remainder in hi
• Moves from lo/hi special registers
– mflo $s2
– mfhi $s3
Chapter 6 <55>
Branching
• Execute instructions out of sequence
• Types of branches:
– Conditional
• branch if equal (beq)
• branch if not equal (bne)
– Unconditional
• jump (j)
• jump register (jr)
• jump and link (jal)
Chapter 6 <56>
Review: The Stored Program
Assembly Code Machine Code
lw $t2, 32($0) 0x8C0A0020
add $s0, $s1, $s2 0x02328020
addi $t0, $s3, -12 0x2268FFF4
sub $t0, $t3, $t5 0x016D4022
Stored Program
Address Instructions
0040000C 0 1 6 D 4 0 2 2
00400008 2 2 6 8 F F F 4
00400004 0 2 3 2 8 0 2 0
00400000 8 C0 A0 0 2 0 PC
Main Memory
Chapter 6 <57>
Conditional Branching (beq)
# MIPS assembly
addi $s0, $0, 4 # $s0 = 0 + 4 = 4
addi $s1, $0, 1 # $s1 = 0 + 1 = 1
sll $s1, $s1, 2 # $s1 = 1 << 2 = 4
beq $s0, $s1, target # branch is taken
addi $s1, $s1, 1 # not executed
sub $s1, $s1, $s0 # not executed
target: # label
add $s1, $s1, $s0 # $s1 = 4 + 4 = 8
Chapter 6 <58>
The Branch Not Taken (bne)
# MIPS assembly
addi $s0, $0, 4 # $s0 = 0 + 4 = 4
addi $s1, $0, 1 # $s1 = 0 + 1 = 1
sll $s1, $s1, 2 # $s1 = 1 << 2 = 4
bne $s0, $s1, target # branch not taken
addi $s1, $s1, 1 # $s1 = 4 + 1 = 5
sub $s1, $s1, $s0 # $s1 = 5 – 4 = 1
target:
add $s1, $s1, $s0 # $s1 = 1 + 4 = 5
Chapter 6 <59>
Unconditional Branching (j)
# MIPS assembly
addi $s0, $0, 4 # $s0 = 4
addi $s1, $0, 1 # $s1 = 1
j target # jump to target
sra $s1, $s1, 2 # not executed
addi $s1, $s1, 1 # not executed
sub $s1, $s1, $s0 # not executed
target:
add $s1, $s1, $s0 # $s1 = 1 + 4 = 5
Chapter 6 <60>
Unconditional Branching (jr)
# MIPS assembly
0x00002000 addi $s0, $0, 0x2010
0x00002004 jr $s0
0x00002008 addi $s1, $0, 1
0x0000200C sra $s1, $s1, 2
0x00002010 lw $s3, 44($s1)
jr is an R-type instruction.
Chapter 6 <61>
High-Level Code Constructs
• if statements
• if/else statements
• while loops
• for loops
Chapter 6 <62>
If Statement
f = f – i;
Chapter 6 <63>
If Statement
Chapter 6 <64>
If/Else Statement
if (i == j)
f = g + h;
else
f = f – i;
Chapter 6 <65>
If/Else Statement
Chapter 6 <66>
While Loops
Chapter 6 <67>
While Loops
Chapter 6 <68>
For Loops
for (initialization; condition; loop operation)
statement
Chapter 6 <69>
For Loops
Chapter 6 <70>
For Loops
Chapter 6 <71>
For Loops
Chapter 6 <72>
Less Than Comparison
Chapter 6 <73>
Less Than Comparison
Chapter 6 <74>
Arrays
• Access large amounts of similar data
• Index: access each element
• Size: number of elements
Chapter 6 <75>
Arrays
• 5-element array
• Base address = 0x12348000 (address of first element,
array[0])
• First step in accessing an array: load base address into a
register
0x12340010 array[4]
0x1234800C array[3]
0x12348008 array[2]
0x12348004 array[1]
0x12348000 array[0]
Chapter 6 <76>
Accessing Arrays
// C Code
int array[5];
array[0] = array[0] * 2;
array[1] = array[1] * 2;
Chapter 6 <77>
Accessing Arrays
// C Code
int array[5];
array[0] = array[0] * 2;
array[1] = array[1] * 2;
Chapter 6 <78>
Arrays using For Loops
// C Code
int array[1000];
int i;
Chapter 6 <79>
Arrays Using For Loops
# MIPS assembly code
# $s0 = array base address, $s1 = i
# initialization code
lui $s0, 0x23B8 # $s0 = 0x23B80000
ori $s0, $s0, 0xF000 # $s0 = 0x23B8F000
addi $s1, $0, 0 # i = 0
addi $t2, $0, 1000 # $t2 = 1000
loop:
slt $t0, $s1, $t2 # i < 1000?
beq $t0, $0, done # if not then done
sll $t0, $s1, 2 # $t0 = i * 4 (byte offset)
add $t0, $t0, $s0 # address of array[i]
lw $t1, 0($t0) # $t1 = array[i]
sll $t1, $t1, 3 # $t1 = array[i] * 8
sw $t1, 0($t0) # array[i] = array[i] * 8
addi $s1, $s1, 1 # i = i + 1
j loop # repeat
done:
Chapter 6 <80>
ASCII Code
• American Standard Code for Information
Interchange
• Each text character has unique byte
value
– For example, S = 0x53, a = 0x61, A = 0x41
– Lower-case and upper-case differ by 0x20 (32)
Chapter 6 <81>
Cast of Characters
Chapter 6 <82>
Function Calls
• Caller: calling function (in this case, main)
• Callee: called function (in this case, sum)
C Code
void main()
{
int y;
y = sum(42, 7);
...
}
Chapter 6 <83>
Function Conventions
• Caller:
– passes arguments to callee
– jumps to callee
• Callee:
– performs the function
– returns result to caller
– returns to point of call
– must not overwrite registers or memory needed by
caller
Chapter 6 <84>
MIPS Function Conventions
• Call Function: jump and link (jal)
• Return from function: jump register (jr)
• Arguments: $a0 - $a3
• Return value: $v0
Chapter 6 <85>
Function Calls
void simple() {
0x00401020 simple: jr $ra
return;
}
Chapter 6 <86>
Function Calls
void simple() {
0x00401020 simple: jr $ra
return;
}
Chapter 6 <87>
Input Arguments & Return Value
MIPS conventions:
• Argument values: $a0 - $a3
• Return value: $v0
Chapter 6 <88>
Input Arguments & Return Value
C Code
int main()
{
int y;
...
y = diffofsums(2, 3, 4, 5); // 4 arguments
...
}
Chapter 6 <89>
Input Arguments & Return Value
MIPS assembly code
# $s0 = y
main:
...
addi $a0, $0, 2 # argument 0 = 2
addi $a1, $0, 3 # argument 1 = 3
addi $a2, $0, 4 # argument 2 = 4
addi $a3, $0, 5 # argument 3 = 5
jal diffofsums # call Function
add $s0, $v0, $0 # y = returned value
...
# $s0 = result
diffofsums:
add $t0, $a0, $a1 # $t0 = f + g
add $t1, $a2, $a3 # $t1 = h + i
sub $s0, $t0, $t1 # result = (f + g) - (h + i)
add $v0, $s0, $0 # put return value in $v0
jr $ra # return to caller
Chapter 6 <90>
Input Arguments & Return Value
MIPS assembly code
# $s0 = result
diffofsums:
add $t0, $a0, $a1 # $t0 = f + g
add $t1, $a2, $a3 # $t1 = h + i
sub $s0, $t0, $t1 # result = (f + g) - (h + i)
add $v0, $s0, $0 # put return value in $v0
jr $ra # return to caller
Chapter 6 <91>
The Stack
• Memory used to temporarily
save variables
• Like stack of dishes, last-in-
first-out (LIFO) queue
• Expands: uses more memory
when more space needed
• Contracts: uses less memory
when the space is no longer
needed
Chapter 6 <92>
The Stack
• Grows down (from higher to lower memory
addresses)
• Stack pointer: $sp points to top of the stack
Address Data Address Data
Chapter 6 <93>
How Functions use the Stack
• Called functions must have no unintended side
effects
• But diffofsums overwrites 3 registers: $t0,
$t1, $s0
# MIPS assembly
# $s0 = result
diffofsums:
add $t0, $a0, $a1 # $t0 = f + g
add $t1, $a2, $a3 # $t1 = h + i
sub $s0, $t0, $t1 # result = (f + g) - (h + i)
add $v0, $s0, $0 # put return value in $v0
jr $ra # return to caller
Chapter 6 <94>
Storing Register Values on the Stack
# $s0 = result
diffofsums:
addi $sp, $sp, -12 # make space on stack
# to store 3 registers
sw $s0, 8($sp) # save $s0 on stack
sw $t0, 4($sp) # save $t0 on stack
sw $t1, 0($sp) # save $t1 on stack
add $t0, $a0, $a1 # $t0 = f + g
add $t1, $a2, $a3 # $t1 = h + i
sub $s0, $t0, $t1 # result = (f + g) - (h + i)
add $v0, $s0, $0 # put return value in $v0
lw $t1, 0($sp) # restore $t1 from stack
lw $t0, 4($sp) # restore $t0 from stack
lw $s0, 8($sp) # restore $s0 from stack
addi $sp, $sp, 12 # deallocate stack space
jr $ra # return to caller
Chapter 6 <95>
The stack during diffofsums Call
FC ? $sp FC ? FC ? $sp
stack frame
F8 F8 $s0 F8
F4 F4 $t0 F4
F0 F0 $t1 $sp F0
Chapter 6 <96>
Registers
Preserved Nonpreserved
Callee-Saved Caller-Saved
$s0-$s7 $t0-$t9
$ra $a0-$a3
$sp $v0-$v1
Chapter 6 <97>
Multiple Function Calls
proc1:
addi $sp, $sp, -4 # make space on stack
sw $ra, 0($sp) # save $ra on stack
jal proc2
...
lw $ra, 0($sp) # restore $s0 from stack
addi $sp, $sp, 4 # deallocate stack space
jr $ra # return to caller
Chapter 6 <98>
Storing Saved Registers on the Stack
# $s0 = result
diffofsums:
addi $sp, $sp, -4 # make space on stack to
# store one register
sw $s0, 0($sp) # save $s0 on stack
# no need to save $t0 or $t1
add $t0, $a0, $a1 # $t0 = f + g
add $t1, $a2, $a3 # $t1 = h + i
sub $s0, $t0, $t1 # result = (f + g) - (h + i)
add $v0, $s0, $0 # put return value in $v0
lw $s0, 0($sp) # restore $s0 from stack
addi $sp, $sp, 4 # deallocate stack space
jr $ra # return to caller
Chapter 6 <99>
Recursive Function Call
High-level code
int factorial(int n) {
if (n <= 1)
return 1;
else
return (n * factorial(n-1));
}
Chapter 6 <100>
Recursive Function Call
MIPS assembly code
Chapter 6 <101>
Recursive Function Call
MIPS assembly code
0x90 factorial: addi $sp, $sp, -8 # make room
0x94 sw $a0, 4($sp) # store $a0
0x98 sw $ra, 0($sp) # store $ra
0x9C addi $t0, $0, 2
0xA0 slt $t0, $a0, $t0 # a <= 1 ?
0xA4 beq $t0, $0, else # no: go to else
0xA8 addi $v0, $0, 1 # yes: return 1
0xAC addi $sp, $sp, 8 # restore $sp
0xB0 jr $ra # return
0xB4 else: addi $a0, $a0, -1 # n = n - 1
0xB8 jal factorial # recursive call
0xBC lw $ra, 0($sp) # restore $ra
0xC0 lw $a0, 4($sp) # restore $a0
0xC4 addi $sp, $sp, 8 # restore $sp
0xC8 mul $v0, $a0, $v0 # n * factorial(n-1)
0xCC jr $ra # return
Chapter 6 <102>
Stack During Recursive Call
Address Data Address Data Address Data
Chapter 6 <103>
Function Call Summary
• Caller
– Put arguments in $a0-$a3
– Save any needed registers ($ra, maybe $t0-t9)
– jal callee
– Restore registers
– Look for result in $v0
• Callee
– Save registers that might be disturbed ($s0-$s7)
– Perform function
– Put result in $v0
– Restore registers
– jr $ra
Chapter 6 <104>
Addressing Modes
How do we address the operands?
• Register Only
• Immediate
• Base Addressing
• PC-Relative
• Pseudo Direct
Chapter 6 <105>
Addressing Modes
Register Only
• Operands found in registers
– Example: add $s0, $t2, $t3
– Example: sub $t8, $s1, $0
Immediate
• 16-bit immediate used as an operand
– Example: addi $s4, $t5, -73
– Example: ori $t3, $t7, 0xFF
Chapter 6 <106>
Addressing Modes
Base Addressing
• Address of operand is:
base address + sign-extended immediate
Chapter 6 <107>
Addressing Modes
PC-Relative Addressing
0x10 beq $t0, $0, else
0x14 addi $v0, $0, 1
0x18 addi $sp, $sp, i
0x1C jr $ra
0x20 else: addi $a0, $a0, -1
0x24 jal factorial
Chapter 6 <108>
Addressing Modes
Pseudo-direct Addressing
0x0040005C jal sum
...
0x004000A0 sum: add $v0, $a0, $a1
JTA 0000 0000 0100 0000 0000 0000 1010 0000 (0x004000A0)
26-bit addr 0000 0000 0100 0000 0000 0000 1010 0000 (0x0100028)
0 1 0 0 0 2 8
Chapter 6 <109>
How to Compile & Run a Program
High Level Code
Compiler
Assembly Code
Assembler
Object Files
Object File
Library Files
Linker
Executable
Loader
Memory
Chapter 6 <110>
Grace Hopper, 1906-1992
• Graduated from Yale
University with a Ph.D. in
mathematics
• Developed first compiler
• Helped develop the COBOL
programming language
• Highly awarded naval officer
• Received World War II
Victory Medal and National
Defense Service Medal,
among others
Chapter 6 <111>
What is Stored in Memory?
• Instructions (also called text)
• Data
– Global/static: allocated before program begins
– Dynamic: allocated within program
Chapter 6 <112>
MIPS Memory Map
Address Segment
0xFFFFFFFC
Reserved
0x80000000
0x7FFFFFFC Stack
Dynamic Data
0x10010000 Heap
0x1000FFFC
Static Data
0x10000000
0x0FFFFFFC
Text
0x00400000
0x003FFFFC
Reserved
0x00000000
Chapter 6 <113>
Example Program: C Code
int f, g, y; // global variables
int main(void)
{
f = 2;
g = 3;
y = sum(f, g);
return y;
}
Chapter 6 <114>
Example Program: MIPS Assembly
.data
int f, g, y; // global
f:
g:
y:
int main(void)
.text
{
main:
addi $sp, $sp, -4 # stack frame
sw $ra, 0($sp) # store $ra
f = 2;
addi $a0, $0, 2 # $a0 = 2
g = 3;
sw $a0, f # f = 2
y = sum(f, g); addi $a1, $0, 3 # $a1 = 3
return y; sw $a1, g # g = 3
} jal sum # call sum
sw $v0, y # y = sum()
int sum(int a, int b) { lw $ra, 0($sp) # restore $ra
return (a + b); addi $sp, $sp, 4 # restore $sp
} jr $ra # return to OS
sum:
add $v0, $a0, $a1 # $v0 = a + b
jr $ra # return
Chapter 6 <115>
Example Program: Symbol Table
Symbol Address
Chapter 6 <116>
Example Program: Symbol Table
Symbol Address
f 0x10000000
g 0x10000004
y 0x10000008
main 0x00400000
sum 0x0040002C
Chapter 6 <117>
Example Program: Executable
Executable file header Text Size Data Size
0x34 (52 bytes) 0xC (12 bytes)
Chapter 6 <118>
Example Program: In Memory
Address Memory
Reserved
0x7FFFFFFC Stack $sp = 0x7FFFFFFC
0x10010000 Heap
$gp = 0x10008000
y
g
0x10000000 f
0x03E00008
0x00851020
0x03E00008
0x23BD0004
0x8FBF0000
0xAF828008
0x0C10000B
0xAF858004
0x20050003
0xAF848000
0x20040002
0xAFBF0000
0x00400000 0x23BDFFFC PC = 0x00400000
Reserved
Chapter 6 <119>
Odds & Ends
• Pseudoinstructions
• Exceptions
• Signed and unsigned instructions
• Floating-point instructions
Chapter 6 <120>
Pseudoinstructions
Chapter 6 <121>
Exceptions
• Unscheduled function call to exception
handler
• Caused by:
– Hardware, also called an interrupt, e.g., keyboard
– Software, also called traps, e.g., undefined instruction
• When exception occurs, the processor:
– Records the cause of the exception
– Jumps to exception handler (at instruction address
0x80000180)
– Returns to program
Chapter 6 <122>
Exception Registers
• Not part of register file
– Cause: Records cause of exception
– EPC (Exception PC): Records PC where exception
occurred
• EPC and Cause: part of Coprocessor 0
• Move from Coprocessor 0
– mfc0 $k0, EPC
– Moves contents of EPC into $k0
Chapter 6 <123>
Exception Causes
Exception Cause
Hardware Interrupt 0x00000000
Chapter 6 <124>
Exception Flow
• Processor saves cause and exception PC in Cause
and EPC
• Processor jumps to exception handler (0x80000180)
• Exception handler:
– Saves registers on stack
– Reads Cause register
mfc0 $k0, Cause
– Handles exception
– Restores registers
– Returns to program
mfc0 $k0, EPC
jr $k0
Chapter 6 <125>
Signed & Unsigned Instructions
• Addition and subtraction
• Multiplication and division
• Set less than
Chapter 6 <126>
Addition & Subtraction
• Signed: add, addi, sub
– Same operation as unsigned versions
– But processor takes exception on overflow
• Unsigned: addu, addiu, subu
– Doesn’t take exception on overflow
Chapter 6 <127>
Multiplication & Division
• Signed: mult, div
• Unsigned: multu, divu
Chapter 6 <128>
Set Less Than
• Signed: slt, slti
• Unsigned: sltu, sltiu
Chapter 6 <129>
Loads
• Signed:
– Sign-extends to create 32-bit value to load into
register
– Load halfword: lh
– Load byte: lb
• Unsigned:
– Zero-extends to create 32-bit value
– Load halfword unsigned: lhu
– Load byte: lbu
Chapter 6 <130>
Floating-Point Instructions
• Floating-point coprocessor (Coprocessor 1)
• 32 32-bit floating-point registers ($f0-$f31)
• Double-precision values held in two floating
point registers
– e.g., $f0 and $f1, $f2 and $f3, etc.
– Double-precision floating point registers: $f0, $f2,
$f4, etc.
Chapter 6 <131>
Floating-Point Instructions
Chapter 6 <132>
F-Type Instruction Format
• Opcode = 17 (0100012)
• Single-precision:
– cop = 16 (0100002)
– add.s, sub.s, div.s, neg.s, abs.s, etc.
• Double-precision:
– cop = 17 (0100012)
– add.d, sub.d, div.d, neg.d, abs.d, etc.
• 3 register operands:
– fs, ft: source operands
– fd: destination operands F-Type
op cop ft fs fd funct
6 bits 5 bits 5 bits 5 bits 5 bits 6 bits
Chapter 6 <133>
Floating-Point Branches
• Set/clear condition flag: fpcond
– Equality: c.seq.s, c.seq.d
– Less than: c.lt.s, c.lt.d
– Less than or equal: c.le.s, c.le.d
• Conditional branch
– bclf: branches if fpcond is FALSE
– bclt: branches if fpcond is TRUE
• Loads and stores
– lwc1: lwc1 $ft1, 42($s1)
– swc1: swc1 $fs2, 17($sp)
Chapter 6 <134>
Looking Ahead
Microarchitecture – building MIPS
processor in hardware
Chapter 6 <135>