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)
/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\

Level Two: Advanced Multiplication

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)
ADD  AX,BX	;we'll add (AX*2)+(AX*4) giving us AX*6.
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
ADD BX,AX	;add ((AX*2)*2) to BX
Smart is'nt it? If we multiply 2 by 2 what do we get? 4 and if we multiplied it by 2 agian? we get 8. hmm ... 4+8=12=2*6. So (a*2)+(a*2*2)=a*6 hehehehe get the point? good. = )


Logic

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

Level One: AND or NOT?

Logic gates are a very useful thing indeed. They can be used for anything from encryption to Graphics programs. Indeed, some RISC processors back in the ol' days relyed mostly on logical gates! Even now, your computer is passing bits through logical gates to get the information you want (using transistors ofcourse). But to tell the truth, you don't really need to know this, I just thought that you might find it handy ... heh heh heh.

Lets start from the start, lets start with the basic concept of a logic gate. A logic gate is alot like a transistor, given two bits it outputs one. I'll start you off with a rather simple gate, the AND gate:

                  _
                 | \
  BIT1  ------- >|  \
                 |   \
                 |AND |----- > OUT
                 |   /
  BIT2  ------- >|  /
                 |_/
If you give AND 1 in BIT1 and 1 in BIT2, It will give 1 in OUT
If you give AND 0 in BIT1 and 1 in BIT2, It will give 0 in OUT
If you give AND 1 in BIT1 and 0 in BIT2, It will give 0 in OUT
If you give AND 0 in BIT1 and 0 in BIT2, It will give 0 in OUT

You can clearly see from the above information that AND will always output 0 unless both bits given to it are 1. That's where they get the name really = ). Lets move back a bit and see this in a real life situation.

"If both my desktop computer AND my laptop are on then I'm going to have a big bill to pay"

If neither machines are on (i.e 0 AND 0) then he will not pay a big bill (0 AND 0 = 0)
If only one of the machines is on (i.e 0 AND 1, 1 AND 0) then he will not pay a big bill (0 AND 1=0, 1 AND 0=0).
If both machines are on (i.e 1 AND 1) then he WILL pay a big bill, the poor guy (1 AND 1=1)

Now, the use of one bit might be trivial but when dealing with computers you can process multiple bits. That's why a computer can process more than one bit at a time, infact as many bits as a register can handel. Here's and example how:

BIT1    1	1	0	1	0	0	1	0
       AND     AND     AND     AND     AND     AND     AND     AND
BIT2	0	1	1	1	1	0	0	0
       ===========================================================
OUT	0	1	0	1	0	0	0	0
here's that up there in code:
mov ax,11010010b
and ax,01111000b
what will remain in ax? 01010000b, ofcourse! Now that we've finished and, it's time to show some more gates.

Level Two: More Gates

Right beside AND is it's cousin OR. OR and AND share quite a relationship, but I'll tell you about that later (and if you've already done mathematical redundants, I suggest skiping it =p). OR uses the same concept as AND in this manner:

                  _
                 | \
  BIT1  ------- >|  \
                 |   \
                 | OR |----- > OUT
                 |   /
  BIT2  ------- >|  /
                 |_/

If you give OR 1 in BIT1 and 1 in BIT2, It will give 1 in OUT
If you give OR 0 in BIT1 and 1 in BIT2, It will give 1 in OUT
If you give OR 1 in BIT1 and 0 in BIT2, It will give 1 in OUT
If you give OR 0 in BIT1 and 0 in BIT2, It will give 0 in OUT

Puting this into a real life situation:

"If Wafn is on #qbcc or #maths then he's going to get no work done" (hehehe plug = p)

If Wafn is only loged on to #qbcc then he will get no work done. (1 OR 0 = 1)
If Wafn is only loged on to #maths then he will also get no work done. (0 OR 1 = 1)
If Wafn is on both channels then he will still get no work done. (1 OR 1 = 1)
Finally, if Wafn is on neither channels then he will kick back and do some proper work. (0 OR 0 = 0)

Using this concept on a byte like the one above:

BIT1    1	1	0	1	0	0	1	0
        OR      OR      OR      OR      OR      OR      OR      OR
BIT2	0	1	1	1	1	0	0	0
       ===========================================================
OUT	1	1	1	1	1	0	1	0
agian in code this is written like so:
mov ax,11010010b
or ax,01111000b
And, as always, 11111010b is left in ax.

Now for the final two-bit logical gate we are going to discuss here. The eXclusive-OR logical gate is probably one of the most useful of these. While others may not behave like this normally, XOR will return the same byte if it is passed through the gate twice! I'll show you this later but for now the basic concept.

XOR will only return 1 if BIT1 does not equal BIT2.

                  _
                 | \
  BIT1  ------- >|  \
                 |   \
                 |XOR |----- > OUT
                 |   /
  BIT2  ------- >|  /
                 |_/
If you give XOR 1 in BIT1 and 1 in BIT2, It will give 0 in OUT
If you give XOR 0 in BIT1 and 1 in BIT2, It will give 1 in OUT
If you give XOR 1 in BIT1 and 0 in BIT2, It will give 1 in OUT
If you give XOR 0 in BIT1 and 0 in BIT2, It will give 0 in OUT

Ok, one last "real" life situation:

"If leroy is bored but not drunk, or if leroy is drunk but not bored nothing interesting will happen in #qbcc"

If leroy was only bored nothing will happen in #qbcc (1 XOR 0=1)
If leroy only had too much to drink nothing will happen in #qbcc (0 XOR 1=1)
If leroy was both drunk and bored (ussally not a good combination =p) then something interesting will happen in #qbcc (1 XOR 1=0)
If leroy was'nt drunk or bored then something interesting will happen in #qbcc (0 XOR 0=1)

This concept applied to the same bytes we used above:

BIT1    1	1	0	1	0	0	1	0
       XOR     XOR     XOR     XOR     XOR     XOR     XOR     XOR
BIT2    0	1	1	1	1	0	0	0
       ===========================================================
OUT	1	0	1	0	1	0	1	0
put into code:
mov ax,11010010b
xor ax,01111000b
thus, 10101010b is left in ax. But lets not stop there! There is something speacil about this number, let me demostrate something. Lets take what's left in ax, and XOR agian by BIT2.
BIT1	1	0	1	0	1	0	1	0
       XOR     XOR     XOR     XOR     XOR     XOR     XOR     XOR
BIT2    0	1	1	1	1	0	0	0
       ===========================================================
OUT	1	1	0	1	0	0	1	0
hehehehehehe, as you can see passing the product through the original BIT2 gives us back the original BIT1!

so a code like this:

mov ax,11010010b
xor ax,01111000b
xor ax,01111000b
will leave 11010010b in ax! One of the main uses of this is encryption, but that's not the topic of this file so we'll leave that for later.

Level Three: NOT.

Theres one more gate that really does'nt fit into the basics of AND, OR or XOR. That would be the infamous NOT. NOT only requires one BIT, and there are only two conditions that can be attained by this. This digram represents NOT which is alot like a .... diode .... well not really. = p

... Just look at the diagram bewehehehehe:

                  _
                 | \
                 |  \
                 |   \
  BIT1  ------- >|NOT |----- > OUT
                 |   /
                 |  /
                 |_/
If you give NOT 1 in BIT1, It will give 0 in OUT
If you give NOT 0 in BIT1, It will give 1 in OUT

All NOT really does is invert a given bit. Here's another "real" life situation:

"If Pasco is loged on don't ask for Pasco, but if Pasco is not loged on Ask where Pasco is."

(this is going to sound as if it's an echo but hey ... NOT is weird =p)

So if Pasco is loged on, then a person wont ask if he's on or not (NOT 1=0)
However, if Pasco is loged off, then a person will ask if he's on (NOT 0=1)

Applying this concept ...

BIT1    1	1	0	1	0	0	1	0
       NOT     NOT     NOT     NOT     NOT     NOT     NOT     NOT
       ===========================================================
OUT	0	0	1	0	1	1	0	1
This can be put into code like so:
mov ax,11010010
not ax
You may have realised by now that NOT has the same properities as XOR, and the invert of an invert byte is the original byte. = D

that is:

mov ax,11010010
not ax
not ax
Will leave 11010010b in AX. Not as useful in encryption because of it's obviousness =)
Level Four: Redundanments

Now I promise you, if you skip this bit you wont miss out on a thing. It's just for completeness' sake! Infact, I still don't think there is a use of this in programming, but hey it's pretty damn useful in maths. ; )

 ________________________ 
|     _____  _____       |
|    /     \/     \      |
|   |      ||      |     |
|   |      ||      |     |
|    \_____/\_____/  _ _ |
|       A      B     A B |
|________________________|
		    |____|
The above is called a Venn diagram. It's very useful, lets say that A was Byte A and B was Byte B. The intersection of A and B (the part where they meet) is A AND B. The combined area of A and B is A OR B. Now, Not A is the surroding area which does not contain anything of A, similarly NOT B is the surroding area which does not contain anything of B. A useful formula can be gain by using the Venn diagram above.
A or B = A + B - A and B
and using algebra:
A and B = A + B - A or B
Lets see these in action:

lets say A=00110101b and B=00011001b. Using the algorithim before we can tell that A and B equals:

A    	0	0	1	1	0	1	0	1
       AND     AND     AND     AND     AND     AND     AND     AND
B	0	0	0	1	1	0	0	1
       ===========================================================
OUT	0	0	0	1	0	0	0	1
thus using binary addition, subtraction A or B = 00110101b + 00011001b - 00010001b

like so:

A    	0	0	1	1	0	1	0	1
        +	+	+	+	+	+	+	+
B	0	0	0	1	1	0	0	1
       ===========================================================
A+B	0	1	0	0	1	1	1	0
	-	-	-	-	-	-	-	-
A and B 0	0	0	1	0	0	0	1
       ===========================================================
A or B	0	0	1	1	1	1	0	1
Using the normal algorithim we get the same answer:
A    	0	0	1	1	0	1	0	1
        OR      OR      OR      OR      OR      OR      OR      OR
B	0	0	0	1	1	0	0	1
       ===========================================================
OUT	0	0	1	1	1	1	0	1
If you can't figure out how the additions and calculations are done, try converting the binary numbers to decimal and doing it, I'm sure you will attain the same result.

What is the use of this? I have no clue, but it is interesting to know and who knows, you might come up with a use for it (I sure as hell have not). If you do, drop me a line, wont ya? = )

Note: nicks and channels contained in here are for displaying purposes only,
if I have used your nick and you don't want me to use it then tell me, and I
will remove it ... but then why would'nt you want it? = D

This article has been written by abionnnn (abionnnn@geocities.com)


© Copyright 1999-2000 The QB Times.
This tutorial was taken from The QB Times with permission from the webmaster(s) of the site. You may not take this article and place it on any other website.