Fill problem

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

Fill problem

bryan5
When I test the code in the CPUEmulator, the screen is always black.
I check my pseudo-code, I can't figure it out.
Plz help.
Here is my code.

@24576 // m = 24576
        D=A
        @m
        M=D
       

        @16384 // arr = 16384
        D=A
        @arr
        M=D

        @8192 // n = 8192
        D=A
        @n
        M=D
(DECISION) // if (m = 0) goto WHITE
                // if (m > 0) goto BLACK
        @i
        M=0

        @m
        D=M

        @WHITE
        D;JEQ

        @BLACK
        D;JGT

(BLACK) // RAM[arr+i] = -1
        @i
        D=M
        @n
        D=D-M
        @DECISION
        D;JEQ
       
        @arr
        D=M
        @i
        A=D+M
        M=-1

        @i
        M=M+1

        @BLACK
        0;JMP

(WHITE) // RAM[arr+i] = 0
        @i
        D=M
        @n
        D=D-M
        @DECISION
        D;JEQ
       
        @arr
        D=M
        @i
        A=D+M
        M=0

        @i
        M=M+1

        @WHITE
        0;JMP

(END)
        @END
        0;JMP
Reply | Threaded
Open this post in threaded view
|

Re: Fill problem

WBahn
Administrator
bryan5 wrote
When I test the code in the CPUEmulator, the screen is always black.

@24576 // m = 24576
        D=A
        @m
        M=D
       
So here you have set the variable 'm' to the value 24576

        @m
        D=M

        @WHITE
        D;JEQ

        @BLACK
        D;JGT
And here you jump to BLACK if the value stored in the variable 'm' is greater than zero.

It is doing EXACTLY what your pseudocode says:

(DECISION) // if (m = 0) goto WHITE
                // if (m > 0) goto BLACK
So start by asking yourself why you want to jump to black if the value stored in the variable 'm' is greater than zero.
Reply | Threaded
Open this post in threaded view
|

Re: Fill problem

clinden
In reply to this post by bryan5
Hi,

I am not sure if you still read it. WBahn has given you the answer that could lead you to the solution.

But there is another hint from me for this.

You can use the KBD symbol, that directly points to the keyboard input address. It never changes, so no need to put it into a variable.

If you want to read from an adress that is stored in a variable, you need to do the following:

@var //set A to the variable address
A=M // read the address that is stored inside the variable and put it to A
D=M  //read whatever is in the Address stored in var into D.

But that is just very complicated if you just want to read the KBD address :-)