Lecture 5
Lecture 5
These slides are based on the book: David A. Patterson, John L. Hennessy, Computer Organization and Design
TOPICS
➢ Introduction
➢ Assembly Language
➢ Machine Language
➢ Programming
➢ Addressing Modes
PROGRAMMING
➢ High-level languages:
➢ e.g., C, Java, Python
➢ Written at more abstract level
➢ Common high-level software constructs:
➢ if/else statements
➢ for loops
➢ while loops
➢ array accesses
➢ procedure calls
➢ Other useful instructions:
➢ Arithmetic/logical instructions
➢ Branching
LOGICAL INSTRUCTIONS
• and, or, xor, nor
Source Registers
$s1 1111 1111 1111 1111 0000 0000 0000 0000
Source Registers
$s1 1111 1111 1111 1111 0000 0000 0000 0000
Source Values
$s1 0000 0000 0000 0000 0000 0000 1111 1111
Source Values
$s1 0000 0000 0000 0000 0000 0000 1111 1111
Machine Code
op rs rt rd shamt funct
Stored Program
Address Instructions
0040000C 01 6 D 4 0 22
00400008 22 6 8 F F F4
00400004 02 3 2 8 0 20
00400000 8C 0 A 0 0 20 PC
Main Memory
WORD-ADDRESSABLE MEMORY
• Each 32-bit data word has a unique address
00000003 40 F 3 0 7 8 8 Word 3
00000002 01 E E 2 8 4 2 Word 2
00000001 F2 F 1A C 0 7 Word 1
00000000 AB C D E F 7 8 Word 0
WRITING WORD-ADDRESSABLE MEMORY
➢ Memory writes are called stores
➢ Mnemonic: store word (sw)
➢ Example: Write (store) the value held in $t4 into memory address 7
➢ Offset can be written in decimal (default) or hexadecimal
➢ Memory address calculation:
➢ – add the base address ($0) to the offset (0x7)
➢ – address: ($0 + 0x7) = 7
➢ Any register may be used to store the base address
Word Address Data
Assembly code .
. .
sw $t4, 0x7($0) # write the value in $t4 .
# to memory word 7 .
.
00000003 40 F 3 0 7 8 8 Word 3
00000002 01 E E 2 8 4 2 Word 2
00000001 F2 F 1A C 0 7 Word 1
00000000 AB C D E F 7 8 Word 0
BYTE-ADDRESSABLE MEMORY
➢ Each data byte has a unique address
➢ Load/store words or single bytes: load byte (lb) and store byte (sb)
➢ Each 32-bit words has 4 bytes, so the word address increments by 4
width = 4 bytes
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)
➢ Load a word of data at memory address 4 into $s3.
➢ $s3 holds the value 0xF2F1AC07 after the instruction completes.
➢ MIPS is byte-addressed, not word-addressed
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
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
BIG-ENDIAN AND LITTLE-ENDIAN MEMORY
➢ How to number bytes within a word?
➢ Word address is the same for big- or little-endian
➢ Little-endian: byte numbers start at the little (least significant) end
➢ Big-endian: byte numbers start at the big (most significant) end
Big-Endian Little-Endian
Byte Address Word Byte 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
BIG-ENDIAN AND LITTLE-ENDIAN MEMORY
➢ From Jonathan Swift’s Gulliver’s Travels where the Little-Endians broke
their eggs on the little end of the egg and the Big-Endians broke their eggs
on the big end.
➢ As indicated by the farcical name, it doesn’t really matter which addressing
type is used – except when the two systems need to share data!
Big-Endian Little-Endian
Byte Address Word Byte 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
BIG- AND LITTLE-ENDIAN EXAMPLE
➢ Suppose $t0 initially contains 0x23456789. After the following program is run
on a big-endian system, what value does $s0 contain? In a little-endian system?
sw $t0, 0($0)
lb $s0, 1($0)
BIG- AND LITTLE-ENDIAN EXAMPLE
➢ Suppose $t0 initially contains 0x23456789. After the following program is run
on a big-endian system, what value does $s0 contain? 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
THE POWER OF THE STORED PROGRAM
➢ 32-bit instructions and data stored in memory
➢ Sequence of instructions: only difference between two applications (for example,
a text editor and a video game)
➢ To run a new program:
➢ No rewiring required
➢ Simply store new program in memory
➢ The processor hardware executes the program:
➢ fetches (reads) the instructions from memory in sequence
➢ performs the specified operation
➢ The program counter (PC) keeps track of the current instruction
➢ In MIPS, programs typically start at memory address 0x00400000
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 01 6 D 4 0 22
00400008 22 6 8 F F F4
00400004 02 3 2 8 0 20
00400000 8C 0 A 0 0 20 PC
Main Memory
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
target:
add $s1, $s1, $s0 #$s1 = 1 + 4 = 5
UNCONDITIONAL BRANCHING / JUMPING (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
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)
THANK YOU!
31