Question
Why does this code to find the hypotenuse of a right triangle only print out 0 as the hypotenuse? extern printf ;Getting external printf from
Why does this code to find the hypotenuse of a right triangle only print out 0 as the hypotenuse?
extern printf ;Getting external printf from driver program extern scanf ;Getting external scanf from driver program global pythagoras
section .data ;Defining all my variables authormessage db "This hypotenuse function is brought to you by .", 10, 0 instruction1 db "Please enter the length of the first side: ", 0 instruction2 db "Please enter the length of the second side: ", 0 numoutputformat db "The number %ld was received.", 10, 0 invalidinput db "ERROR: Integer must be a positive value. Please try again", 10, 0 hypotenuseformat db "The hypotenuse of the triangle is %.6lf", 10, 0 endprogram db "The hypotenuse of the triangle is returned. Please enjoy your triangles.", 10, 0 stringoutputformat db "%s", 0 signedintinputformat db "%ld", 0 floatoutputformat db "%.6lf", 10, 0
section .bss var1: resq 1 ;Reserving 1 quadword for user input var2: resq 1 ;Reserving 1 quadword for user input var3: resd 1
section .text pythagoras: ;Assembly module push rbp ;Alligning stack to 16 bits
;printing intro onto screen mov rdi, stringoutputformat mov rsi, authormessage mov rax, 0 call printf
promptUser1:
;Prompting user for input of first side mov rsi, instruction1 mov rdi, stringoutputformat mov rax, 0 call printf
takeInput1:
;Taking input of first side by calling scanf mov rdi, signedintinputformat mov rsi, var1 mov rax, 0 call scanf
;Printing user input mov rdi, numoutputformat ;"The number %ld was received" mov rsi, [var1] ;var1 is address. [var1] is value of address mov rax, 0 call printf
;Checking if user input is positive or negative mov r14, [var1] ;Moving user input into r14 cmp r14, 0 ;Comparing r14 to 0 jle error ;Jump to error block if negative value jg promptUser2 ;Jump to promptUser2 block if positive value
error: mov rdi, stringoutputformat mov rsi, invalidinput mov rax, 0 call printf
jmp promptUser1 ;Looping back to taking input until ;positive integer is entered
promptUser2: ;Prompting user for input of second side mov rsi, instruction2 mov rdi, stringoutputformat mov rax, 0 call printf
takeInput2:
;Taking input of second side by calling scanf mov rdi, signedintinputformat mov rsi, var2 mov rax, 0 call scanf
;Printing user input mov rdi, numoutputformat ;"The number %ld was received" mov rsi, [var2] ;var1 is address. [var1] is value of address mov rax, 0 call printf
;Checking if user input is positive or negative mov r15, [var2] ;Moving user input into r14 cmp r15, 0 ;Comparing r14 to 0 jle error1 ;Jump to error block if negative value jg calculate ;Jump to promptUser2 block if positive value error1: mov rdi, stringoutputformat mov rsi, invalidinput mov rax, 0 call printf
jmp promptUser2 ;Looping back to taking input until ;positive integer is entered calculate: fld qword [var1] ;Loading first side into floating-point stack fld qword [var2] ;Loading second side into floating-point stack fmul fadd ;Adding both values on top of floating-point stack fsqrt ;Calculating square root of sum of both sides fstp qword [var3] ;Storing result in var3 as a quadword jmp Results
Results: ;Printing result mov rdi, hypotenuseformat mov rsi, var3 mov rax, 0 call printf
endProgram:
;Printing end message mov rdi, stringoutputformat mov rsi, endprogram mov rax, 0 call printf
pop rbp ;Un-aligning stack ret ;Returning control to caller
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access with AI-Powered 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