0

I've just started to learn MIPS, and as part of our assignment, I'm trying to save a counter value that runs inside a loop.

So, I was able to exit the loop, thanks to Michael. But now the counter will not increase, I noticed that it won't enter the incount label.

  .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     

Thanks in advance

dor-b
  • 75
  • 1
  • 9
  • Hi, welcome to SO. What have you tried? – Kenney Nov 15 '15 at 20:54
  • @Kenney Hi, So I don't know if I need to write : la $a0, $s2 ; li $v0,4 ; syscall . will this work? – dor-b Nov 15 '15 at 21:15
  • Very close, but it looks like that would print a string; [see here](http://stackoverflow.com/questions/4580367/mips-output-syscall). – Kenney Nov 15 '15 at 21:25
  • Thank you for the tip, I've changed that part. Now i get this error: "Error in : invalid program counter value: 0x1001004c" – dor-b Nov 15 '15 at 21:31
  • I don't have the tools here, but, if I paste the code you pasted into this [online MIPS assembler](http://alanhogan.com/asu/assembler.php) then the address of the last instruction is `...1c`. Is there any way for you to know which source line is on that address? – Kenney Nov 15 '15 at 21:50
  • I i look at the text segment , in the address column the instruction that end with ..1c is the command : li $a1,10 – dor-b Nov 15 '15 at 21:57
  • Do you have a debugger or simulator that allows you to step through the program? – Kenney Nov 15 '15 at 22:00
  • Yes, I use mars 4.5. But our university didn't bother to teach us about the simulator – dor-b Nov 15 '15 at 22:10
  • The code you've posted is not sufficient to tell what the problem is. Based on your comments it sounds like you've placed some code in the `.data` section and then try to execute it. – Michael Nov 16 '15 at 07:07
  • Hi, I've added the full program. I'm trying to count the number of given char in a given string. – dor-b Nov 16 '15 at 07:25
  • @Kenney Looks like you found my online MIPS assembler — cheers – Alan H. Sep 06 '16 at 14:59

1 Answers1

1

You've placed your exit routine in the .data section, which is why you're getting the "invalid program counter value" error message. All code needs to be in the .text section.

Michael
  • 57,169
  • 9
  • 80
  • 125
  • Thank you, I have not noticed that. Maybe you can help me with the infinite loop that I'm stuck in? – dor-b Nov 16 '15 at 08:37
  • Considering that you get to `exit:`, it doesn't sound like your loop is infinite. – Michael Nov 16 '15 at 12:51
  • But I don't get there, after rearranging the code as you suggested:( – dor-b Nov 16 '15 at 12:57
  • I have moved the 'lb $t3, ($t0) ' to the loop, but now every irritation the value of $t3 revert. – dor-b Nov 16 '15 at 13:04
  • You change the code in your latest edit so that you're incrementing `$t0` instead of `$t3` inside the loop. Not sure why you did that. – Michael Nov 16 '15 at 13:18
  • Because that what caused the infinite loop, with $t0 instead of $t3 i was able to exit the loop. Don't know why it's working to be honest. – dor-b Nov 16 '15 at 13:21
  • Ok, so you're saying that the loop finishes now? In that case I'm not sure what you mean by the value of `$t3` _"reverting"_. – Michael Nov 16 '15 at 13:25
  • the value of $t3 starts with a value of '...72' and after one iteration goes to '...73' and after another iteration back to '....72', and so on. I really think messed up this code. The loops end with $t0 instead of $t3, but it won't enter the incount label. – dor-b Nov 16 '15 at 13:31
  • Your condition for going to `incount` is that `$t3` equals `$t1`. Does `$t1` contain a value that you'd expect to find in your string? (use the register viewer in SPIM/MARS). – Michael Nov 16 '15 at 13:35
  • I have loaded the given char from the user into $t1, it's valued on the mars register viewer is: 0x10010000 . I have no idea what it's stand for :( – dor-b Nov 16 '15 at 13:39
  • 0x10010000 is not a valid char - it's the address of the beginning of the `.data` section, so I suggest that you go over the code where you set `$t1` and make sure that what you're doing there is correct. – Michael Nov 16 '15 at 13:45
  • I really don't know if I saved the char in the right way, if I add the command: 'lb $t1, char1 ' before the loop, I get a value of 0x...20 in $t1. But there is still a problem getting the char from the string. – dor-b Nov 16 '15 at 14:17