Mult.asm: not entering value into R2

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

Mult.asm: not entering value into R2

nblackburn
When testing Mult.asm, I get a comparison error on line 5 because R2 always remains 0. Not sure what I am doing wrong, but I am assuming it is a syntax problem. The idea is to loop for the range of R1, add R1 to the current value of R2, update the counter until the counter is > R0 and then when the counter is > R0 terminate the program by entering the infinite loop (like in the book and other examples).

// Pseudocode
// for i in R0
    // if (counter > R0)
        // Terminate program
    // R2 = R2 + R1
    // counter = counter + 1
    // goto beginning of for loop

    // Initalise counter, R0, R1, R2 to 0
    @counter
    M=0

    @R0
    M=0

    @R1
    M=0

    @R2
    M=0

(LOOP)

    // if (counter > R0) goto TERMINATE
    @counter
    D=M
    @R0
    D=D-M                                   // Difference between counter and R0; counter - R0
    @TERMINATE
    D;JGT

    // R2 = R2 + R1
    @R2
    D=M
    @R1
    D=D+M
    @R2
    M=D

    // counter++
    @counter
    M=M+1

    // goto LOOP
    @LOOP
    D;JLT


(TERMINATE)

    @TERMINATE
    0;JMP
Reply | Threaded
Open this post in threaded view
|

Re: Mult.asm: not entering value into R2

dolomiti7
The main issue is that you are overwriting the parameters R0 and R1. Therefore your code always multiplies 0 with 0. You should only initialize R2 and your counter.

Additionally the conditional jump at the end of your loop looks a bit odd. I presume from your comment that this is intended to be an unconditional jump to the beginning of the loop?:
// goto LOOP
    @LOOP
    D;JLT // ???

Reply | Threaded
Open this post in threaded view
|

Re: Mult.asm: not entering value into R2

nblackburn
Yes the purpose is to repeatedly jump back to the beginning of the loop and then jump to the terminate loop when the difference between the counter and R0 is 0 meaning the multiplication should be complete. Originally I did have the same logic as the jump to the TERMINATE label but thought it would be easier to just unconditionally loop back to the LOOP.
Reply | Threaded
Open this post in threaded view
|

Re: Mult.asm: not entering value into R2

dolomiti7
Ok, then change it to an unconditional jump in your source code. However, as mentioned your main issue is that the program is supposed to multiply the input parameters in R0 and R1 (whatever is in these registers/memory addresses when the program is started). Your source code is overwriting the values given in these 2 registers during your initialization.
Reply | Threaded
Open this post in threaded view
|

Re: Mult.asm: not entering value into R2

nblackburn
Thanks for your help. I have solved it.