Page 1 of 1

divMod Function

Posted: Sat May 05, 2012 9:11 pm
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?

Re: divMod Function

Posted: Sat May 05, 2012 9:24 pm
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.

Re: divMod Function

Posted: Sat May 05, 2012 9:26 pm
by georgemorgan
Thank you. I should have specified that I need to write a function in C, which is a bit harder.

Re: divMod Function

Posted: Sat May 05, 2012 9:40 pm
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....

Re: divMod Function

Posted: Sat May 05, 2012 10:03 pm
by georgemorgan
SonyQrio wrote:The ARM architecture does not have hardware support for division...

Re: divMod Function

Posted: Sat May 05, 2012 10:10 pm
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.

Re: divMod Function

Posted: Sat May 05, 2012 10:34 pm
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....

Re: divMod Function

Posted: Sat May 05, 2012 11:43 pm
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.

Re: divMod Function

Posted: Sun May 06, 2012 3:29 am
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.