we have a mult.asm. Can anyone do it for division.asm

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

we have a mult.asm. Can anyone do it for division.asm

david
This post was updated on .
CONTENTS DELETED
The author has deleted this message.
Reply | Threaded
Open this post in threaded view
|

Re: we have a mult.asm. Can anyone do it for division.asm

david
CONTENTS DELETED
The author has deleted this message.
Reply | Threaded
Open this post in threaded view
|

Re: we have a mult.asm. Can anyone do it for division.asm

cadet1620
Administrator
What type of algorithm are you expected to write? The division equivalent of the project 4 Mult.asm would be simple repeated subtraction.
quotient = 0
while dividend >= divisor
    dividend = dividend - divisor
    quotient  = quotient  + 1
This will be really slow for large dividend / small divisor combinations.

Do you have a .tst file that shows what range of values you need to divide?

--Mark
Reply | Threaded
Open this post in threaded view
|

Re: we have a mult.asm. Can anyone do it for division.asm

david
CONTENTS DELETED
The author has deleted this message.
Reply | Threaded
Open this post in threaded view
|

Re: we have a mult.asm. Can anyone do it for division.asm

cadet1620
Administrator
That sounds like you can use the algorithm I posted above.  The code will
be very similar except that you are incrementing R2 and subtracting R1 from
R0.

--Mark
Reply | Threaded
Open this post in threaded view
|

Re: we have a mult.asm. Can anyone do it for division.asm

david
CONTENTS DELETED
The author has deleted this message.
Reply | Threaded
Open this post in threaded view
|

Re: we have a mult.asm. Can anyone do it for division.asm

cadet1620
Administrator
You must write your own ASM code.  Use the pseudocode I posted above.  Here's a concrete example for 13 / 4 = 3:
dividend divisor quotient
   13   >=  4        0
   -4               +1
    9   >=  4        2
    -4              +1
    5   >=  4        3
    -4              +1
     1  <   4        4
       stop
The final dividend value is the remainder, if it's needed.

--Mark
Reply | Threaded
Open this post in threaded view
|

Re: we have a mult.asm. Can anyone do it for division.asm

david
In reply to this post by david
CONTENTS DELETED
The author has deleted this message.
Reply | Threaded
Open this post in threaded view
|

Re: we have a mult.asm. Can anyone do it for division.asm

david
This post was updated on .
CONTENTS DELETED
The author has deleted this message.
Reply | Threaded
Open this post in threaded view
|

Re: we have a mult.asm. Can anyone do it for division.asm

cadet1620
Administrator
Labels are case sensitive; you have @Count and @count which are two different variables.

R0>=R1
R0=R0-R1

These are not Hack instructions.  You need to do comparisons by subtracting and using conditional jumps.  What you want is something like.

@R0
D-M
@R1
D=D-M
@END
D;JLT


I highly recommend that you start by writing the pseudocode as comments and then fill in the ASM that does the pseudocode.
// quotient = 0

    @R2 // quotient
    M=0

// while dividend >= divisor {

(LOOP)
    @R0 // dividend
    D=M
    @R1 // divisor
    D=D-M
    @END
    D;JLT

//     dividend = dividend - divisor

    more
    code
    here

//     quotient  = quotient  + 1

    more
    code
    here

// }

(END)
    @END
    0;JMP

--Mark