Struggling to understand how to make Fill.asm

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

Struggling to understand how to make Fill.asm

nblackburn
I am struggling to understand how to implement Fill.asm. I can understand what needs to happen; when any key is pressed set the whole screen to black, else set the whole screen to white.

This is the first draft of my code:

    @counter
    M=0

(LOOP)

    @KBD
    D=M
    // If key is pressed
    @FILL_SCREEN
    D;JGT
    // If no key is pressed
    @CLEAR_SCREEN
    D;JEQ

(FILL_SCREEN)

    // *(SCREEN + counter) = -1
    @SCREEN
    D=M
    @counter
    A=D+M
    M=-1

(CLEAR_SCREEN)

    // *(SCREEN + counter) = 0
    @SCREEN
    A=M+1
    M=0

    // TODO: find some way to limit the accessible ram
    @LOOP
    0;JMP

My current thinking is to evaluate if KBD is > 0 then a key has been pressed, then loop to FILL_SCREEN and increment a pointer to the adresses starting from SCREEN else if KBD == 0 then loop to CLEAR_SCREEN and increment a pointer to the adresses starting from SCREEN.

What I think I need to do now is to find some way to prevent the program from accessing illegal memory.

Is my thinking correct and am I on the right path?
Reply | Threaded
Open this post in threaded view
|

Re: Struggling to understand how to make Fill.asm

WBahn
Administrator
There are multiple ways to solve this problem and the project specifications are intentionally vague on implying the mechanism to be used.

The one you described should work fine, but your verbal description doesn't match your code very well.

Let's say that your program decides that a key is pressed. So it jumps to FILL_SCREEN. Then what happens?

It turns a set of 16 pixels at (SCREEN + counter) to black. (where counter is equal to 0, so it's actually setting the 16 pixels at (SCREEN) to black).

Then what happens?

It immediate drops through to the CLEAR_SCREEN code!

At this point, it turns the 16 pixels located at (SCREEN + 1) white.

This it goes up and checks the keyboard again.

With this code, you never have to worry about walking out of the SCREEN memory region because on each pass through the loop you always set (SCREEN+1) to white and sometimes set (SCREEN) to black.
 
Here's a suggestion -- tackle the project in pieces by simplifying the assignment and then slowing adding in pieces.

First one -- write a program that does nothing more than turn the entire screen black and then stops.

Once that is working, expand that program so that it first turns the entire screen black, and then turns the entire screen white, and then stops.

Next, have it run continuously, turning the screen black, then white, then black, and so on.

Now add some code so that if you initialize a particular variable to 0 it skips the part that makes it black and only executes the part that makes it while, while if that variable is anything other than 0, it does the opposite.

Finally, add some code to that, after finishing making the screen white or black, it checks the keyboard and sets that variable to zero or nonzero accordingly.

This should give you a program that meets the project requirements, but it is far from the most efficient or eye-pleasing (neither goal, of which, was part of the project specifications). But you now have something that you can try to whittle away at to attain both of those goals, and learning quite a bit in the process.
Reply | Threaded
Open this post in threaded view
|

Re: Struggling to understand how to make Fill.asm

nblackburn
Fantastic, thank you for the response.