Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Use MARS to write and simulate a MIPS assembly language program to implement the

ID: 3850308 • Letter: U

Question

Use MARS to write and simulate a MIPS assembly language program to implement the strLen function. Write the main function to call strLen.

The main function should: • Pass a string to strLen using registers $a0.

• Preserve (i.e. push onto the stack) any T registers that the main function uses. The strLen function should:

• Preserve any S registers it uses.

• Determine the length of the string.

• Return the length of the string to main using register $v0.

Note: A string is passed to a function by passing the starting address of the string.

Note: A C-style string is a null-terminated character array

Explanation / Answer

data
# first we initialise strings. we are using different strings for different test cases
S1: .asciiz "take"
S2: .asciiz "me "
S3: .asciiz "back"
string: .asciiz "Display strings first: "
S4: .asciiz " all strings together are: "
S5: .asciiz " When third string is clone into fifth string, fifth string is: "
S6: .asciiz " When forth string is copied into sixth string , sixth string is: "
L1: .asciiz " The length of the first string is: "
L2: .asciiz " The length of string 4 is: "
.text
main:
#show all strings first
li $v0, 4
la $a0, string
syscall
la $a0, S1
syscall
la $a0, S2
syscall
la $a0, S3
syscall
la $a0, S1 #load address of string
jal strlen #call string length procedure
move $a0, $v0
jal print
addi $a1, $a0, 0 #move address of string to $a1
addi $v1, $v0, 0 #move length of string to $v1
addi $v0, $0, 11 #syscall code for message
la $a0, L1 #address of message
syscall
li $v0, 10
syscall

## int strlen(char*)
strlen:
addi $t0, $zero, 1 #initialize count to start with 1 for first character
j strlen.test
strlen.loop:
addi $a0, $a0, 1 #load increment string pointer
addi $t0, $t0, 1 #increment count
strlen.test:
lb $t1, 0($a0) #load the next character to t0
bnez $t1, strlen.loop #end loop if null character is reached
move $v0, $t0
jr $ra
void print_str (char*)
print:
addi $sp, $sp, -12
sw $fp, 8($sp)
addi $fp, $sp, 16
sw $ra, 0($fp)
sw $s0, -4($fp)
move $s0, $a0 # store the argument in a save register
li $v0, 4
la $a0, L1
syscall
li $v0, 1
move $a0, $s0 # retrieve the argument from $s0
syscall
lw $s0, -4($fp)
lw $ra, 0($fp)
lw $fp, 8($sp)
addi $sp, $sp, 12   
jr $ra

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at drjack9650@gmail.com
Chat Now And Get Quote