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

Please enter a number:4 Please enter a number:2 Please enter a number:1 The sum

ID: 3565074 • Letter: P

Question

Please enter a number:4 Please enter a number:2 Please enter a number:1 The sum of 4 , 2 ,and 1 is 7. The difference of 4 , 2 ,and 1 is 1. The product of 4 , 2 ,and 1 is 8. The quotient of 4 ,and 2 is 2 with remainder 0. -- program is finished running --

Explanation / Answer

# MIPS basic calculator .data #Identify data section welcome: #Welcome text .asciiz " Operations Supported and Syntax: Addition: 5+6 Subtraction: 5-6 Multiplication: 5*6 Division: 5/6 Power: 5^6 Factorial: 5! Permutation: 7P6 Combination: 7C6 Only Integer arguments are allowed. There are to be no spaces anywhere in the input string." inputprompt: .asciiz " >>" #Input Prompt quotient: .asciiz "Quotient: " remainder: .asciiz " Remainder: " outputprompt: #Output Prompt .asciiz "Your Integer = " endtext: .asciiz " Program Finished." string: .space 100 arg0: .space 100 arg1: .space 100 .text #Identify text section. i.e. Instructions main: la $a0,welcome #PI# Load address of welcome string into a0 addi $v0,$0, 4 #Set syscall operation to 4 for print_string syscall INPUT: la $a0,inputprompt #PI# Load address of inputprompt string into a0 addi $v0,$0, 4 #Set syscall operation to 4 for print_string syscall la $a0, string #PI# Load address of Buffer into a0 for read_string addi $a1,$0, 100 #Set syscall length to 100 for read_string addi $v0,$0, 8 #Set syscall operation to 8 for read_string syscall #Execute syscall la $a0, string #PI# Load address of string into a0 jal CLEAN_STRING #Execute CLEAN_STRING procedure, which strips off ASCII value 10 or newline la $a0, string #PI# Load address of string into a0 jal CHECK_FOR_SENTINEL #checks whether 'exit' has been typed into the prompt jal PARSE_STRING #Parse String. The opcode will be returned in v0 and #strings arg0 and arg1 will be filled with the arguments in string format move $s0, $v0 #copy opcode to s0 la $a0, arg0 #load string arg0 into a0 li $t0,0 #Clear Temporary Vars li $t1,0 li $t2,0 li $t3,0 li $t4,0 li $t5,0 li $t6,0 li $t7,0 li $t8,0 li $t9,0 jal ASCII2INT #Execute ASCII2INT procedure, which converts the ASCII number into and INT move $s1, $v0 #s1 = v0 = output from ASCII2INT on arg0 la $a0, arg1 #load string arg1 into a0 li $t0,0 #Clear Temporary Vars li $t1,0 li $t2,0 li $t3,0 li $t4,0 li $t5,0 li $t6,0 li $t7,0 li $t8,0 li $t9,0 jal ASCII2INT #Execute ASCII2INT procedure, which converts the ASCII number into and INT move $s2, $v0 #s1 = v0 = output from ASCII2INT on arg1 move $a0, $s1 #load (int)arg0 into a0 move $a1, $s2 #load (int)arg1 into a1 addi $t2, $0, 0 #t2 = 0 = + addi $t3, $0, 1 #t3 = 1 = - addi $t4, $0, 2 #t4 = 2 = * addi $t5, $0, 3 #t5 = 3 = / addi $t6, $0, 4 #t6 = 4 = ^ addi $t7, $0, 5 #t7 = 5 = ! addi $t8, $0, 6 #t8 = 6 = P addi $t9, $0, 7 #t9 = 7 = C beq $s0, $t2, launch_add #IF current byte == 43 (+) break loop and go to launch_add beq $s0, $t3, launch_sub #IF current byte == 45 (-) break loop and go to launch_sub beq $s0, $t4, launch_mult #IF current byte == 42 (*) break loop and go to launch_mult beq $s0, $t5, launch_div #IF current byte == 47 (/) break loop and go to launch_div beq $s0, $t6, launch_exp #IF current byte == 94 (^) break loop and go to launch_exp beq $s0, $t7, launch_fact #IF current byte == 33 (!) break loop and go to launch_fact beq $s0, $t8, launch_perm #IF current byte == 80 (P) break loop and go to launch_perm beq $s0, $t9, launch_comb #IF current byte == 67 (C) break loop and go to launch_comb launch_add: jal ADDITION #call ADDITION function move $s0, $v0 addi $a0, $s0, 0 #load returned int into a0 addi $v0,$0, 1 #Set syscall operation to 1 for print_int syscall #Execute syscall li $v1, 0 #clear V1 li $v0, 0 #clear V0 mtlo $0 #clear lo mthi $0 #clear hi la $a0, arg0 #clear data memory la $a1, arg1 addi $t0, $0, 99 memclean1: sb $0, 0($a0) sb $0, 0($a1) addi $a0, $a0, 1 addi $a1, $a1, 1 addi $t0, $t0, -1 bne $t0,$0, memclean1 j INPUT #jump to input launch_sub: jal SUBTRACTION #call SUBTRACTION function move $s0, $v0 addi $a0, $s0, 0 #load returned int into a0 addi $v0,$0, 1 #Set syscall operation to 1 for print_int syscall #Execute syscall li $v1, 0 li $v0, 0 mtlo $0 mthi $0 la $a0, arg0 la $a1, arg1 addi $t0, $0, 99 memclean2: sb $0, 0($a0) sb $0, 0($a1) addi $a0, $a0, 1 addi $a1, $a1, 1 addi $t0, $t0, -1 bne $t0,$0, memclean2 j INPUT #jump to input launch_mult: jal MULTIPLICATION #call MULTIPLICATION function move $s0, $v0 addi $a0, $s0, 0 #load returned int into a0 addi $v0,$0, 1 #Set syscall operation to 1 for print_int syscall #Execute syscall li $v1, 0 li $v0, 0 mtlo $0 mthi $0 la $a0, arg0 la $a1, arg1 addi $t0, $0, 99 memclean3: sb $0, 0($a0) sb $0, 0($a1) addi $a0, $a0, 1 addi $a1, $a1, 1 addi $t0, $t0, -1 bne $t0,$0, memclean3 j INPUT #jump to input launch_div: jal DIVISION #call DIVISION function move $s0, $v0 move $s1, $v1 la $a0,quotient #Load address of endtext string into a0 addi $v0,$0, 4 #Set syscall operation to 4 for print_string syscall #Execute syscall addi $a0, $s0, 0 #load returned int into a0 addi $v0,$0, 1 #Set syscall operation to 1 for print_int syscall #Execute syscall la $a0,remainder #Load address of endtext string into a0 addi $v0,$0, 4 #Set syscall operation to 4 for print_string syscall #Execute syscall addi $a0, $s1, 0 #load returned int into a0 addi $v0,$0, 1 #Set syscall operation to 1 for print_int syscall li $v1, 0 li $v0, 0 mtlo $0 mthi $0 la $a0, arg0 la $a1, arg1 addi $t0, $0, 99 memclean4: sb $0, 0($a0) sb $0, 0($a1) addi $a0, $a0, 1 addi $a1, $a1, 1 addi $t0, $t0, -1 bne $t0,$0, memclean4 j INPUT #jump to input launch_exp: jal POWER #call POWER function move $s0, $v0 addi $a0, $s0, 0 #load returned int into a0 addi $v0,$0, 1 #Set syscall operation to 1 for print_int syscall #Execute syscall li $v1, 0 li $v0, 0 mtlo $0 mthi $0 la $a0, arg0 la $a1, arg1 addi $t0, $0, 99 memclean5: sb $0, 0($a0) sb $0, 0($a1) addi $a0, $a0, 1 addi $a1, $a1, 1 addi $t0, $t0, -1 bne $t0,$0, memclean5 j INPUT #jump to input launch_fact: jal FACTORIAL #call FACTORIAL function move $s0, $v0 addi $a0, $s0, 0 #load returned int into a0 addi $v0,$0, 1 #Set syscall operation to 1 for print_int syscall #Execute syscall li $v1, 0 li $v0, 0 mtlo $0 mthi $0 la $a0, arg0 la $a1, arg1 addi $t0, $0, 99 memclean6: sb $0, 0($a0) sb $0, 0($a1) addi $a0, $a0, 1 addi $a1, $a1, 1 addi $t0, $t0, -1 bne $t0,$0, memclean6 j INPUT #jump to input launch_perm: jal PERMUTATION #call PERMUTATION function move $s0, $v0 addi $a0, $s0, 0 #load returned int into a0 addi $v0,$0, 1 #Set syscall operation to 1 for print_int syscall #Execute syscall li $v1, 0 li $v0, 0 mtlo $0 mthi $0 la $a0, arg0 la $a1, arg1 addi $t0, $0, 99 memclean7: sb $0, 0($a0) sb $0, 0($a1) addi $a0, $a0, 1 addi $a1, $a1, 1 addi $t0, $t0, -1 bne $t0,$0, memclean7 j INPUT #jump to input launch_comb: jal COMBINATION #call COMBINATION function move $s0, $v0 addi $a0, $s0, 0 #load returned int into a0 addi $v0,$0, 1 #Set syscall operation to 1 for print_int syscall #Execute syscall li $v1, 0 li $v0, 0 mtlo $0 mthi $0 la $a0, arg0 la $a1, arg1 addi $t0, $0, 99 memclean8: sb $0, 0($a0) sb $0, 0($a1) addi $a0, $a0, 1 addi $a1, $a1, 1 addi $t0, $t0, -1 bne $t0,$0, memclean8 j INPUT #jump to input #END MAIN ASCII2INT: #ASCII2INT procedure code addi $t2, $a0, 0 #t2 is current mem address, will be incremented and decremented addi $v1, $0, 9 #v1 = 9, used to test if a character is indeed a number. #I used v1 because I ran out of registers. while1: lb $t0, 0($t2) #Load the first character into t0. #Here first means the first character typed in console. addi $t1, $t0, -48 #t1 = t0 - 48. ASCII val - 48 = decimal val beq $t0, $0, endwhile1 #If ASCII val == 0, exit this while slt $t4, $t1, $0 #set t4 == 1 IF t1 0 bne $0, $a3, doit # if n > 0 goto generic case li $v0, 1 # base case, 0! = 1 jr $ra # return to calling procedure doit: # Generic case (n > 0) addi $sp, $sp, -8 # make room for $s0 and $ra sw $s0, 0($sp) # store previous argument sw $ra, 4($sp) # store return address add $s0, $0, $a0 # save current argument addi $a0, $a0, -1 # factorial(n-1) jal FACTORIAL # v0 = (n-1)! mul $v0,$s0,$v0 # n*(n-1)! lw $s0, 0($sp) # restore previous argument lw $ra,4($sp) # restore $ra add $sp,8 # reduce stack size jr $ra # return to calling procedure PERMUTATION: # this is the permutation function addi $sp, $sp, -16 # make room for $a0, $a1, $ra and the first $v0 from factorial sw $a0, 0($sp) # store previous argument sw $a1, 4($sp) # store previous argument sw $ra, 8($sp) # store return address jal FACTORIAL # this jumps to the factorial function label sw $v0, 12($sp) # this stores the value obtained from factorial on the stack lw $a0, 0($sp) # this loads the original $a0 off the stack lw $a1, 4($sp) # this loads the original $a1 off the stack subu $a0, $a0, $a1 # this subtracts $a1 from $a0 and stores the result in $a0 jal FACTORIAL # this jumps to the factorial function label lw $a0, 12($sp) # this loads the result from the first factorial computation from the stack into $a0 add $a1, $0, $v0 # this loads the second factorial output into $a1 divu $v0, $a0, $a1 # this divides a0/a1 lw $ra, 8($sp) # this loads the original $ra off the stack addi $sp, $sp, 16 # this resets the stack pointer back to its original position jr $ra # this jumps back to the program calling this function COMBINATION: # this designates the combination function addi $sp, $sp, -12 # this makes room on the stack for $ra and $v0 from permutation sw $ra, 0($sp) # this stores the original $ra on the stack sw $a1, 4($sp) # this stores the original value of $a1 on the stack jal PERMUTATION # this jumps to the permutation function label sw $v0, 8($sp) # the output from permutation is placed on the stack lw $a0, 4($sp) # this places the original value of $a1 from the stack into $a0 jal FACTORIAL # this jumps to the factorial function label lw $a0, 8($sp) # this loads the output of permutation, stored on the stack, into $a0 add $a1, $0, $v0 # this places the output of factorial into $a1 divu $v0, $a0, $a1 # this divides a0/a1 lw $ra, 0($sp) # this restores the original value of $ra from the stack addi $sp, $sp, 12 # this resets the stack pointer to its original position jr $ra # this jumps back to the program originally calling this function done: #END OF PROGRAM. NO MORE CODE. IM SO TIRED.