Casting in MIPS

江枫思渺然 提交于 2019-12-06 10:38:26

All characters may be represented as ascii when entered from keyboard to memory, but the read_int syscall will try to parse an integer from that ascii and it will fail for non-numeric characters.

The only alternative is to use strings. Create a buffer

buf: .byte 0,0,0      #10-99, last byte is for null

for the user input. After reading a string, check if the first character in the buffer is 'H'.

la $t1, buf
lb $t2, 0($t1)
li $t3, 72
beq $t2, $t3, help

If there's no 'H', proceed to parse an integer from the string.

i = buf.length-2;
j = 1;
k = 0;
while(i >= 0) {    //go in reverse
    if(buf[i] != NULL) {
        k += (buf[i] - 48) * j
        j *= 10;
    }
    i--;
}
if(k == target)print("success");

Don't forget to clear the buffer each time you prompt.

Maybe you can use read character (code 12) instead of read integer (code 5)

If you do read character, you'll need to convert from ascii to integer. So that means some manipulation will be required. But it can be done easier than the other way around I think.


blackcompe made a good point about read character.

I guess you'll have to do read string (code 8) and then validate the user input by looping and checking for digits or an H in ascii. And then returning an error message or something if the input was invalid.


Also here's some pseudo code to convert from ascii string to number. This assumes a valid string of digits. Granted you only have two digit numbers, so this may be unnecessary, but should work.

number = 0
for(i = 0; i < str.length; i++)
    number = 10 * number + ascii(str[i]) - ascii('0');

I agree that blackcompe's answer is much better. But I just want to show that the conversion loop actually works. And I want iterate that this is only to convert an ascii string of digits to a number. It doesn't check for H and assumes that the number can be any length long. Additionally, you still need to make adjustments for it work in MIPS.

Here is an example that shows that my conversion loop actually works. Here is the example rewritten in python, so it can be tested. Granted it's kind of pointless to do in python.

def pointlesslyConvertStringToNumber(str):
    number = 0
    for c in str:
        //Note: ord is a function to convert a character to ascii
        number = 10 * number + ord(c) - ord('0')
    print number 

Let's use the number 543:

pointlesslyConvertStringToNumber("543")

Here is the step-by-step of the loop:

  1. 10 * 0 + 53 - 48 = number = 5
  2. 10 * 5 + 52 - 48 = number = 54
  3. 10 * 54 + 51 - 48 = number = 543

And here is the MIPS subroutine based off of the above. I know this works, but I'm a MIPS beginner, so please excuse any dumb mistakes. Again this assumes a valid string of digits. In other words another subroutine would validate the input first.

$a0 is the address of the string buffer that is null-terminated. The comments on the side are a slightly modified python function to better fit the MIPS code. But it is not exactly one to one.

getNumber:                                  # def getNumber(input):
    addi  $sp, $sp, -8                      #
    sw    $ra, 4($sp)                       #
    sw    $a0, 0($sp)                       #
    add   $t0, $a0, $zero                   #
    add   $t1, $zero, $zero                 # i = 0
    addi  $t4, $zero, 10                    #
gnLoop:                                     # while(input[i] != None):
    lb    $t2, 0($t0)                       #
    beq   $t2, $zero, gnEnd                 # 
    mult  $t1, $t4                          # number = 10 * number
    mflo  $t1                               # 
    addi  $t3, $t2, -48                     # 
    add   $t1, $t1, $t3                     # number += ord(input[i]) - ord('0')
    addi  $t0, $t0, 1                       # i +=1
    j     gnLoop                            #
gnEnd:                                      # 
    lw    $a0, 0($sp)                       #
    lw    $ra, 4($sp)                       #
    addi  $sp, $sp, 8                       # 
    add   $v0, $zero, $t1                   # return number  
    jr    $ra                               #
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!