Instructions: Commands in A Computer's Language
Instructions: Commands in A Computer's Language
1. Assembly Language
Instructions: commands in a computer’s language
a. Assembly language: human-readable format of instructions
b. Machine language: computer-readable format (1’s and 0’s)
MIPS architecture:
c. Developed by John Hennessy and his colleagues at Stanford and in the 1980’s.
d. Used in many commercial systems, including Silicon Graphics, Nintendo, and Cisco
Once you’ve learned one architecture, it’s easy to learn others
4. 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
5. Multiple Instructions
More complex code is handled by multiple MIPS instructions.
6. 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)
7. Operands
Operand location: physical location in computer
a. Registers
b. Memory
c. Constants (also called immediates)
8. 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
9. Design Principle 3
Smaller is Faster
• MIPS includes only a small number of registers
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
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
17. Writing Word-Addressable Memory
Memory write are called store
Mnemonic: store word (sw)
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
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
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
20. Writing Byte-Addressable Memory
• Example: stores the value held in $t7 into memory address 0x2C (44)
• MIPS assembly code
sw $t7, 44($0) # write $t7 into address 44
Word Address Data
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
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
• 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 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
25. R-Type
• Register-type
• 3 register operands:
- rs, rt: source registers
- rd: destination register
• Other fields:
a. op: the operation code or opcode (0 for R-type instructions)
b. funct: the function
with opcode, tells computer what operation to perform
c. 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
R-Type Examples
Field Values
Assembly Code op rs rt rd shamt funct
0 17 18 16 0 32
add $s0, $s1, $s2 0 11 13 8 0 34
6 bits 5 bits 5 bits 5 bits 5 bits 6 bits
. sub $t0, $t3, $t5
Machine Code
op rs rt rd shamt funct
26. I-Type
Immediate-type
3 operands:
- rs, rt: register operands
- imm: 16-bit two’s complement immediate
Other fields:
a. op: the opcode
b. Simplicity favors regularity: all instructions have opcode
c. Operation is completely determined by opcode
I-Type
op rs rt imm
6 bits 5 bits 5 bits 16 bits
I-Type Examples
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
op rs rt imm
Main Memory
(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
31. 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
Machine Code
op rs rt rd shamt funct
39. 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)
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 C 0 A 0 0 2 0 PC
Main Memory
41. 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
Labels indicate instruction location. They can’t be reserved words and must be followed by colon (:)
48. While Loops => Assembly tests for the opposite case (pow == 128) of the C code (pow != 128).
51. Arrays
• Access large amounts of similar data
• Index: access each element
• Size: number of elements
• 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]
52. Accessing Arrays
58. Function Calls => void means that simple doesn’t return a value
FC ? $sp FC ? FC ? $sp
s ta c k fra m e
F8 F8 $s0 F8
F4 F4 $t0 F4
F0 F0 $t1 $sp F0
Preserved Nonpreserved
Callee-Saved Caller-Saved
$s0-$s7 $t0-$t9
$ra $a0-$a3
$sp $v0-$v1
stack above $sp stack below $sp
Compiler
Assembly Code
Assembler
Object Files
Object File
Library Files
Linker
Executable
Loader
Memory
Reserved
0x80000000
0x7FFFFFFC Stack
Dynamic Data
0x10010000 Heap
0x1000FFFC
Static Data
0x10000000
0x0FFFFFFC
Text
0x00400000
0x003FFFFC
Reserved
0x00000000
Symbol Address
f 0x10000000
g 0x10000004
y 0x10000008
main 0x00400000
sum 0x0040002C
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
78.