flat assembler
Message board for the users of flat assembler.

Index > Windows > problem with div instruction

Author
Thread Post new topic Reply to topic
dave_30



Joined: 15 Dec 2012
Posts: 14
dave_30
guys i'm having serious problems with this div instruction.
it doesn't matters what number i put in eax of course it does the division and restores the result in eax but always returns zero as the reminder!!
for example this number:3BC42761
div instruction returns zero as reminder
but when i calculate it using windows calculator's MOD function returns 1 as the reminder
wtf is the problem with the fucking div instruction?
Post 28 Dec 2012, 17:44
View user's profile Send private message Reply with quote
JohnFound



Joined: 16 Jun 2003
Posts: 3502
Location: Bulgaria
JohnFound
Post the code.
Post 28 Dec 2012, 18:00
View user's profile Send private message Visit poster's website ICQ Number Reply with quote
HaHaAnonymous



Joined: 02 Dec 2012
Posts: 1180
Location: Unknown
HaHaAnonymous
[ Post removed by author. ]


Last edited by HaHaAnonymous on 28 Feb 2015, 22:08; edited 1 time in total
Post 28 Dec 2012, 18:08
View user's profile Send private message Reply with quote
dave_30



Joined: 15 Dec 2012
Posts: 14
dave_30
well,i'm trying to make a function that returns a rundom number between 2 value ranges.
here is the code:
Code:
proc RandomNum uses edx ebx,val1:DWORD,val2:DWORD

.if [CurrentVal]=0
invoke GetTickCount
mov [CurrentVal],eax
.endif
inc [val2]
back:
mov eax,[CurrentVal]
.if dword [mult] = 0
imul eax,459678324
mov dword [mult],1
.elseif dword [mult] = 1
imul eax,467854123
mov dword [mult],2
.elseif dword [mult] = 2
imul eax,954762312
mov dword [mult],0
.endif
mov [CurrentVal],eax
xor edx,edx
mov ebx,[val2]
div ebx
xchg eax,edx
.if dword [val1] > eax
mov eax,[val1]
.endif
.if dword [flag] = 0
mov dword [flag],1
jmp done
.endif
cmp eax,[OldVal]
jne done
cmp [OldVal],eax
je back
done:
mov [OldVal],eax
ret

endp
    


but the fucking problem is it crashes or either returns 0 or repeative numbers!i even did added check for it that if it's the same number then generate another number but still it doesn't works.i'm pretty fucking confused.
Post 28 Dec 2012, 18:46
View user's profile Send private message Reply with quote
baldr



Joined: 19 Mar 2008
Posts: 1651
baldr
dave_30,

Why do you think this function will provide random numbers? Only one of those multipliers (second) is uneven, first is divisible by 4 and third -- by 8, thus CurrentVal quickly (around 13 calls) overflows and become 0 (and be reseeded with GetTickCount()).

Another point is your transformation of [0, 2**32) to [val1, val2) range. Even if your generator has uniform distribution, output of RandomNum() will be biased toward val1 (the only case for uniform distribution will be when val1==0 and val2 is a power of 2).
Post 29 Dec 2012, 15:45
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-2020, Tomasz Grysztar. Also on YouTube, Twitter.

Website powered by rwasa.