Question
Help with MIPS code: Calculate the area of a circle (expressed in square inches) for a radius that a user enters as an integer number
Help with MIPS code:
Calculate the area of a circle (expressed in square inches) for a radius that a user enters as an integer number (in mm). Your program should also ask the user to enter the value for PI as a double-precision floating point number. You can use a system call for this. If the user enters a value of 0 for the radius, your program should exit, otherwise, it should ask the user to enter another set of inputs. Because the program is very simple, pay special attention to user interface display prompts before accepting inputs and messages after getting the data and before displaying results. Show the units for all data entered and calculated. To practice peripheral programming and communication, for this assignment you are supposed to write your own myread_int code. Your program should let a user keep entering digits (0-9) until ENTER is pressed (hint: under Windows, ENTER sends two characters CR (0x10) followed by LF (0x0a); CR will be ignored in MARS. Under Linux, ENTER sends LF (0x0a) only). You can assume that the user will behave, i.e. you dont have to worry about what will happen if the user enters something other than digits. Of course, feel free to provide additional functionality.
Code:
# Calculate area of a circle (sqr-inch) # from user entered integer radius (mm) and PI float value (double-precision "3.14")
.data
conv: .double 25.4 #stored value for converson from "in" to "mm"
# User Prompt texts prompt0: .asciiz "Enter radius (mm): " prompt1: .asciiz " Enter PI value: " prompt2: .asciiz " The area of the circle is: " prompt3: .asciiz " square inches"
.text # main
#myread_int: # Get radius val # Print "Enter radius (mm): " to screen la $a0, prompt0 #load address of pompt into $a0 li $v0, 4 #specify address of prompt syscall #print heading, to prompt for input #get keyboard entry from keyboard simulator jal myread_int li $v0, 10 # system call for exit syscall # we are out of here.
# Get user input, the value for radius (integer) li $t3, 0 #initialize radius
myread_int: # while ascii ready bit not "0" loop through inputs ascii: li $s0, 0xFFFF0000 #look at control register lw $t0, 0($s0) #put contents into temp andi $t0, 1 #check that the ready bit is available (LSB) beqz $t0, ascii #loop again while ready bit is still "0"
# new input li $s1, 0xFFFF0004 lw $t2, 0($s1) #ascii code now in $t2 beq $t2, 10, PI #check if ascii for return key mul $t3, $t3, 10 #multiply value by 10 (slide left) sub $t2, $t2, 48 #subtract 48 from ascii to see what user entered
add $t3, $t3, $t2 #move value j myread_int #let user enter integers until return
# Get PI val PI: # Print "Enter PI value: " to screen la $a0, prompt1 #load address of pompt into $a0 li $v0, 4 #specify address of prompt syscall #print heading, to prompt for input # Get user input, the value for PI (set to float, double-pres) # li $v0, 7 #user input stored in register $v0 "double read" # syscall
#Calculate area #Area:
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started