How do I do Multiplication in Mult.asm?

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

How do I do Multiplication in Mult.asm?

BLOB
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?
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

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

cadet1620
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
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

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

BLOB
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) ?



Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

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

ybakos
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.

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

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

BLOB
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 :(
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

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

BLOB
I figured it out..
I thought I could change the address value of the the label but I was wrong.. "WE CANNOT CHANGE THE ADDRESS VALUE OF THE LABEL, WE CAN ONLY ACCESS IT "

I got my fill.asm working but the CPU emulator is getting stuck once in a while when it comes to a certain instruction. I need to pause the simulation, press the ">" button to unstuck it.. Why is it getting stuck? Is it a common problem someone faces or does it indicate a bug in my program?
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

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

cadet1620
Administrator
Some people have seen problems that appear to be associated with auto-repeating and action keys like enter.  Try using a key like 'x'.

If you're still having problems with it hanging and you think that it may be a problem with your program, I'll be happy to look at it if you email it to me.  (more>reply to author)

--Mark
Loading...