0

Im trying to display the floating point in dosbox in order for debuging. I think the floating point number is store as IEEE 754 single precision. Due the the constraint im only able to use dosbox, hence im not able to use extern _printf to print the floating point in the assembly file. Is this possible to print floating point number in assembly 8086 dosbox? Thanks!

.code

a dd 3.2
z dd 2.5

main proc

mov ax,@data
mov ds,ax

finit
fldpi
fld [a]
fld [z]
fsub st(0),st(1)

mov eax,st(0)
mov dl,10
div dl

add ah,30h
mov dl,ah   
mov ah,02h
int 21h 

add al,30h
mov ah,02h
mov dl,al
int 21h

mov ax,4c00h
int 21h
Novice
  • 19
  • 1
  • 8
  • There are C implementations for 8086, you could find a `printf` implementation there. Otherwise, what format do you want? The option that probably takes the least code is "hex float" representation, like `0x1.fp3` (https://gcc.gnu.org/onlinedocs/gcc-7.1.0/gcc/Hex-Floats.html or [hexadecimal floating constant in C](https://stackoverflow.com/q/4825824)): mantissa in hex, base-2 exponent in decimal. This format can exactly represent every IEEE binary32 in a fixed amount of digits, unlike a decimal format (where infinite repeating fractions are not uncommon) – Peter Cordes Jun 30 '18 at 03:15
  • BTW, `mov eax,st(0)` doesn't exist. Do you want to round your float to the nearest integer, like `fistp dword [bp-4]` / `mov eax, [bp-4]`? Or did you want to get the FP bit-pattern into an integer register? IDK why you're dividing the low 16 bits of it by 10. – Peter Cordes Jun 30 '18 at 03:17
  • I want to get the FP bit-pattern into an integer register @PeterCordes – Novice Jun 30 '18 at 03:23
  • i Just want to display the floating point 1.5 in dosbox via eax, is this possible? how ? – Novice Jun 30 '18 at 03:26
  • So you want a decimal format. It's probably easier to write a simple float-to-string using FP operations, rather than getting the bits that represent it into an integer register. You could start with a float to int *conversion* to get the integer part, though. – Peter Cordes Jun 30 '18 at 03:57
  • Yup, turns out there's a simple algorithm which is probably good enough what you need. Get the integer part with `fist` (and subtract it from your FP value with `fisub` or something), then keep multiplying by 10 to get fraction digits. Or multiply by 10000 to get an integer with multiple fraction digits, and use the usual divide-by-10 algorithm to turn that into a string. I linked this question to an existing one showing the algorithms. Lots of other google hits for `site:stackoverflow.com assembly "float" to string` come up, too, so check them out if you want more detail. – Peter Cordes Jun 30 '18 at 04:07
  • @PeterCordes but when i use fist, if the value is 1.5 it will auto round off to 2 as a integer, is there another instruction to store integer without caring its floating point? – Novice Jun 30 '18 at 08:11
  • You mean truncate towards zero? You can set the x87 rounding mode to truncate instead of the default round-to-nearest, or use SSE3 `fisttp` if DOSBOX emulates a CPU that support it. Look at C compiler output (https://godbolt.org/g/ASFriN) from `int foo(float x) { return x; }` to see (32-bit) x87 code that does exactly that, and then sets it back. – Peter Cordes Jun 30 '18 at 08:48

0 Answers0