The MASM Forum

General => The Campus => Topic started by: Force on October 12, 2012, 08:16:48 AM

Title: Div Problem
Post by: Force 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


.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 ?
Title: Re: Div Problem
Post by: dedndave 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"
push   edx
invoke dwtoa,eax,addr buf1
pop    edx
invoke dwtoa,edx,addr buf2
Title: Re: Div Problem
Post by: Force on October 12, 2012, 08:33:34 AM
Dedndave

I tried your code
now result is 61.2
Title: Re: Div Problem
Post by: qWord 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:
.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)
Title: Re: Div Problem
Post by: Force on October 12, 2012, 09:50:07 AM
It is ok. now

I solved problem  :biggrin:



.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

Title: Re: Div Problem
Post by: Gunther on October 12, 2012, 10:24:25 AM
Hi Force,

Quote from: Force on October 12, 2012, 09:50:07 AM
It is ok. now

I solved problem  :biggrin:

That's fine. Welcome to the forum.

Gunther
Title: Re: Div Problem
Post by: Force on October 12, 2012, 10:33:27 AM
Thanks Gunther
Title: Re: Div Problem
Post by: Force 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


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