I'm getting an error which says MIPS: line 27: Runtime exception at 0x0040003c: address out of range 0x00000000. Don't really understand why - runtime

Write a MIPS assembly language program to count the number of occurrences of the lower-case "t" in a string. After running your code, the variable count (in memory) should contain the number of "t"'s in the string.
.text
.globl main
main: la $s1, strA
la $s2, count
lbu $s4, 0($s2)
add $s3, $zero, $zero
addi $t0, $zero, 116
add $t1, $zero, $zero
loop: lbu $s3, 0($s1)
addi $s1, $s1, 1
beq $s3, $t0, eqult
beq $s3, $zero, strgend
j loop
eqult: addi $t1, $t1, 1
j loop
strgend: sb $t1, 0($s4)
nop
.data 0x10010000
strA: .asciiz "the house on the left"
count: .byte 0

Related

Assembly: how does this algorithm know when to stop

This is a pretty easy code, but i was wondering how this algorithm could know how to stop himself.
I mean, is there a way i can tell the program to stop when it reaches the end of the last array (forth) without using a "Branch equal to zero" in the loop section?
.data
first: .word 1,second
second: .word 2,third
third: .word 3,forth
forth: .word 4
.text
la $t1, first
lw $t2, 0($t1)
add $t4, $zero, $t2
j loop
loop:
addi $t1, $t1, 8
lw $t3, 0($t1)
add $t4, $t3, $t4
j loop
li $v0, 1
move $a0, $t4
syscall
li $v0, 10
syscall
Sorry for my bad english, and thank you :)

MIPS/MARS Runtime exception at 0x00400018: address out of range 0x00000000

Im trying to run some code in MARS, but i get a runtime error
the error happens at line 9
lb $t1, ($t0)
my code is:
MAIN: addi $sp, $sp, -4
sw $ra, ($sp)
add $t6, $0, 0x30 # '0'
add $t7, $0, 0x39 # '9'
add $s0, $0, $0
add $t0, $a0, $0
LOOP:
lb $t1, ($t0)
slt $t2, $t1, $t6
bne $t2, $0, DONE
slt $t2, $t7, $t1
bne $t2, $0, DONE
sub $t1, $t1, $t6
beq $s0, $0, FIRST
mul $s0, $s0, 10
FIRST:
add $s0, $s0, $t1
addi $t0, $t0, 1
j LOOP
DONE:
add $v0, $s0, $0
lw $ra, ($sp)
addi $sp, $sp, 4
jr $ra
im fairly new to mips so i would love an explonation whats happening or is it just an syntax error ?

Assembly MIPS Stack, keep .data

Hello I'm trying to make a Quicksort, sorting algorithm using assembly (MIPS). I'm fairly new to assembly and I'm having some problems with how to use the stack in my program.
What I am trying to do is to use the stack in order to make the quicksort subroutine recursive. The problem I get when doing this is that the array that I'm trying to sort when moving the stack pointer.
Here's the code:
main:
li $s4, 0 # Min value of the range inside the array you want to sort
li $s5, 15 # Max value of the range inside the array that you want to sort
add $t6, $zero, $s4 # left-value
add $t7, $zero, $s5 # right-value
addiu $sp, $sp, -12 # We need space for the register $ra and register $a0
sw $ra, 0($sp) # A must have, if you wanna come back and avoid strange behaviour
sw $s4, 4($sp)
sw $s5, 8($sp)
jal quickSort
nop
ori $v0,$zero,10 # Prepare syscall to exit program cleanly
syscall
quickSort:
move $a0, $zero
add $a0, $zero, $t6 # Min value of the range inside the array you want to sort
move $a1, $zero
add $a1, $zero, $t7 # Max value of the range inside the array that you want to sort
li $t5, 4 # Temp to mult with
move $t3, $zero
add $t3, $a0, $a1
div $t3, $t3, 2
mult $t3, $t5
mflo $t3
lw $s0, data($t3) # Pivot = (min size + max size) / 2
mult $a1, $t5 # offset for min value
mflo $s1, # offset for array min
mult $a2, $t5 # offset for max value
mflo $s2 # offset for array max
sortLoop:
bgt $a0, $a1, endLoop # While min value is less or equal to max value
nop
leftLoop:
lw $t4, data($s1)
bge $t4, $s0, rightLoop
nop
add $s1, $s1, 4
add $a0, $a0, 1
j leftLoop
nop
rightLoop:
lw $t5, data($s2)
ble $t5, $s0, switchValue
nop
add $s2, $s2, -4
add $a1, $a1, -1
j rightLoop
nop
switchValue:
bgt $a0, $a1, repeat
nop
sw $t5, data($s1)
sw $t4, data($s2)
add $s1, $s1, 4
add $a0, $a0, 1
add $s2, $s2, -4
add $a1, $a1, -1
repeat:
j sortLoop
endLoop:
recursion:
bge $s4, $a1, recursion2
nop
addiu $sp, $sp, -12
sw $ra, 0($sp)
sw $a0, 4($sp)
sw $a1, 8($sp)
move $t6, $zero
add $t6, $t6, $s4 # left-value
move $t7, $zero
add $t7, $t7, $a1
jal quickSort
nop
recursion2:
bge $a0, $s5,endSorting
nop
addiu $sp, $sp, -12
sw $ra, 0($sp)
sw $a0, 4($sp)
sw $a1, 8($sp)
move $t6, $zero
add $t6, $zero, $a0
move $t7, $zero
add $t7, $zero, $s5
jal quickSort
nop
endSorting:
lw $ra, 0($sp)
lw $s4, 4($sp)
lw $s5, 8($sp)
addi $sp, $sp, 12
jr $ra
nop
My questions are as following:
Does the .data values also go to the stack?
Is this the proper use of the stack?
If not is there any good tutorials or something that can help me learn it better?

Asm program... something goes wrong

I load two arrays. I have to return a third array that contain the sum of the two and has the dimension of the smaller. Print the last one.
Example:
INPUT: Array1: [1 2 3 4 5] Array2: [2 6 1]
OUTPUT: Array3: [3 8 4]
Program runs... it give me the exact number of elements but every element is 0.
.data
vett1: .word 0:100
vett2: .word 0:100
vett3: .word 0:100
x: .word 0
space: .asciiz " "
.text
.globl main
main:
la $a0, vett1
la $a1, vett2
la $a2, vett3
la $a3, x
li $t0, 0
li $t1, 0
li $t2, 0
jal loadA1 #mi salvo 26 salto a caricavettore1
jal loadA2 #mi salvo 27 salto a caricavettore2
jal lenght #mi salvo 28 salto a controllalunghezza
lw $t2, ($a3)
lw $t3, ($a3)
la $a0, vett1
la $a1, vett2
la $a2, vett3
jal summ
jal print
li $v0, 10
syscall
loadA1:
li $v0, 5
syscall
beq $v0, -1, exit
sw $v0, ($a0)
addi $t0, $t0, 1
addi $a0, $a0, 4
j loadA1
exit: jr $ra
loadA2:
li $v0, 5
syscall
beq $v0, -1, exit2
addi $t1, $t1, 1
sw $v0, ($a1)
addi $a1, $a1, 4
j loadA2
exit2: jr $ra
lenght:
blt $t0, $t1, cond1
sw $t1, ($a3)
jr $ra
cond1: sw $t0, ($a3)
jr $ra
summ:
subi $sp, $sp, 4
sw $ra, 0($sp)
bnez $t2, rec
j exit3
rec: lw $s0, ($a0)
lw $s1, ($a1)
add $v0, $s0, $s1
sw $v0, ($a2)
addi $a0, $a0, 4
addi $a1, $a1, 4
addi $a2, $a2, 4
subi $t2, $t2, 1
jal summ
exit3:
lw $ra, 0($sp)
addi $sp, $sp, 4
jr $ra
print:
beqz $t3, exit4
lw $a0, ($a2)
li $v0, 1
syscall
la $a0, space
li $v0, 4
syscall
addi $a2, $a2, 4
subi $t3, $t3, 1
j print
exit4: jr $ra
When you sum the two arrays you add 4 to $a2 at the end of each iteration to point it to the next element of vett3. So after the summing is complete $a2 will point to the first memory location after the end of vett3.
Then you call print but don't reset $a2 to point at the start of vett3, so you end up printing garbage data (which could happen to be all zeroes - or mostly zeroes at least, since x and space will be there).

Printing to Screen in MARS for MIPS program

Hi I have to write a program in MIPS that does a bubble sort and then prints the sorted array and plays a note(s). I am having trouble getting it to execute in Mars and I am wondering what I need to add to my program:
.include "ec2_data.asm" # load values into array
add $s0, $zero, 0x10010000
add $t0, $zero, 0x10010000 #how many times to loop
addi $t1, $zero, 0 #initilize the counter
addi $t2, $zero ,0 #initilize position x
addi $t3, $zero, 4 #initilize position y
lw $s2, $t3($s0) #get second position of $s0=y
LOOP:
addi $t0, $t0, -1 #subtract one from the counter
slt $t4, $s1, $s2 #t4 set to 1 if x > y
beqz $t4, BREAK #if t0 is zero (x<y) we dont' have to sort, so go to break
# sort:
add $t5, $zero, $s1 #temp. stores the value of x in t5
add $s1, $zero, $s2 #moves the value of y into x
add $s2, $zero, $t5 #moves the value of x into y
sw $s1, $t2($s0) #save the new x to register
sw $s2, $t3($s0) #save the new y to register
j BREAK
BREAK:
#in here: check to see if we have reached the end
#if not, increment t2 and t3 by 4 to get the next values and jump back to loop to go again
beq $t0, $zero, END #if we are done with the array, go to end
addi $t2, $t2, 4
addi $t3, $t3, 4
j LOOP #jump back to loop again
END:
li $v0, 1 # print integer
syscall
addi $a0, $0, 0xA # load line code into register
addi $v0, $0, 0xB # print new line
syscall
addi $v0, $zero, 33 # midi out synchronous
addi $a0, $zero, 60 # Middle-C
addi $a1, $zero, 250 # ms
addi $a2, $zero, 120 # some instrument
addi $a3, $zero, 64 # some volume
add $a0, $t7, $zero # load value into $a0
syscall # play note!
addi $t6, $t6, 4 # shift memory location by 32 bits
addi $s1, $s1, 1 # increment counter ++
j loop # loop
Exit:
li $v0, 10 # load exit code
syscall # exit
If by "having trouble getting it to execute", you mean it acts strange with the .include file, then you might not have a required option checked in MARS. Go to the Settings menu and check the box for "Initialize Program Counter to global Main if defined". That seems to be something that has to be in effect for included files to work - and for some reason, it isn't turned on by default.

Resources