In: Computer Science
1. Write an assembly language program that prompts the user for and reads four integers (x1, y1, x2, y2) which represent the coordinates of two points. Make sure you keep track of which number is which.
2. Treat the line between the points as the radius of a sphere and compute the surface area of the sphere. Print the output with a label, such as “The surface area of the sphere is: …”. Hint: The distance between the points is the square root of ((x2 – x1)2 + (y2 – y1)2 ). However, since you have to square the radius, to get the area, you don’t need to worry about the square root. To work with π, do what the text does on page 14. Use 314156 as π and do the calculations. At the end, divide by 100000 and the quotient is your answer. Note that the answer will be an integer, which is fine. Formula: Surface Area = 4πr2
3. Once part 2 is working properly, add code to treat the line between the points as the side of a cube and compute the surface area of the cube. The calculation is similar to what you did in part 2 except for the multiplication by π. Print the output with an appropriate label. Formula: Surface Area = 6*side2
4. Add labels above each of the calculation code segments such (sphere: and cube:). Add a menu to the program that prompts the user as follows: “To quit enter 0” “To calculate the surface area of a sphere enter 1” “To calculate the surface area of a cube enter 2” “Enter your choice:”
# #########################################
This is what i have so far. I cant get the answer for the area of
the sphere to print.
Im a beginner at this so can you show what im doing wrong.
######################################################################
.data
Prompt1: .asciiz "\nEnter the
number for x1: "
Prompt2: .asciiz "\nEnter the
number for x2: "
Prompt3: .asciiz "\nEnter the
number for y1: "
Prompt4: .asciiz "\nEnter the
number for y2: "
Prompt5: .asciiz "\nTo quit enter
0"
Prompt6: .asciiz "\nTo calculate
the surface area of a sphere enter 1"
Prompt7: .asciiz "\nTo calculate
the surface area of a cube enter 2"
Prompt8: .asciiz "\nEnter your
choice: "
Prompt9: .asciiz "\nSurface area of
sphere: "
numPI: .double 314156.00
num4: .double 4.00
num1: .double 100000.00
.text
main:
la $a0,Prompt1
li $v0,4
syscall
li $v0,7
syscall
mov.d $f2,$f0
la $a0,Prompt2
li $v0,4
syscall
li $v0,7
syscall
mov.d $f4,$f0
la $a0,Prompt3
li $v0,4
syscall
li $v0,7
syscall
mov.d $f6,$f0
la $a0,Prompt4
li $v0,4
syscall
li $v0,7
syscall
mov.d $f8,$f0
la $a0,Prompt5
li $v0,4
syscall
la $a0,Prompt6
li $v0,4
syscall
la $a0,Prompt7
li $v0,4
syscall
la $a0,Prompt8
li $v0,4
syscall
li $v0,5
syscall
beq $v0,$0,end
end:
li $v0,10
syscall
beq $v0,'S',sphere
sphere:
#((x2 – x1)2+ (y2 – y1)2)
sub.d $f10,$f4,$f2 #x2-x1
sub.d $f12,$f8,$f6 #y2-y1
mul.d $f10,$f10,$f10 #squared
mul.d $f12,$f12,$f12 #squared
add.d $f14,$f10,$f12 #addition
l.d $f20,num4
l.d $f22,numPI
mul.d $f16,$f20,$f22
mul.d $f16,$f16,$f14
l.d $f24,num1
div.d $f16,$f16,$f24 #Surface area of the sphere
#display Sphere prompt
la $a0,Prompt9
li $v0,4
syscall
#display Surface Area
li $v0,3
syscall
Please find the code below::
.data
prompt: .asciiz "\nEnter choice \n1 For circle\n2 For Square\n3 For rectangle \n0 for exit : " #prompt for string
msg : .asciiz "Invalid choice try again."
msg1 : .asciiz "Enter value x1 y1 x2 y2 below \n"
msg2 : .asciiz "Area of circle is : "
msg3 : .asciiz "Area of square is : "
msg4 : .asciiz "Area of rectangle is : "
.text
loop: #loop for user choice
li $v0,4
la $a0,prompt #it will print prompt
syscall
li $v0,5
syscall #ask user input
move $t1,$v0 #store A in t1
#based on input jump to location
beqz $t1,exit
beq $t1,1,circle
beq $t1,2,square
beq $t1,3,rectangle
#if not any option show error
li $v0,4
la $a0,msg #it will print prompt
syscall
j loop
circle:
li $v0,4
la $a0,msg1 #it will print prompt
syscall
li $v0,5
syscall #ask user input
move $t1,$v0 #store A in t1
li $v0,5
syscall #ask user input
move $t2,$v0 #store A in t1
li $v0,5
syscall #ask user input
move $t3,$v0 #store A in t1
li $v0,5
syscall #ask user input
move $t4,$v0 #store A in t1
#values stored in t1 t2 t3 t4
sub $s0,$t1,$t3 #x2-x1
sub $s1,$t2,$t4 #y2-y1
mul $s0,$s0,$s0 #square
mul $s1,$s1,$s1 #square
add $s2,$s0,$s1 #add
mul $s2,$s2,314156 #mul by pi
div $s2,$s2,100000 #divide by 100000
li $v0,4
la $a0,msg2 #it will print prompt
syscall
li $v0,1
move $a0,$s2
syscall
j loop #loop for next
#same for square
square:
li $v0,4
la $a0,msg1 #it will print prompt
syscall
li $v0,5
syscall #ask user input
move $t1,$v0 #store A in t1
li $v0,5
syscall #ask user input
move $t2,$v0 #store A in t1
li $v0,5
syscall #ask user input
move $t3,$v0 #store A in t1
li $v0,5
syscall #ask user input
move $t4,$v0 #store A in t1
sub $s0,$t1,$t3
sub $s1,$t2,$t4
mul $s0,$s0,$s0
mul $s1,$s1,$s1
add $s2,$s0,$s1
li $v0,4
la $a0,msg3 #it will print prompt
syscall
li $v0,1
move $a0,$s2
syscall
j loop
#same for rectangle
rectangle:
li $v0,4
la $a0,msg1 #it will print prompt
syscall
li $v0,5
syscall #ask user input
move $t1,$v0 #store A in t1
li $v0,5
syscall #ask user input
move $t2,$v0 #store A in t1
li $v0,5
syscall #ask user input
move $t3,$v0 #store A in t1
li $v0,5
syscall #ask user input
move $t4,$v0 #store A in t1
sub $s0,$t1,$t3
sub $s1,$t2,$t4
mul $s2,$s0,$s1
li $v0,4
la $a0,msg4 #it will print prompt
syscall
li $v0,1
move $a0,$s2
syscall
j loop
exit: