 This time we're going to be looking at how we can implement multiplication in our hardware. We will start with a simple example using binary arithmetic to show how we would walk through one of these problems. Then we'll look at how we can apply that same structure. Then we will look at how we can translate that algorithm in order to get some hardware that will do the same thing for us. I'll start with a simple problem where I have two 4-bit numbers. I'd just like to multiply these together. So I'll start with a little bit of terminology. So our multiplicand is the number on the top, the multiplier is the number on the bottom, and our product will be our result. We start just by looking at the rightmost bit of the multiplier. Since it's one, we're going to just copy down whatever's in the top because one times anything is whatever's on top anyway. Then we go to the next place. It's a zero, so we don't copy anything down. We just ignore it and go on. The third bit is a one, so now we copy down the multiplicand again, but we do it so that it's shifted over in line with this bit. Then the fourth bit is a zero, so we don't do anything else. Now we're going to add all of these up, so our product is this number in binary. So we'd like to build some hardware that does this same thing for us. Well, we can notice a few things. One is that I didn't really do anything terribly complicated. I did some copying. When I saw one, I copied something down. When I saw zero, I didn't. So this one, I needed to shift my multiplicand over two places and then I copied it down. Then at the very end, I went through and added everything up. To implement this, we're going to look at an iterative algorithm rather than this one that works in a couple of stages. We had this kind of copy stage and then an addition stage. In order to build the same thing with stages, we would have to be able to hold all of these values some place until we got to the add stage. Then we do all the addition. Instead, we could make a loop where we do a copy and then an add. So we'd copy this number down, add it to our total. Then we'd notice, oh, I need to do this again. So we'd copy the number down and then add it to the total. This will do the same thing that we had for, but it'll be a little easier to build hardware for since we won't have to keep all of these intermediate terms. I will start by having the multiplicand on top and we're going to add this to our product. We're going to keep around this running product and at the end it will be useful. So now we'll be able to add our multiplicand to our product whenever we'd like to. We also need to be able to control when do we actually do this. We clearly don't need to do this every cycle. We only want to do that when the bit that we're looking at in the multiplier is 1. If it's 0, then we don't want to do any addition. So going to add some hardware over here that we'll be able to look at our multiplier. And this will basically just control whether or not our product updates. We can actually go through that addition stage every time and it wouldn't really matter. We would only choose to keep the product if we needed to add something. So there's the basic pieces that we've got, but there's one more issue. First, we only want to look at one bit of our multiplier at a time and it's going to change each time we step through our algorithm. We'll look at the rightmost bit, then the next rightmost bit, then the next one, then the next one, so on working towards the left. So we're only really interested in one bit of our multiplier at a time. One easy way to handle this is to just always take the rightmost bit of our multiplier and at the end of the cycle shift that rightmost bit out. So we would just shift out one bit of our multiplier every time. Similarly, we don't just copy our multiplicand straight down. We don't add it straight down every time. It shifts over for each bit. So when we got to this bit, we shifted our multiplicand over two places and added it there. When the one was in the first place, then we didn't shift our multiplicand at all, we just added it straight down. So we also want to move our multiplicand one bit to the left every time. So that's another shift operation. One last thing to keep in mind is how big some of these registers are. I've got three registers here, an outer and a control unit. Our product will need to be twice as large as whatever our inputs are, because if we add two really large numbers together, our result can come out twice as long. In this case I had a 3-bit multiplier and a 4-bit multiplicand, and my result has 6 bits to it. So certainly much larger than either of my inputs. If I'm going to be working with 32-bit integers, then both of these slots are going to end up being 64 bits in size. But our multiplier only needs to be 32 bits because we're not doing anything complicated with it. We're just picking off one bit at a time and throwing the rest away. Finally, we'll go through and we'll see how this problem will work with this architecture. I'll pretend for a moment that this is actually a 4-bit architecture, so our multiplicand will have 8 bits of space. Our multiplier will have 4 bits just so that everything fits nicely. So our multiplicand is going to go in the right-hand side of this. We have 8 bits of space, but I only have 4 bits of number. So I'm going to put them in the least significant 4 bits. I'm going to initialize my product to 0. Then the multiplier will get the 4 bits from the multiplier. So this is what my registers would have when I start the algorithm. My multiplicand is up here, my multiplier is over there, and I don't have anything for a product yet. First cycle, I take the product which is 0 and I add the multiplicand to it, 1011. That will give me a result, which would be 1011. Then I'm going to look at my multiplier over here. The right-most bit is a 1. So my control unit will tell the product register to store what you get, which is that 1011. Now I'm going to shift my multiplicand 1 bit to the left, my multiplier 1 bit to the right. So this is what all of my registers would have at the end of the first cycle. The second cycle, I will take my product, 1011, and I'm going to add my multiplicand to it. That will give me this result, that's sitting down here. Then I go look at my multiplier, the right-most bit is a 0. So my control unit is going to tell the product register not to store this result. This is not a useful result, we don't want this, so we're just going to ignore it. Next, we'll shift our multiplicand 1 bit to the left, our multiplier 1 bit to the right. So this is what all of my registers would have at the end of the second cycle. I need to walk through this algorithm 4 times because my multiplier has 4 bits in it. I need to be sure to multiply each of my 4 bits by the multiplicand. For cycle 3, I'm going to take my product and I'm going to add it to the multiplicand. So this time my adder will produce this result. That's sitting down here. Now I go look at my multiplier, the right-most bit is a 1. So my control unit will tell the product register to store this result. So I'm going to copy this value into my product. Last, I need to take my multiplicand, shift it 1 bit to the left, take my multiplier, shift it 1 bit to the right. So this is what my registers would look like at the end of the third cycle. I've got one more cycle to go through. This time I would add my product to the multiplicand. This would be the result of my addition computation. Then I'd look over at my multiplier. I would say the right-most bit is a 0, so I'm not going to store this result. I would just keep this as my product. Lastly, I would take my multiplicand, shift it 1 bit to the left, my multiplier 1 bit to the right, and then I'd be done. I'd looked at all the bits in my multiplier, all the bits in my multiplicand. So I wouldn't really have anything more to do. If you notice, this is the same result that we got originally. Our hardware is structured a little differently, so it does the algorithm iteratively rather than in two stages, but it still computes the product the way we expect. In the next video, we will look at how we can improve this hardware, how we can make it more efficient, and maybe just a little bit faster.