Explain a part of the code c64 6502 Assembly

139 Views Asked by At

try to understand a code In the following website is a code a scrollers with wave-like movements. In general, I also understand the code very well. What I don't quite understand IS why he uses Ypos and Yposh the way he uses them. So why, bits 0 -2 were masked with the 7 and stored in Ypos? And why are the values ​​then shifted 3 to the right and stored in Yposh?

This code you can see in this Website: https://codebase64.org/doku.php?id=magazines:chacking6#the_demo_cornerdycp_-_horizontal_scrolling

Thanks for Your answers.

Understanding the code

1

There are 1 best solutions below

1
On

Our "modern" computer systems are byte addressable.  That means that the smallest item we can address to access memory is 8 bits, and each successive memory address holds a different 8 bit byte.

When we want to address something larger or smaller than 8 bits we have to do some work.

As an example of composing bytes to make a large item, a 16-bit integer takes 2 bytes of memory, and to access an element of a 16-bit integer array, as in a[i], we have to do address arithmetic that scales i by the size of the integer, in bytes, so a+i*2, since each 2-byte integer takes two byte addresses.  (We refer to multi-byte items by just one of its addresses, namely the lowest/smallest address, but since a multi-byte item takes multiple byte addresses, the next multi-byte item in an array needs to skip over all the addresses of the current item.)

To address something smaller than a single byte, as in a bit for a bit-mapped display, then we also need to do some work.  First, we'll invent a notion of bit address.  The bit address is a number that represents a single bit of memory.  Like byte addresses and byte offsets, the bit address or bit offset is just a number, and unsigned integer.  Each successive bit address refers to the next bit of memory.  Just like byte addresses and byte offsets, we can do arithmetic on bit addresses and bit offsets, indexing computations and such, e.g. for a 2-D display.

Of course, the hardware doesn't support bit addressing, only byte addressing — so to read a single bit, we need to read the entire byte that contains the bit, and to write a single bit, we need to write the entire byte that contains the bit. 

(Writing is a bit more complex, because usually when we write a single bit we don't want to disturb the other nearby bits, so writing a single bit is really logically a read byte (that contains the bit of interest), modify that byte, and then write it back.  How we accomplish the read-modify-write depends on the CPU's instruction set.)

A bit address or bit offset differs from byte address or byte offsets by a factor of 8 (because bytes hold 8 bits)!  Thus, the byte containing the bit at a given byte address has an address that is the bit address divided by 8, and, given that one byte, the bit number within that byte is the bit address modulus 8, a number from 0 to 7.

Division by 8 and modulus by 8 are easy for binary hardware to do: divide by 8 is shift right 3 bit positions, e.g. in C, b >> 3, and modulus by 8 is masking keeping only the lower 3 bits, e.g. in C as b & 7.