flat assembler
Message board for the users of flat assembler.

flat assembler > Heap > Smallest Divide!

Author
Thread Post new topic Reply to topic
rocketsoft



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


Description:
Download
Filename: divider.txt
Filesize: 348 Bytes
Downloaded: 55 Time(s)

Post 01 Feb 2019, 21:28
View user's profile Send private message Visit poster's website Reply with quote
vivik



Joined: 29 Oct 2016
Posts: 669
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;
}    
Post 02 Feb 2019, 09:41
View user's profile Send private message Reply with quote
bitRAKE



Joined: 21 Jul 2003
Posts: 2776
Location: dank orb
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.
Post 02 Feb 2019, 19:26
View user's profile Send private message Visit poster's website Reply with quote
rocketsoft



Joined: 26 Jan 2010
Posts: 189
I meant Small AND Fast... for implementing into a CPU!
Post 12 Feb 2019, 03:31
View user's profile Send private message Visit poster's website 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 can attach files in this forum
You can download files in this forum


Copyright © 1999-2019, Tomasz Grysztar.

Powered by rwasa.