Thread: Question

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    34

    Question

    Does anyone truly understand how the ALU works? yeah i know it is able to add/subtract/div/mult all by addition but how? What is the way in which you can divide by adding?

    Thanks.


    I am Puzzled

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    I realise this is prolly the hardest kind of code to understand, but I wrote up a C program that kinda mimics how an ALU could divide...
    Code:
    #include <stdio.h>
    
    typedef unsigned short reg;
    
    void divide (reg dividend, reg divisor, reg * quotient, reg * remainder);
    
    int main ()
    {
     reg d1, d2, d3, d4;
     d1 = 1;
     for (;d1 != 0;)
     {
      scanf ("%hl", &d1);
      scanf ("%hl", &d2);
      divide (d1, d2, &d3, &d4);
      printf ("%d r %d\n", (int) d3, (int) d4);
     }
     return 0;
    }
    
    void divide (reg dividend, reg divisor, reg * quotient, reg * remainder)
    {
     unsigned long divisor2;
     int i;
     divisor2 = divisor << 16;
     *quotient = 0x00;
     for (i = 0; i <= 16; i++)
     {
      if (dividend >= divisor2)
      {
       dividend -= divisor2;
       *quotient |= 0x01;
      }
      *quotient <<= 1;
      divisor2 >>= 1;
     }
     *quotient >>= 1;
     *remainder = dividend;
     return;
    }
    Don't have time to explain it much more. Just need to point out that the divide function obviously has all the logic, I'm not too sure how they'd set up the count i in the ALU, but I don't doubt that it's possible.
    Callou collei we'll code the way
    Of prime numbers and pings!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Alice....
    By Lurker in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 06-20-2005, 02:51 PM
  2. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 05:51 PM
  3. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 01:47 AM