Sunteți pe pagina 1din 4

Name: Christophe El Haddad

ID: 201800866
Section: 2

EECE 321 – Homework 4

1) The function counts the number of characters in the first line of the string. The loop exits
when x[i] is ‘\0’ (end of string) or ‘\n’ (detect a new line).

Funct: #the address of x is in $a0


addi $v0, $0, 0
Loop:
add $t0, $v0, $a0
lbu $t0, 0($t0)
beq $t0, $0, Exit
addi $t1, $0, 10
beq $t0, $t1, Exit
addi $v0, $v0, 1
j Loop
Exit: jr $ra

2) For each entry of the string, as long as the entry is not a letter (from a to z, or A to Z) and
it’s not the end of the string, the code shifts the entries of the string to the left by 1 (and
reduce its length by one). The function removes all the letters from the strings, and it
becomes a string of non-letter characters.

3) sort: #the address of array is in $a0 and len is in $a1


addi $t0, $0, 0 #temp
addi $t1, $0, 0 #i
addi $t2, $0, 0 #j
Loop :
slt $t0, $t1, $a1
beq $t0, $0, Exit
add $t2, $0, $t1
Loop2:
slt $t0, $t2, $a1
beq $t0, 0, Exit2
sll $t3, $t1, 2
add $t3, $t3, $a0
lw $t3, 0($t3) #array[i]
sll $t4, $t2, 2
add $t4, $t4, $a0
lw $t4, 0($t4) #array[j]
slt $t0, $t4, $t3 # array[j] < array[i]
Name: Christophe El Haddad
ID: 201800866
Section: 2

beq $t0, $0, Continue


add $t0, $0, $t3 #$t0 has array[i]
sll $t3, $t1, 2
add $t3, $t3, $a0 #$t3 has address of array[i]
sw $t4, 0($t3) #store array[j] to array[i]
sll $t4, $t2, 2
add $t4, $t4, $a0 #$t4 has address of array[j]
sw $t0, 0($t4) #store array[i] to array[j]
Continue :
addi $t2, $t2, 1
j Loop2
Exit2:
addi $t1, $t1, 1
j Loop
Exit : jr $ra

4) sum: #n is in $a0
addi $sp, $sp, -8
sw $ra, 4($sp)
sw $a0, 0($sp)
bne $a0, $0, Check1 # if n != 0 skip
lw $ra, 4($sp)
addi $sp, $sp, 8
addi $v0, $0, 0
jr $ra
Check1:
slt $t0, $a0, $0
beq $t0, $0, Check2 #if n >= 0 skip
addi $a0, $a0, 1
jal sum
lw $a0, 0($sp)
add $v0, $v0, $a0
lw $ra, 4($sp)
addi $sp, $sp, 8
jr $ra
Check2:
addi $a0, $a0, -1
jal sum
lw $a0, 0($sp)
add $v0, $v0, $a0
Name: Christophe El Haddad
ID: 201800866
Section: 2

lw $ra, 4($sp)
addi $sp, $sp, 8
jr $ra

5) # Assume that the array is passed through $s0, len: $a0, lo:$a1, hi:$a2, key:$a3

catchme:
addi $sp, $sp, -20
sw $s0, 16($sp)
sw $a2, 12($sp)
sw $a1, 8($sp)
sw $a0, 4($sp)
sw $ra, 0($sp)
addi $v0, $0, -1
slt $t0, $a2, $a1 # hi < lo
bne $t0, $0, Exit
slt $t0, $a1, $a0 # lo < len
beq $t0, $0, Exit
slt $t0, $a2, $a0 # hi < len
beq $t0, $0, Exit
add $t0, $a1, $a2
srl $t0, $t0, 1 # mid
addi $v0, $t0, 0
sll $t1, $t0, 2
lw $t2, 16($sp) # address of array
add $t1, $t1, $t2
lw $t1, 0($t1) # array[mid]
beq $t1, $a3, Exit
slt $t2, $a3, $t1
bne $t2, $0, Case # array[mid] > key
addi $a1, $t0, 1
jal catchme
j Exit
Case :
addi $a2, $t0, -1
jal catchme
Exit:
lw $ra, 0($sp)
addi $sp, $sp, 20
Name: Christophe El Haddad
ID: 201800866
Section: 2

jr $ra

6) Simulating the MIPS code in QtSPIM counts the size of a string inputted to the console.

S-ar putea să vă placă și