MIPS Help. Exception error/bad address?

  • Thread starter trouty323
  • Start date
  • Tags
    Mips
In summary, the conversation discusses a MIPS code that stores hard-coded data in an array, finds the largest value in the array, and places it at the end of the array. The code uses a loop and comparison to find the largest value and then stores it back into the array. The conversation also includes suggestions for improving the code, such as using labels for the array and automating the counting of array elements.
  • #1
trouty323
24
0
Hello. My task here is to store hard-code data directly into an array, find the largest in the array, then put that number at the end of the array. My code that I have is below, but I am getting an error. The error is:

"Exception occurred at PC=0x0040003c"
"Bad address in data/stack read = 0x2002003c"

Any ideas?

Code:
.data
.word 7
.word 39
.word 42
.word 16
.word 15
.word 21
.word 3
.word 2
.word 37
.word 11
.word 32
.word 28
.word 40
.word 27
.word 20

.text

.globl main

main:

lui $s0, 0x1001		# base address
add $t0, $t0, $zero	# index count
add $t1, $t1, $zero	# max element, $t1 = 0
addi $t5, $t5, 15	# number of elements
add $t2, $t2, $s0	# offset plus base

Loop:

beq $t0, $t5, Exit	# branch if counter is equal to number of elements in array
lw $t3, 0($t2)		# reading first element
addi $t0, $t0, 1	# increment counter by 1
addi $t2, $t2, 4	# increment offset by 4
slt $t4, $t1, $t3	# $t4 = 1 if $t1 is less than $t3, otherwise $t4 = 0
beq $t4, $zero, Loop	# branch to loop if value in $t4 is equal to value in $zero
add $t1, $t3, $zero	# 

j Loop

Exit:

sw $t1, 0($t2)		# store highest back into array

li $v0, 10 		# syscall to terminate
syscall
 
Technology news on Phys.org
  • #2
You're doing some things that seem questionable to me. I haven't programmed in MIPS, but I have a lot of experience in x86 assembly and some M68000 assembly.

1. lui $s0, 0x1001 # base address
Are you sure that's the right address?
2. add $t0, $t0, $zero # index count
You're adding 0 to $t0, and then storing the sum in $t0, which is likely to be whatever garbage value was already in $t0.
It might be better to do this:
andi $t0, $t0, 0
3. Same as above for $t1.

I have no idea what sort of development environment you are working in. Is there a debugger of some sort? I'm assuming that you're running some MIPS emulator on a PC. If you have a debugger, you could use it to single-step through your code, and see the actual values of the registers you're using.

If you don't have access to a debugger, you'll need to use the age-old technique of inserting print statements. For MIPS, this entails using syscall statements with the registers set up appropriately for what you're trying to print. For example, to print (display) an integer, do something like this:
Code:
  # display the integer value in int_value
  li $v0, 1
  la $a0, int_value
  syscall

I would do this in the loop, where you store a value from the array in $t3.
 
  • #3
I'm not seeing the error you reported. Your code seems to be working fine, but it's hard to follow from your comments.

I changed the form a little bit in the data block, and to make it run more quickly, I have fewer elements in the array.

Code:
.data	
   .word 7, 39, 42, 16, 0xffff

.text

.globl main

main:

lui $s0, 0x1001		# base address
add $t0, $t0, $zero	# Initialize the array index, $t0
add $t1, $t1, $zero	# Store max element in $t1
addi $t5, $t5, 4	# Store the number of elements in $t5
add $t2, $t2, $s0	# Store address of first element in $t2

Loop:

beq $t0, $t5, Exit	# If index == number of elements in array, branch to Exit
lw $t3, 0($t2)		# Read the first element
addi $t0, $t0, 1	# increment counter by 1
addi $t2, $t2, 4	# increment offset by 4
slt $t4, $t1, $t3	# $t4 = 1 if $t1 is less than $t3, otherwise $t4 = 0
beq $t4, $zero, Loop	# branch to loop if value in $t4 is equal to value in $zero
add $t1, $t3, $zero	# Copy $t3 to $t1

j Loop

Exit:

sw $t1, 0($t2)		# store highest back into array

li $v0, 10 		# syscall to terminate
syscall
 
  • #4
Yes, It did work after all. I think I was accidentally clicking run more than once, which was causing the error. It was a stupid mistake. Thanks for the tips!
 
  • #5
Sure, you're welcome. One other thing I did but didn't show in the code I posted was to make a label for the array. Doing this means I don't have to hard-code the address of the array, which isn't very good practice.

Code:
.data	
array:   .word 7, 39, 42, 16, 0xffff

.text
    la $s0, array
   .
   .
   .

One of the other changes I made, besides working with a smaller array, was to add a "don't care" value at the end, 0xFFFF. Using your code, that's the value that gets overwritten by the largest of the values in the array proper.

A nicer way of doing this might be like so:
Code:
.data	
array:   .word 7, 39, 42, 16
max:    .word  0xffff # any value here is fine - it will be overwritten

I'm sure it's also possible to automate the counting of the array elements, instead of counting them by hand and loading $t5 with an immediate value.

The basic idea is this:
1) Calculate max - array. This gives you the number of bytes between the start of the array and max.
2) Divide that value by 4 by shifting left by two bits. That will give you the number of words in the array itself.

For example, suppose that the array has 8 elements, and the array starts at 0x00400100, and that max is at 0x00400120. The difference is 0x20, or 0010 0000 in binary. A left shift by two bits results in 0000 1000 in binary, or 8, the number of elements in the array. This is a much safer way to do things. If you hard-code the number of elements, and you add elements to the array but forget to change the line that stores the hard-coded array size, it will screw things up. If you let the computer count them, it will always get the right result when you add elements to the array or remove them from it.
 

What is a "MIPS Help. Exception error/bad address?"

A "MIPS Help. Exception error/bad address" is an error message that appears when a computer program using the MIPS (Microprocessor without Interlocked Pipeline Stages) architecture encounters an instruction or data address that is invalid or inaccessible.

What causes a "MIPS Help. Exception error/bad address?"

There are several possible causes for a "MIPS Help. Exception error/bad address" including attempting to access a memory address that does not exist, trying to execute an invalid instruction, or accessing a memory address that is protected or reserved for the operating system.

How do I fix a "MIPS Help. Exception error/bad address?"

The solution to fixing a "MIPS Help. Exception error/bad address" depends on the specific cause of the error. This may involve debugging the program, checking for memory leaks or errors in the code, or modifying the program to use valid memory addresses and instructions.

Can a "MIPS Help. Exception error/bad address" be prevented?

It is possible to prevent a "MIPS Help. Exception error/bad address" by writing code that properly handles exceptions and errors, and by thoroughly testing the program for memory leaks and other potential issues before running it.

Is a "MIPS Help. Exception error/bad address" a serious problem?

A "MIPS Help. Exception error/bad address" can be a serious problem if it occurs frequently or if it prevents the program from executing properly. It may also indicate underlying issues with the code or memory management that need to be addressed for the program to run smoothly.

Similar threads

  • Programming and Computer Science
Replies
1
Views
2K
  • Programming and Computer Science
Replies
2
Views
8K
  • Programming and Computer Science
Replies
1
Views
8K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Programming and Computer Science
Replies
4
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
4K
  • Programming and Computer Science
Replies
1
Views
5K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
  • Programming and Computer Science
Replies
5
Views
9K
Back
Top