Code:
;=========== Show the initial message =====================================================================================================================================
mov qword rax, 0 ;No data from SSE will be printed
mov rdi, stringformat ;"%s"
mov rsi, initialmessage ;"Welcome to Areas of Trapezoids"
call printf ;Call a library function to make the output
;=========== Prompt floating point number for first base ==================================================================================================================
mov qword rax, 0 ;No data from SSE will be printed
mov rdi, stringformat ;"%s"
mov rsi, promptmessage1 ;"Please enter one of the base numbers: "
call printf ;Call a library function to make the output
;===== Obtain a floating point number from the standard input device and store a copy in xmm0 =============================================================================
push qword 0 ;Reserve 8 bytes of storage for the incoming number
mov qword rax, 0 ;SSE is not involved in this scanf operation
mov rdi, eight_byte_format ;"%lf"
mov rsi, rsp ;Give scanf a point to the reserved storage
call scanf ;Call a library function to do the input work
movsd xmm0, [rsp] ;Copy the inputted number to xmm0
pop rax ;Make free the storage that was used by scanf
;=========== Prompt floating point number for second base =================================================================================================================
mov qword rax, 0 ;No data from SSE will be printed
mov rdi, stringformat ;"%s"
mov rsi, promptmessage2 ;"Please enter the other base number: "
call printf ;Call a library function to make the output
;===== Obtain a floating point number from the standard input device and store a copy in xmm1 =============================================================================
push qword 0 ;Reserve 8 bytes of storage for the incoming number
mov qword rax, 0 ;SSE is not involved in this scanf operation
mov rdi, eight_byte_format ;"%lf"
mov rsi, rsp ;Give scanf a point to the reserved storage
call scanf ;Call a library function to do the input work
movsd xmm1, [rsp] ;Copy the inputted number to xmm1
pop rax ;Make free the storage that was used by scanf
;=========== Prompt floating point number for height ======================================================================================================================
mov qword rax, 0 ;No data from SSE will be printed
mov rdi, stringformat ;"%s"
mov rsi, promptmessageheight ;"Please enter the height: "
call printf ;Call a library function to make the output
;===== Obtain a floating point number from the standard input device and store a copy in xmm2 =============================================================================
push qword 0 ;Reserve 8 bytes of storage for the incoming number
mov qword rax, 0 ;SSE is not involved in this scanf operation
mov rdi, eight_byte_format ;"%lf"
mov rsi, rsp ;Give scanf a point to the reserved storage
call scanf ;Call a library function to do the input work
movsd xmm2, [rsp] ;Copy the inputted number to xmm2
pop rax
;===== Add the 2 base numbers together ===================================================================================================================================
movsd xmm3, xmm0 ;There are 2 copies of the first base: xmm0 and xmm3
movsd xmm4, xmm1 ;There are 2 copies of the second base: xmm1 and xmm4
addss xmm3, xmm4 ;Adds the copy xmm4 to the copy xmm3
movsd xmm5, xmm3 ;Stores the new value of xmm3 in xmm5
;===== Divide the sum of both bases by a constant =========================================================================================================================
mov rbx, 0x4000000000000000 ;Constant 0x4000000000000000 = 2.0 (decimal).
push rbx ;Place the constant on the integer stack
divsd xmm5, [rsp] ;Divide the sum of both bases by the constant
movsd xmm6, [rsp] ;Copy the divisor to xmm6
pop rax ;Discard the divisor from the integer stack
;===== Save a copy of the quotient before calling printf ==================================================================================================================
push qword 0 ;Reserve 8 bytes of storage
movsd [rsp], xmm5 ;Place a backup copy of the quotient in the reserved storage
;===== Multiply the quotient by the height ================================================================================================================================
mulsd xmm5, xmm2 ;Multiplies the sum of both bases by the height
movsd xmm7, xmm5 ;Copy the outcome to xmm7
;===== Show the outcome ===================================================================================================================================================
mov rax, 4 ;4 floating point numbers will be outputted
mov rdi, outputmessage ;"The area of a trapezoid with sizes %1.18lf, %1.18lf, and %1.18lf is %1.18lf"
call printf ;Call a library function to do the hard work
;===== Conclusion message =================================================================================================================================================
mov qword rax, 0 ;No data from SSE will be printed
mov rdi, stringformat ;"%s"
mov rsi, goodbye ;"Have a nice day. Enjoy your trapezoids."
call printf ;Call a library function to do the hard work.
;===== Retrieve a copy of the quotient that was backed up earlier =========================================================================================================
pop r14 ;A copy of the quotient is in r14 (temporary storage)
;Now the stack is in the same state as when the application area was entered. It is safe to leave this application area.
Bookmarks