MIPS: Runtime exception - mips

.data
string: .ascii "String"
.text
main:
lb $t0, string
lb $a0, 1($t0)
li $v0, 11
syscall
li $v0, 10
syscall
I'm trying to run a code to print the first letter of "String", but I'm getting this:
line 8: Runtime exception at 0x00400008: address out of range 0x00000054

Try this:
.data
string: .ascii "String"
.text main:
la $t0, string ;gets the address of the string into $t0
lb $a0, 1($t0) ;loads the "t" in "String" into $a0
li $v0, 11
syscall
li $v0, 10
syscall
lb $t0,string isn't valid MIPS, since the CPU isn't able to load a byte from a constant memory address. (I'm surprised you didn't get an error message for that, actually.)

Related

Memory address out of bounds error in MIPS but works in a different function

I am making a program in MIPS that will compare two strings but I can't seem to access the proper locations. I have a function that calculates the lenght of the strings which works fine, but when I try to use a different function (in this case its just a test to see if it works), it says that i'm acessing the wrong memory. I am using QTSpim to test it. The function that works is the duzinaStringa, the one that does not is the krajStringa. Currently it's just trying to print out the first character of the string so I can make sure it works
.text
krajStringa:
li $v0,0
li $t0,0 #brojac1
move $t2, $a0 #adresa prvog
move $t3, $a1 #adresa drugog
lb $a0,0($t2)
li $v0,4
syscall
jr $ra
duzinaStringa:
move $t1,$a0
li $t0,0
petlja:
lb $t2,0($t1)
beqz $t2,krajDuzine
addi $t1,$t1,1
addi $t0,$t0,1
j petlja
krajDuzine:
addi $t0,$t0,-1
move $v0,$t0
jr $ra
main:
la $a0,str1
li $v0, 4
syscall
la $a0, arr
li $a1,200
li $v0, 8
syscall
la $a0,str1
li $v0, 4
syscall
la $a0, arr2
li $a1,200
li $v0, 8
syscall
la $a0,arr
jal duzinaStringa
move $a2,$v0
la $a0,arr2
jal duzinaStringa
move $a3,$v0
la $a0,arr
la $a1,arr2
jal krajStringa
li $v0,10
syscall
.data
arr: .space 200
arr2: .space 200
str1: .asciiz "Unesi string: \n"
As #ErikEidt pointed out, I used the wrong system call when printing the first character of a string.

How Read file "Comma Separated file" using Mips Assembly line by line then Char by Char and convert it from String of ASCII to Int?

enter image description here
the CSV file that i am trying to read
I am trying to read a CSV "Comma Separated file" In Mips assembly language, as string of ASCII then put it in Register or buffer as Integers .
If any one had the same problem or can help me with this .
I have tried to read a CSV file, put i couldn't continue .
.data
file: .asciiz "helloWorld.csv"
.word 0
buffer: .space 4
.text
main:
#open file
li $v0, 13
la $a0, file
add $a1, $zero, $zero
add $a2, $zero, $zero
syscall
add $s0, $v0, $zero
#read 4 bytes from file
li $v0, 14
add $a0, $s0, $zero
la $a1, buffer
li $a2, 4
syscall
#print
li $v0, 1
lw $a0, buffer
syscall
done:
li $v0, 16
add $a0, $s0, $zero
syscall
exit:
li $v0, 10
syscall

MIPS: writing on file, encoding problems

whenever I'm trying to write something on a file. I want the user to write the string that will be then saved into this file instead of having one previously prepared. But, when I do this, every word saved into the file ends with ^# so my usual text editor (gedit) won't read it for encoding reasons (but that's not a problem, emacs does the work successfully) and those characters marks somehow the end of the text (even if there's other following).
How can I remove it?
.data
fout: .asciiz "text.txt"
buffer: .space 100
cont: .asciiz "reading file... "
stringname: .space 10
nl: .asciiz "\n"
.text
main:
create:
#input a name
move $s6, $v0
li $v0, 8
la $a0, stringname
li $a1, 5
syscall
#open
li $v0, 13
la $a0, fout
li $a1, 9
li $a2, 0
syscall
move $s6, $v0
#write on file
li $v0, 15
move $a0, $s6
la $a1, stringname
li $a2, 5
syscall
#close
li $v0, 16
move $a0, $s6
syscall
exit:
li $v0, 10
syscall

accessing first character of an string and comparing it with an char MIPS

I want to compare a first character of a string with '#' char. If these are equal I want to print "they're equal" in mips. To do this, I've written a piece of code as below.However it does not give me an output even if they're equal. Is there anyone to help me ?
Thanks in advance.
.data
input: .space 201
string2: .asciiz "they're equal.\n"
finish: .byte '#'
.text
main:
la $a0,input
li $a1,201 #read 200 char
li $v0,8 #read string
syscall
jal evaluate
evaluate:
lw $t1, 0($a0)
lw $t2,finish
beq $t1,$t2,testi
testi:
la $a0,string2
li $v0,4
syscall
li $v0, 10
syscall
Yes, you have placed the branch in such a way that the next instruction is the same regardless of whether or not the branch is taken.
Consider changing it to something like this:
evaluate:
lw $t1, 0($a0)
lw $t2,finish
bne $t1,$t2,testi
la $a0,string2
li $v0,4
syscall
test1:
li $v0, 10
syscall

MIPS syscall on MARS: "address out of range"

I'm a newbie with MIPS & MARS. As a part of my program, I want to read a string from the user.
I have a simple code as follows
.globl test
.data 0x10010000
foo: .asciiz "Input a string"
.data 0x10020000
in: .asciiz "xyz"
.text 0x00400000
test:
li $v0, 54
add $a0, $zero, 0x1001
add $a1, $zero, 0x1002
add $a2, $zero, 3
syscall
The idea is to read a string of say length at most 3 into memory at 0x1002. Running the code gives me "Runtime exception at 0x00400010: address out of range 0x00001001".
Why does this happen? How do I fix it? I suspect I'm doing something very silly, but cannot figure it out.
I think you're mixed up with addressing. You've defined your buffer space at 0x10020000 but you're using an address of 0x1002 for the syscall. Same thing with the dialog string.
Your $a0 should be the address of the string that gets shown in the dialog; I suspect this should be foo:
la $a0, foo
And your buffer should be in:
la $a1, in
Edit: these are replacements for the adds, so in the end it should look like:
test:
li $v0, 54
la $a0, foo
la $a1, in
li $a2, 3
syscall
I've also replaced the $a2 line with something simpler. Try to see how they're equivalent.