Hey guys I am writing a mips program for a comp. arch. class that I am taking. The professor asked us to:
prompt for four integers that are between 65 and 90 inclusive, then print out a string of 4 ASCII character corresponding the input integers. For instance if user inputs '65 66 67 and 68' the program would print 'ABCD'. Assuming that we have only syscall #4 and do not have syscall #11.
I have added my code below, whenever I run this I get Runtime exception at 0x004000e8: address out of range 0x00000041 as an error. Any help into what i am doing wrong would be awesome, thank you.
.data #data Segment
stringMsg: .asciiz "Enter 4 integers that are between 65-90, inclusive: "
#Stores string in data segment + null terminator
alphaTable: .byte 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'
.text
string:
li $v0, 4 #Syscall #4 prints string
la $a0, stringMsg #Loads address of string "inputMsg"
syscall #Prints inputMsg
li $v0, 5 #Syscall #5 reads integer
syscall #reads inputted val into $v0
la $s0, ($v0) #save first number into s0
li $v0, 5 #Syscall #5 reads integer
syscall #reads inputted val into $v0
la $s1, ($v0) #save second number into s1
li $v0, 5 #Syscall #5 reads integer
syscall #reads inputted val into $v0
la $s2, ($v0) #save third number into s2
li $v0, 5 #Syscall #5 reads integer
syscall #reads inputted val into $v0
la $s3, ($v0) #save fourth number into s3
sub $s0, $s0, 65 #Subtract 65 from the 4 # to get correlating value in array
sub $s1, $s1, 65
sub $s2, $s2, 65
sub $s3, $s3, 65
la $s4, alphaTable #Loads table into $s4 (used to get index val)
la $s5, alphaTable #Loads table into $s5 (used to reset index val)
add $s5, $s5, $s0
li $v0, 4 #Syscall #4 prints string
lb $a0, ($s5) #Loads address of string "inputMsg"
syscall #Prints inputMsg
la $s5, ($s4)
add $s5, $s5, $s1
li $v0, 4 #Syscall #4 prints string
lb $a0, ($s5) #Loads address of string "inputMsg"
syscall #Prints inputMsg
la $s5, ($s4)
add $s5, $s5, $s2
li $v0, 4 #Syscall #4 prints string
lb $a0, ($s5) #Loads address of string "inputMsg"
syscall #Prints inputMsg
la $s5, ($s4)
add $s5, $s5, $s3
li $v0, 4 #Syscall #4 prints string
lb $a0, ($s5) #Loads address of string "inputMsg"
syscall #Prints inputMsg
exit:
li $v0, 10 #syscall #10: exit
syscall #ends program
You have two problems.
The first one which is the one that is giving you the exception is that your are loading int $a0
the character to print, and you are using service 4 which needs in $a0
the address of the (null-terminated) string to print.
So to fix that you would just change the lines
lb $a0, ($s5)
with
move $a0, $s5
or equivalent (e.g. add $a0, %s5, $zero
).
But you also need the string to be null-terminated. Thus, you should add a zero ascii code after each letter, and take into account that extra byte when you set the offset.
Thus, alphaTable
should be something like this:
alphaTable: .byte 'A', 0, 'B', 0, 'C', 0, 'D', 0, 'E', 0, 'F', 0, 'G', 0, 'H', 0, 'I', 0, 'J', 0, 'K', 0, 'L', 0, 'M', 0, 'N', 0, 'O', 0, 'P', 0, 'Q', 0, 'R', 0, 'S', 0, 'T', 0, 'U', 0, 'V', 0, 'W', 0, 'X', 0, 'Y', 0, 'Z', 0
and to take into account the extra byte in each offset you have to add twice the offset of the character to print.
Therefore instead of the move $a0, $s5
mentioned above you could just issue
add $a0, $s5, $s0
so that $a0
will hold the base address of the alphaTable
($s5
) plus twice the value of $s0
.
User contributions licensed under CC BY-SA 3.0