Author Topic: Div Problem  (Read 5422 times)

Force

  • Regular Member
  • *
  • Posts: 38
Div Problem
« on: October 12, 2012, 08:16:48 AM »
Hello all
I am late for this new forum ..... in fact i m far from assembly for a long time

so i m confused with this simple code

Code: [Select]
.386
.model flat, stdcall
option casemap:none
include \masm32\include\windows.inc
include \masm32\include\kernel32.inc
include \masm32\include\user32.inc
include \masm32\include\masm32.inc

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

.data
MsgCaption      db "Test",0
buffer db 50 dup (0)
buf1 db 20 dup (0)
buf2 db 20 dup (0)
point db ".",0
.code
start:

xor edx, edx
mov eax,185
mov ebx,3
div ebx

invoke dwtoa,eax,addr buf1
invoke dwtoa,edx,addr buf2

invoke lstrcat,addr buffer,addr buf1
invoke lstrcat,addr buffer,addr point
invoke lstrcat,addr buffer,addr buf2


invoke MessageBox, NULL,addr buffer, addr MsgCaption, MB_OK
invoke ExitProcess,NULL
end start




185 / 3 = 61.66

but in my code result is 61.0 

what is wrong with edx ?

dedndave

  • Member
  • *****
  • Posts: 8823
  • Still using Abacus 2.0
    • DednDave
Re: Div Problem
« Reply #1 on: October 12, 2012, 08:24:14 AM »
first, EDX will probably have 2 in it
DIV does not really yield a "remainder", even though that's what we call it
it is actually the "modulus" - the numerator of a fraction that creates the remainder
the denominator of that fraction is the divisor

second - when you call these functions, EAX, ECX, and EDX are "expendable"
Code: [Select]
push   edx
invoke dwtoa,eax,addr buf1
pop    edx
invoke dwtoa,edx,addr buf2

Force

  • Regular Member
  • *
  • Posts: 38
Re: Div Problem
« Reply #2 on: October 12, 2012, 08:33:34 AM »
Dedndave

I tried your code
now result is 61.2

qWord

  • Member
  • *****
  • Posts: 1473
  • The base type of a type is the type itself
    • SmplMath macros
Re: Div Problem
« Reply #3 on: October 12, 2012, 08:36:34 AM »
If you want a floating point value as result, use the FPU (floating point unit) or SSEx instructions:
Code: [Select]
.686
.mmx
.xmm
...

.data
x REAL8 ?  ; REAL8 = double
u REAL8 185.0
v REAl8 3.0
.code

; FPU version
fld u
fdiv v
fstp x

; SSE2 version
movsd xmm0,u
divsd xmm0,v
movsd x,xmm0
use crt_printf with "%.4G" to print the result (msvcrt.inc/lib, the crt_ prefix is needed for all functions)
MREAL macros - when you need floating point arithmetic while assembling!

Force

  • Regular Member
  • *
  • Posts: 38
Re: Div Problem
« Reply #4 on: October 12, 2012, 09:50:07 AM »
It is ok. now

I solved problem  :biggrin:

Code: [Select]

.386
.model flat, stdcall
option casemap:none
include \masm32\include\windows.inc
include \masm32\include\kernel32.inc
include \masm32\include\user32.inc
include \masm32\include\masm32.inc

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

.data
MsgCaption      db "Test",0
buffer db 50 dup (0)
buf1 db 20 dup (0)
buf2 db 20 dup (0)
point db ".",0

.data?
remain dd ?

.code
start:

xor edx, edx
mov eax,185
mov ebx,3
div ebx
push ebx
push edx

invoke dwtoa,eax,addr buf1

pop edx
mov eax,edx
mov ebx,100
mul ebx
xor ebx,ebx
pop ebx
div ebx
mov remain,eax

invoke dwtoa,remain,addr buf2

invoke lstrcat,addr buffer,addr buf1
invoke lstrcat,addr buffer,addr point
invoke lstrcat,addr buffer,addr buf2


invoke MessageBox, NULL,addr buffer, addr MsgCaption, MB_OK
invoke ExitProcess,NULL
end start


Gunther

  • Member
  • *****
  • Posts: 3585
  • Forgive your enemies, but never forget their names
Re: Div Problem
« Reply #5 on: October 12, 2012, 10:24:25 AM »
Hi Force,

It is ok. now

I solved problem  :biggrin:

That's fine. Welcome to the forum.

Gunther
Get your facts first, and then you can distort them.

Force

  • Regular Member
  • *
  • Posts: 38
Re: Div Problem
« Reply #6 on: October 12, 2012, 10:33:27 AM »
Thanks Gunther

Force

  • Regular Member
  • *
  • Posts: 38
Re: Div Problem
« Reply #7 on: October 12, 2012, 10:43:49 AM »
I got that problem when i make a dwtoa.inc

so i was confused if there is problem in dwtoa.inc or in codes

but dwtoa works good  :biggrin:

dwtoa.inc

Code: [Select]
proc dwtoa, dwValue, lpBuffer

pushad   
xor     ecx,ecx
mov     eax, [dwValue]
mov     edi, [lpBuffer]
mov     ebx,10

count:
xor edx,edx
div ebx
add edx,30h
push edx
inc ecx
or eax,eax

jnz count

check:
pop  edx
mov [edi],edx
inc edi

loop check

popad
ret
   
endp