MIPS assembly calculating hypotenuse -
i have mips program compute hypotenuse 2 integers. program prints weird number not actual result, best guess there wrong square root function.
.data prompt1: .asciiz "enter value a: " prompt2: .asciiz "enter value b: " result: .asciiz "the hypotenuse is: " .text main: # save $ra addi $sp, $sp, -4 sw $ra, 0($sp) # accept input la $a0, prompt1 addi $v0, $0, 4 # print prompt1 string syscall addi $v0, $0, 6 # read float syscall add.s $f12, $f5, $f0 # f12 = a(input float) la $a0, prompt2 addi $v0, $0, 4 # print prompt2 string syscall addi $v0, $0, 6 # read float syscall add.s $f13, $f5, $f0 # f13 = b(input float) hypotenuse: mul.s $f12, $f12, $f12 # a*a mul.s $f13, $f13, $f13 # b*b add.s $f12, $f12, $f13 # a*a + b*b addi $sp, $sp, -4 # allocate sw $ra 0($sp) add.s $f0, $f12, $f12 jal sqrt li $v0, 3 # print double syscall # print area lw $ra 0($sp) addi $sp, $sp, 4 # de-allocate mov.s $f0, $f12 jr $ra sqrt: add $v0, $zero, 0 # r := 0 loop: mul $t0, $v0, $v0 # t0 := r*r bgt $t0, $a0, end # if (r*r > n) goto end addi $v0, $v0, 1 # r := r + 1 j loop # goto loop end: addi $v0, $v0, -1 # r := r - 1 jr $ra # return r-1 in $v0 # exit li $v0, 10 # loads op code $v0 exit program syscall # reads $v0 , exits program
your sqrt
function returns bogus value, because before call calculate a*a + b*b
in register $f0
, function performs integer arithmetic on register $v0
, contains junk.
Comments
Post a Comment