Title: Data Transfer Operations
1Data Transfer Operations And Array
2The answer to the bonus questions from last
time Compile this C statement If (glth) goto
exit Where exit is the label that refers to the
end of the program Set the value of g10 and
h20 and place them into registers s0 and s1,
respectively. Use command slt, and beq (bne)
only Answer .data str1 .asciiz "
" .text main li s0 , 10 li s1 ,
20 slt t1, s0,s1 li t2, 1 beq t1,t2,
exit exit
In order to run this program, you may want to
add some printing command to see the result
3- The answer to the bonus questions from last time
.data str1 .asciiz " " .text main li v0
, 5 syscall add s5, v0, 0 k that is s5
is going to get the value of the input li t0,
0 beq s5, t0, case0 addi t0, t0,
1 beq s5, t0, case1 addi t0, t0,
1 beq s5, t0, case2 addi t0, t0,
1 beq s5, t0, case3 j exit case0 add s0,
s3, s4 j exit case1 add s0, s1,
s2 j exit case2 sub s0, s1,
s2 j exit case3 sub s0, s3, s4 exit
Declare Variable Input value for k each
time compare values with num 0-3 and jump to the
proper case. In order to run this program, you
may want to add some printing command to see the
result
4- Using Complex data Structures (Arrays)
- Programming languages have simple variables that
contain single data elements as shown in the
previous example - They also have more complex data structures like
arrays - This complex data structure can contain many more
elements than there are registers in the machine
- So, how can the computer represent and access
such a large structure ? - C variables map onto registers, what about large
data structures like arrays? - The processor can keep only a small amount of
data in registers but computer memory contains
millions of data elements so data structures like
arrays are kept in memory.
5- MIPS includes instructions that transfer data
between memory and registers. Such instructions
are called data transfer instructions - Since MIPS arithmetic instructions only operate
on registers and never directly on memory, MIPS
include instructions that transfer data between
memory and registers - So first we will go through some facts about MIPS
memory architecture and then we will introduce
data transfer instructions.
6- Memory Organization of MIPS
- The purpose of the memory is to store groups of
bits and deliver them (to the processor) for
loading into registers upon demand - Most recent computers store information in
multiples of 8-bits - In addition, most computers also assign a numeric
address to each byte (8-bits) - Memory addresses are 32-bit numbers ranging from
0x00000000 to 0xFFFFFFFF. - For each running program, data and instructions
are located in different fixed blocks of memory
7- The SPIM simulator always assigns your program to
the fixed even number locations for your
convenience - 0x00400000 Text segment program instructions
- 0x10000000 Data segment
- 0x7FFFFFFF is stack segment
- A word generally means the number of bits that
can be transferred at one time on the data bus
and stored in a register - In the case of MIPS, a word is 32 bits, that is 4
bytes - Words are always stored in 4 consecutive bytes in
memory. Starting with an address that is
divisible by 4
8- To access a data in memory, the instruction must
have the memory address. - Memory is just a large single dimensional array
with addresses acting as index to that array
. . .
. . .
100
Ox0000000C 12
10
Ox00000008 8
101
Ox00000004 4
1
Ox00000000 0
Address
Data
Processor
Memory
9- Data Transfer Instructions
- Load Instruction Syntax
- 1 2 3 4
- lw t0, 8 (s0)
- The name of the operation (load word from memory
to register) - Register that will receive value from memory
- A numerical constant (offset) that tells us how
many memory location we should add to or subtract
from pointer - Register containing pointer to certain block of
memory
10- Example
- lw t0, 12 (s0)
- This instruction will find out what is the
address inside register s0 (pointer) - Then, it adds that address to 12 (offset) and
then loads the content of the memory12s0 to
t0 - Notes
- s0 is called the base register
- 12 is offset
- Offset is generally used in accessing elements of
memory - Base register points to beginning of certain
block of memory
11- Example
- Lets assume that A is an array of 100 words and
that the compiler has associated the variable g
to register s1. Lets also assume the starting
address or base address of the array is in s3.
Translate this C statement - g A9
- In order to compile this statement, we need to
transfer A9 from memory to register s1. - We need to use the load instruction,
-
- Need to find out the offset
- Need to have the base register (s3)
- Need to have register to load ninth element of
array into it (s1) - To find out offset
- Since each word takes four place in memory, the
offset value is 9436 - So the compiled instruction is
- lw s1, 36 (s3)
12- Example
- f (ij)-A12
- Assume that variable f is associated with
register s2 and variable i and j to register
s0, s1 and the base address of the array is in
s3. What is MIPS assembly code? - First load A12 from memory to t0 temporary
register - lw t0 , 48(s3)
- Add ij and put it into temporary register t1
- add t1, s0, s1
- Subtract t0 from t1 and put the result back
in register s2 - sub s2, t1, t0
13- Store Instruction Syntax
- We also want to store value from register into
memory - Store instruction syntax is identical to load
instruction syntax - Store Instruction Syntax
- 1 2 3 4
- sw t0, 8 (s0)
- The name of the operation (store word from
register to memory) - Register that its content needs to be stored in
the memory - A numerical constant (offset) that tells us how
many memory - Location we should add to or subtract from
pointer - Register containing pointer to certain block of
memory
14- Example
- sw t0, 12(s0)
- This instruction first finds out the memory
location that the content of register needs to be
stored by adding 12 byte offset to the pointer
that is in s0 - Then store the content of register t0 to the
memorys0 12
15- Example
- Assume variable h is associated with register
s2, and the base address of the array a is in
s3. What is the MIPS assembly code for the C
assignment statement below - A12 h A8
- Now two of the operands are in memory.
- So we need several MIPS instructions to perform
the above single C code - 1) Store A8 from memory to temporary register
t0 - lw t0, 32(S3) 32 is 84
- 2) add t0, s2, t0 t0 gets the result of
hA8 - 3) We should store the result of addition from
t0 to memory A12 - sw t0, 48 (s3) 48 is 124
16- Example
- Assume variables g, h and i are associated with
register s1, s2, and s4 and the base address
of the array A is in s3. What is the MIPS
assembly code for the C assignment statement
below - g h A i
- We need to load A i in to temporary register
t0 , then add t0 to s2 (h) and store the
result in to s1. - load Ai from memory to temporary register t0
- add t1, s4, s4 Temp Reg t1 2 i
- add t1, t1, t1 Temp Reg t1 4 i
- add t1, t1, s3 t1 address 4i s3
- lw t0, 0(t1) t0 A i
- add A i to h and place it in g
- add s1 , s2, t0 g h A i
17- How to create space in memory Using MIPS
instruction - Method 1 Allocate a space and place some values
in there - for example making space for storing an integer,
a character, or small size arrays. - Method 2 Allocate space without initialization
- This is typically done for creating big size
arrays -
18- Method 1Memory Space Allocation with
Initialization -
- The general format is
-
- label .storage_type value(s)
- It is the name of the location (like a variable
name in C). This label holds the address of the
location in the memory - .storage_type shows the type of the location.
- .word allocates 4 bytes (4 consecutive places in
memory) - .byte allocates 1 byte (1 place in memory)
- Value(s) are the values that are used for
initialization purpose
1
2
3
19- For Example, the instruction
- num1 .word 100
- This instruction will allocate space for one word
(4 bytes) in the memory starting from the address
labeled by num1, and initialize that location of
the memory with 100.. - Another example The instruction
- arr1 .byte a , b
- Creates a two elements character array with
elements initialized to a and b - Question
- How can we create a 5 element array, called
numbers and assign the values 10, 20, 30, 40,
and 50 to this array? - Answer ??
20 .data str1 .asciiz "Enter a number
" str2 .asciiz "The result is
" num1 .word 0 creating a space
called num1 and initializing to 0 num2 .word 0
creating a space called num2 and
initializing to 0 sum .word 0 creating a
space called sum and initializing to
0 .text main li v0, 5 read the
first number syscall sw v0,
num1 store the first number into memory
location num1 li v0, 5 read
the second number syscall sw v0, num2
store the first number into memory location
num2 lw t0, num1 take the numbers
out of memory location num1 and lw t1, num2
num 2and load them in to registers to and
t1 add t3, t0, t1 do addition on them
sw t3, sum store the result back
to memory li v0, 4 la a0, str2
print the result is syscall lw a0,
sum in order to print the result ,load
the result value li v0, 1 from
memory location sum to a0 syscall
21- Method 2Memory Space Allocation
-
- The general format is
-
- label .space NumberOfSpace
- It is the name of the location (like a variable
name in C). This label holds the address of the
location in the memory - .space is a keyword to create some space in
memory - NumberOfSpace is an integer to indicate how many
bytes should be allocated, starting from the
address specified by the label
3
1
2
22-
- For Example, the instruction
- array2 .space 40
- This gives 40 bytes of location in the memory,
starting from the address that is specified by
label array2 - Note that because a character takes only one
byte, you can store 40 characters in this space - You can also store integers. However, because
each integer requires 4 bytes, you can only place
10 integers in a 40 bytes location