# Solution # Quadratic Equations # Name: # Class: # Date: # Start with data declarations # .data # begin the data segment str_first: .asciiz "Result for a=3, b=5, c=2, x=5 : " str_second: .asciiz "Result for a=1, b=-7, c=23, x=-6 : " str_third: .asciiz "Result for a=7, b=9, c=-1, x=15 : " str_minmax: .asciiz "Min/Max : " newline: .asciiz "\n" .align 2 # align next value on word boundary args_first: .float 3.0, 5.0, 2.0, 5.0 args_second: .float 1.0, -7.0, 23.0, -6.0 args_third: .float 7.0, 9.0, -1.0, 15.0 .text # begin the text segment .globl main main: # prolog subu $sp, $sp, 32 # 32-byte stack frame sw $ra, 20($sp) # save return address sw $fp, 16($sp) # save frame pointer addu $fp, $sp, 32 # set up a new frame pointer #------------------------------------------------- #------------------------------------------------- la $a0, args_first jal loadArgs jal quad # push the result to stack subu $sp, $sp, 4 s.s $f12, 4($sp) la $a0, str_first jal printStr # pop the result from the stack l.s $f12, 4($sp) addi $sp, $sp, 4 jal printFloat jal printLine #------------------------- la $a0, args_first jal loadArgs jal minmax # push the result to stack subu $sp, $sp, 4 s.s $f12, 4($sp) la $a0, str_minmax jal printStr # pop the result from the stack l.s $f12, 4($sp) addi $sp, $sp, 4 jal printFloat jal printLine #------------------------------------------------- #------------------------------------------------- la $a0, args_second jal loadArgs jal quad # push the result to stack subu $sp, $sp, 4 s.s $f12, 4($sp) la $a0, str_second jal printStr # pop the result from the stack l.s $f12, 4($sp) addi $sp, $sp, 4 jal printFloat jal printLine #------------------------- la $a0, args_second jal loadArgs jal minmax # push the result to stack subu $sp, $sp, 4 s.s $f12, 4($sp) la $a0, str_minmax jal printStr # pop the result from the stack l.s $f12, 4($sp) addi $sp, $sp, 4 jal printFloat jal printLine #------------------------------------------------- #------------------------------------------------- la $a0, args_third jal loadArgs jal quad # push the result to stack subu $sp, $sp, 4 s.s $f12, 4($sp) la $a0, str_third jal printStr # pop the result from the stack l.s $f12, 4($sp) addi $sp, $sp, 4 jal printFloat jal printLine #------------------------- la $a0, args_third jal loadArgs jal minmax # push the result to stack subu $sp, $sp, 4 s.s $f12, 4($sp) la $a0, str_minmax jal printStr # pop the result from the stack l.s $f12, 4($sp) addi $sp, $sp, 4 jal printFloat jal printLine #------------------------------------------------- #------------------------------------------------- lw $ra, 20($sp) # restore $ra to jump lw $fp, 16($sp) # restore the frame to $fp addu $sp, $sp, 32 # restore the stack to $sp jr $ra # jump back to the caller # end of main # Functions quad: # prolog # first get the arguments from the stack l.s $f0, 12($sp) l.s $f1, 8($sp) l.s $f2, 4($sp) l.s $f3, 0($sp) # now we can shrink the stack back (extended by loadArgs) addi $sp, $sp, 16 subu $sp, $sp, 32 # 32-byte stack frame sw $ra, 20($sp) # save return address sw $fp, 16($sp) # save frame pointer addu $fp, $sp, 32 # set up a new frame pointer #------------------------- # a=f0, b=f1, c=f2, x=f3 mul.s $f4, $f0, $f3 # f4 = a * x add.s $f4, $f4, $f1 # f4 = a * x + b mul.s $f4, $f4, $f3 # f4 = x * (a * x + b) add.s $f4, $f4, $f2 # f4 = x * (a * x + b) + c mov.s $f12, $f4 # save the result in $f12 #------------------------- lw $ra, 20($sp) # restore $ra to jump lw $fp, 16($sp) # restore the frame to $fp addu $sp, $sp, 32 # restore the stack to $sp jr $ra # jump back to the caller # end of quad minmax: # prolog # first get the arguments from the stack l.s $f0, 12($sp) l.s $f1, 8($sp) l.s $f2, 4($sp) l.s $f3, 0($sp) # now we can shrink the stack back (extended by loadArgs) addi $sp, $sp, 16 subu $sp, $sp, 32 # 32-byte stack frame sw $ra, 20($sp) # save return address sw $fp, 16($sp) # save frame pointer addu $fp, $sp, 32 # set up a new frame pointer #------------------------- # a=f0, b=f1 li.s $f4, 2.0 # f4 = 2 mul.s $f4, $f4, $f0 # f4 = 2 * a div.s $f4, $f1, $f4 # f4 = b / (2 * a) neg.s $f4, $f4 # f4 = -b / (2 * a) mov.s $f12, $f4 # save the result in $f12 #------------------------- lw $ra, 20($sp) # restore $ra to jump lw $fp, 16($sp) # restore the frame to $fp addu $sp, $sp, 32 # restore the stack to $sp jr $ra # jump back to the caller # end of minmax loadArgs: # address of the array is in $a0 # first extend the stack downwards (quad will shrink it back) subu $sp, $sp, 16 # then push the arguments l.s $f0, 0($a0) s.s $f0, 12($sp) l.s $f0, 4($a0) s.s $f0, 8($sp) l.s $f0, 8($a0) s.s $f0, 4($sp) l.s $f0, 12($a0) s.s $f0, 0($sp) jr $ra # end of loadArgs printFloat: # integer to be printed is in $f12 li $v0, 2 syscall jr $ra # end of printInt printStr: # address of the string to be printed is in $a0 li $v0, 4 syscall jr $ra # end of printStr printLine: la $a0, newline # print new line li $v0, 4 syscall jr $ra # end of printLine