Class Notes (1,000,000)
CA (620,000)
UTSG (50,000)
CSC (1,000)
Lecture

CSC258H1 Lecture Notes - Array Data Structure, Memory Address, Data Element


Department
Computer Science
Course Code
CSC258H1
Professor
Steve Engels

This preview shows half of the first page. to view the full 3 pages of the document.
Other MIPS syntax
o Data storage
At the beginning o program, can create labels for memory
locations that are used to store values
Always in form: label type value
# create a single integer variable with
# initial value 3, an int is 32 bytes
var1: .word 3
# create a 2-element character array with
# elements initialized to a and b
# note that a char is 4 bits
array1: .byte „a‟, „b‟
# allocate 40 consecutive bytes, with
# storage unitialized could be used as a
# 40-element character array or a 10
# element integer array
array2: .space 40
.space 40 same as malloc(sizeof(int)*10) in C
array2 is a label that’s the address of a 40 bytes space
Can jump to var1, array1, and etc. like a label
When jump to var1, will execute 3 as an instruction
o Code section syntax
.data
indicates the start of the data declarations, not a label
.text
indicates the start of the program instructions, not a label
main:
the initial line to run when executing the program
o Arrays in assembly language
The address of the first element of the array is used to store and
access the elements of the array
To access an element of the array, get the address of that
element by adding an offset distance to the address of the first
element
offset = array index * size of a single array element
arrays are stored in memory
For example like this, fetch the array values and store
them in registers
Operate on them and then store them back into memory
o Example. array program A
In C
1. int A[100], B[100];
2. for (i = 0; i < 99; i++) {
3. A[i+1] = A[i] + B[i];
4. }
In assembly language
1. .data
2. A: .space 400 # array of 100 integers
3. B: .space 400 # array of 100 integers
4.
5. # reg usage:
6. # $t9 = address of B, $t8 = address of A
7. # $t0 = i, $t1 = 400,
8. # $t4 = A + i, $t3 = B + i, $t2 = A + i + 1
9. # $s5 = A[i], $s4 = B[i], $s6 = A[i] + B[i]
10.
11. .text
12. main: add $t0, $zero, $zero # load “0” into $t0
13. addi $t1, $zero, 400 # load “400” into $t1
14. addi $t9, $zero, B # store address of B
15. addi $t8, $zero, A # store address of A
16.
17. loop: add $t4, $t8, $t0 # $t4 = A + i
18. lw $s5, 0($t4) # $s5 = A[i]
19. add $t3, $t9, $t0 # $t3 = B + i
20. lw $s4, 0($t3) # $s4 = B[i]
21. addi $t0, $t0, 4 # i = i + 1
22. add $t2, $t8, $t0 # $t2 = A + i + 1
23. add $s6, $s4, $s5 # $s6 = B[i] + A[i]
24. sw $s6, 0($t2) # A[i+1] = $s6
25.
26. bne $t0, $t1, loop # branch if $t0!=400
Designing Assembly Code
o Making sense of assembly code
When reading/writing assembly code, recognize portions of
code that represent higher-level operaitons
o Example. deconstruct array program A
First stage: initialization
Store location of A[0] in $t8 and B[0] in $t9
Create a value for i in $t0 and set it to 0
Create a value to store the max value (400) for i in $t1,
as a stopping condition
o since i < 99 $t0 < 400
Note: best to initialize all the regsiters that will be used at once,
even ones that don’t have variable names in the original code
Second Stage: Main algorithm
Fetch A[i]
o Get the memory address of A[i] by adding i to the
memory address of A[0] and store it in $t4
o Load the value of A[i] from that memory address
to $s5
Fetch B[i]
o Get the memory address of B[i] by adding i to the
memory address of B[0] and store it in $t3
o Load the value of B[i] from that memory address
to $s4
Increament i to the next offset value
Add A[i] and B[i] and storing the result in $s6
Store this new value into A[i + 1]
o Same as fetching a value from memory, but reversed
Loop to the beginning if i hasn’t reached 400
o Example: deconstruct another array program
figure out the main purpose of this code ignoring the comments
1. .data
2. a1: .space 400 # declare 12 bytes to
3. # store 3 ints
4. .text
5. main: addi $t0, $zero, a1 # load base address of
6. # array into register $t0
7. addi $t1, $zero, 5 # $t1 = 5
8. sw $t1, 0($t0) # first array element
9. # set to 5
10. addi $t1, $zero, 13 # $t1 = 13
11. sw $t1, 4($t0) # second array element
12. # set to 13
13. addi $t1, $zero, -7 # $t7 = -7
14. sw $t1, 8($t0) # third array element
15. # set to 13
The sw lines indicate that values in $t1 are being stored at
$t0, $t0 + 4 and $t0 + 8
Each previous line sets the value of $t1 to store
Therefore this code stores the values 5, 14, and -7 into the
array at memory location a1
o Example. Turn this C program into assembly language
In C
1. int j = 0; // j needs to be stored in memory
2. for (i = 0; i < 57; i++) {
3. j += i; // i doesn‟t need to be in memory
4. }
In assembly language
1. .data
2. j: .space 4 # initialize j
3.
4. main: addi $t0, $zero, j # get address of j to $t0
5. sw $zero, 0($t0) # j = 0
6. add $t1, $zero, $zero # i = 0
7. addi $t9, $zero, 57 # end at i == 57
8. loop: beq $t1, $t9, end # branch if i == 57
9. lw t2, 0($t0) # $t2 = j
10. add $t2, $t2, $t1 # j = j + i
11. sw $t2, 0($t0) # store j
12. addi $t1, $t1, 1 # i++
13. j loop
14. end: # do the next thing
You're Reading a Preview

Unlock to view full version