How do I do a multiplication and a division in the hack language???

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

How do I do a multiplication and a division in the hack language???

Tathiane
// Computes R0 = 2 + 3
// R0 refers to RAM[0]

@2
D=A     // D = 2

@3
D=D+A   // D = D + 3

@0
M=D     // RAM[0] = D

// I'm only able to do sum and subtraction
Reply | Threaded
Open this post in threaded view
|

Re: How do I do a multiplication and a division in the hack language???

ivant
You cannot directly do multiplication or division, but you can implement them as a program. For example m * n = m + m + ... + m, n times.

For division you can start subtracting the divisor from the dividend until the result is less than zero. The number of times minus 1 you did that subtraction is the quotient. You can easily find the remainder as well.

In later chapter you'll implement better multiplication and division algorithms.