0

I'm in the process of creating an assembly program that calculates the roots of a quadratic equation. I was able to find one root, but the formatting is off.

enter image description here

Here is the code:

INCLUDE Irvine32.inc    
INCLUDE macros.inc

.data                                                   


msg1 byte "Result: ", 0
msg2 byte "With precision: ", 0
msg3 byte "a", 0

a real8 ?
b real8 ?
cc real8 ?

two real8 2.0
four real8 4.0
ten dword 10

.code                                   
main PROC       
  
    finit 
    mWrite "Enter coefficient (a): "
    call ReadFloat
    fstp a

    mWrite "Enter coefficient (b): "
    call ReadFloat
    fstp b

    mWrite "Enter coefficient (c): "
    call ReadFloat
    fstp cc

    fld b
    fmul b
    fchs
    fst b
    
    call Crlf

    fld four
    fmul a
    fmul cc
    fchs
    fsub b
    fsqrt
    fst four
    ;call WriteFloat

    call Crlf

    fld b
    fchs
    fsqrt
    fchs
    fadd four
    fst b

    fld two
    fmul a
    fst two

    fld b
    fdiv two
    
    mWrite "Root: "
    call WriteFloat

    ;call showfpustack
exit
main ENDP
end main

The result I'm getting is correct. However, I want to show it as -0.35425 (5 floating point precision). How can I format the output? I tried dividing the value by 10 which should work in theory. I would appreciate any help!

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
Nico
  • 73
  • 8
  • 2
    (a) Are you aware that `-3.5424868E-001` means −3.5424868•10^-1, which is −.35424868? So the printed number is correct, and dividing by 10 would make it .035424868. – Eric Postpischil Nov 19 '20 at 01:24
  • 1
    (b) Given that the result is a correct display of the value, are you sure you need to format it as `-0.35425` rather than `-3.5424868E-001`? – Eric Postpischil Nov 19 '20 at 01:25
  • (c) You are printing the result with `WriteFloat`. That is some custom routine. At first glance, there do not appear to be any arguments to it other than the value to write. If you want to format the number differently, are you looking for a way to do it with `WriteFloat`, are you asking whether there is some other routine to do it, or are you asking how to write code to format and print a floating-point number? – Eric Postpischil Nov 19 '20 at 01:26
  • Please avoid pasting images of output. Just copy and paste the exact text. You can use the `
    ` tag to mark it as preformatted text.
    – Eric Postpischil Nov 19 '20 at 01:27
  • Yes, I am aware that I already got the correct answer. However, I was wondering if there's a way to format it as -0.35425? If not, then I am completely content with what I got. – Nico Nov 19 '20 at 01:27
  • c) I am willing to use other routines to print the value. I'm only using WriteFloat since I'm not very proficient in assembly. – Nico Nov 19 '20 at 01:29
  • How would I go about displaying 5 decimal point values? – Nico Nov 19 '20 at 01:34
  • 1
    The easiest way might be to call the `printf` routine, assuming a standard C library is or can be linked in with your program. Somebody more familiar with Irvine32 might have to help further. – Eric Postpischil Nov 19 '20 at 01:37
  • Unfortunately, I am not allowed to use and other external libraries apart from Irvine32. I really appreciate your input, though! Thank you! – Nico Nov 19 '20 at 01:39
  • If you insist on using a "toy" library like Irvine32, accept its limitations or implement FP->string conversion manual. (This is a surprisingly hard problem to get right for the general case, especially without losing any precision. [Algorithm to convert an IEEE 754 double to a string?](https://stackoverflow.com/q/7153979)) – Peter Cordes Nov 19 '20 at 02:03

0 Answers0