# QB Times Issue #9

## Multiplications

Warning : This is ment for advanced QB users or advanced C/C++ users wanting to expand their knowelge and make their own libraries. QB Begineers beware, this would be a frighting scare!
Level One:
It begins

Ever wonder how people can multiply in under 7 clocks? You can sometimes get away in multiplying with MUL with accoward numbers, but for pure speed it's time to learn an alternative. You've seen SHL and SHR right? Well, we all know that all they do is Shift a registers bit by a number specified. yipee, that's exactly what we need! SHL Shifts them to the left, while SHR shifts them to the right. This is the Synatix:
```SHL Reg,Value
```
You might be wondering what all this has to do with multiplaction, well if we shift all the bits of a number this is what happens:
```	0	1	0	0	0	0	0	1
(65)
============> Shift Left by 1 (SHL AX,1)

1	0	0	0	0	0	1	0
(130)
```
Hehe, you gota be slow not to know what that does! See how the number got multiplied by 2? Ding ding ding! You've just found a way of quick multiplication!

But how often do you need to multiply something by two? Not very, but you can multiply by powers of 2 VERY easily. You see, every time you shift the bits of a number to the left by 1 you multiply by 2, but what would happen if you now shift in 2s or 3s or even 4s?

If your getting confused on what I'm going on about, here's another diagram to clear things up!

```	0	0	1	0	0	0	0	1
(33)
============> Shift Left by 2 (SHL AX,2)

1	0	0	0	0	1	0	0
(132)
```
Wow we multiplyed by four, all by shifting the whole register by 2 bits to the left. That's multiplication at it's best, because you've just done a relevently difficult task in under 4 clocks!

But, you can multiply with other numbers too! Here's a table that shows you what happens when you shift a register's numberto the left by x:

```                 ___________________________________
| X | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |
|MUL| 2 | 4 | 8 | 16| 32| 64|128|256|
|___|___|___|___|___|___|___|___|___|
```
So if we shift 20 to the left by 3 (SHL AX,3) you'll get 160 in AX. Wonderful.
```\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/
C/C++ programmers, you know the << and  >> commands? Well this is their ASM
equivalant! You usally see people using them like this for speed
y=(a<<3)+(a<<2)
/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
```

Now, not all of us are lucky enough to need to multiply by 2 4 8 16 32 64 128 or 256. You ussaly have to multiply by Base 10 relative numbers like 200. Now comes the trick bit, to be or not to be! Ok, we'll start with something simple, lets say multiplying by 6.

Before getting to the ASM bit, we'll start some theory work. Lets say y is the outcome, and a is the input (a is AX, and y is what AX will be after we finish off with it). Ok to multiply a by 6 we do this :

```y=(a<<1)+(a<<2)
```
Hmmm ... well what it's saying up there is we will shift a to the left by 1, and then get the orignal a back and shift it by two. This is easier than it sounds.
```(a is in AX,y is AX at the end)
MOV  BX,AX	;We'll save AX's value for later use.
SHL  AX,1	;Shift ax to the left by 1 (multiply by 2)
XCHG AX,BX	;Ok we'll swap (AX*2) with AX, so we can multiply the orignal again.
SHL  AX,2	;Now we'll shift ax to the left by 2 (multiply by 4)
```
ok so we basicly did this : (AX*2)+(AX*4), in maths that would give us AX+AX+AX+AX+AX+AX, which is ofcourse AX*6.

If you get it, read the source code below it does it a tad faster and with a smaller code but does the exact same thing, if you did'nt email me (abionnnn@geocities.com) and tell me what you don't get, so I can add it to the new version of the tutorial.

```(a is in AX)
SHL AX,1	;shift ax to the left by 1.
MOV BX,AX	;add (AX*2) to BX
SHL AX,1	;shift (ax*2) to the left by 1