0% found this document useful (0 votes)
7 views20 pages

COE480 Lecture3

Uploaded by

10121258
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views20 pages

COE480 Lecture3

Uploaded by

10121258
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

COE480

Computer Architecture
Instruction Set Architecture
MIPS

Lecture 3

1
Memory Operands
• Programming languages have simple variables that
contain single data elements

• They also have more complex data structures—arrays


and structures.
Array = [ element1, element2, …. ]

• Complex data structures can contain many more data


elements than there are registers in a computer.

How can a computer represent and


access such large structures? 2
Memory Operands
• The processor can keep only a small amount of data in
registers

• Computer memory contains billions of data elements.

• Main memory used for composite data


– Arrays, structures, dynamic data

• Arithmetic operations are register operands!

3
Registers vs. Memory

• Registers are faster to access than memory

• Operating on memory data requires loads and


stores
– More instructions to be executed

• Compiler must use registers for variables as


much as possible
– Only spill to memory for less frequently used variables
– Register optimization is important!

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.

• Memory is byte addressed


– Each address identifies an 8-bit byte

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

Big Endian Little Endian

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

– Consider the following table showing the memory


address and value

1-Complete the addressing.


Address Value
0x001000A0 0A0B0C0D
2- What byte is saved at address 0x001000A4 11223344
001000A0? 0D 0x001000A8 04030201
0x001000AC 00FF00FF
3- What byte is saved at address 0x001000B0 ABCDEF
001000A2? 0B

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?

001000A4 + 4 = 001000A8 Address Value


0x001000A0 0A0B0C0D
0x001000A4 11223344
04030201 0x001000A8 04030201
0x001000AC 00FF00FF
0x001000B0 ABCDEF

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;

– v in $a0, k in $a1, temp in $t0

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]

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy