Professional Documents
Culture Documents
Lecture
Count DD
i j DD DD
10
? ?
Strings
string str (hello world); str .asciiz hello world
Arrays
int age[100];
age .word 0:100 ; 100 words initialized to zero age dw 100 dup(0); 100 words initialized to zero
Assignment
count = count +1;
la lw addi sw R1, count R2, 0(R1) R2, R2, 1 R2, 0(R1) # (load address works in spim) #R1 has address of count # R2 has the value of count # Increment # Save count, if need be
Another Example
i = age[20] +age[0] + count;
la lw lw add add R3, R4, R5, R4, R4, age 80[R3] 0(R3) R4, R5 R4, R2 #R3 has base address of age; #20 * 4 = 80, R4 = age[20] #R5 = age[0] # #R2 had value of count last slide
Control
count = 0; while (count < 21) { age[count] = age[count+1]; count = count+1; }
R2, R0, 0
; R2 = count = 0
R6, R2, 2
R6, R6, R4
lw sw
addi slti bnez
For Statement
Total = 0; For (i= 0; i<count; i ++) { total = age[i] + total;}
la lw addi addi Xy: slt be sll add lw add sw addi b Labl: R1, age R2, 0(R1) R5, R0, 0 R15, R0, 0 R11,R5,R10 R11, R0, labl R6, R5, 2 R6, R6, R4 R20, 0(R6) R15, R15, R20 R15, 0(R22) R5,R5,1 Xy: #R1 has address of count # R2 has the value of count # i = 0 ; R5 is i # total = 0 ; R15 is total # check R5 is R5 < R10 (R10 is count) # loop until # R6 has address of ith element of age # R4 = address of age #R20 = age[i] # total = total + age[i] # R22 is address of total # i = i+1; # Unconditional branch
IF then
If (count == max) I = 5; else I = 20; #assume R4 has bne addi b else: addi exit: the value of count, R7 has value of MAX and I is R5 R4, R7, else R5, R0, 5 exit R5, R0, 20