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.
Related
.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 ?
I have a function called saisir that needs to take in user input text and lets you enter \n 's in the input as part of the text. It returns the text that was entered by the user avec reading \n\n.
.data
buffer: .space 300
msg: .asciiz "You entered:\n"
.text
main: #calling the function
jal saisir
li $v0,10 #end the program
syscall
saisir:
li $v0,8 #take in input
la $a0, buffer #address where we store buffer
li $a1, 300
syscall
li $t0, 0
lb $t0, ($a0)
li $t1, 10 #\n caracter in ASCII
bne $t0, $t1, saisir
#Displays "You entered:"
li $v0, 4
la $a0, msg
syscall
#Displays User's input
li $v0, 4
la $a0, buffer
syscall
jr $ra #goes back
The code written right now overwrites every line of text entered by the user, therefore returns a string that is empty.. How to add the strings up as you would in a high level language with "+=" in MIPS?
# Data section
.data
insert1: .word 20 # make space in memory for a word with address insert1
insert2: .word 20
insert3: .word 20
insert4: .word 20
insert5: .word 20
Input: .asciiz "Enter a Series of 5 formulae\n"
Output: .asciiz "The values are:\n"
CR: .asciiz "\n"
# Text Section
.text
.globl my_main
my_main:
la $a0, Input
li $v0, 4
syscall
la $a0, insert1 # sets $a0 to point to the space allocated for writing a word
la $a1, insert1 # gets the length of the space in $a1 so we can't go over the memory limit
li $v0, 8
move $t0, $a0
syscall
la $a0, insert2
la $a1, insert2
li $v0, 8
li $v0, 8
move $t1, $a0
syscall
la $a0, insert3
la $a1, insert3
li $v0, 8
li $v0, 8
move $t2, $a0
syscall
la $a0, insert4
la $a1, insert4
li $v0, 8
li $v0, 8
move $t3, $a0
syscall
la $a0, insert5
la $a1, insert5
li $v0, 8
li $v0, 8
move $t4, $a0
syscall
la $a0, Output
li $v0, 4
syscall
la $a0, insert1
li $v0, 4
syscall
la $a0, insert2
li $v0, 4
syscall
la $a0, insert3
li $v0, 4
syscall
la $a0, insert4
li $v0, 4
syscall
la $a0, insert5
li $v0, 4
syscall
# Exits Program
li $v0, 10
syscall
Results I'm getting:
Enter a Series of 5 formulae:
hey
man
how
are
you
The values are:
hey
man
how
are
you
man
how
are
you
how
are
you
are
you
you
Results I want:
Enter a Series of 5 formulae:
hey
man
how
are
you
The values are:
hey
man
how
are
you
So basically I need to take 5 strings from the console and then print out as seen above. Weirdly enough when I use words that are only 1 letter long it prints out correctly but anything else other than single letters returns a sequence of words similar to what I've shown above. I'm new to MIPS and not sure what I'm doing wrong here. Any help would be appreciated thanks.
EDIT: It seems I just needed to use .space instead of .word. Thanks to those who commented/answered the question for helping out.
I'm trying to finish writing a mips palindrome function that "reads the same forward as backwards" and my function is not working as needed. This is hurting my brain and any help is greatly appreciated. Thank you!
A recursive definition of a palindrome is as follows:
Let the string S be represented as S = S1 S2 … SN, where N is the length of the string.
S is a palindrome
if N = 0 or N = 1 or
if N 2 and S1 = SN and the substring (S2 .. SN-1) is a palindrome.
Function Palin($a0, $a1)
Precondition:
$a0 holds the address of the first byte in the string (or substring) being tested while $a1 holds the length of the string (or substring)
Postcondition:
1 (true) is returned in the register $v0 if the given string (or substring) is a palindrome; otherwise, 0 (false) is returned in $v0.
.data
prompt1: .asciiz "Enter length of string to be read: "
prompt2: .asciiz "Enter the string "
ItIs: .asciiz "\nThe string IS a palindrome!"
IsNot: .asciiz "\nThe string is NOT a palindrome!"
string: .asciiz ""
.text
.globl main
main:
la $a0, prompt1
li $v0, 4
syscall
li $v0, 5
syscall
move $a1, $v0
la $a0, prompt2
li $v0, 4
syscall
addi $a1, $a1, 1
la $a0, string
li $v0, 8
syscall
addi $a1, $a1, -1
jal Palin
bne $v0, $zero, label1
la $a0, IsNot
j label2
label1:
la $a0, ItIs
label2:
li $v0, 4
syscall
li $v0 10
syscall
nop
Palin:
# HERE IS THE FUNCTION
li $v0, 10
syscall
addu $ra, $zero, $s7 #restore $ra since the function calles
#another function
jr $ra
add $zero, $zero, $zero
add $zero, $zero, $zero
EndPalin:
The problem is this:
Palin:
# HERE IS THE FUNCTION
li $v0, 10 # These
syscall # lines
The value 10 is the trap to exit the program and since you loaded that into register $v0 and then did a syscall, the program exits. Remove those offending lines and you should be on your jolly way.
HTH
.data:
str2: .space 20
#read string
la $a0,str2
li $a1,20
li $v0,8
syscall
#print string
la $a0, str2
li $v0, 4
syscall
#print string
la $a0, str2
li $v0, 4
syscall
The result:
EXAMPLETEX
(I DONT WANT THIS BREAK... HOW DO I REMOVE IT?)
EXAMPLETEXT
-- program is finished running (dropped off bottom) --
Your problem is that service 8 adds a newline at the end of the input string, so you have to replace it with a null byte (0)
Add this code after the syscall that reads str2:
# remove trailing newline
li $a3, '\n'
newlineloop:
beqz $a1, newlineloopend
subu $a1, $a1, 1
lb $a2, str2($a1)
bne $a2, $a3, newlineloop
li $a3, 0
sb $a3, str2($a1)
newlineloopend:
It will search for that newline character and replace it with a null byte