3

I'm using a MIPS simulator.

Occurs that when I'm trying to open the text file that contains my code in the simulator

I am having trouble understanding why this will not run on QTSpim. I get the error

spim: (parser) syntax error line 8

Code:

#This program read the given array value one by one
#then compare and find largest
#Display largest and it's count

#Data declaration
.data 
a: .word 5,2,15,3,7,15,8,9,5,2,15,3,7                             #Initialize an array
space: .asciiz " "                                                #Get space
nextLine: .asciiz "\n"                                            #for \n
big: .asciiz "bigger....\n"                                       #Display bigger   
small: .asciiz "smaller....\n"                                    #Display smaller
equal: .asciiz "same....\n"                                       #Display same
#Output display strings
Largest: .asciiz "The largest number is "
LargestCount: .asciiz "The largest number is included "
times: .asciiz " times\n"
#Main program
.text
.globl main
main:
la $s0,a                                              #Get the address of the array
addi $s1,$0,13                                        #size of the array
addi $s2,$0,0                                        #for largest number
addi $s3,$0,0                                        #For largest count
addi $t0,$0,0                                        #i
addi $t1,$0,0                                        #j

#Loop for array data print
Loop:
beq $t0,$s1,nextLoop                               #check the counter reach array size
    lw $a0,0($s0)                                      #Get value from array to print
addi $v0,$0,1                                      #Integer print system caa
syscall                                            #Print integer value in a0
la $a0,space                                       #get the address of space string
addi $v0,$0,4                                      #System call to print string
syscall                                            #Print space
addi $t0,$t0,1                                     #Increment counter
addi $s0,$s0,4                                    #to get next data contain address
j Loop                                           #repeat loop

#Find larget and it's count
nextLoop:
la $a0,nextLine                                  #get the address of \n print
addi $v0,$0,4                                    #String print system call
syscall                                          #print string
addi $t0,$0,0                                      #for loop counter
la $s0,a                                          #Get the address of the array
#Loop through array value
forLoop:
beq $t0,$s1,print                                  #check the counter reach array size  
move $a1,$s2                                       #For compare method argument
lw $a0,0($s0)                                      #Get value from array
jal compare                                       #call compare function
addi $t1,$v0,0                                   #j=compare(largest,a[i])
beq $t1,0,biggest                               #If the compare result 0 means value bigger
beq $t1,1,same                                   #If the compare result 1 means same value
beq $t1,2,smallest                              #If the compare result 2 means value smaller

#Bigger case
biggest:
la $a0,big
addi $v0,$0,4
syscall
lw $s2,($s0)
addi $s3,$0,1
addi $t0,$t0,1
addi $s0,$s0,4
j forLoop
#Same case
same:
la $a0,equal
addi $v0,$0,4
syscall
addi $s3,$s3,1
addi $t0,$t0,1
addi $s0,$s0,4
j forLoop
smallest:
#Smaller case
la $a0,small
addi $v0,$0,4
syscall
addi $t0,$t0,1
addi $s0,$s0,4
j forLoop

#Print result
print:
la $a0,Largest                       #Largest string display string address
addi $v0,$0,4                       #System call to print string
syscall                             #print
move $a0,$s2                       #To pri t largest number move into a0
addi $v0,$0,1                     #System call to print integer
syscall                           #Integer print
#Print \n
la $a0,nextLine
addi $v0,$0,4
syscall
#Print largest count atring
la $a0,LargestCount
addi $v0,$0,4
syscall
#Print count
move $a0,$s3
addi $v0,$0,1
syscall
#Times string print
la $a0,times
addi $v0,$0,4
syscall
#End of the program
exit:
addi $v0,$0,10                                     #Terminate the program normally system call
syscall                                             #End the program  
#Compare method
compare:
move $t3,$ra
jal subt                                           #Call subtract function
bgt $v0,0,return2                                 #If sub value greaterthan 0 return 2
beq $v0,0,return1                                 #If sub value = 0 return 1
addi $v0,$0,0                                     #Other wise return 0 as result
move $ra,$t3
jr $ra                                            #Return to main

#Return value 2 as result of the function call
return2:
addi $v0,$0,2
move $ra,$t3
jr $ra
#Return value 1 as result of the function call
return1:
addi $v0,$0,1
move $ra,$t3
jr $ra
#Subtract function
subt:
sub $v0,$a1,$a0
jr $ra

This runs on MARS and I'm not sure why it wont run on QTSpim as well.

halfer
  • 19,824
  • 17
  • 99
  • 186
bobbyjon
  • 31
  • 1
  • 2
  • 1
    Well, which is line 8? Presumably `spim` does not like one of the directives ... maybe `.asciiz`? Try with `.string` or `.asciz` (one `i`). Or maybe it doesn't like `\n`. Try changing stuff until you find out what the cause is. It works for me using `xspim`. I don't have `qtspim` but I expect it uses the same engine. – Jester Feb 26 '20 at 00:48
  • Try putting spaces after commas. – Erik Eidt Feb 26 '20 at 00:55
  • This isn't a [mcve]. If you have a parse error on only one line, you don't need any of the other lines except maybe `.data` to demonstrate the error. – Peter Cordes Feb 26 '20 at 01:03

1 Answers1

1
a: .word 5,2,15,3,7,15,8,9,5,2,15,3,7

replace , with space . mips will not take ,

Try this:

a: .word 5 2 15 3 7 15 8 9 5 2 15 3 7 
Esocoder
  • 1,032
  • 1
  • 19
  • 40
  • 1
    Some MIPS assemblers accept commas. I think you mean SPIM's assembler specifically. (Also, yuck. Forcing you to use space-separated elements seems like poor design.) – Peter Cordes Mar 01 '20 at 00:23