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

1. Use nano to edit the provided program3.m: nano program3.m Fill in the body of

ID: 3674416 • Letter: 1

Question

1. Use nano to edit the provided program3.m: nano program3.m Fill in the body of the program to find the ASCII encodings of 5 digits of the decimal representation of the number in register n_r. o Read from but do not write to the n_r register; you may use q_r and r_r for intermediate values - Store digits 0 through 4 in registers %05 through %01 - Note that the order is reversed * Store the ASCII character code for each digit For extra credit * o o o Use spaces for leading zeros Use delay slot optimizations Include a comment which identifies the sequence of numbers generated by the provided code Do not modify any of the provided code If you define your own macros for intermediate results, put the definitions at the top of the body -

Explanation / Answer

Put this code in the Loop and it works as follows.

Scan(&X:$t6, N:$t2, U:$t3, L:$t4, D:$t5)

                $t6 = Mem(sp)                  # &X

                $t2 = Mem(sp+4)                             #   N

                $t3=$t4=$t5=0;

  Label       Op-Code    Dest. S1, S2   Comments

scan:                     

                lw           $t6, 0($sp)          # Get &X

                lw           $t2, 4($sp)          # Get Value N

                li             $t3, 0                    # Count of Upper Case

                li             $t4, 0                    # Count of Lower Case

                li             $t5, 0                    # Count of Decimal Digits

                blez       $t2, done

                li             $t0, 48                  # ASCII “0”

                li             $t9, 57                   # ASCII “9”

                li             $t7, 97                   # ASCII “a”

                li             $t8, 122                                # ASCII “z”

                addiu    $sp, $sp, -8         # Allocate Temp Space

                sw          $s6, 0($sp)          # Save s6

                sw          $s7, 4($sp)          # Save s7

                li             $s6, 65                  # ASCII “A”

                li             $s7, 90                  # ASCII “Z”

loop:     lbu         $t1, 0($t6)         

                addi       $t6, $t6, 1          

                blt          $t1, $s6, num    # “A”

                bgt         $t1, $s7, lowc    # “Z”

                addi       $t3, $t3, 1

                b             check

lowc:

                blt          $t1, $t7,   check                # “a”

                bgt         $t1, $t8, check # “z”

                addi       $t4, $t4, 1

                b             check

num:

                blt          $t1, $t0, check # “0”

                bgt         $t1, $t9, check # “9”

                addi       $t5, $t5, 1

check:

                addi       $t2, $t2, -1

                bgtz       $t2, loop

lw           $s6, 0($sp)          # Restore s6

                lw           $s7, 4($sp)          # Restore s7

                addiu    $sp, $sp, -8         # Deallocate Temp Space

                sw          $t3, 8($sp)

                sw          $t4, 12($sp)

                sw          $t5, 16($sp)

                jr             $ra

                For ( ; $t2> 0; $t2=$t2-1)

                {

                $t1 = mem($t6)                 # get a byte

                $t6 = $t6 + 1

                if ( $t1 >= 65 && $t1 <= 90 )     $t3 = $t3+1;

                else if ( $t1 >= 97 && $t1 <= 122) $t4=$t4+1;

                else if ( $t1 >= 48 && $t1 <= 57 ) $t5=$t5+1;

                }

                Mem(sp + 8 ) = $t3;

                Mem(sp + 12 ) = $t4;

                Mem(sp + 16 ) = $t5;

                return;