MIPS: Printing string with no pseduo instructions - string

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)

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 ?

mips-linux-gun-as and mips-linux-gnu-ld compile mips failed

I use the mips-linux-gnu-as and mips-linux-gun-ld compile the mips assembly code! The code is here!
.data
message: .asciiz "Hi, everybody.\nMy name is Alchemist.\n"
.text
main:
jal displayMessage
addi $s0, $zero, 5
li $v0, 1
add $a0, $zero, $s0
syscall
# Tell the program that the program is done
li $v0, 10
syscall
displayMessage:
li $v0, 4
la $a0, message
syscall
jr $ra
2.Then i compile the mips asseebe on ubuntu 18.04 TLS!This is my error:
mips-linux-gnu-ld: warning: cannot find entry symbol __start; defaulting to 00000000004000f0
3.How can i due to this error?
Thanks!!

MIPS not recognizing NULL

Forgive me for the easiness of this question but I am new to MIPS and am getting an odd error:
.data
myString: .asciiz "P5"
.text
li $v0 4
la $a0, myString
newLoop:
syscall
addi $a0, $a0, 1
beq $a0, $zero, done
j newLoop
done:
li $v0, 10
syscall
My program never terminates despite the fact that there is a null character (as I have gleaned from other posts, equivalent to $zero) at the end of myString. I would have thought that $a0 would point to "P", then "5", then "\0" and then jump to done and terminate.
Thanks for your eyes!
What you're comparing to zero is the address, not the value at that address. You need something like:
lbu $t0,($a0) # load the byte pointed to by $a0
beq $t0,$zero,done

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.

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)).

Resources