Intel 汇编指令DIV实现

看过一篇文章将的是在AVR上实现intel 汇编daa指令.刚好我看到了<vhdl数字电路设计教程>这本书,上面讲到了定点除法.然后我也实现了下.

#include <stdio.h>

unsigned int my_div(unsigned int *src,unsigned int dst);

int main()

{



        unsigned int src = 100;

	unsigned int dst = 7;

	

	unsigned int out;

	out = my_div(&src,dst);

	

	printf("out:= %d \nsrc:= %d",

			out,src);

	return 0;

}



/*

 *  *src = *src mod dst

 *  return *src/dst

 */



unsigned int my_div(unsigned int *src,unsigned int dst)

{

	unsigned int tmpsrc = *src;

	unsigned int dstcount = 0;

    unsigned int srccount = 0;

	unsigned int tmp;

	unsigned int left;

	unsigned int out = 0;

	tmp = tmpsrc;

	while(tmp)

	{

		srccount++;

		tmp = tmp>>1;

	}

	printf("srccount:%d\n",srccount);



	tmp = dst;

	while(tmp)

	{

		dstcount++;

		tmp = tmp>>1;

	}

	printf("dstcount:%d\n",dstcount);

	

	left = srccount - dstcount+1;

	printf("left:%d\n",left);

	while(left--)

	{	

		printf("xx\n");

		printf("tmpsrc>>left:%x\n",tmpsrc>>left);

		printf("dst:%x\n",dst);

		if((tmpsrc >> left) > dst)

		{

			printf("int >: 1<< left\n",dst<<left);

			out |= 1<<left;//set out

			tmpsrc-=dst<<left;			

		}

		printf("\n");

	}

		



	*src = tmpsrc;

	return out;

}

  

你可能感兴趣的:(div)