flat assembler
Message board for the users of flat assembler.

Index > Windows > mod operator help

Author
Thread Post new topic Reply to topic
swight



Joined: 02 Aug 2008
Posts: 4
swight
I have been looking for a way to get the remainder of a idiv operation in my program. unfortunately where ever I try to use the idiv or div operations my program crashes when it finishes the thread in which it is used. I read that mod was a valid operator in fasm but have found relatively few examples of it's use and my tries have failed(probably in wrong context). It would be helpful for me if someone could write a macro or proc that could do

eax=eax mod inputnumber

that works inside a thread. in case it matters my thread declaration is
Code:
  mov eax,ThreadProc
  invoke CreateThread,NULL,0,eax,NULL,NORMAL_PRIORITY_CLASS,[ThreadId]
  invoke CloseHandle,eax          
Post 02 Aug 2008, 17:21
View user's profile Send private message Reply with quote
dxl



Joined: 17 Sep 2005
Posts: 16
dxl
a thread has its own stack and you have to assume registers are different in the thread i mean, if you didnt set them IN THE THREAD you cannot rely on their values.
Generally the problem occurs with ebp.

Prototype for CreateThread:

HANDLE WINAPI CreateThread(
__in_opt LPSECURITY_ATTRIBUTES lpThreadAttributes,
__in SIZE_T dwStackSize,
__in LPTHREAD_START_ROUTINE lpStartAddress,
__in_opt LPVOID lpParameter,
__in DWORD dwCreationFlags,
__out_opt LPDWORD lpThreadId
);

If i read well, your [ThreadId] is probably not correct if you expect the thread id to be returned in the dword place in memory called ThreadId. Don't use []
Post 02 Aug 2008, 19:36
View user's profile Send private message Reply with quote
dxl



Joined: 17 Sep 2005
Posts: 16
dxl
Example of MOD programming

Routine to compute (seed*A) MOD M (32 bits numbers)
after execution eax contains this value.

prototype: int rand(int)

rand:

M equ 7fffffffh ;2^31-1
seed equ dword [ebp+8]
A equ 16807

push ebp
mov ebp,esp
push edx ebx
mov eax,A
xor edx,edx ;clear edx

mul seed ;edx:eax<-seed*A
mov ebx,M
div ebx ;edx<-(seed*A) mod M
mov eax,edx

pop ebx edx
mov esp,ebp
pop ebp
ret 4 ;don't forget it!

Routine used in a Linear congruential generator.
Post 02 Aug 2008, 19:51
View user's profile Send private message Reply with quote
swight



Joined: 02 Aug 2008
Posts: 4
swight
I fixed the brackets,didn't change anything since I wasn't using that handle(just following a tutorial).think div may be working, it just causes a crash on thread exit. replaced div with a subtraction loop and it runs without crashing.
Post 03 Aug 2008, 13:36
View user's profile Send private message Reply with quote
Yardman



Joined: 12 Apr 2005
Posts: 245
Location: US
Yardman
[ Post removed by author. ]


Last edited by Yardman on 04 Apr 2012, 03:23; edited 1 time in total
Post 03 Aug 2008, 17:28
View user's profile Send private message Reply with quote
swight



Joined: 02 Aug 2008
Posts: 4
swight
that proc worked for me I will have to analyze later what I was doing wrong.
Post 04 Aug 2008, 01:21
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.