News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests
NB: Posting URL's See here: Posted URL Change

Main Menu

how to get decimal value in the remainder ?

Started by allen, February 12, 2014, 04:51:43 PM

Previous topic - Next topic

allen

im trying to divide  2 & 7 3 & 6 but the remainder is 0 how to show the rest of the decimal point 

.386
.model flat, stdcall
option casemap :none

include \masm32\include\windows.inc
include \masm32\include\kernel32.inc
include \masm32\include\masm32.inc

includelib \masm32\lib\kernel32.lib
includelib \masm32\lib\masm32.lib






.data
    Number5 dword 0
    Number6 dword 0
    Number9 dword 0
    Number10 dword 0
    UserInput5 db 10 DUP(0)
    UserInput6 db 10 DUP(0)
    UserInput9 db 10 DUP(0)
    UserInput10 db 10 DUP(0)
    Message3 db "Enter Number 2 & 7: ", 0
    Message5 db "Enter Number 3 & 6: ", 0
    DivString db 20 DUP(0)
    DivideString db 20 DUP(0)
    NumDiv dword 0
    NumDivide dword 0
    Message13 db " Number 2 & 7 div: ", 0
    Message15 db " Number 3 & 6 div: ", 0
    buf1 db 20 dup (0)
   

.code

start:
        invoke StdOut, addr Message3
        invoke StdIn, addr UserInput5, 10   
        invoke StdIn, addr UserInput6, 10

        invoke StdOut, addr Message5
        invoke StdIn, addr UserInput9, 10
        invoke StdIn, addr UserInput10, 10



        invoke StripLF, addr UserInput5
        invoke StripLF, addr UserInput6


        invoke StripLF, addr UserInput9
        invoke StripLF, addr UserInput10



        invoke atodw, addr UserInput5
        mov Number5, eax

        invoke atodw, addr UserInput9
        mov Number9, eax

        invoke atodw, addr UserInput6
        mov Number6, eax

        invoke atodw, addr UserInput10
        mov Number10, eax

       
        xor edx,edx
        mov eax,Number5
        mov ebx, Number6
        mov NumDiv, eax
        div ebx
        mov NumDiv,eax

        xor edx,edx
        mov eax,Number9
        mov ebx, Number10
        mov NumDiv, eax
        div ebx
        mov NumDiv,eax


       
       
        invoke dwtoa,eax,addr buf1
        invoke dwtoa, NumDiv, addr DivString
        invoke dwtoa, NumDivide,  addr DivideString

        invoke StdOut, addr Message15
  invoke StdOut, addr DivideString


        invoke StdOut, addr Message13
  invoke StdOut, addr DivString


   invoke ExitProcess, NULL
       
end start

jj2007

Check for remainder in the "div" para, \Masm32\help\opcodes.chm

And welcome to the Forum, Allen :icon14:

allen

thanks but when i read it alot of times and read it again there was no indication or hint on how to deal or showing  with decimal value for idiv im already confused how to fix this problem

jj2007

Take an example, 1000/77:
   mov edx, 0
   mov eax, 1000
   mov ecx, 77
   div ecx

; Result 1000/77:
; eax             12
; edx             76

As you can see, you get 12 in eax, and the remainder 76 in edx. What else do you expect? 12.987? Registers return integers, not floats.

TWell

Full example for debugging.
It's a mess, but i'm not a masm programmer at all ;)
.486
.MODEL FLAT, STDCALL
OPTION CASEMAP :NONE

ExitProcess PROTO :DWORD
INCLUDELIB kernel32.lib
printf PROTO C :DWORD,:VARARG
INCLUDELIB msvcrt.lib

.data
fmt db "%d/%d = %d.%d",13,10,0

.code
start PROC
mov ebx, 165 ; divident
mov ecx, 4 ; divider
cdq   ; look it up in opcodes.hlp - sets edx to zero in this case
mov eax, ebx ; divident to EAX
div ecx ; divide eax with ecx -> result EAX:EDX
push eax ; save result
mov eax, edx ; modulo
push ecx ; divider
mov ecx, 1000 ; multiplier/ decimals
mul ecx ; multiply EAX with 1000
pop ecx ; divider back
div ecx ; div EAX 1000x modulo with divider
mov edx, eax ; save decimals to EDX
pop eax ; result back
INVOKE printf, ADDR fmt, ebx, ecx, eax, edx
INVOKE ExitProcess,eax
start ENDP
END start

allen

Thanks alot I been Trying to Figure out The float Point in Masm32 And Now read and trying to understand how to use it

Gunther

Hi allen,

I think your problem is solved. Welcome to the forum.

Gunther
You have to know the facts before you can distort them.

jj2007

Quote from: allen on February 12, 2014, 11:23:21 PM
Thanks alot I been Trying to Figure out The float Point in Masm32 And Now read and trying to understand how to use it

In case you don't have it, find attached fphelp.hlp

mabdelouahab

include masm32rt.inc
.data?

.code   
;result -----------------------------------------------  ( EAX = X / Y )
   DivS proc X:dword,Y:dword
      xor edx,edx           
      mov eax,X
      mov ecx,Y
      .if ecx == 0
         invoke MessageBox,0,chr$(" division by 0"),chr$("Er"),MB_OK
      .else
         div ecx
      .endif
      ret
   DivS endp
;modulo ----------------------------------------------- (EAX = X MOD Y )
   DivSm proc X:dword,Y:dword
      xor edx,edx           
      mov eax,X
      mov ecx,Y
      .if ecx == 0
         invoke MessageBox,0,chr$(" division by 0"),chr$("Er"),MB_OK
      .else
         CDQ 
         IDIV ECX
         MOV EAX,EDX
      .endif
      ret
   DivSm endp   
start:
   mov eax,rv(DivS,17,2)   ;( EAX = 17 / 2 =8)
   ;.......
   mov eax,rv(DivSm,17,2)   ;( EAX = 17 MOD 2 =1)
   ;.......
   inkey
   exit
end  start
       

dedndave

after performing DWORD division, EDX holds the modulus
the remainder is actually
remainder = modulus/divisor

if you want to display 4 digits after the decimal, multiply the modulus by 10000, then divide it by the original divisor
you may then want to round to nearest
then convert the scaled remainder into a 4 digit decimal value

it's probably simpler to use the FPU and a floating point conversion routine   :biggrin: