flat assembler
Message board for the users of flat assembler.

 Index > Heap > Smallest Divide!
Author
rocketsoft

Joined: 26 Jan 2010
Posts: 189
rocketsoft
Here U go...

01 Feb 2019, 21:28
vivik

Joined: 29 Oct 2016
Posts: 671
vivik
Code:
`use this: [code][/code]    `

Code:
```#include <stdio.h>
using namespace std;
int q;
//standard 32 bit by 32 bit divide
//32 bit result!
//smallest divide?
void divide(int a,int b){
int  n=1;q=0;loop:;a=a-b;
if (a<0) {a=a+b;n=n>>1;b=b>>1;
if (n==0) return;goto loop;}
q=q+n;n=n<<1;b=b<<1;goto loop;}
int main()
{
divide (1000000001,3);
printf ("The Quotient:%d\n",q);
return 0;
}    ```

somebody unrape my eyes please (says the guy whose coding habbits are terrible)

Code:
```#include <stdio.h>

int q;

//standard 32 bit by 32 bit divide
//32 bit result!
//smallest divide?

void divide(int a, int b){
int n=1;
q=0;
while(true) {
a=a-b;
if (a<0) {
a=a+b;
n=n>>1;
b=b>>1;
if (n==0) return;
} else {
q=q+n;
n=n<<1;
b=b<<1;
}
}
}

int main()
{
divide (1000000001,3);
printf ("The Quotient: %d\n",q);
return 0;
}    ```
02 Feb 2019, 09:41
bitRAKE

Joined: 21 Jul 2003
Posts: 2800
Location: dank orb
bitRAKE
n is just a bit index and x86 supports bit strings. So,shift on b is needed, but nothing else. The general solution is to BSR to set initial shift on b, and value of n. Works for arbitrary size operands in O(Log2 a - Log2 b) time.

Assuming we couldn't use DIV/IDIV (locked out of using specific registers); small would be:
Code:
```; ECX <= EAX / EBX

or ecx,-1
.back:  inc ecx
sub eax,ebx
jnc .back
retn    ```
...not very useful.
02 Feb 2019, 19:26
rocketsoft

Joined: 26 Jan 2010
Posts: 189
rocketsoft
I meant Small AND Fast... for implementing into a CPU!
12 Feb 2019, 03:31
 Display posts from previous: All Posts1 Day7 Days2 Weeks1 Month3 Months6 Months1 Year Oldest FirstNewest First

 Jump to: Select a forum Official----------------AssemblyPeripheria General----------------MainDOSWindowsLinuxUnixMenuetOS Specific----------------MacroinstructionsCompiler InternalsIDE DevelopmentOS ConstructionNon-x86 architecturesHigh Level LanguagesProgramming Language DesignProjects and IdeasExamples and Tutorials Other----------------FeedbackHeapTest Area

Forum Rules:
 You cannot post new topics in this forumYou cannot reply to topics in this forumYou cannot edit your posts in this forumYou cannot delete your posts in this forumYou cannot vote in polls in this forumYou can attach files in this forumYou can download files in this forum