0

So I'm converting a binary number that the user gives to its decimal equivalent. I finished the conversion without problems, but now I am trying to validate the input. If the user enters any characters besides 1 or 0, I want to branch to a label that will tell the user that their input is improper. This is what I have right now:

ValidateInput:
        lb $s1, userNumber($t8)
        beq $s1, 0, Calculate
        bgt $s1, '1', InvalidInput
        blt $s1, '0', InvalidInput
        addi $t8, $t8, 1
        j ValidateInput

I thought I could validate the input by using their ASCII table values, but it now thinks any input I try is improper. Even if I only enter 1s and 0s. Any help on this would be greatly appreciated.

  • I'd do `c -= '0'` then unsigned compare to check that `c < 2`, because that leaves you with the `0` or `1` integer value and only needing one branch to validate it. [double condition checking in assembly](https://stackoverflow.com/a/5197264). I'm not sure what you mean by "table" values; `'0'` and `'1'` already are the ASCII codes as integers. – Peter Cordes Mar 30 '21 at 05:03
  • What you have looks like it should work, so the problem perhaps lies elsewhere not shown. What do you see when you single step through some valid input? – Erik Eidt Mar 30 '21 at 09:19

1 Answers1

0

So, what I think is that your logic is correct. If the number is greater than 1 or it is less than 0 we must branch to InvalidInput function. But I think bgt and blt are not working properly with string characters.

You can try to change your input to int by following this:

Convert String of ASCII digits to int in MIPS/Assembler

And then use the same code