Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

2.37 [5] <§2.9> Write a program in MIPS assembly language to convert an ASCII nu

ID: 3664776 • Letter: 2

Question

2.37 [5] <§2.9> Write a program in MIPS assembly language to convert an ASCII number string containing positive and negative integer decimal strings, to an integer. Your program should expect register $a0 to hold the address of a nullterminated string containing some combination of the digits 0 through 9. Your program should compute the integer value equivalent to this string of digits, then place the number in register $v0. If a non-digit character appears anywhere in the string, your program should stop with the value 1 in register $v0. For example, if register $a0 points to a sequence of three bytes 50ten, 52ten, 0ten (the nullterminated string “24”), then when the program stops, register $v0 should contain the value 24ten.

Explanation / Answer

str2int:                                                                   # convert string to integer

            li              $t5, 0x2B                                              # $t5 = '+'

            li          $t6, 0x30                                 # $t6 = '0'

            li          $t7, 0x39                                 # $t7 = '9'

            li          $v0, 0                                      # initialize $v0 = 0

            add      $t0, $0, $a0                             # $t0 = pointer to string

            lbu       $t1, 0($t0)                                # load $t1 = sign character

            add      $t3, $t1, $0                              # $t3 = sign character

            addiu   $t0, $t0, 1                                # point to next char

            lbu       $t1, 0($t0)                                # load $t1 = digit character

LOOP:

            slt         $t2, $t1, $t6                             # char < '0'

            bne      $t2, $0, NoDigit

            slt         $t2, $t1, $t7                             # char > '9'

            beq      $t2, $0, NoDigit

            subu     $t1, $t1, $t6                             # convert char to integer

            mul      $v0, $v0, 10                            # multiply by 10

            add      $v0, $v0, $t1                           # $v0 = $v0 * 10 + digit

            addiu   $t0, $t0, 1                                # point to next char

            lbu       $t1, 0($t0)                                # load $t1 = next digit

            bne      $t1, $0, LOOP                         # branch if not end of string

            bne      $t3, $t5, negative                     # if $t3 != '+', go to negative

            jr          $ra                                           # return integer value

negative:

            sub       $v0, $0, $v0                            # -$v0

            jr          $ra                                           # return integer value

NoDigit:

            li          $v0, -1                                     # return -1 in $v0

            jr          $ra