divMod Function

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
georgemorgan
Posts: 20
Joined: Mon May 23, 2011 7:41 pm

divMod Function

Post by georgemorgan »

I'm writing an OS and I need to perform the modulo operand, '%'.

The ARM architecture does not have hardware support for division, so I'll have to write my own function. Any ideas on how to do this?
Rudster816
Member
Member
Posts: 141
Joined: Thu Jun 17, 2010 2:36 am

Re: divMod Function

Post by Rudster816 »

I'll do the work you should have done (Google)...

Simple\With Code:
http://www.virag.si/2010/02/simple-divi ... assembler/

Best:
http://gmplib.org/~tege/divcnst-pldi94.pdf

You could also compile a simple C program that uses modulo using GCC and look at the assembly.
georgemorgan
Posts: 20
Joined: Mon May 23, 2011 7:41 pm

Re: divMod Function

Post by georgemorgan »

Thank you. I should have specified that I need to write a function in C, which is a bit harder.
Rudster816
Member
Member
Posts: 141
Joined: Thu Jun 17, 2010 2:36 am

Re: divMod Function

Post by Rudster816 »

SonyQrio wrote:Thank you. I should have specified that I need to write a function in C, which is a bit harder.
Why do you need to write a function? Just use the % operator and your compiler will generate the code for you....
georgemorgan
Posts: 20
Joined: Mon May 23, 2011 7:41 pm

Re: divMod Function

Post by georgemorgan »

SonyQrio wrote:The ARM architecture does not have hardware support for division...
gerryg400
Member
Member
Posts: 1801
Joined: Thu Mar 25, 2010 11:26 pm
Location: Melbourne, Australia

Re: divMod Function

Post by gerryg400 »

What about this....

Code: Select all

int mod_func(int num, int den) {

    return num % den;
}
I haven't tested it but it might work.
If a trainstation is where trains stop, what is a workstation ?
Rudster816
Member
Member
Posts: 141
Joined: Thu Jun 17, 2010 2:36 am

Re: divMod Function

Post by Rudster816 »

SonyQrio wrote:
SonyQrio wrote:The ARM architecture does not have hardware support for division...
Rudster816 wrote: Why do you need to write a function? Just use the % operator and your compiler will generate the code for you....
pcmattman
Member
Member
Posts: 2566
Joined: Sun Jan 14, 2007 9:15 pm
Libera.chat IRC: miselin
Location: Sydney, Australia (I come from a land down under!)
Contact:

Re: divMod Function

Post by pcmattman »

And if it doesn't generate code directly, it'll inject a call to libgcc. Conveniently enough, the source code for libgcc is available (you can use libgcc's code for other things too, such as finding out how to do things like atomic operations).

All else aside, what you need to do here is determine an algorithm for calculating the modulus, and then implement that algorithm.

Your most basic implementation might just involve subtraction and nothing else.
OSwhatever
Member
Member
Posts: 595
Joined: Mon Jul 05, 2010 4:15 pm

Re: divMod Function

Post by OSwhatever »

GCC generates modulo and division code automatically. If you don't want that, you can search the linux source code which has ARM division example in assembly language.
Post Reply