MIPS error - find a maximum in an array - max

I want to write a program which finds the maximum value in an array.
When I try to start my programm I get the error "Exception occurred at PC=0x0040004c". I have no idea what this means and I have no idea whats wrong with my code.
.data
liste: .word 1, 2 ,3 ,41, 5, 6, 7
.text
main:
la $a0 liste #adresse des ersten Elements in $a0 gespeichert
la $a1 28($a0) #adresse des letzten Elements in $a1 gespeichert
li $v0 0 #speichere 0 in v0
tester:
beq $a0 $a1 exit #test, if we are at the end of the array
j findmax
findmax:
la $t0 ($a0) #copy a0 in t0
speichermax:
la $v0 ($t0) #adress of t0 (Maximum) in v0
lw $v1 ($v0) #value of the max in v1
loop:
la $t0 4($t0) #go to the next field content
beq $v0 $a1 exit #end of field is reached
lw $t1 ($t0)
sub $t1 $t1 $v1 # t1 = t1 - v1
bgtz $t1 speichermax
#bgt $t1 $v1 speichermax #test if t1>v1 -> yes: save the new max in v1 (über speichermax)
j loop
exit:
lw $a0 ($t0) #print the maximum
li $v0 1
syscall
li $v0 10 #exit
syscall

Skipping the rest of the program I believe that this: la $a1, 28($a0)
is wrong. Try this: addi $s1, $a0, 28.

Related

Runtime exception at 0x0040002c: address out of range 0x00000001 in MIPS Assembler for ceasar encryption

The Programm compiles just fine but as soon as i give input this error is thrown:
"Error in line 28: Runtime exception at 0x0040002c: address out of range 0x00000001
Processing terminated due to errors."
Here ist the code for my programm:
.data
line: .space 80
.text
main:
# read line from stdin
li $v0, 8
la $a0, line
li $a1, 80
syscall
# call caesar_line
li $a0, 1
jal caesar_line
# return 0
li $v0, 0
jr $ra
caesar_line:
# save start of line
move $t0, $a0
# loop through line
loop:
# load the character from memory
move $t1, $a0
lb $a0, 0($t1)
beqz $a0, end_loop
jal caesar_char
sb $v0, 0($t1)
addi $t1, $t1, 1
move $a0, $t1
j loop
end_loop:
# print line
li $v0, 4
move $a0, $t0
syscall
jr $ra
caesar_char:
# check if c is lowercase
blt $a0, 'a', upper
bgt $a0, 'z', not_alpha
addi $a1, $a1, 'a'
jal caesar_helper
j done
upper:
# check if c is uppercase
blt $a0, 'A', not_alpha
bgt $a0, 'Z', not_alpha
addi $a1, $a1, 'A'
jal caesar_helper
j done
not_alpha:
# not an alphabetical character
move $v0, $a0
j done
caesar_helper:
# subtract base from c
sub $t0, $a0, $a2 # $t0 = c - base
# add distance + 26 to c
addi $t0, $t0, 26 # $t0 += 26
add $t0, $t0, $a1 # $t0 += distance
# calculate c % 26
li $t1, 26 # $t1 = 26
div $t0, $t1 , $t1 # $lo = c % 26
mflo $t0
# add base to c
add $v0, $t0, $a2 # $v0 = c + base
done:
jr $ra
Not even ChatGBT could find an error.
The code should shift all chars of an string by a certain amount, but as soon as it is run the error above is thrown.

MIPS find end of string

I have the following mips code (running it in QTSPIM), that is supposed to count the number of characters in a string and print them.
The logic behind it is very simple but is does not work as it should. Everything goes well until it reaches the end of the string and then it continues counting even though I compare each element to $zero to find the end of string (\0).
Is there something wrong with my condition to exit the loop, or my_string does not contain \0 in the end so it won't exit?
.data
endl: .asciiz "\n"
my_string: .asciiz "thisisastring"
star: .asciiz "*"
str_end: .word 0
space: .asciiz " "
.text
.globl main
main:
la $a0, my_string
li $v0, 4
syscall
la $a0, endl
li $v0, 4
syscall
la $t0, my_string # load mystring to $t0
li $t1, 0 # make $t1 = 0, character counter
lb $t2, ($t0) # make $t2 point to the first character of "my_string"
li $t3, 1 # $t3 is the ++ register to go to the next character
li $t4, 0 # character counter
la $t5, str_end
cont:
beqz $t0, print # if \0 is found print and exit
addi $t4, $t4, 1 # increase the counter
lbu $a0, ($t0) # print current character
li $v0, 11
syscall
addi $t0, $t0, 1 # go to next char
#move $t2, $t0
j cont
print:
move $a0, $t4
li $v0, 1
syscall
j exit
exit:
li $v0, 10
syscall
The problem is in the order of the instructions, the logic of the code.
Here is the corrected version with no redundant code:
.data
endl: .asciiz "\n"
my_string: .asciiz "thisisastring"
str_end: .word 0
.text
.globl main
main:
la $a0, my_string
li $v0, 4 # print the string
syscall
la $a0, endl # print endl
li $v0, 4
syscall
la $t0, my_string # load mystring to $t0
li $t1, 0 # make $t1 = 0, character counter
lb $t2, 0($t0) # make $t2 point to the first character of "my_string"
la $t5, str_end
cont:
lb $a0, 0($t0) # print current character
beqz $a0, print # if \0 is found print and exit
addi $t1, $t1, 1 # increase the counter
addi $t0, $t0, 1 # go to next char
li $v0, 11
syscall
j cont
print:
move $a0, $t1
li $v0, 1
syscall
j exit
exit:
li $v0, 10
syscall

Multiplying and Combining Matrices

Hi i am trying to write a code in mips assembly. The purpose of the code is to multiply two matrices. the user will be asked to enter two matrix parameters, the program will combine them and then the user will be asked to enter values into the matrix. finally the program will print the matrix. eg: user enters 3 x 2, 2 x 7 the program combines them to get 3 x 7, the user enters 21 values and then the matrix is printed
.text
start:
li $v0 4
la $a0 msg1
syscall
li $v0 8
la $a0 matrix1
li $a1 3
syscall
li $v0 4
la $a0 msg2
syscall
li $v0 8
la $a0 matrix2
li $a1 3
syscall
li $s0 2
lb $t0, matrix1($s0)
lb $t1, matrix2($0)
beq $t1 $t0 values
li $v0 4
la $a0 msg5
syscall
exit: li $v0 10
syscall
values: lb $t0, matrix2($s0)
sb $t0 matrix1($s0)
lb $t1, matrix1($0)
li $v0 4
la $a0 msg3
syscall
mult $t0 $t1
mflo $s0
loop: beq $s1 $s0 print_matrix
li $v0 5
syscall
sb $v0 matrix_values($t2)
addi $s1 $s1 1
addi $t2 $t2 1
j loop
print_matrix: li $v0 4
la $a0 msg4
syscall
li $v0 4
la $a0 matrix1
syscall
print: beq $s2 $s0 exit
li $v0 1
lb $a0 matrix_values($t3)
syscall
li $v0 4
la $a0 spaces
syscall
addi $s2 $s2 1
addi $t4 $t4 1
bne $t0 $t4 print
li $v0 4
la $a0 newline
syscall
li $t4 0
j print
.data
msg1: .asciiz "please enter first matrix parameters"
msg2: .asciiz "please enter second matrix parameters"
msg3: .asciiz "please enter matrix values"
msg4: .asciiz "the result is"
msg5: .asciiz "error"
spaces: .asciiz " "
newline: .asciiz "\n"
matrix1: .space 3
matrix2: .space 3
matrix_values: .byte
.align 0

Subtracting two user input numbers, with a move and display the result

I'm new to MIPS and I'm using MARS. I can't get my move right and when I execute it gives me some nuts o number. Here is what I have so far, any help would be appreciated.
.data
message1: .asciiz "Enter the any number to subtract :"
message2: .asciiz "\nEnter the any number to subtract :"
n1 : .word 0
n2 : .word 0
message3: .asciiz "\nThe subtraction of the two numbers is "
.text
main:
li $v0 4 #print out message1
la $a0 message1
syscall
li $v0 5 #read message1 as number1
syscall
sw $v0 n1 #store number
li $v0 4 #print out message2
la $a0 message2
syscall
li $v0 5 #read message2 as number2
syscall
sw $v0 n2 #store number
li $v0 4
la $a0 message3
syscall
lw $t0 n1
lw $t1 n2
sub $t0, $v0, $v0 # t0 = number1 s1 - number2 s2
li $v0, 1 # print integer
move $t0, $a0 # move t0 to a0
syscall # run
Your code is doing well until here sub $t0, $v0, $v0.When you subtract you should put the the result in the argument register $a0then you can use move to put that result in return register $v0 for printing.
change them as following it will work.
sub $t2, $t0, $t1 # t2 = t0 - t1
move $a0, $t2 # copy t2 to a0
li $v0, 1 # print integer
syscall #
Another way of doing this subtraction is that you don't need .word and lwat all. As following
.data
message1: .asciiz "Enter the any number to subtract :"
message2: .asciiz "\nEnter the any number to subtract :"
message3: .asciiz "\nThe subtraction of the two numbers is "
.text
main:
li $v0 4 #print out message1
la $a0 message1
syscall
li $v0 5 #read message1 as number1
syscall
move $t0,$v0 # set $t0 to the content of $v0
li $v0 4 #print out message2
la $a0 message2
syscall
li $v0 5 #read message2 as number2
syscall
move $t1,$v0
li $v0 4
la $a0 message3
syscall
sub $a0, $t0, $t1 # t0 = number1 t1 = number2
li $v0, 1 # print integer
syscall # run

comparing a string in MIPS

Could someone tell me what is wrong with my code so far. I am trying to make a program that takes input from the user (roman numerals) and then converts it to integers. So far this is what I have:
.data
buffer: .space 20
onlyCaps: .asciiz "Please enter only Capital Numbers\n"
enter1: .asciiz "Number out of range. Please enter another number\n"
enter2: .asciiz "Please enter your roman numeral: "
debug: .asciiz "reach"
M: .asciiz "1000"
D: .asciiz "500"
C: .asciiz "100"
L: .asciiz "50"
X: .asciiz "10"
V: .asciiz "5"
I: .asciiz "1"
.text
main:
la $a0, onlyCaps # "Enter Only Capital numbers"
li $v0, 4
syscall
la $a0, enter2 #prompt user with "Please enter a roman numeral:"
li $v0, 4
syscall
la $a0, buffer #load byte space into address
li $a1, 3 # allot the byte space for string
li $v0, 8 #read user input, j
syscall
li $t4, 1
loop:
lb $t0, 0($a0) # Save $v0 value to $t0
beqz $t4, done # if it is equal to zero end the loop
add $a0, $a0, $t4 #increment the address
j mCheck
#while loop ends here
mCheck:
beq $t0, 'M', mChar # if $t0 equal M go to mChar
bne $t0, $t1, dCheck # move to see if equals D
dCheck:
beq $t0, 'D', dChar # if $t0 equal D go to dChar
bne $t0, $t1, cCheck
cCheck:
beq $t0, 'C', cChar # if $t0 equal C go to cChar
bne $t0, $t1, lCheck
lCheck:
beq $t0, 'L', lChar # if $t0 equal L go to lChar
bne $t0, $t1, xCheck
xCheck:
beq $t0, 'X', xChar # if $t0 equal X go to xChar
bne $t0, $t1, vCheck
vCheck:
beq $t0, 'V', vChar # if $t0 equal V go to vChar
bne $t0, $t1, iCheck
iCheck:
beq $t0, 'I', iChar # if $t0 equal I go to iChar
bne $t0, $t1, error
mChar:
la $a0, M #puts one hundred in $t3
li $v0, 4
syscall
j loop
dChar:
la $a0, D # prints out 500
li $v0, 4
syscall
j loop
cChar:
la $a0, C # prints out 500
li $v0, 4
syscall
j loop
lChar:
la $a0, L # prints out 500
li $v0, 4
syscall
j loop
xChar:
la $a0, X # prints out 500
li $v0, 4
syscall
j loop
vChar:
la $a0, V # prints out 500
li $v0, 4
syscall
j loop
iChar:
la $a0, I # prints out 500
li $v0, 4
syscall
j loop
error:
la $a0, enter1 # Print error meg. then back to main
li $v0, 4
syscall
j done
done:
li $v0, 10 # Exit
syscall
My question is how can you take input from the user and put it in $a0, and then use a while loop after that? I can't get the bytes to go to the next spot (i.e. MXX to go from M to X.) currently the program reads M, but not X. Eventually I will switch the program to sum these numbers and to also check to see if the number before is less (i.e. IV) to account for those roman numerals but I need help with the while loop first.

Resources