.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).
Related
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.
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
I am working on my first project in mips, trying to print the sum of all the positive numbers in an array. I am now testing my code with QtSpim and have been getting an error saying I am using the main label twice even though there is only one occurrence. There is also no output, regardless of whether or not I include the main label. Here's the code:
.data
A: .word -89, 19, 91, -23, -31, -96, 3, 67, 17, 13, -43, -74
.text
main:
addi $s0, $zero, 0 #set $s0 for sum of positive nums to 0
la $s1, A #set $s1 to array address
addi $t0, $s1, 48 #set $t0 to exit point
while:
beq $s1, $t0, end
lw $t1, A($s1)
slt $t2, $t1, $zero
bne $t2, $zero, else #skips addition step if A at $s1 is negative
add $s0, $s0, $t1
else:
addi $s1, $s1, 4
j while
end:
li $v0, 1
move $a0, $s0
syscall
li $v0, 10
syscall
Sorry if this is bad formatting for mips, I have only ever worked with Java and C++.
there's an error on this line
lw $t1, A($s1)
What I believe you want is
lw $t1, 0($s1)
A positive integer must be in the offset of the lw instruction.
The main label error seems like a rabbit hole as you see no label was used more than once.
.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.
For a homework assignment in school, I need to use a MMIO LED display where each led is exactly 2 bits stored within a byte. For the assignment I need to "move" these LEDs up, down, left, and right. I also need to set the color (I will be using 0x40 for this). Here's my issue:
When I click the "right" arrow to move the LED over 1 column, it remains in the current column when it should be returning to black (0x00). If I click right 4 times (moving over exactly 1 byte), I get another lit LED, leaving the original one there.
Here is my MIPS code:
getLedPattern:
move $t2, $s2
andi $t1, $t2, 0x3 #remainder of x / 4 is in $t0
sll $t0, $t2, 2 #x / 4 is in $t0
beq $t0, 0, case0
beq $t0, 1, case1
beq $t0, 2, case2
case3:
andi $a0, 0xFFFFFFFC
#insert $a1 into bits 0 and 1 of $a0 into $v0
or $v0, $a0, $a1
jr $ra
case2:
andi $a0, 0xFFFFFCFF
#insert $a1 into bits 2 and 3 of $a0 into $v0
#srl $a1, $a1, 2
or $v0, $a0, $a1
jr $ra
case1:
andi $a0, 0xFFFCFFFF
#insert $a1 into bits 4 and 5 of $a0 into $v0
#srl $a1, $a1, 4
or $v0, $a0, $a1
jr $ra
case0:
andi $a0, 0xFCFFFFFF
#insert $a1 into bits 6 and 7 of $a0 into $v0
#srl $a1, $a1, 6
or $v0, $a0, $a1
jr $ra
setLED:
addi $sp, $sp, -20
sw $ra, 0($sp)
sw $t0, 4($sp)
sw $t1, 8($sp)
sw $t2, 12($sp)
sw $t3, 16($sp)
move $t5, $a0
sll $t6, $a1, 5 # y*32
srl $t2, $a2, 2 # x/4
add $t5, $t5, $t6
add $t5, $t5, $t2
lb $a0, 0($t5)
move $a1, $a3
jal getLedPattern
sb $v0, 0($t5)
move $s3, $t5
lw $ra, 0($sp)
lw $t0, 4($sp)
lw $t1, 8($sp)
lw $t2, 12($sp)
lw $t3, 16($sp)
addi $sp, $sp, 20
jr $ra
The logic is that it starts out at at memory location 0xFFFFOOO8 (top left LED), moves down one row (+32 bytes) and over x columns (plus x*bits). However, I can't seem to unset the current LED and move it over one. Any help would be appreciated. I believe that my or in getLedPattern: is wrong, but not 100% sure.
Hopefully, getting this correct I will be able to get this correct in a general sense (no LED display).
I guess that your constants for clearing bits are wrong.
try the following instead:
0xfffffffc // or ~0x03
0xfffffff3 // or ~0x0C
0xffffffcf // or ~0x30
0xffffff3f // or ~0xC0
There are other oddity in your code:
s2 is used, but never set
s3 is set, but never used
case1 and case2 will never be reached because $t0 can hold nor 1 neither 2