News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests

Main Menu

Div Problem

Started by Force, October 12, 2012, 08:16:48 AM

Previous topic - Next topic

Force

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 ?

dedndave

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

Force

Dedndave

I tried your code
now result is 61.2

qWord

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)
MREAL macros - when you need floating point arithmetic while assembling!

Force

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


Gunther

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
You have to know the facts before you can distort them.

Force


Force

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