Documente Academic
Documente Profesional
Documente Cultură
ID: 201800866
Section: 2
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).
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.
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.