 In this video, we're going to be building some hardware to do division for us. We'll approach this about the same way we did the multiplication algorithm, where we'll start with an example to see how division works for us, and then see how we can transfer that information into some hardware that we'll do about the same thing. We'll start with an example of two numbers. We'll start with a couple of numbers. Take this number, and I will divide it by three in binary. I'll add some terminology so we can talk about the various parts of our algorithm as we're working. This number is our dividend. We divide it by the divisor. The result that we get up here is called the quotient, and down at the bottom at the end, we'll have our remainder. Probably used to walking through this algorithm by now, so I have a two-bit divisor. It's not going to divide into a one-bit dividend, so I'll have a zero in this position. Once I have a two-bit dividend, then I might be able to divide a two-bit divisor into it, but 10 is still smaller than 11, so I can't. So I'll put a zero in that position. Now that I have a three-bit dividend, I can definitely divide a two-bit divisor into it. So I will subtract 11 from 101. That will leave me with 10, and I'll put a one in the quotient in that position. Next, I'll pull down the next one. I'll see 11 will go into 101, just the same as it did before, and I'll get 10 out, and I'll put a one in the quotient there. Next, I'll pull down the zero. 11 will go into 100, exactly one time, and 100 minus 11 will leave me with one. Then I'll pull down the last zero. 11 won't go into 10, so I'll put a zero in my quotient, and now I've got a remainder of 10. So now we'd like to build some hardware that will walk through this same algorithm for us. This time, we nicely did it in an iterative fashion, where we're shifting our divisor down our dividend each time and kind of doing some subtraction. We can make it a little easier for ourselves by noticing that there are these extra bits that are part of our running dividend that we just haven't written down. So I've got these two zeros that are really part of this running dividend. That's the result of subtracting 11 at this position, and I've got another one over here. You see I'm gradually pulling them in, but now I have this running dividend as I go, and whatever I'm left with at the end is my remainder. I've gone from calling it a dividend to calling it a remainder instead. So now I have something that sort of looks like what I did for the multiplication. I've got this dividend register, and I'm just going to shift my divisor over and try doing subtraction. If my subtraction works, then I can put a 1 in that position of the quotient. If it doesn't, I put a 0 in and I keep the original dividend. So I'm going to build some hardware that sort of implements this algorithm. I'm going to start with my divisor, and I'll have an outer here that will do subtraction for me. And then down here I'll have my dividend remainder register. It will start out holding our dividend, and by the end it will hold the remainder. So each time through my loop I'm going to try to subtract my divisor from my dividend. If my result is negative, then I wouldn't have been able to do the subtraction here. So I won't update the dividend register. Instead I will just move on to the next cycle. But if my result coming out of the adder is positive, then I will want to update my dividend. At the same time if I discover I can do the division at that stage, then I also want to update my quotient. Now I could have some fancy hardware that figures out exactly which of these bits in my quotient I need to set for each cycle in my algorithm. Alternatively, I could decide, okay, I'm just going to set the rightmost bit each time, and I will shift it over however many times I need to. So each time I will decide what the rightmost bit of my quotient is, and then I will shift it over until it's in the right position. Sort of like the way we do here where I started with the leftmost bit, then I moved to the next one, and the previous one effectively got shifted to the left. Then I decided about this bit, which meant that the previous two had been shifted to the left, and so on until I got to the very end. So that's effectively what I've been doing already. Now we'll just have it implemented using shift hardware. So again, I've got a bunch of 64-bit hardware here, but my quotient will only be 32 bits. So next let's walk through our problem again and see how our hardware handles this. I'm working with six-bit numbers this time, so you can see I've got my six-bit divisor here, my six-bit dividend here. I also have lots of empty blocks of six bits, so these six bits of my divisor are not useful yet. I'll use them in later cycles as I shift my original divisor to the right. These six bits of my dividend aren't being used, and then I don't know what my quotient is yet, so I've just got zeroes over there. So this is what my hardware would look like at the very beginning. The first cycle, I will take my dividend and try to subtract my divisor from that. My dividend has a whole bunch of zeroes in the leftmost six bits, where my divisor has the original divisor in the leftmost six bits. So obviously I'm not going to be able to do that subtraction effectively, and I'm going to end up shifting in a zero on the right-hand side of my quotient. So my quotient isn't going to change for quite a while. So the first cycle, I can't do the subtraction. I shift in a zero on my quotient, and I don't change the dividend. But I will shift my divisor one bit to the right. My quotient shifts in a zero, so this zero would be shifted off. So now this is what my hardware looks at the beginning of the second cycle. I've got this divisor here, and my dividend is still the same as I had before. Now I'll try to subtract my divisor from the dividend again. I'll notice the divisor is clearly still larger than my dividend. So I can't do the subtraction. I don't update my dividend, and I shift a zero into my quotient. So my quotient is going to stay the same again, and my divisor will shift one bit to the right. Now my divisor is fully into the right-hand side of the register, so I might get to do something interesting. But when I do the subtraction, I'll notice that yes, my divisor is still larger than my dividend. So I'm not going to do anything interesting. I'm just going to shift in one more zero. Now I'll have three zeros, and my divisor will shift one more bit to the right. So this is the beginning of my fourth cycle. Now my divisor has been shifted over to the right several times, and it's finally smaller than my dividend. So now I can do my subtraction. I've got one zero, one one zero zero, which is in fact larger than one one zero zero zero. In fact, it's the same as doing this problem right here. So I know this is going to be my result. This is the value that I'll end up sticking in my dividend. And now, since I can do the subtraction, I'm going to shift in a one on the right-hand side of my quotient. I'll also shift my divisor one bit to the right. Now my dividend has been updated with the running result that we've got, and I've shifted in a one, indicating that I've finally got to do some subtraction. My divisor has been shifted over one place, so now I'm at the beginning of my fifth cycle. I look at my divisor, it's clearly smaller than my dividend, so I know the results are going to be positive. When I do the subtraction, it's like doing this problem again. So I expect to get this result out. That's what my adder will give me. So this is positive, so I'm going to be able to shift in a one on the right-hand side of my quotient. And I'm going to be able to update my dividend and then shift my divisor one bit to the right again. At the beginning of my sixth cycle, you can start to see that the quotient is starting to take shape. I've got 4 bits of my quotient figured out now. This time when I look at my divisor, it's smaller than my dividend still. So I know I'm going to be able to do the subtraction again. It should be the same as this problem over here, so my results should be 10. So I'll have 10 as the result in my dividend. I'll be able to shift in another one on my quotient. And my divisor will shift one more bit to the right. Now we're at the beginning of the seventh cycle. My divisor has been shifted all the way into the right-hand side of the register. And I'm almost out of stuff in my remainder register. I've got something useful in my quotient now. And I get to check one more time to see is my dividend greater than my divisor. This time it's not. So I'm going to shift in a zero on my quotient. I can't really shift my divisor off any farther. It's been shifted over all six bits so that it's at the edge. If I shifted anything farther off, I'd be doing floating point arithmetic. And we're just worried about integer arithmetic at this point. So I'm going to shift my quotient over one bit. And now I'm going to notice that what I've got left here is my remainder now. Again, this started out as my dividend, started out with this value here. But now it's got my remainder. This is all that's left over after I've done my division seven times. The first time was kind of useless because there was no way I was ever going to be able to divide this into something over here. But we tried. So this is my remainder now. My quotient is over here. And I'm done with my divisor. So if I look, my quotient matches my quotient. And my remainder matches my remainder. In the next video, we'll look at how we can improve this hardware a little bit more, make it more efficient so that it will also be faster.