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

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?

Related

How to store/read string on MIPS?

I'm having trouble reading a string that previously was introduced by the user and saved in memory.
The function to read the string:
.data
table: .space 1200
buffer: .space 30
empty: .asciiz "The string is empty\n"
.text
la $t7,table
li $s4,0
li $v0, 8
la $a0,buffer
li $a1, 30
syscall
move $t0,$a0
move $a0,$t0 # parameter
jal insert
Then using the readed string, I pass through $a0 as a parameter to another function that stores the string to a certain memory position (with 60 bytes space within each string).
insert: mul $s6,$s4,60 # padding (index*padding)
add $t7,$t7,$s6 # address of the next free position
sw $a0,0($t7)
addi $s4,$s4,1
Also I've got a function that given an index (introduced by the user as an integer), recovers the string stored in that position (if is not empty):
read_table: move $t5,$a0 # a0 contains the index as a parameter
la $t4, table
mul $t5,$t5,60
add $t4,$t4,$t5
lw $s1,0($t4)
beqz $s1,empty_str
li $v0,4
move $a0,$s1
syscall
empty_str: li $v0,4
la $a0,empty
syscall
My problem comes when regardless of the index I enter, the program always return the last string introduced by the user although it seems to be stored properly. I understand that it might be some misuse of the buffer, but any of the solutions I've thought worked.
[EDIT] Here goes the whole code block to clarify the question, as suggested (supposing that all the data that the user introduces is OK):
):
.data
table: .space 1200
buffer: .space 30
empty: .asciiz "The string is empty\n"
.text
la $t7,table
li $s4,0
main: li $v0,4
la $a0,tira1
syscall
li $v0,5
syscall
beqz $v0,index # If 0, read from table, 1 insert to the table
beq $v0,1,string
string: li $v0, 8
la $a0,buffer
li $a1, 30
syscall
move $t0,$a0
move $a0,$t0 # parameter
jal insert
index: li $v0,5 # function to get index
syscall
move $a0,$v0
jal read_table
insert: mul $s6,$s4,60 # padding (index*padding)
add $t7,$t7,$s6 # address of the next free position
sw $a0,0($t7)
addi $s4,$s4,1
b main
read_table: move $t5,$a0 # a0 contains the index as a parameter
la $t4, table
mul $t5,$t5,60
add $t4,$t4,$t5
lw $s1,0($t4)
beqz $s1,empty_str
li $v0,4
move $a0,$s1
syscall
b main
empty_str: li $v0,4
la $a0,empty
syscall

Split String and store into 2 integer variables in MIPS assembly language

I am currently doing a program that needs user input a string of 7 digits, then split into 2 different integer variables to perform an equation.
Example for the string is 1900991: I would like to split the first 3 digits 190 into integer G, and the 0991 into integer K so i can use them to do equation with other 2 user defined integer.
Any help is appreciated. I am super new to the assembly language.....
Thank you!
Below is my code so far
.data
prompt: .asciiz "Please enter a value between 1-99: "
prompt2:.asciiz "Please enter a value between 2-4: "
message1:.asciiz "The first value is "
message2:.asciiz "\nThe second value is "
messageName: .asciiz "\nYour name is "
messageID: .asciiz "\nYour student id is "
messageResult: .asciiz "\nYour result is "
nameInput: .space 20
idInput:.space 20
.text
main:
li $v0,8
la $a0,nameInput
li $a1,20
syscall
li $v0,8
la $a0,idInput
li $a1,20
syscall
#Display hello
li $v0,4
la $a0,messageName
syscall
#Display name
li $v0,4
la $a0,nameInput
syscall
#Display hello
li $v0,4
la $a0,messageID
syscall
#Display name
li $v0,4
la $a0,idInput
syscall
#Prompt for user value 1
li $v0,4
la $a0,prompt
syscall
#get value 1
li $v0,5
syscall
#store the value 1 into temp value 1
move $t0,$v0
#Prompt for user value 2
li $v0,4
la $a0,prompt2
syscall
#get value 2
li $v0,5
syscall
#store value 2 into temp value 2
move $t1,$v0
#display value 1
li $v0,4
la $a0,message1
syscall
li $v0,1
move $a0,$t0
syscall
#display value 2
li $v0,4
la $a0,message2
syscall
li $v0,1
move $a0,$t1
syscall
#Display result (1)
li $v0,4
la $a0,messageResult
syscall
#Display result
jal equation
li $v0,1
addi $a0,$v1,0
syscall
#tell the system this is the end of main
li $v0,10
syscall

how can i break a String into characters in MIPS assembly,byte by byte.Basicly i want to take a user inputed array and print its letters one by one

.data
str10: .asciiz "Give me a string:\n "
.align 2
B: .Space 100
.align 2
C: .Space 100
.text
main:
add $10,$0,$0
StringChanger:
addi $12,$0,100
addi $11,$0,0 #j
addi $13,$0,0 #i
la $a0,str10
li $v0,4
syscall
li $v0,8
la $a0,C
li $a1,100 #arrays length
syscall
la $t0,1($a0)
lb $t1,($t0)
move $v0,$t1
li $v0,4
syscall

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.

How to prevent the user from inputting strings in MIPS

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

Resources