COE480 Lecture3
COE480 Lecture3
Computer Architecture
Instruction Set Architecture
MIPS
Lecture 3
1
Memory Operands
• Programming languages have simple variables that
contain single data elements
3
Registers vs. Memory
4
Memory Operands
• To apply arithmetic operations
– Load values from memory into registers
– Store result from register to memory
• MIPS include instructions that transfer data between
memory and registers.
data transfer instructions
5
Memory Operands
• To access a word in memory, the instruction
must supply the memory address.
6
Memory Operands
• In MIPS, words (4 bytes) are aligned in memory
– Address must be a multiple of 4
7
Memory Operands
• MIPS is Big Endian
– Big Endian: Most-significant byte at least address of a
word
– Little Endian: least-significant byte at least address
8
Addresses Specify Byte Locations
32-bit Half Bytes Addr.
– Addresses are aligned: Words Words
addresses are multiples of X, 0000 0000
where X is the number of Addr
0001
bytes. =
0000
?? 0002 0002
0003
– In MIPS, word (4 bytes) 0004 0004
addresses are multiples of 4; Addr
0005
=
0004
?? 0006 0006
0007
0008 0008
Addr
=
0009
0008
?? 000A 000A
000B
000C 000C
Addr
=
000D
000C
?? 000E 000E 9
000F
Addressing example
10
Memory Transfer Instructions
• To load/store a word from/to memory:
– LOAD: move data from memory to register
• lw $t3, 4($t2) # $t3 M[$t2 + 4]
• Description:
– Take the value in $t2, add to it the Immediate 4 and you get a
memory address.
– Go to the computed memory address, get the data and store it in
register $t3
11
Memory Transfer Instructions
Example:
lw $t3, 4($t2)
if $t2 has the value 001000A4, what will be loaded in $t3?
12
Memory Transfer Instructions
– STORE: move data from register to memory
• sw $t4, 8($t1) # M[$t1 + 8] $t4
• Description:
– Take the value in $t1, add to it the Immediate 8 and you get a
memory address.
– Take the value in register $t4 and save it at the computer
memory location.
13
Memory Transfer Instructions
Example:
sw $t4, 8($t1)
if $t1 has the value 001000A4, and $t4 = 98765432.
What will change in memory?
001000A4 + 8 = 0x001000AC
Address Value
0x001000A0 0A0B0C0D
0x001000A4 11223344
0x001000A8 04030201
0x001000AC 00FF00FF
98765432
0x001000B0 ABCDEF
14
Memory Operand Example
• C code: Address Value
value in $s3 A[0]
g = h + A[8];
+1 x 4 A[1]
– g in $s1, h in $s2, base address of A in $s3
+2 x 4 A[2]
+3 x 4 A[3]
+4 x 4 A[4]
+5 x 4 A[5]
• Compiled MIPS code: +6 x 4 A[6]
– Index 8 requires offset of 32 +7 x 4 A[7]
• 4 bytes per word +8 x 4 A[8]
lw $t0, 32($s3) # load word
add $s1, $s2, $t0
offset base register 15
Memory Operand Example 2
Address Value
• C code: value in $s3 A[0]
+1 x 4 A[1]
A[12] = h + A[8]; +2 x 4 A[2]
– h in $s2, base address of A in $s3
+3 x 4 A[3]
+4 x 4 A[4]
+5 x 4 A[5]
+6 x 4 A[6]
• Compiled MIPS code: +7 x 4 A[7]
– Index 8 requires offset of 32 +8 x 4 A[8]
+9 x 4 A[9]
lw $t0, 32($s3)# load word
+10 x 4 A[10]
add $t0, $s2, $t0
+11 x 4 A[11]
sw $t0, 48($s3)# store word +12 x 4 A[12]
16
C Sort Example
• Illustrates use of assembly instructions for a C bubble
sort function
• Swap:
int temp;
temp = v[k];
v[k] = v[k+1];
v[k+1] = temp;
17
C Sort Example
$a0: pointer to V array
Step1: Find the address of V[k] $a1: k
• Address V[k]: address of V[0] + offset
• In MIPS, the address is a word (4 bytes) offset =kx4 Address Value
• The value of k is in $a1 value in $a0 V[0]
• Shift left by i is a multiplication my 2i +1 x 4 V[1]
• A shift by 2 x 4
+2 x 4 V[2]
• address of V[k] = address of V[0] + offset
+3 x 4 .
.
.
Code:
+k x 4 V[k]
sll $t0, $a1, 2 # $t0 = $a1*4 = kx4
+(k+1) x 4 V[k+1]
add $t1, $a0, $t0 # $t1 = address of V[k]
.
.
.
18
C Sort Example
$a0: pointer to V array
Step2: load data from V[k] & V[k+1] $a1: k
• load a word from memory position (value in $t1) and
save it to temporary register ($t3) Address Value
• load a word from memory position (value in $t1 +4) value in $a0 V[0]
and save it to temporary register ($t4) +1 x 4 V[1]
+2 x 4 V[2]
+3 x 4 .
.
.
Code:
+k x 4 V[k]
sll $t0, $a1, 2 # $t0 = $a1*4 = kx4
+(k+1) x 4 V[k+1]
add $t1, $a0, $t0 # $t1 = address of V[k]
.
lw $t3, 0($t1) # $t3 = V[k] .
lw $t4, 4($t1) # $t4 = V[k+1] .
19
C Sort Example
$a0: pointer to V array
Step3 : Save swapped data to V[k] & V[k+1] $a1: k
• save the value in $t4 to the memory position of V[k]
(value in $t1) Address Value
• save the value in $t3 to the memory position of V[k+1] value in $a0 V[0]
(4 + value in $t1) +1 x 4 V[1]
+2 x 4 V[2]
+3 x 4 .
.
.
Code: +k x 4 V[k]
sll $t0, $a1, 2 # $t0 = $a1*4 = kx4 +(k+1) x 4 V[k+1]
add $t1, $a0, $t0 # $t1 = address of V[k] .
lw $t3, 0($t1) # $t3 = V[k] .
lw $t4, 4($t1) # $t4 = V[k+1] .
sw $t4, 0($t1) # save V[k+1] in the place of V[k] 20
sw $t3, 4($t1) # save V[k] in the place of V[k+1]