在mips循环中保存一个值

时间:2015-11-15 20:51:24

标签: loops mips

我刚刚开始学习MIPS,作为我们作业的一部分,我试图保存在循环内运行的计数器值。

所以,由于迈克尔,我能够退出循环。但现在柜台不会增加,我注意到它不会输入incount标签。

  .text
.globl main 

main:
# get string from user
la $a0, str1              # load the addr of the given string into $a0. 
li $v0, 4         # 4 is the print_string syscall.
syscall               # do the syscall.

li $v0, 8                 # take in input
la $a0, buffer        # load byte space into addr
li $a1, 10        # allot the byte space for the string
move $t0, $a0             # save the string into $to
syscall

# get char from user
la $a0, char1              # load the addr of the given char into $a0. 
li $v0, 4         # 4 is the print_string syscall.
syscall               # do the syscall.

li $v0, 8                 # take in input
la $a0, buffer        # load byte space into addr
li $a1, 10        # allot the byte space for the char
move $t1, $a0             # save the char into $t1
syscall


addi $s2, $zero,0     # s2 holds the counter 




loop:
    lb $t3, ($t0) 
    beq $t3, $t1, incount      # go to incount if char was found
    beqz $t3, exit             # go to exit if we arrived to the end of the string
    addi $t0, $t0, 1           # incrase t3 by 1
    j loop             # go to loop 


incount:

    addi $s2, $s2, 1     # increase the counter by 1
    addi $t0, $t0, 1      # incrase char pos by 1
    j loop               # go back to loop


exit:      
    la $a0, ($s2)             # counter to be printed   
    li $v0, 1         # 1 is the print_int syscall.
    syscall 
    li $v0, 10                # return control to SPIM OS
    syscall

    .data
      buffer: .space 10
      str1:  .asciiz  "Please enter your string: "
      char1: .asciiz "Now, please enter your char of choice: "  



# end countchar.s     

提前致谢

1 个答案:

答案 0 :(得分:1)

您已将exit例程放在.data部分,这就是您收到“无效程序计数器值”错误消息的原因。所有代码都必须位于.text部分。