# How do I do Multiplication in Mult.asm? Classic List Threaded 7 messages Open this post in threaded view
|

## How do I do Multiplication in Mult.asm?

 How do i write multiplication program? I have one technique to do multiplication of binary but don't know how to implement it... say we need to get product of 1010 and 110 = 1010 x 110 = 1010 x (100 + 10) = 101000 + 10100 = 111100  ( addition using ALU function ) I don't realize how do I shift the value of binary bits as in above example where 1010 was shifted to 101000 (when multiplied by 100) How do I get this shifting effect using the hack language?
Open this post in threaded view
|

## Re: How do I do Multiplication in Mult.asm?

 Administrator BLOB wrote How do i write multiplication program? I have one technique to do multiplication of binary but don't know how to implement it... say we need to get product of 1010 and 110 = 1010 x 110 = 1010 x (100 + 10) = 101000 + 10100 = 111100  ( addition using ALU function ) I don't realize how do I shift the value of binary bits as in above example where 1010 was shifted to 101000 (when multiplied by 100) How do I get this shifting effect using the hack language? The easiest way to write mult.asm is to write a loop the does repeated addition. The idea in chapter 4 is just to learn the assembly language.  In chapter 12 (OS) you will write a multiply that can handle larger numbers. On the Hack processor it is easy to left shift by doubling:  A=D; D=D+A; There is no easy way to right shift. You need to test the top bit, double, add 1 if the top bit was set; repeat 14 more times.  This does a 15 bit left rotation. --Mark
Open this post in threaded view
|

## Re: How do I do Multiplication in Mult.asm?

 Ok. I will do multiplication by repeated addition. I got some questions: 1) I wrote this code... @i M = 2  // it gave me an error at this point saying expression expected There was no error when I changed it to M = 1 Why is this happening? 2) When I use the code @5, a binary equivalent of 5 (in 16 bits)  which is equal to 0000000000000101 is loaded right? This is stored in the A register right? so, what will the M register contain when A register points to no address (but has a value instead) ?
Open this post in threaded view
|

## Re: How do I do Multiplication in Mult.asm?

 BLOB wrote 1) I wrote this code... @i M = 2  // it gave me an error at this point saying expression expected There was no error when I changed it to M = 1 Why is this happening? The only valid constants in hack assembly are 0, 1 and -1 and these are within the scope of C instructions. All other values must use an A instruction. If you want to use the value 2, you must first @2. BLOB wrote When I use the code @5, a binary equivalent of 5 (in 16 bits)  which is equal to 0000000000000101 is loaded right? This is stored in the A register right? Right and right. BLOB wrote  so, what will the M register contain when A register points to no address (but has a value instead) ? The instruction set does not inherently distinguish whether the value in the A register is meant to be used as some constant value or an address. As such, when A contains the value 5, then M references memory address 5. Try to re-read the chapter on Hack assembly, it's worth it.
Open this post in threaded view
|

## Re: How do I do Multiplication in Mult.asm?

 Thanx Mark and ybakos.. you cleared it up for me...I finally managed to do Mult.asm More Questions arise... When I use this command  @SCREEN, the assembler automatically converts it to @16384 which is equal to 0100000000000000 (in binary) will be loaded in register A right? so, I did this @SCREEN D = A What will happen when I load another label after the previous step say @SCREEN2 and assign the value of D to A like this:   A = D When I assign the value of D (which is equal to 0100000000000000) to A, the address of the label SCREEN2 will be changed to the same right? Since the value of address of SCREEN2 is now changed, if I have to use the @ SCREEN2 command again at some other point of the program, the address of SCREEN2 will now be same as that of SCREEN right? Confused :(