arraysassemblymipspcspim

User Prompt is not appearing on Screen


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

Solution

  • 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.