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

Write a MIPS program that asks the user to input string. The program then remove

ID: 3700228 • Letter: W

Question

Write a MIPS program that asks the user to input string. The program then removes all space characters from the string. The program finally displays the resulting string without spaces.

The following C code shows the proposed algorithm. The string is traversed with two indices, called old_index and new_index, where the latter always takes a value less or equal to the former. When a non?space character is found, the character at position old_index is copied to position new_index, and both indices are incremented. When a space is found, the current character is not copied, and only old_index is incremented. The algorithm stops when a null character is found.

#include int main () /7 Read string char s[100] printf ("Enter string: "); gets (s); // Remove spaces char c int old index0 int new index-0; do / Read character cs[old_index] // Old position moves ahead old indext+ /7 If it's a space, ignore if (c '') continue; 7Copy character s [new index] - ci // New position moves ahead new index++ while (c)i // Print result printf("New string: %s ", s);

Explanation / Answer

.data
string1: .space 100
string2: .space 100
ask: .asciiz "Enter string: "
newstring: .asciiz "Input string: "

.text

main:
la $a0,ask #input for string1
li $v0,4
syscall

#load String1
la $a0,string1
li $a1, 100
li $v0,8 #get string
syscall


load:
la $s0,string1 #Load address of string1 into s0
lb $s1, ($s0) #set first char from string1 to $t1
la $s2 ' ' #set s2 to a space
li $s3, 0 #space count


compare:
#for space

beq $s1, $zero, print #if done, move to end
beq $s1, $s2, space #if char s1 is a space move on
bne $s1, $s2, save #if s1 is a character save that in the stack

save:
#save the new string
sub $s4, $s0, $s3,
sb $s1, ($s4)
j step

space:
addi $s3, $s3, 1 #add 1 if space

step:
addi $s0, $s0, 1 #first string array increment
lb $s1, ($s0) #incremented value is loaded
j compare


print:
la $a0, newstring
li $v0,4
syscall

#printing the new string
la $a0, string1
li $v0, 4
syscall

end:
#end program
li $v0, 10
syscall #end

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