Divide by 2 (fills.asm)

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

Divide by 2 (fills.asm)

Saroupille
Hi,
I'm wondering how can divde a number by 2 ? I need of this operation to clean the screen. Indeed if in some address  the number 0000000000000011 is stored, then I need to have the number 0000000000000001.
I thought to store all the different numbers possible (1,3,7,15,31, etc...) but i need a counter to select the good register. So what is the simplest way to do this ?
Thanks.
Reply | Threaded
Open this post in threaded view
|

Re: Divide by 2 (fills.asm)

cadet1620
Administrator
Saroupille wrote
Hi,
I'm wondering how can divde a number by 2 ? I need of this operation to clean the screen. Indeed if in some address  the number 0000000000000011 is stored, then I need to have the number 0000000000000001.
I thought to store all the different numbers possible (1,3,7,15,31, etc...) but i need a counter to select the good register. So what is the simplest way to do this ?
Thanks.
Division is a hard problem on the Hack processor. As part of chapter 12 you will write an integer division function.

For the Fill.asm program, try to think of an algorithm that does not need division. Hint: you don't need to write to each pixel individually; write them 16 pixels at a time.

If you truly need division by 2, and your input values are limited to small values, the easiest way would be an algorithm something like:
    // compute y = x/2, unsigned
    x = 0
    y = y AND NOT 1    // clears bit 0
    while (x < y) {
        x = x + 1
        y = y - 2
        }

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

Re: Divide by 2 (fills.asm)

Saroupille
Thanks for the answer. So I don't have to store any numbers, or to divide by 2 ? It's comforting. I hop I will find the good method.
Reply | Threaded
Open this post in threaded view
|

Re: Divide by 2 (fills.asm)

cadet1620
Administrator
If you get stuck and want to discuss the Fill.asm program in more detail, become a registered member of the forum and you can then send me mail directly using "Reply to author" which will appear on the "More" dropdown list.

--Mark