Modulo

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
joke
Member
Member
Posts: 56
Joined: Sat Jul 22, 2006 6:20 am

Modulo

Post by joke »

ive been reading this artice for fat
http://scottie.20m.com/fat.htm

It says :

Now that we know the logical sector, you might be wondering
how to get it to absolute sector so we can read it. Well
we use a few formulas to figure it out.
They are:
sector = ( logical sector MOD sector per track) + 1
head = (logical sector \ sector per track ) MOD number of heads
track = logical sector \ (sector per track * number of heads)

Note that this is integer division instead of floating point
division. And also Modulo math. If you can't figure this out
just turn on QBasic and enter a quick formula.
So. Using the formulas above we get:
sector= (34 MOD 512)+1= 17
head= (34 \ 512) MOD 2= 1
track= 34 \ (512 *2)= 0
Our file starts at Head 1 Track 0 Sector 17
The next sector Head 1 Track 0 Sector 18
The next sector Head 0 Track 1 Sector 1
and so on till the end.
My question is how can i implement modulo, i mean assembly doesn't have a modulo instruction. And mathematicaly modulo has many meanings. Please tell me is there a way to do modulo in assembly and if not can i do the above calculations without modulo?

Best Regards
I need someone to show me the things in life that I cant find
I cant see the things that make true happiness, I must be blind
User avatar
matthias
Member
Member
Posts: 158
Joined: Fri Oct 22, 2004 11:00 pm
Location: Vlaardingen, Holland
Contact:

Post by matthias »

I don't know if this will help you but I have written some code for you in c:

Code: Select all

int modulo(int a, int b)
{
	return (a % b);
}
Disassembly:

Code: Select all

00000000 <_modulo>:
   0:	55                   	push   %ebp
   1:	89 e5                	mov    %esp,%ebp
   3:	8b 55 08             	mov    0x8(%ebp),%edx
   6:	89 d0                	mov    %edx,%eax
   8:	c1 fa 1f             	sar    $0x1f,%edx
   b:	f7 7d 0c             	idivl  0xc(%ebp)
   e:	89 d0                	mov    %edx,%eax
  10:	5d                   	pop    %ebp
  11:	c3                   	ret 
The source of my problems is in the source.
User avatar
carbonBased
Member
Member
Posts: 382
Joined: Sat Nov 20, 2004 12:00 am
Location: Wellesley, Ontario, Canada
Contact:

Re: Modulo

Post by carbonBased »

joke wrote: My question is how can i implement modulo, i mean assembly doesn't have a modulo instruction. And mathematicaly modulo has many meanings. Please tell me is there a way to do modulo in assembly and if not can i do the above calculations without modulo?
Assembly does have a modulo (aka modulus) function. Take a closer look at your intel instruction set docs -- particularly div and idiv.

You're lookin' for dx.

--Jeff
SpooK
Member
Member
Posts: 260
Joined: Sun Jun 18, 2006 7:21 pm

Post by SpooK »

The modulo operator is a logical abstraction, derived from mathematics, that specifies the remainder of a division operation.

On the x86, this is achieved by the use of the DIV/IDIV instruction(s) with the source number (base) to divide in EAX. Using a 32-bit divisor (i.e. div ebx), EDX:EAX will be divided by EBX, and the result will be put into EAX with the remainder (modulo) in EDX... just make sure EDX is zeroed-out before performing the DIV.

Now, since you mentioned Assembly Language, and you are using powers of two (2 and 512) in your formula... you may wish to use a simple bitwise AND instead of a time consuming division operator.

Code: Select all

;Modulo example for "Sector", power of 2
mov eax, 34 ;Base
mov ebx, 511 (512-1) ;Divisor->Bitmask
and eax,ebx; Remainder in EAX
This code idea is good to keep in mind, but only use it if you are interested in just the remainder. It would take a few more instructions to implement result+remainder, probably not worth the effort ;)
User avatar
carbonBased
Member
Member
Posts: 382
Joined: Sat Nov 20, 2004 12:00 am
Location: Wellesley, Ontario, Canada
Contact:

Post by carbonBased »

SpooK wrote: This code idea is good to keep in mind, but only use it if you are interested in just the remainder. It would take a few more instructions to implement result+remainder, probably not worth the effort ;)
Actually, it's probably entirely worh the effort, in terms of speed. DIV and IDIV are two of the slowest instructions on an Intel CPU (and most others, I assume).

If working with powers of two, I definitly recommend computing division and modulo using shifts and ands. Just make sure to comment what your intention is, for those that don't see why you're doing it.

--Jeff
Post Reply