MIPS Assembly: Immediate value is too large for field error - string

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

Related

String input in MIPS omits the first four characters that are inputted

.data
EntryReq:
.asciiz "Please enter an 8 digit hexadecimal MIPS instruction: \n"
InputLongError:
.asciiz "\nYour input was too long, make sure it is 8 digits. "
InputShortError:
.asciiz "\nYour input was too short, make sure it is 8 digits. "
CharInvalidError:
.asciiz "\nYour input contains an invalid character. "
ValidChars:
.asciiz "0123456789abcdef\n\b\0"
.align 4
input:
.space 20
.text
main:
#Print input request
la $a0, EntryReq #loads input into arg. reg.
li $v0, 4 #op code for print string
syscall
#take input for input (stored)
li $v0, 8 #op code for take user input
la $a0, input #provide address for syscall
li $a1, 20 # tell syscall the byte space required for the string
syscall
#move to input(stored)
sw $v0, input #move inputted into from $v0 to input(stored)
#check validity of input
la $a0, input #load address of input to arg. reg. for method call
la $a1, ValidChars #load address of string of valid chars
jal verifyInput #call the verifyInput method which does as expected
#test if string length count works
addi $a0, $v0, 0 #load from $v0 to arg. reg.
li $v0, 1 #op code for print int
syscall
terminate:
li $v0, 10
syscall
verifyInput:
li $v0, -1 #start length count at 0
verifyLoop:
lb $t0, ($a0) #load current
li $a2, 0 #loop for char check, loops up to length of validChar string
la $a1, ValidChars
j checkChar
charVerified: #ignore this, is entry point back into verifyLoop for checkChar
addi $a0, $a0, 1 #increment
addi $v0, $v0, 1
bgt $v0, 8, printTooLongError #if result bigger than 8, error
bne $t0, 10, verifyLoop #10 is string end, so check if string is end
blt $v0, 8, printTooShortError #if result less than 8, error
jr $ra #if here string input was confirmed okay
checkChar: # loops through valid chars for each char in $a0 | Valid Chars: 0123456789abcdef\n |
lb $t1, ($a1) #loads in byte from char string
addi $a1, $a1, 1 #increment address, for the next char
addi $a2, $a2, 1 #increment until length of valid char string is reached
beq $t0, $t1, charVerified
bne $a2, 19, checkChar #if length of valid chars changes, change second argument here
j charNotValidError
charNotValidError:
la $a0, CharInvalidError #loads input into arg. reg.
li $v0, 4 #op code for print string
syscall
j terminate
printTooLongError:
la $a0, InputLongError #loads input into arg. reg.
li $v0, 4 #op code for print string
syscall
j terminate
printTooShortError:
la $a0, InputShortError #loads input into arg. reg.
li $v0, 4 #op code for print string
syscall
j terminate
The general gist of this code is for the user to input an 8 digit hexadecimal string, and then the program checks whether it is a valid hexadecimal string (i.e. includes only 0-9 and a-f). However, whenever I run it, the string that I input is missing the first four characters. So if I place invalid characters in the first four digits, like wwww1abc, then the code runs fine, which it shouldn't. But if I do 1abcwwww, it outputs an invalid character error, which it should. I'm genuinely confused as to why this is the case, nor have I seen anyone else experience this issue. Any help is greatly appreciated.
The problem is this line:
#move to input(stored)
sw $v0, input #move inputted into from $v0 to input(stored)
Unlike read integer syscall, read string puts the result in the input buffer, in your case input. So you don't need to read out the value in $v0 and by storing it in input you're overwriting the first 4 bytes of the buffer with the value of $v0, which is still 0x00000008, which conveniently is the same as the string "\b\0\0\0" for little endian machines, all of which are in your validity list. Removing that line should fix your program (though I didn't look over all the rest of the code for errors).

Mips error printing length of string

This seems like it should work but clearly doesn't. When I try to count the length of the string it returns some crazy long number. When I one step the program it works fine but prints out the wrong number. Can somebody point me back on track so that I can work on the next function? I just need to figure out what is throwing off the count
############################################
# Program Name: strings.s
# Programmer: David Bierbrauer
# Date: 9/07/2017
############################################
# Functional Description:
#
#
############################################
# Cross References:
#
#
############################################
.data # Data declaration section
stringa: .asciiz "Enter the first string: "
stringb: .asciiz "\nEnter the second string: "
.text
main: # Start of code section
#li $v0, 4 #get first string input
la $a0, stringa
jal read
jal length
jal print
#get second string input
la $a0, stringb
jal read
jal length
jal print
j end
length:
li $t0, -1 #loop count 0
j lengthloop
lengthloop: #exit address $s1
lb $t1, 0($a0) #load next char into $t1
beqz $t1, endlength
addi $a0, $a0, 1 #increment string pointer
addi $t0, $t0, 1 #increment count
j lengthloop
endlength:
jr $ra
equality:
lb $t2, 0($a0)
lb $t3, 0($a1)
bne $t2,$t3 notequal
append:
print:
li $v0, 1
la
syscall
jr $ra
read:
li $v0, 4
syscall #print prompt
li $v0,9
li $a0,80
syscall
move $a0,$v0
li $v0,8
li $a1,80
syscall
jr $ra
end:
li $v0, 10
syscall
The code you've posted doesn't even assemble, because there's a stray la in the print routine.
Aside from that, your length routine returns the length in $t0, while your print routine assumes that the value is in $a0. That's why you get the wrong output.
Your print looks odd. spim yells at me - bad syntax of la pseudoinstruction. I guess, you mean
print:
li $v0, 1
la $a0, 0($t0)
syscall
jr $ra

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

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)

Pig Latin in MIPS Assembly

In my MIPs Assembly Programming class I've been tasked with writing a program that converts a string into simplified pig latin.
Simplified pig latin assumes all words in the string are at least 2 characters long, and every word has its first letter shifted to the end followed by "ay". Also, assume there are no punctuation marks of any kind.
So, "stackoverflow is great" becomes "tackoverfloway siay reatgay".
I've taken a stab at this program and I'm nearly completed. It works fine, except that for some reason I have a line break in the middle of the last word every time.
For example:
tackoverflowsay siay reat
gay
Instead of:
tackoverflowsay siay reatgay
I've tried decuding what would be causing this line break in the last exit portion of my program but I don't see it. Do I have a pre-mature null terminated string? If I do, I don't see it.
Here is my code:
####################################################
# Text Segment
####################################################
.text
.globl main
main:
la $t0, char
la $t1, buffer
lb $t3, space
la $a0, prompt
li $v0, 4
syscall
la $a0, buffer
li $a1, 200
li $v0, 8
syscall
lb $t2, ($t1) # Load first char
addi $t1, $t1, 1
loop:
lb $t4, ($t1) # Load next character into $t4
addi $t1, $t1, 1
beqz $t4, exit # Reached end of string, exit?
beq $t3, $t4, loop2 # If $t4 = " " char, second loop
move $a0, $t4 # else, lets keep printing chars
li $v0, 11
syscall
j loop
loop2:
move $a0, $t2
li $v0, 11
syscall
la $a0, aystr
li $v0, 4
syscall
lb $t2, ($t1)
addi $t1, $t1, 1
j loop
exit:
move $a0, $t2
li $v0, 11
syscall
la $a0, aystr
li $v0, 4
syscall
li $v0, 10
syscall # Cya...
####################################################
# Data Segment
####################################################
.data
prompt: .asciiz "Enter Phrase: "
result: .asciiz "Pig Latin: "
space: .ascii " "
aystr: .asciiz "ay "
char: .byte 1
buffer: .byte 200
You almost certainly have a string of the form:
stackoverflow is great\n
where \n is a newline character. This would translate into:
tackoverflowsay siahy reat\ngay
if you simplistically detected the end of the word as either space or null-terminator.
I'm not going to give you the code (since this is homework) but the easiest solution, in my opinion, would be to have another loop processing the entire string, replacing all "\n" characters with spaces.
This would be done before your latinization loops.

Resources