MIPS store exception error - mips

So I am using QtSpim to run my mips program for school. I basically made a working program, but now i keep getting an exception error. I've tried cutting down my code, so here is where the error starts now.
.text
.globl main
main:
.data
message1: .asciiz "The maximum is "
message2: .asciiz "The summation is "
myArray: .space 32
.text
addi $s0, $zero, 11
addi $s1, $zero, 12
addi $s2, $zero, -10
addi $s3, $zero, 13
addi $s4, $zero, 9
#addi $s5, $zero, 12 not needed
addi $s5, $zero, 14
addi $s6, $zero, 15
addi $s7, $zero, -20
addi $t0, $zero, 0
sw $s0, myArray($t0)
addi $t0, $t0, 4
li $v0,10
syscall
The error is sw $s0, myArray($t0)

You likely need to make sure myArray is aligned to a 32-bit boundary. I suggest trying a .align directive before its declaration.

Related

MIPS Stack Segment can't be Expanded

I'm writing a function that should return the square root of a perfect square recursively as part of a longer assignment.
I was following this mathematical method before I reverted to an even simpler algorithm to see if the error would repeat itself and it did.
The following code:
.data
prompt: .asciiz "num2sqrt: "
.text
.globl main
sqrt:
# save return address & t0
addi $sp, $sp, -8
sw $ra, 0($sp)
sw $t0, 4($sp)
# t0 = n
move $t0, $a0
# a0 = n/2
srl $a0, $t0, 1
jal sqrtRecursor
#restore return address & t0
lw $t0, 4 ($sp)
lw $ra, 0 ($sp)
addi $sp, $sp, 8
jr $ra
sqrtRecursor:
# save return address & t1
addi $sp, $sp, -8
sw $ra, 0($sp)
sw $t1, 4($sp)
# square test
mult $a0, $a0
mflo $t1
beq $t1, $t0, returnAnswer
bne $t1, $t0, newGuess
#restore return address & t1
lw $t1, 4 ($sp)
lw $ra, 0 ($sp)
addi $sp, $sp, 8
jr $ra
returnAnswer:
move $v0, $a0
newGuess:
# t1 = (((x+1)*guess)/x)/2
# x+1
addi $t1, $t0, 1
# *guess
mult $a0, $t1
mflo $t1
# /x
div $t1, $t0
mflo $t1
# /2
srl $t1, $t1, 1
move $a0, $t1
jal sqrtRecursor
main:
#print "Enter num2sqrt: "
la $a0, prompt
li $v0, 4
syscall
#input num2sqrt
li $v0, 5
syscall
move $s1, $v0
move $a0, $s1
jal sqrt
# print result
move $a0, $v0
li $v0, 1
syscall
# end
li $v0, 10
syscall
returns the following error on QTSpim:
Can't expand stack segment by 12 bytes to 524288 bytes. Use -lstack # with # > 524288
which then hangs the app for a minute or so.
I've double checked that I'm saving and returning all my return addresses and used variables, and also attempted implementing the same algorithm in Java separately (which worked), but have yet been unable to figure out what I need to fix and where.
I've been able to implement a power function before this so I'm not a complete novice and am asking after putting in approximately 5 hours of research and debugging on this.
It could be a stack management problem or an if/else implementation error from the intuition I have about my own code.

MARS emulator for MIPS Error: address out of range 0x20000000

I am not sure where I am messing up, but I am using the MARS emulator for MIPS and am running into an error at my
lw $s6, 0($s7) #s6 now holds c[i] line.
here is my error:
Error in C:\Programming\mips1.asm line 14: Runtime exception at 0x00400020: address out of range 0x20000000
Go: execution terminated with errors.
.text
.globl main
main:
add $s0, $zero, $zero
lui $s3, 0x2000
addi $s5, $zero, 100
addi $sp, $sp, -8
Loop:
slt $t2, $s0, $s5
beq $zero, $t2, Exit
sll $s7, $s0, 2
add $s7, $s7, $s3 #s7 is now the address of c[i]
lw $s6, 0($s7) #s6 now holds c[i]
slt $t3, $s0, $s1 #set t3 if i is less than a
bne $zero, $t3, Else
sw $s2, 0($s7) #stores b into c[i]
add $s0, $s0, 1
j Loop
Else: add $s2, $zero, $s6
add $s0, $s0, 1
j Loop
Exit: addi $sp, $sp, 8
li $v0, 10
syscall
I solved it, turns out I needed an address that was lower in order for it to be a part of the heap. MARS heap starts at 0x10040000 and moves up.
I changed 0x2000 to 0x1005 and it worked in MARS

mips code solutions - found error myself

.data
.word 5
.word 4
.word 3
.word 2
.word 1
.text
.globl main
addi $t5, $zero, 0x10010000 #base address - 0x10010000
main:
Loop:
addi $t0, $t0, 0
bge $t0, 5, Exit
sll $t1,$t1,2
add $t2, $t5, $t1
lw $t3, 0($t2)
sll $t3,$t3,1
sw $s0, 0($t3)
addi $t0,$t0,1
j Loop
Exit:
The error I discovered was in sw $s0, 0($t3). The exception is the following: store address not aligned on word boundary 0x0000000a. Based on this error, it looks like there is no word alignment. What is the best way to fix the error?
Your feedback (or constructive criticism) is greatly appreciated.

Reversing a string in MIPS

Trying to get this program to work but I can't figure out why I keep getting an error. The program is suppose to take an entry of 11 characters, then output the reversed input. The error is as below.
Runtime exception at 0x00400034: store address not aligned on word boundary 0x7fffeffb
The code is posted below. I also need to store the final reversed string into 'revstr'. I am just having trouble with the reversal.
.data
str: .space 12
revstr: .space 12
prompt: .asciiz "Enter in string, max 11 characters: "
.text
main:
la $a0, prompt #calling opening prompt
li $v0, 4
syscall
li $v0, 8
la $a0, str
li $a1, 12
syscall
li $t0, 0
subu $sp, $sp, 1
sw $t0, ($sp)
li $t1, 0
bump1:
lbu $t0, str($t1)
beqz $t0, stend
subu $sp, $sp, 4
sw $t0, ($sp)
addu $t1, $t1, 1
j bump1
stend: li $t1, 0
populate:
lw $t0, ($sp)
addu $sp, $sp, 4
beqz $t0, done
sb $t0, str($t1)
addu $t1, $t1, 1
j populate
done:
li $v0, 4
la $a1, str
syscall
li $v0, 10
syscall
You are using a 32-bit machine, you should use offsets that are multiples of 4 bytes
=> 4*8=32
for you words to be aligned
otherwise your instructions must be byte oriented (lb sb lbu etc.. not lw..)
also correct these:
lw $t0, ($sp) it is better to specify the offset to avoid confusion
The code you posted confused me pretty good. You are modifying $sp in the middle of a function which is generally a no-go, and I couldn't figure out why you were doing it. Also, you say you want to store the reversed string in revstr but all sb instructions are pointed at str.
It's a good idea when communicating assembly to others to give them some pseudo-code to show them the general approach you are attempting to use. Here's mine:
str := user_input()
$t1 := len(str)
$t2 := 0
while ($t1 >= 0) {
revstr[$t2] := str[$t1]
$t1 := $t1 - 1
$t2 := $t2 + 1
}
With that mental picture, it should be fairly easy to understand how I've modified your assembly:
str: .space 12
revstr: .space 12
prompt: .asciiz "Enter in string, max 11 characters: "
.text
main:
la $a0, prompt #calling opening prompt
li $v0, 4
syscall
li $v0, 8
la $a0, str
li $a1, 12
syscall
li $t1, 0
bump1:
lbu $t0, str($t1)
beqz $t0, stend
addu $t1, $t1, 1
j bump1
stend:
li $t2, 0
addi $t1, $t1 -1
populate:
blt $t1, $zero, done
lb $t3, str($t1)
sb $t3, revstr($t2)
sub $t1, $t1, 1
add $t2, $t2, 1
j populate
done:
sb $zero, revstr($t2) #null terminate revstr
li $v0, 4
la $a0, revstr
syscall
li $v0, 10
syscall

Attempt to execute non-instruction in mips assembler?

.data
stack: .word 3, 2
.text
.globl main
main:
la $s1, stack #assign stack start memory to $s1
addi $t3, $t3, 0 #clear $t3
addi $t3, $t3, 4 #assign 4 to $t3
add $s1, $s1, $t3 #second member of stack
lw $t1, 0($s1) #d2
addi $t3, $t3, -4 #move $t3 forward
add $s1, $s1, $t3 #first member of stack
lw $t0, 0($s1) #d1
add $t0, $t0, $t1 #d1 = d1 +d2
sw $t0, 0($s1) #store new d1 at d1's location
I'm trying to create a mips program, which gets last-1 and last member of the stack, and add them, and store it. I don't care about addi $t3, $t3, 4 or stack: .word 3, 2 it's just for test.
However, when I run this at qtspim I got an error message "attempt to execute non-instruction at 0x0040004c" please enlighten me as to what the problem is.
You need to end your program with a jr $ra, otherwise the processor will just keep executing whatever random instructions that happen to come after the sw $t0, 0($s1).