0

I just started to learn Assembly language, so I wrote a program to compare two integer numbers, using Dosbox. Now I want to update the program so it compares float numbers, but there's not much on the internet that could help me understand the difference. This is the code I wrote:

    dosseg
.model small
.stack 100h
.data
     msg1 db 10,13,"Enter first Number ...$"
     msg2 db 10,13, "Enter second Number ...$"
     msg3 db 10,13, "Numbers are equal ...$"
     msg4 db 10,13, "Numbers are not equal ...$"

.code
main proc
     mov ax, @data
     mov ds,ax

     mov dx,offset msg1
     mov ah,9
     int 21h

     mov ah,1
     int 21h
     mov cl,al
     mov dx, offset msg2
     mov ah,9
     int 21h

     mov ah,1
     int 21h
     mov dl,al
     cmp dl, cl

     je l1
     mov dx,offset msg4
     mov ah,9
     int 21h

           jmp exit
         l1:

         mov dx, offset msg3
         mov ah,9
         int 21h

         exit:
        mov ah,4ch
        int 21h




main endp
end main
Jester
  • 56,577
  • 4
  • 81
  • 125
  • 3
    Floats are notoriously hard to get right, but you can put together something that works for the simple cases. Note that even your integer code should be first updated to handle multiple digits so you get an idea about how to do string to number conversion. – Jester Jun 04 '21 at 12:11
  • Probably the easiest way to handle fractional numbers would be to still treat them as strings, but remove leading and trailing `0`s so e.g. `"00123.456000"` compares equal to `"123.456"`. Unless you want full IEEE `double` behaviour like `-0.0 == +0.0`, and also that `18446744072558215904 == 18446744072558215168` (nearest representable `double` rounds to the same value: see [Why does AWK print "0xffffffffbb6002e0" as "ffffffffbb600000" using printf?](https://unix.stackexchange.com/a/649017)). – Peter Cordes Jun 04 '21 at 13:52
  • Getting rounding correct for string->float and float->string is *hard*, when the integer and fractional part of a number can each be larger than 64-bit. e.g. https://www.exploringbinary.com/how-strtod-works-and-sometimes-doesnt/. And [Entering and dealing with floating point numbers on IEEE 784 with assembler (NASM 32bit)](https://stackoverflow.com/q/32836428) (NASM and MASM have different syntax in a few places). – Peter Cordes Jun 04 '21 at 13:56
  • The other direction is hard, too: [Turn float into string](https://stackoverflow.com/q/17632150) unless you're willing to accept inaccuracy and precision limits. – Peter Cordes Jun 04 '21 at 13:58

0 Answers0