So I am debugging some code and keep getting a weird result. I am looping through a array and printing each element to the console. After I print each element I want to print a string that consist of only the “\n” , newline character. I run the code in QtSpim. When I do it print the new line AND the next string, str3 . I just want it to print str2 so I have the next element ready to print on a new line. Anyone run into a problem like this or see anything wrong with my code.
Problem is with second syscall in print_sorted_data function
.data
x: .word 1,2,3,4,5,6,7,8 #OR x: .space 32 #since x contains 8 words,we have to reserve 32 bytes.
min: .word 0
max: .word 0
mean: .word 0
str1: .ascii "\n The Min, Max and Mean of the array are : \n "
str2: .ascii " \n"
str3: .ascii "Enter Number: \n"
.text
#=======================================================================
main:
#=======================================================================
#Push $ra into stack
addi $sp, $sp, -4
sw $ra, 0($sp)
#-------------------------------------------------------------------
#***jal read_data
#***nop
#-------------------------------------------------------------------
la $a0, x
ori $a1, $0, 8
#-------------------------------------------------------------------
jal print_sorted_data
nop
#-------------------------------------------------------------------
jr $ra
nop
#=======================================================================
read_data:
#=======================================================================
#reading data from console , storing it in memory
#initialization for the counter of the loop
addi $t1, $0, 0 ## $t1<-stores counter for loop.
## don't need addi $t2, $0, 8 ## max value of loop
#lodad The base adderss of array
la $t0, x
check_cond1: ##############################
#check condition of the loop, if not met branch to read_done
slti $t3, $t1, 8
beq $t3, $0, read_done1
nop
#read an int from console and store it in &x[i]
ori $v0, $0, 4 ## print " enter number" to screen
la $a0, str3 ## changed lw to la
syscall
ori $v0, $0, 5
syscall
sw $v0, 0($t0)
#update both counter of the loop and pointer to the next element in the array
addi $t1, $t1, 1
addi $t0, $t0, 4
j check_cond1
nop
read_done1:
jr $ra
nop
#=======================================================================
#printing the sorted data
print_sorted_data:
#=======================================================================
#initialization for the counter of the loop
#lodad The base adderss of array
ori $t9, $a0, 0 ## We get base address in $a0
ori $t0, $0, 0 ##counter for loop
check_cond2: ##############################
#check condition of the loop, if not met branch to print_done1
slt $t1, $t0, $a1
beq $t1,$0, print_done1
#print x[i]
sll $t2,$t0 ,2 ## Multiply counter by 4 for offset
add $t2, $t2, $t9 ## Add to base address
ori $v0, $0, 1 ## set syscall up to print integer
lw $a0, 0($t2)
syscall ## not sure if this is right func name## it is
#go to next line by printing "\n"
ori $v0, $0, 4 ## set syscall up to print string
la $a0, str2
syscall
#update both counter of the loop and pointer to the next element in the array
##Dont think i have to update array. It does that in the loop.
add $t0, $t0, 1
j check_cond2
nop
print_done1: #######################
jr $ra
nop
**Upon further investigation .ascii should be .asciiz Anyone know the difference?
The difference is the trailing zero after the string given with
.asciiz. Basically its almost the same as.ascii, it only appends a 0 char, which serves as terminating char for the string.So if you use
.asciiand don’t manually specify a terminating 0, the string gets merged with the following data in memory – the\nis not a string termination (only a line termination).