2015-11-15 3 views
0

Я только начал изучать 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  

Заранее спасибо

+0

Привет, добро пожаловать в SO. Что вы пробовали? – Kenney

+0

@ Kenney Привет, Поэтому я не знаю, нужно ли мне писать: la $ a0, $ s2; li $ v0,4; syscall. будет ли это работать? –

+0

Очень близко, но похоже, что это напечатает строку; [см. здесь] (http://stackoverflow.com/questions/4580367/mips-output-syscall). – Kenney

ответ

1

Вы разместили свой exit процедуру в разделе .data, поэтому вы получаете сообщение о ошибке «недопустимое значение счетчика программы». Весь код должен находиться в разделе .text.

+0

Спасибо, я этого не заметил. Может быть, вы можете помочь мне с бесконечным циклом, в который я застрял? –

+0

Учитывая, что вы добираетесь до 'exit:', это не похоже на то, что ваш цикл бесконечен. – Michael

+0

Но я не добираюсь туда, после того, как вы изменили код, как вы предложили :( –

Смежные вопросы