MIPS - write a function to remove blanks

In summary, the conversation discusses writing a function in MIPS to remove extra blanks from a string and another function to get alphabetic characters from a string entered through the keyboard. The main program is also mentioned, which uses these functions to remove blanks and print the resulting string. The conversation also includes advice and tips for completing the assignment, including using the MARS assembler and checking the code in the designated assembler.
  • #1
Sasha7440
1
0
MIPS -- write a function to remove blanks

So I am new to MIPS and I have been working on a program that my brother had for a class a while back ago and I am trying to do the following...

Write a function RemoveBlanks() that inputs an asciz string
consisting of a list of words. The function returns the
string with all extra blanks removed; e.g there should be
only 1 blank separating words.

Examples:
input "the fat dog ate my sandwich \0"
output "the fat dog ate my sandwich\0"

input " where is my hat\0"
output "where is my hat\0"

There is only argument to the function - the array of characters
that is the string. The result modifies the original string.
e.g the call in c would be void RemoveBanks ( char A[] );

I also want to continue by writing the following...

Write a function getAlpha() to input a string from the keyboard.
Any chars that are not alphabetic (a-z or A-Z) or blanks are replaced
with blanks in the string.

void getAlpha( char A[] , int N)

Examples:

input from keyboard " where 45 to me 789 Joe%"
output " where to me Joe\0"

Read the string 1 char at a time. (Use the read character syscall.)
Use a special char of % as a flag to denote the end of the input
string. Replace it with a \0 in the output string.

Read a maximum of N chars into the array ( including the \0 )

Then last the main function...

Write a main program that reads in a string from the keyboard
using getAlpha(), prints the string, uses RemoveBanks()
to remove extra blanks and then prints the resulting
string.

Clearly identify each function in the source code. Test the
code on the keyboard input:
" 34 big 45old 674;jack%" (without the " "s)

***** If anyone ould be willing to send me advise or code to look at that would be great! So far I have been able to take some instruction on how to set up the stack and to tear it back down and how to call the system to enter the string. here is what I have ...


.globl removeBlanks
.text
removeBlanks:
sw $ra, -4
sw $fp, -8
move $fp, $sp
addiu $sp, $sp, -8
addi $v0, $0, 8
syscall

** HELP HERE PLEASE***


setBack:
move $sp, $fp
lw $ra, -4($sp)
lw $fp, -8($sp)
jr $ra


Thanks so much!
 
Technology news on Phys.org
  • #2


Well, you'll want to jal around until a condition (aka eof or rather a \0 is encountered at the end of the string). I doubt that anyone here will write this homework assignment for you. If, however you do provide at least an attempt at a solution I can help you, or I'm sure someone else will guide you :)

A word of advice - use the free MARS assembler to initially write your code (even though you're probably using SPIM). The help menu of MARS shows all the MIPS directives, and it also has an explanation of every syscall and how to use them. It will definitely be of help to you.

Then after writing your code, make sure to run it in whatever assembler you're supposed to use, just to double check the functionality.

Good luck.
 
  • #3


Hello there,

It's great to see that you are diving into MIPS programming and working on a project from your brother's class. I can offer some suggestions for your code and provide some guidance on how to approach this task.

First, let's break down the task into smaller steps:

1. Write a function to remove extra blanks from a string.
2. Write a function to input a string from the keyboard and replace non-alphabetic characters with blanks.
3. Write a main function that calls these two functions and prints the resulting string.

Now, let's look at each of these steps in more detail.

1. Write a function to remove extra blanks from a string:
For this function, you will need to use a loop to go through the string character by character. As you go through the string, you will need to keep track of the previous character and check if the current character is a blank. If it is, you will need to check if the previous character was also a blank. If both are blanks, then you can skip over the current character and continue to the next one. If the previous character was not a blank, then you can add the current character to the resulting string. Once you have finished going through the entire string, you can add a null terminator at the end to mark the end of the string.

2. Write a function to input a string from the keyboard and replace non-alphabetic characters with blanks:
For this function, you will need to use a loop to go through the string character by character, using the read character syscall to get each character from the keyboard. As you go through the string, you will need to check if the current character is alphabetic or a blank. If it is, then you can add it to the resulting string. If it is not, then you can replace it with a blank in the resulting string. Once you have finished going through the entire string, you can add a null terminator at the end to mark the end of the string.

3. Write a main function that calls these two functions and prints the resulting string:
In the main function, you can first declare an array of characters to store the input string. Then, you can call the getAlpha() function to input the string from the keyboard and replace non-alphabetic characters with blanks. Next, you can call the removeBlanks() function to remove extra blanks from the string. Finally, you can print the resulting
 

What is MIPS?

MIPS stands for Microprocessor without Interlocked Pipeline Stages, and it is a reduced instruction set computer (RISC) architecture used in many modern microprocessors. It is commonly used in embedded systems and is particularly popular in the computer science field.

What is a function in MIPS?

In MIPS, a function is a self-contained block of code that performs a specific task. It can be called from other parts of the program, making it reusable and efficient. Functions in MIPS are similar to functions in other programming languages, but they have their own unique syntax and calling conventions.

What does it mean to remove blanks?

Removing blanks refers to the process of eliminating any empty spaces or tabs from a given string or text. This is often done to clean up data or improve the readability of the text.

How do I write a function to remove blanks in MIPS?

To write a function that removes blanks in MIPS, you will need to use the appropriate MIPS instructions and syntax. This will involve using loops and conditional statements to scan through the string and eliminate any blank spaces. You will also need to use the appropriate system calls to read and write data. It is important to carefully plan and test your code to ensure it functions correctly.

Why is it important to write efficient code in MIPS?

Efficient code in MIPS is important because it allows for faster execution and better use of system resources. Given that MIPS is often used in embedded systems and other low-level applications, writing efficient code can greatly improve the performance and functionality of a program. Additionally, efficient code can save time and resources during the development and debugging process.

Similar threads

  • Programming and Computer Science
Replies
4
Views
7K
  • Programming and Computer Science
Replies
4
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
3K
  • Programming and Computer Science
Replies
1
Views
7K
  • Programming and Computer Science
Replies
1
Views
14K
  • Programming and Computer Science
Replies
1
Views
8K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
5
Views
9K
  • Programming and Computer Science
Replies
17
Views
2K
  • Programming and Computer Science
Replies
3
Views
2K
Back
Top