代码正在读取指定的字符串

时间:2012-03-08 04:33:37

标签: assembly mips

所以我正在调试一些代码并继续得到一个奇怪的结果。我循环遍历一个数组并将每个元素打印到控制台。打印每个元素后,我想打印一个只包含“\ n”换行符的字符串。我在QtSpim中运行代码。当我这样做时,打印新行和下一个字符串str3。我只是想让它打印str2所以我准备好在新行上打印下一个元素。任何人遇到这样的问题或看到我的代码有任何问题。

问题在于print_sorted_data函数中的第二个系统调用

.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 

**经过进一步调查.ascii应该是.asciiz任何人都知道差异吗?

1 个答案:

答案 0 :(得分:1)

差异是用.asciiz给出的字符串后的尾随零。基本上它与.ascii几乎相同,它只附加一个0字符,用作字符串的终止字符。

因此,如果您使用.ascii并且不手动指定终止0,则字符串将与内存中的以下数据合并 - \n不是字符串终止(仅限行终止)