How to prevent the user from inputting strings in MIPS - string

I'm trying to make a program where the user inputs a integer, not a string. Thing is, if the user actually puts a string, I get an error. Is there any way to prevent the user from inputting a string?
Here's the code I'm using to test:
.data
Output_1:
.asciiz "\Enter a number: "
.text
.globl main
main: la $a0, Output_1
li $v0, 4
syscall
li $v0, 5
syscall

Related

How do I read a string from terminal input in Linux MIPS32 assembly?

I'm currently learning MIPS programming in my university for the MARS simulator. However, I wanted to try to learn to write MIPS assembly programs for a MIPS Linux virtual machine. I managed to create a MIPS32 Debian 11 virtual machine. However, I having trouble with the Linux kernel system calls and their parameters. After some confusion, and a long time of searching the internet, I managed to create a basic hello world program. Then, I tried to create a program that would ask the user for some text, and then print it out, but it doesn't work.
I'm expecting something like this:
Please enter a string: This is text
The input is: This is text
However, I get:
Please enter a string: This is text
The input is:
My code is:
.data
prompt: .asciiz "Please enter a string: "
promptLength: .quad . - prompt
input: .space 81 #Create a 80-character empty string
inputLength: .word 80
answer: .asciiz "The input is: "
answerLength: .quad . - answer
newline: .asciiz "\n"
.text
.globl __start
__start:
#Display the prompt
li $a0, 1
la $a1, prompt
lw $a2, promptLength
li $v0, 4004
syscall
#Read the input
li $a0, 1
la $a1, input
lw $a2, inputLength
li $v0, 4003
syscall
move $s0, $v0
#Write the user input
li $a0, 1
la $a1, answer
lw $a2, answerLength
li $v0, 4004
syscall
la $a1, input
move $a2, $s0
syscall
#Exit
b exit
printNewline:
li $a0, 1
li $a1, newline
li $a2, 1
li $v0, 4004
syscall
jr $ra
exit:
li $v0, 4001
syscall
PS. Is there any book/YouTube tutorial/website that teaches MIPS programming for Linux?

Separate words of the string and store them in stack in MIPS

.data
string: .asciiz "Please enter the string : "
newLine: .asciiz "\n"
buffer: .space 100
.text
.globl main
main:
la $a0, string
li $v0, 4
syscall
li $v0, 8
la $a0, buffer
li $a1, 100
syscall
la $t0, buffer
li $t0, 0
subu $sp, $sp, 4
li $v0, 4
syscall
Here above is beginning of the my code. And I need to separate each words of the string and then store them in the stack. First I tried to store word in stack as allocation memory with stack pointer and it works now. But I need to store each word(not characters) of the sentence such as
Hello I am Furkan
So for this example there should be 4 words in stack.
Hello
I
am
Furkan
How can I solve that ?

Print a char extracted from a string in mips

I'm trying to print the first char from a given string. But no matter what I try, I'm not able to do so.
Here is my code:
.data
string: .asciiz "stringer"
char: .asciiz "c"
.text
.globl main
main:
li $v0, 4 # 4 is the print_string syscall.
la $a0, string # load the addr of the given string into $a0.
syscall
li,$v0, 11 # service 11 is print character
lb $t1, 0 ($a0)
syscall
exit:
li $v0, 10 # return control to SPIM OS
syscall
Thanks.

Accessing the x number of characters in a string (MIPS)

I have this code:
.text
.globl __start
__start:li $v0,8
li $a1,20
la $a0,str
syscall
print_str:li $v0,4
syscall
print_endl:la $a0,Endl
syscall
la $a0,str
sb $zero,5($a0) # <--------- #
print_5chars: syscall
Exit:li $v0,10
syscall
.data
Endl:.asciiz "\n"
str:.asciiz "____________________"
As shown to me,this code first gets a string input, then prints the string entered and after that it prints the first 5 characters of the string.My question is what does exactly the line i marked do?

Sorting a string in mips?

I am trying to sort a string that is entered by the user. Is there any way to access a specific element of that string? Here is my mips code:
.data
prompt: .asciiz "\n\nEnter an string of characters: "
result: .asciiz "\n\nHere is the string you entered: "
buffer: .space 20
.text
main:
li $v0, 4 #code for printing a string
la $a0, prompt
syscall
li $v0, 8 #code for reading a string
la $a0, buffer
li $a1, 80
syscall
li $v0, 4 #code for printing a string
la $a0, result
syscall
la $a0, buffer #code for printing a string
li $v0, 4
syscall
You can access a specific byte (character) of the string using lb. This will load the character in a register, to be used in your sorting algorithm.

Resources