• Commodore Free Magazine, Issue 79 - Part 12

    From Stephen Walsh@39:901/280 to All on Tue Apr 15 19:49:03 2014
    nt bit has a value a multiple of two higher (double) than the
    bit lower before it, has half the value of the bit following, and so on for
    all 8 bits, but a bit itself knows only 0 or 1. The processor assigns a
    value to each bit. 8-bit data has a value range limit of 0-255, or 2^8
    bytes, while 16-bit data ranges 0-65535, or 2^16 bytes. Individual bit
    values, numbered from bit 0 through bit 7, are 1, 2, 4, 8, 16, 32, 64, and
    128, respectively. For multi-byte numbers, bits 8-15 have values of 256,
    512, 1024, 2048, 4096, 8192, 16384, and 32768. 2^0 to 2^15. These
    bit-values are assigned by the processor, so knowing them well will help
    you in countless ways as you develop more complex code in the future.

    REPRESENTING BINARY NUMBERS IN SOURCE CODE

    Just as in base 10, when all the digits to the left of a non-zero digit are
    0, you may omit the "0" digits to the left when representing a binary
    number in source code. It can be much easier to read small numbers without
    all the leading 0s sometimes, although I generally leave 0s in when
    referring to a variable or location for which some or all bits are
    important - I like to see the bits for reference while programming.

    For example, the value %00000001 could be written as %1 if the programmer
    was certain none of the other seven bits were going to be referenced in
    some way elsewhere in the program, because simply storing a value of %1
    (which is really %00000001) into the byte might cause a disastrous result
    (by clearing the other seven bits) if some part of the program needs other
    bits in the byte.

    - - - - - - - - - - - - - - - - - - -

    The Composition of a Two-Byte Word (lower 16-bits)

    Bit Value Binary Hex
    7 128 %0000 0000 1000 0000 $0080
    6 64 %0000 0000 0100 0000 $0040
    5 32 %0000 0000 0010 0000 $0020
    4 16 %0000 0000 0001 0000 $0010
    3 8 %0000 0000 0000 1000 $0008
    2 4 %0000 0000 0000 0100 $0004
    1 2 %0000 0000 0000 0010 $0002
    0 1 %0000 0000 0000 0001 $0001

    The Composition of a Two-Byte Word (upper 16-bits)

    Bit Value Binary Hex
    15 32768 %1000 0000 0000 0000 $8000
    14 16384 %0100 0000 0000 0000 $4000
    13 8192 %0010 0000 0000 0000 $2000
    12 4096 %0001 0000 0000 0000 $1000
    11 2048 %0000 1000 0000 0000 $0800
    10 1024 %0000 0100 0000 0000 $0400
    9 512 %0000 0010 0000 0000 $0200
    8 256 %0000 0001 0000 0000 $0100

    Table 1. A word is just two adjacent bytes, with the lower byte addressed
    in the lower memory position.

    - - - - - - - - - - - - - - - - - - -

    ABOUT TABLE 1

    Traditional Commodore assembler syntax has "%" before the number, as in %10000000. In the alternative, some assemblers append a "b" to the binary number (no %). I split the nybbles of 16-bit binary numbers just for demonstration, but many assemblers dis-allow spaces to represent a binary number. Notice how the base 2 (binary) number is written just like a base
    10 number, as is hexadecimal, which is most significant digit first.
    (That's for us to understand it easier. In memory the lowest significant
    byte is always placed first.)

    THE NYBBLE

    We went right on by the nybble! Well, sort of. A nybble is conceptual.
    It exists as a convenient 4-bit grouping of bits that can represent any
    value with a range of (0-15). Four adjacent bits form a nybble, so there
    two nybbles in each byte, a lower nybble and a higher nybble. Lower
    nybbles span bits 0-3 while upper nybbles span bits 4-7, so there are two nybbles per byte. Nybbles are convenient especially because they can accommodate numbers with a range of (0-15). A familiar use is the system
    used by the C64's color memory. A word consists of four nybbles, two in
    each byte, but the methods for accessing specific bits in each nybble are
    the same.

    HEXADECIMAL NUMBERS

    We managed to evade the use of hexadecimal numbers until now, and in a way, that's a good thing. Hexadecimal numbers, like nybbles, are conceptual in nature. They are both also closely related. Hexadecimal numbering is the convention used to represent the 16 members of a nybble. It's actually
    quite simple. The first 10 members are given their decimal equivalents (starting at 0), so they become 0-9. The remaining 6 members (10-15) are assigned the letters A-F. The number 13 (decimal) is equivalent to binary number %1101 -> (1*2^3) + (1*2^2) + (0*2^1) + (1*2^0), so in hexadecimal it becomes $D. We use the "$" or "0x" prefix to represent hexadecimal
    numbers, which themselves represent 4-bit values.

    A MULTI-BYTE NUMBER DISPLAYED IN DIFFERENT FORMATS

    2014 - Decimal

    %11111011110 or %0000011111011110 -
    Binary

    $7DE or $07DE - Hexadecimal

    (0*2^15)+(0*2^14)+(0*2^13)+(0*2^12)+(
    1*2^11)+(1*2^10)+(1*2^9)+(1*2^8)+(1*2
    ^7)+(0*2^6)+(0*2^5)+(1*2^4)+(1*2^3)+(
    1*2^2)+(1*2^1)+(0*2^0)

    IN CONCLUSION

    Today was about binary numbers, with sections on bits, bytes, words,
    nybbles, and hexadecimal numbers. Binary numbers are what programming is
    all about, and further reading on binary and hexadecimal number systems may
    be necessary in order to get a better understanding. Unquestionably, to succeed in assembly language programming one must first thoroughly
    understand binary numbers.

    I look forward to next time when we will discuss the 6502 "Load" and
    "Store" instructions, as well as (hopefully) introducing the Native mode of
    the 65816. So, until our next meeting, take 'er easy.

    Please send errors, omissions, or suggestions to bert@winc64.com or on
    Lemon64, username satpro.




    --- MBSE BBS v1.0.01 (GNU/Linux-i386)
    * Origin: Dragon's Lair ---:- bbs.vk3heg.net -:--- (39:901/280)