MIPS Assembly - how to only accept integers between 1 and 15?

6.1k Views Asked by At

was wondering what instruction i would need to do to make sure the integers inputted from the user using PCspim would make sure only numbers 1-15 can be entered and if not display an error message?

i have read the instructions such as SLT but i dont quite understand the definition - ""If $s is less than $t, $d is set to one. It gets zero otherwise."" i dont want to print zero....

is there a way to efficiently do a greater than 1 but less than 15?

i would do

    beq $t0, 1, add_num      #if content in $t0 = 1, branch to add numbers
    beq $t0, 2, add_num
    beq $t0, 3, add_num
    beq $t0, 4, add_num
    beq $t0, 5, add_num  etc...right up to 15. but this is soo inefficient
2

There are 2 best solutions below

0
On

MIPS processors don't have traditional condition codes. Conditional tests instead set the contents of a register to 1 or 0, as you indicated. You can then test the result register using a beq against the zero register.

slt  $t5, $t3, $t4                set $t5 = 1 if $t3 < $t4
beq  $t5, $zero, done             branch if $t5 = 0

if here, $t3 < $t4
0
On

You don't need a beq for every possible value. You could use a range check, with something like the following (instruction may need slight modifications):

blez $t0, error_msg        ; disallow antyhing less than 1.

addi $t0, $t0, -15         ; subtract 15 from t0, now
                           ;   valid values are <= 0.

bgtz $t0, error_msg        ; disallow anything greater than 15.

addi $t0, $t0, 15          ; re-adjust

...                        ; add the numbers here