In: Computer Science
The following are to be completed using MIPS Assembly code.
A.
Using syscall #4, display a prompt for the user to enter a number. Then using syscall #5, get a decimal number from the user and save it to data memory (not into a register). After you have stored the number, display it back to the user as a 32-bit binary value.
B.
Prompt the user to enter a decimal number and save it to data memory. Using addition and/or bit shifting, take the number and multiply it by 5 (do not use the multiply instruction). Finally, output the result using syscall #1.
C.
Output the number from Part B as 8 hex digits.
The system calls mentioned in the question have the following uses as per the MIPS instruction set.
syscall #4 is the print_string service used to display a string/text to the user. The address of string to be printed must be in $a0
syscall #5 is the read_int service used to take integer as input and it is stored in $v0
syscall #1 is the print_int service used to print integer. The address of integer to be printed must be in $a0
The part corresponding to line of code is mentioned as code comment. The program below solves all three parts of the question.
.data
str1: .asciiz "Enter a number"
str2: .asciiz "Enter a decimal number"
.text
main:
la $a0, str1 #Load the string to be printed in $a0
li $v0, 4 #Load the syscall in $v0
syscall #Call service defined in $v0
li $v0, 5 #Load value for next syscall
syscall #Call the service to store integer in $v0
move $s0,$v0 #Move this value into $s0
li $a1,32 #Specify the number of bits
jal binary #The solution for part A ends here
la $a0, str2 #Load the string to be printed in $a0
li $v0, 4 #Load the syscall in $v0
syscall #Call service defined in $v0
li $v0, 5 #Load value for next syscall
syscall #Call the service to store integer in $v0
sw $v0,50($2) #Store the value in $v0 in memory location mem[$2+50] can be custom
move $s2,$v0 #Move number to multiply by 5
sll $t1,$s2,2 #Store s2*4 in t1. Two left shifts is multiplication by 4
add $t2,$t1,$s2 #Add $s2 and $t1 = $s2 + 4*$s2 = 5*$s2 and store in $t2
li $a0,$t2 #Store $t2 in $a0 for priniting
li $v0, 1 #Load value for next syscall
syscall #Call the service to store integer in $v0 to print
#The answer to part B ends here
move $s0,$t2 #Move the answer into s0 for printing in hex
li $a1,8 #Specify number of digits
jal hexa #This prints in hexa and answers part C
binary:
li $a2,1 # number of bits for base digit
j printfunc # $a0 and $a1 define output string and number of bits respectively
hexa:
li $a2,4 #number of bits for base digit
j printfunc # $a0 and $a1 have been defined in the main function
printfunc:
li $t7,1
sllv $t7,$t7,$a2
subu $t7,$t7,1
la $t6,obufe # printing till the end of buffer
subu $t6,$t6,1 # this is the last character
sb $zero,0($t6) # storing value
move $t5,$s0 # getting number
All functions must be added to the main program for a successful compilation.