Question: In MIPS: Use the sample code: Write a program which determines the length of a null terminated string We'll write a program named strlength.asm that
In MIPS: Use the sample code:
Write a program which determines the length of a null terminated string We'll write a program named strlength.asm that count number of characters in a string. In the below you can see it's C code. int strlen( char* string ) { int count = 0; while( *string != \0 ) { string++; count++; } return count; } In order to write the program, please follow below steps: 1. Load the address of string into $a0 2. Use $t0 as counter 3. Print $t0. 4. Exit. # Your Name - DATE # strlength.asm-- A program that determine the length of a null terminated string # Registers used: # $t0 - used to hold the loop counter # $a0 - used to hold the address of string # $v0 - syscall parameter and return value .data
str: .asciiz "abcde" .text # Load address of string into a0 |-------------------| |Put your code here | |-------------------| strlen: li $t0, 0 # initialize the count to zero loop: lb $t1, 0($a0) # load the next character into t1 beqz $t1, exit # check for the null character |-------------------| |Put your code here | |-------------------| exit: li $v0, 1 move $a0, $t0 syscall li $v0, 10 syscall
The lb instruction on line four may be unfamiliar, but it works exactly the same way as the load word instruction with which you are familiar. Rather than loading an entire word, the lbu instruction loads a single byte (the size of a C char). If characters were four bytes each
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
