0

I made a program in which I am using an array. The problem is the program is not displaying a prompt on the screen which it should. Before I used array in this program, the prompt was working properly. Here is the code:

.data   

User: .asciiz "\nEnter 10 number:\n"    
Reverse: .asciiz "\nThe reverse order array is:\n"    
array: .space 10

.text

main:    
  la $a0,User    
  li $v0,4    
  syscall    
  li $t1,0    
  li $t3,0    # counter    
  la $t0,array    
  j Input    

Input:    
  li $v0,5    
  syscall    
  add $t2,$t3,$t0    
  sw $v0,0($t2)    
  addi $t3,$t3,4         
  beq $t1,9,ReverseInitialization    
  addi $t1,$t1,1    
  j Input

ReverseInitialization:      
  li $t3,36        
  la $a0,Reverse      # NOT DISPLAYING THIS PROMTE    
  li $v0,4    
  syscall    
  j ReverseDisplay

ReverseDisplay:


lw $a0,0($t2)

li $v0,1
syscall

beq $t1,0,Exit


j ReverseDisplay
Naruto
  • 1,710
  • 7
  • 28
  • 39
  • are you sure it is not displaying the string. Have you entered the 10 numbers ? – gusbro Oct 11 '12 at 13:44
  • sure. it is not displaying it. more over when i tried to print spaces by creating a data label like this, it isn't displaying that too. – Naruto Oct 11 '12 at 13:58
  • I was asking because y ran your program in MARS and it worked fine (after I removed the last jump to ReverseDisplay) – gusbro Oct 11 '12 at 14:29
  • ReverseDisplay is another jump which displays an array in backward order. This jump is valid. Why is this program not displaying the promt when I jump to reverseDisplay – Naruto Oct 11 '12 at 14:49
  • I removed the jump because you didn't provide the code for that label. The "promt" is displayed fine (at least in MARS) when you jump to ReverseInitialization. – gusbro Oct 11 '12 at 14:51
  • I have added reverseDisplay code too. Now can you figure out the real problem – Naruto Oct 11 '12 at 14:54
  • You have to align the array. That is, put a .align 2 in the line before your array label – gusbro Oct 11 '12 at 15:06

1 Answers1

0

You're mixing up your datum size again, like you did in a previous question.

You have an array of numbers. There are 10 numbers, according to the first prompt. You're using syscall 5 to read each number. syscall 5 reads an integer into $v0, and as such, the maximum size of the integer is the maximum size of the $v0 register. The size of the register is four bytes, not one. Multiply that by 10, and suddenly your array uses 40 bytes, not 10. So change your array to have a size of 40 instead of 10.

Another main problem is the alignment, pointed out by gusbro in the comments. You're using lw and sw to access the numbers in the array. These instructions operate on words, not bytes. A word is four bytes. Your address needs to therefore be aligned to four bytes. When I run your code as-is, sure enough I get an alignment exception:

Runtime exception at 0x00400030: store address not aligned on word boundary 0x10010031

The address 0x10010031 is not aligned to four bytes because it is not divisible by 4.

As gusbro advised, you need to use the .align directive before your array to force the assembler to store it at a properly aligned address. You actually need an alignment of 4, not 2 I was wrong; align n aligns to 2^n, not n bytes:

.align 2
array: .space 40

Now it works fine.

Jeff
  • 7,504
  • 3
  • 25
  • 34
  • thanks Jeff E.You pointed out literally very crucial points and bugs in this program. The program was working fine without .align on my QTspim and our teacher which is kind of iliterate in this subject, didn't introduced us to align at all. In future I will keep these things in mind – Naruto Oct 12 '12 at 11:09