Print a char extracted from a string in mips - string

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.

Related

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 ?

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?

MIPS Assembly: Immediate value is too large for field error

When trying to store a user's inputed string, for part of a project, I receive the following error in spim when I simply load the file:
Immediate value is too large for field: [0x0040009c]
Below is my code:
.globl main
.data
prompt: .asciiz "0: exit, 1: enter string, 2: convert, 3: mean, 4: median, 5: display string, 6: display array: " #94 char long
enter: .asciiz "Enter string: "
.text
main:
display: addi $v0, $v0, 4 #print prompt
lui $a0, 0x1000 #grabbing prompt
syscall
addi $v0, $0, 5 #get integer
syscall
beq $v0, 0, rtn #if user type's 0, exit program
nor $0, $0, $0 #nop
beq $v0, 1, enterString #if user type's 1, enterString
nor $0, $0, $0 #nop
enterString:
addi $v0, $0, 4 #printing string
lui $a0, 0x1000 #grabbing prompt
addi $a0, $a0, 95 #grabbing enter
syscall
addi $v0, $0, 8 #grabbing input
sw $a0, 0x10000100 #storing inpuit - this specific address is a requirement
syscall
rtn: jr $ra
Now, when I run this I get the above mentioned error. However, I'm not quite sure why. It may be due to a string being 32 bit? Any explanations as to why would be appreciated. Thanks again!
I see a couple of problems in your code:
This is way longer than 94 chars:
prompt: .asciiz "0: exit, 1: enter string, 2: convert, 3: mean, 4: median, 5: display string, 6: display array: " #94 char long
Even if you remove those extra spaces, I still count 95 chars.
Don't assume that registers start out with a certain value:
addi $v0, $v0, 4 #print prompt
This should be addi $v0, $zero, 4.
This should probably be 0x1001, since the data section starts at 0x10010000:
lui $a0, 0x1000
Same goes for all other places where you're trying to access the data section.
I don't know if SPIM translates this into a valid instruction:
sw $a0, 0x10000100
If not, you should load the address into a register first (e.g. $a1), and access memory through that register (e.g. sw $a0, ($a1)).

MIPS: Printing string with no pseduo instructions

Below I have
.globl main
.data
prompt:
.asciiz "Hello world!"
.text
main:
addi $v0, $v0, 4
lui $a0, 0x1000
syscall
rtn:
jr $ra
Now, I'm not sure why the string is not printing, it runs without syntax errors. Also, I am not permitted to use any pseudo-instructions, so that is why I am doing this the slightly longer way. That is where the error is coming in, I do not know where the prompt string is being stored? Any help is appreciated!
Thanks!
In SPIM, the .data section starts at address 0x10010000 by default. So to print the Hello World string without using pseudo-instructions you could use this:
.globl main
.data
prompt:
.asciiz "Hello world!"
.text
main:
addi $v0, $zero, 4
lui $a0, 0x1001 # $a0 = 0x10010000
syscall
rtn:
jr $ra
Note that if the address is 0x1000, that means the upper 16 bits are all zero, and the bottom 16 bits are 0x1000. You are loading the upper 16 bits. So instead of lui $a0, 0x1000 try addiu $a0, $0, 0x1000
However, your assembler's symbol manipulation expressions shouldn't count as pseudo-instructions, so something like this GAS code (or the equivalent in your assembler) should also be allowed:
lui $a0, %hi(prompt)
ori $a0, $a0, %lo(prompt)

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