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
can anyone help me with this
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
it was similar to the mult.asm.   but i am looking something like this for the division.

// This file is part of www.nand2tetris.org
// and the book "The Elements of Computing Systems"
// by Nisan and Schocken, MIT Press.
// File name: projects/04/Mult.asm

// Multiplies R0 and R1 and stores the result in R2.
// (R0, R1, R2 refer to RAM[0], RAM[1], and RAM[2], respectively.)

        @sum    
        M=0     // Initialize sum to 0
        @R1
        D=M
        @count  
        M=D     // Initialize count to the value at RAM[1]
(LOOP)  
        @count
        D=M
        @END
        D;JEQ   // Check if count is 0, if so, goto END
        @R0
        D=M
        @sum
        M=M+D   // Add RAM[0] to sum
        @count
        M=M-1   // Decrement count by 1
        @LOOP
        0;JMP   // Loop again
(END)  
        @sum
        D=M
        @R2
        M=D     // Write sum to RAM[2]
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
I am sorry I didn't get you. can you please show me how
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
can anyone help me with this
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