# SXMicrocontrollerMathMethod Tutorial

```Multiplying works just like long multiplication you do on pencil and paper,
but you just use binary. Makes it pretty easy.

Take a look,
10010100    or   148
x 00010010       x  18
-----------       -----
00000000        1184
10010100       + 148
00000000
00000000
10010100
00000000
00000000
+ 00000000
=================        ====
000101001101000    or  2664

The way I actually implemented it was like this:
var.lo=multiplicand (i.e. 148)
var.hi=0
W=multiplier (i.e. 18)
I right shift the LSB out of var.
Then 8 times, I do this:
If the bit is set, I add W to the high part.
Right shift the high part (which will shift in overflow from the add)
Right shift the low part. The low part will, as I pull bits out from the
right, will get new bits in from the left from the high part, so IOW, the
multiplicand is being replaced, bit by bit, with the low byte of the answer.

Here's what would happen in the 148 x 14 case:
W=18 (00010010)
lo=148 (10010100)
hi=0
rr	lo (?1001010) C=0

no carry
rr	hi &	; lo (0000000 0?100101) C=0

no carry
rr	hi &	; lo (0000000 00?10010) C=1

carry! hi+=W (00010010)
rr	hi &	; lo (00001001 000?1001) C=0
(answer so far is 18*4 or 72)

no carry
rr	hi &	; lo (00000100 1000?100) C=1

carry! hi+=W (00010110)
rr	hi &	; lo (00001011 01000?10) C=0
(answer so far is 18*4+18*16 or 360)

no carry
rr	hi &	; lo (00000101 101000?1) C=0

no carry
rr	hi &	; lo (00000010 1101000?) C=1

carry! hi+=W (00010100)
rr	hi &	; lo (00000001 01101000) C=?
(answer so far is 18*4+18*16+18*128 or 2664)

See how easy that was?

How about division... A little more complicated, but the same basic idea.
Pretty much binary long division.

Think of show 2664 / 18 would work in real life:
___148__
18 ) 2664
-18
--
86
-72
--
144
-144
---
0

Now in binary:
______10010100__
10010 ) 101001101000
-10010
-----
10110
-10010
-----
10010
-10010
-----
0

```

 file: /Techref/scenix/lib/math/mul/tutorial_sx.htm, 2KB, , updated: 2004/6/10 14:40, local time: 2024/7/18 04:14, TOP NEW HELP FIND:  35.172.230.21:LOG IN

 ©2024 These pages are served without commercial sponsorship. (No popup ads, etc...).Bandwidth abuse increases hosting cost forcing sponsorship or shutdown. This server aggressively defends against automated copying for any reason including offline viewing, duplication, etc... Please respect this requirement and DO NOT RIP THIS SITE. Questions?Please DO link to this page! Digg it! / MAKE! SX Microcontroller Math Method Tutorial

After you find an appropriate page, you are invited to your to this massmind site! (posts will be visible only to you before review) Just type a nice message (short messages are blocked as spam) in the box and press the Post button. (HTML welcomed, but not the <A tag: Instead, use the link box to link to another page. A tutorial is available Members can login to post directly, become page editors, and be credited for their posts.

Attn spammers: All posts are reviewed before being made visible to anyone other than the poster.
 Did you find what you needed? "No. I'm looking for: " "No. Take me to the search page." "No. Take me to the top so I can drill down by catagory" "No. I'm willing to pay for help, please refer me to a qualified consultant"

 PICList 2024 contributors: o List host: MIT, Site host massmind.org, Top posters @none found - Page Editors: James Newton, David Cary, and YOU! * Roman Black of Black Robotics donates from sales of Linistep stepper controller kits. * Ashley Roll of Digital Nemesis donates from sales of RCL-1 RS232 to TTL converters. * Monthly Subscribers: Gregg Rew. on-going support is MOST appreciated! * Contributors: Richard Seriani, Sr.

.