flat assembler
Message board for the users of flat assembler.

Index > Windows > Problem with maths.

Author
Thread Post new topic Reply to topic
Barf



Joined: 17 Sep 2004
Posts: 34
Location: Poland
Barf 23 Feb 2006, 15:07
I use Windows XP, and I want to use maths. Here i have got a little problem. When I use div or mul instruction, program crashes. When i want to use FPU it doesn`t work propertly. I've Code:
Code:
        finit
        shl     eax, 1
        mov     ebx, [ResX]
        sub     ebx, eax
        mov     [fBuff], ebx
        fild    [fBuff]
        fdiv    [svFontWidth]
        fprem
        fist    [svCharInLine]     
    

What's wrong, and why div and mul instructions doesn`t work.[/code]
Post 23 Feb 2006, 15:07
View user's profile Send private message Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7105
Location: Slovakia
vid 23 Feb 2006, 15:25
what kind of crash? Maybe you missed the fact that both "mul" and "div" modify EDX, and that caused crash
Post 23 Feb 2006, 15:25
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
Barf



Joined: 17 Sep 2004
Posts: 34
Location: Poland
Barf 23 Feb 2006, 16:13
It's a classic windows XP error window. In details there's in Exception Information: code: 0xc0000095, I think, that is that, what says, what was wrong Smile If it's problem with EDX, what should I do?
What about FPU problems, anybody has any idea?
Post 23 Feb 2006, 16:13
View user's profile Send private message Reply with quote
velox



Joined: 06 Jan 2006
Posts: 14
velox 23 Feb 2006, 16:18
yes, the problem with EDX can throw this exception, zero EDX!
Post 23 Feb 2006, 16:18
View user's profile Send private message Reply with quote
Barf



Joined: 17 Sep 2004
Posts: 34
Location: Poland
Barf 23 Feb 2006, 16:51
ok thanks, now div is working. But what with this FPU :/
Post 23 Feb 2006, 16:51
View user's profile Send private message Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7105
Location: Slovakia
vid 23 Feb 2006, 16:54
are you sure that "svFontWidth" never contains zero?
Post 23 Feb 2006, 16:54
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
Barf



Joined: 17 Sep 2004
Posts: 34
Location: Poland
Barf 24 Feb 2006, 16:09
I am sure because when i do it with div function it works propertly
Post 24 Feb 2006, 16:09
View user's profile Send private message Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 8351
Location: Kraków, Poland
Tomasz Grysztar 24 Feb 2006, 16:50
Maybe it should be FIDIV instead of FDIV? What type is "svFontWidth"?
Post 24 Feb 2006, 16:50
View user's profile Send private message Visit poster's website Reply with quote
Hicel



Joined: 09 Sep 2004
Posts: 55
Hicel 24 Feb 2006, 20:22
I tried the code you provided here... could not figure out any problem even
when I assigned values to all used registers and vars. maybe your problem
is that you do not quit the program properly with ExitProcess? Just a guess

Code:
format PE GUI

include "C:\fasmw164\include\win32a.inc"

ResX dd ?
fBuff dd ?
svFontWidth dd ?
svCharInLine dd ?

entry $
;     Even if all the rigsters and
;     vars hold values it doesn't crash
;
;        mov ecx,12345678h
;        mov ebx,12345678h
;        mov edx,12345678h
;        mov eax,12345678h
;        mov [ResX],12345678h
;        mov [fBuff],12345678h
;        mov [svFontWidth],12345678h
;        mov [svCharInLine],12345678h

        finit
        shl     eax, 1
        mov     ebx, [ResX]
        sub     ebx, eax
        mov     [fBuff], ebx
        fild    [fBuff]
        fdiv    [svFontWidth]
        fprem
        fist    [svCharInLine]
        invoke ExitProcess,0

data import

 library kernel32,'KERNEL32.DLL'

 import kernel32,\
        ExitProcess,'ExitProcess'

end data    


If it is not the exit then it seems mor like it is another part of your code.
I would also say try the "p" (Pop) variants of the instructions you used.. maybe
you fill the stack. Some systems don't automatically clear the FPU Stack. (That's just what I noticed some time ago) like "fdivp", "faddp" and so on..
Post 24 Feb 2006, 20:22
View user's profile Send private message Reply with quote
Degski



Joined: 25 Feb 2006
Posts: 1
Degski 25 Feb 2006, 14:37
Hi, I'm new to this board and new to assembler, but I had the same problem and figured it out, it's slightly more subtle than the above.

Of course you can zero the EDX and that might be what you want, In reality the EDX doesn't need to be zero, IT (EDX) JUST NEEDS TO BE SMALLER THAN THE DIVISOR, since div will divde the number as EDX:EAX / DIV and will put the Quotient in EAX and the Remainder in EDX. If EDX it larger than the divisor, the rsult of your little division will be larger than 32 bits and therefor will overflow (and crash). So one can divide a 64bit dividend by a 32bit divisor, provided the high part (EDX) is smaller than the dividend. This should remind you of primary school long division (EDX is the 'borrow'). If EDX is larger than the divisor, you should first divide EDX.

see the code below (it's for YASM and AMD64, but you''ll get the idea)

It divides a multidigit (base 2^64) number by a 64 bit number, puts the quotient in rcx and returns the remainder.

;
; rax function ( rcx, rdx, r8, r9 );
; reaminder divrem ( *quotient, *dividend, *lengthofdividend (in limbs), ; dividend)
;

%define dst rcx
%define len r8
%define dvr r9
%define src r10 ; from rdx on input
%define null r11

bits 64

global my_divrem_1

section .text

my_divrem_1:

movsxd len, r8d
mov src, rdx
cmp len, 1
mov null, 0
je .d2 ; limb by limb
dec len
cmp [src+len*8], dvr
jl .d0 ; highest limb < dvr

; highest limb >= dvr

mov rax,[src+len*8]
xor rdx,rdx
div dvr
mov [dst+len*8],rax
dec len
jmp .d1

; highest limb < dvr

.d0: mov rdx,[src+len*8]
xor [dst+len*8],null ; high dest limb set to 0
dec len

.d1: mov rax,[src+len*8]
div dvr
mov [dst+len*8],rax
dec len
cmp len,0
jge .d1 ;loop .d1

mov rax,rdx ; output the remainder
ret
ret

; limb by limb

.d2: cmp src,dvr ; is src < dvr
mov rax,[src]
jl .d3
xor rdx,rdx
div dvr
mov [dst],rax
mov rax,rdx ; output the remainder
ret
ret

.d3: xor [dst],null
ret
ret

end

ciao
Post 25 Feb 2006, 14:37
View user's profile Send private message Reply with quote
Barf



Joined: 17 Sep 2004
Posts: 34
Location: Poland
Barf 25 Feb 2006, 16:19
Tomasz Grysztar wrote:
Maybe it should be FIDIV instead of FDIV? What type is "svFontWidth"?

I didn't know about instruction like fidiv :/ If fdiv doesn't work with integers We know, why it didn't work. Thanks.
Post 25 Feb 2006, 16:19
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.