• Commodore Free Magazine, Issue 78 - Part 9

    From Stephen Walsh@39:901/280 to All on Sun Mar 23 17:59:21 2014
    ode
    STA SCREEN+2 ;copy to the next
    screen location

    …and so on until reaching 400 instructions in order to move a mere 200
    bytes of data. Hardly a good return on your investment. The next snippet
    does exactly the same thing but instead uses indexing. We load the X (or
    Y) Register with a value and the processor will understand that the value
    in the index register is an offset from the base address. The only thing
    we have to concern ourselves with is the value of the index. The effective address, which is just computer-speak for the "final" or actual address
    loaded from or stored to, will be = base address + index.

    LDX #0 ;load X Register (the
    index) with the value
    0
    LOOP: ;a label (placemarker)
    LDA 49152,X ;load the A Register
    with the contents of
    address 49152+offset X
    STA SCREEN,X ;some other block of
    memory, perhaps the
    screen
    INX ;increment the index in
    the X Register (add 1
    to the index value)
    CPX #200 ;compare the value in
    the X Register to the
    value 200
    BNE LOOP ;branch if not equal to
    the label LOOP at ins-
    truction LDA 49152,X

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

    Instead of writing 400 instructions requiring 1200 bytes we wrote 6 instructions requiring 13 bytes. This was a simple example for now, and
    the more experienced programmers will have noticed we did not optimize the
    loop in any way (it is often possible to shorten loops even further by
    counting in reverse). In assembly language the number 0 is the starting
    (and often the ending) point for counting and indexing, a concept which can
    be quite confusing at first because humans usually start at 1, but once you
    get used to it you will frequently find yourself counting from 0 for things
    in life outside the computer as well (and it will drive non-programmers batty!). With 8-bit registers you are limited to an indexed range of 256
    bytes but with the 16-bit processing power of the 65816 your indexed range extends to 65536 bytes!

    PROCESSOR STATUS (P)

    The P Register, or Processor Status, or more commonly known as the Status Register, contains a number of flags which describe the status of the microprocessor and its operations. A flag is a single bit within the
    status register. Its value, set (1) or reset, (0), indicates one of two conditions. It is common to use the word "clear" when talking about
    resetting a bit to 0. Both processors have bits for four status register condition code flags - negative, zero, overflow, and carry. These
    condition code flags are used to determine the success or failure of the
    branch on condition instructions, and their values indicate various
    conditions that result from the execution of many instructions. Some instructions affect none of the condition code flags, others affect only
    some, and still others affect all. The effect that an instruction has on
    the condition flags is an important part of describing what the instruction does. It cannot be overstated that in order to master assembly language a programmer must learn how each of the flags work, and which flags are
    affected by each instruction.

    There are two other flags, interrupt disable and decimal, which are known
    as mode control flags. They may be manipulated by the programmer, but are otherwise unaffected by conditions that may be set by instructions.

    FLAGS COMMON TO BOTH THE 6502 AND 65816

    CARRY (C)

    The carry, or C flag, is associated with the arithmetic instructions, and
    is useful in addition, subtraction, shifts and rotates, and comparisons.
    The C flag, as all flags, maintains its value until some condition or instruction forces it to change. Looking at Table 1, one instruction, CPX, sets or resets the C flag each time it is executed based upon the result of
    the comparison. If the value in the X Register is < 200 then the C flag is cleared to 0; if the value in the X Register is >=200 then the carry flag
    is set to 1. We could have just as easily used the instruction BCC (branch
    if carry clear) instead of BNE (branch if not equal) for the conditional
    jump after the CPX instruction.

    ZERO (Z)

    The zero, or Z flag, indicates a zero or non-zero condition, and is usually
    the most heavily used flag. Many instructions modify the value of the Z
    flag, and its meaning is often confusing to beginning programmers. If the result of some operation is = 0 then the Z flag sets to 1. Conversely,
    when the result of an operation != 0 the Z flag clears to 0. Yes, that is
    the confusing part. The Zero flag's job is to signal when the result of
    some operation = 0, and that signal is a 1. The Z flag is actually very
    busy in our loop from Table 1. The instructions LDX, LDA, INX, and CPX all affect the value of the Z flag when they execute, so you can see that the
    value of the flag may change several times even within a small section of
    code.

    INTERRUPT DISABLE (I)

    The interrupt disable, or I flag, is one of two flags known as control
    flags (the other is decimal). Its purpose is for allowing or disallowing hardware and software interrupts, or IRQs. It does not have any effect on non-maskable interrupts, or NMIs. You manually alter the I flag with the
    SEI (set interrupt disable) and CLI (clear interrupt disable) instructions. This flag is also confusing, primarily because many programmers new to
    assembly language forget that the flag is called "interrupt disable" and
    that its value will = 1 when interrupts are disabled.

    DECIMAL (D)

    The decimal, or D flag, is seldom needed in assembly language programming.
    When = 1, the flag forces the processor to carry out arithmetic
    instructions in normal base-10 format, which means in decimal mode
    arithmetic the largest value a byte will contain is 99. All other other instructions are unaffected by the setting of the D flag. You manually
    alter the flag with the SED (set decimal mode) and CLD (clear decimal mode) instructions. Decimal mode is not as easy to use as it could be. The negative, overflow, and zero flags in the P register are not valid in
    decimal mode and the setting of the decimal flag, which toggles the
    processor between binary and decimal math, is unknown after the processor
    has received a hardware reset. The 65816 corrects this last behavior and clears (0) the D flag on reset.

    OVERFLOW (V)

    The overflow, or V flag, is another seldom-used, and consequently, misunderstood P Register component. It is useful when working with signed integers. The V flag is set when there is a carry from bit 6 into bit 7
    and there is no external carry; it is also set when there is no carry from
    bit 6 into bit 7, but there is an external carry. Another use of the V
    flag is by hardware. A good example is the Commodore 1541 disk drive,
    which sets the V flag in the drive's 6502 processor each time a byte is
    read from or written to the disk. You manually alter the flag with the CLV (clear overflow flag) instruction. There is no complementary "set flag" instruction to set the V flag. The 65816 SEP instruction can set the V
    flag; on the 6502, a BIT instruction with a mask in memory that has bit 6
    set can be used to set the overflow flag. We will look at the V flag more closely when we cover arithmetic.

    NEGATIVE (N)

    The negative, or N flag, is fairly straightforward. It reflects the state
    of the most significant bit in a transfer operation (bit 7 on the 6502 or
    bit 15 on the 65816 in 16-bit mode). The N flag is used with signed arithmetic, and is also frequently used in loops to check for a wraparound
    past 0.

    THE 6502 P REGISTER

    This 8-bit register could provide eight flags, but only seven of them are
    used. A mode control flag, break, is used by the 6502 but not by the
    65816.

    6502 Status Register Bit-fields

    Bit 7 6 5 4 3 2 1 0
    N V - B D I Z C

    BREAK FLAG (B)

    The break, or B flag, is used only for interrupt pro

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