Atari Assembler Course ---------------------------------- 2.1 Accumulator and Arithmetic 2.1.1 The "PEEK & POKE" of Assembly Language ,-----. | LDA | -- Load Acc. from Memory '-----' M -> A The LDA instruction transfers data from memory and stores it in the accumulator. This may be thought of as "PEEKing" a memory location using BASIC. Depending upon the data value, this command may effect the zero and negative flags. The LDA instruction may be used with all major "addressing modes". (They will be fully discussed later in the course.) For now we will use two of these modes: "immediate" and "absolute". In the immediate mode, the VALUE of the operand will be loaded into the accumulator. This mode is symbolized by a pound sign before the operand: LDA #42 ;Load a decimal number ;into the accumulator. LDA #$2A ;Load a hex number. In the absolute addressing mode, the operand is the memory location (2 bytes) where the data will be found: LDA 1536 ;Get the value at ;address decimal 1536. LDA $0600 ;Get the value at ;address hex 600. ,-----. | LDX | -- Load X-Reg. from Memory '-----' M -> X ,-----. | LDY | -- Load Y-Reg. from Memory '-----' M -> Y The LDX and LDY instructions are similar to the LDA but use the X and Y registers, respectively. LDX #214 ;Load the X-register ;with the number 214. LDY $02E4 ;Put the value from ;location hex 2E4 in the Y-reg. ,-----. | STA | -- Store Acc. in Memory '-----' A -> M This instruction transfers the contents of the accumulator to memory and changes none of the status flags. Also, the value in the accumulator is unchanged after the operation. The STA instruction uses all the major addressing modes. In the absolute mode, the accumulator is stored at the address given in the operand: STA $22A7 ;Save the accumulator ;at location hex 22A7. STA DATA ;In symbolic notation, ;the address 'DATA' ;must be given a value. ,-----. | STX | -- Store X-Reg. in Memory '-----' X -> M ,-----. | STY | -- Store Y-Reg. in Memory '-----' Y -> M The STX and STY instructions save the value of the X and Y index registers in memory. STX KEEPX ;Save the X-register ;at address 'KEEPX'. STY 1000 ;Store the X value at ;location decimal 1000. 2.1.2 Register Transfers The ability to exchange the contents of the registers proves to be very useful in assembly programming. The following commands transfer data between the accumulator and the index registers. ,-----. | TAX | -- Transfer Acc. to X-Reg. '-----' A -> X ,-----. | TAY | -- Transfer Acc. to Y-Reg. '-----' A -> Y These two instructions transfer the value in the accumulator to the corresponding index register. The accumulator retains its value but the negative and zero status flags are effected. This instruction requires no operand (implied addressing). ,-----. | TXA | -- Transfer X-Reg. to Acc. '-----' X -> A ,-----. | TXY | -- Transfer Y-Reg. to Acc. '-----' Y -> A The TXA and TYA instructions transfer the index registers to the accumulator and effect the negative and zero flags. Again, the number remains in the originating register and no operand is needed. LDA #A9 ;Put the number hex A9 ;into the accumulator. TAX ;Move it to the X-reg. LDY TEMP ;Load the Y-register ;with the number at 'TEMP'. TYA ;and transfer it to the ;accumulator. 2.1.3 Addition and Subtraction The arithmetic functions are used in many machine language programs. Addition and subtraction are the only two math operations that the 6502 can execute; all other computations are derived from them. Because all operations are done one-byte at a time, larger numbers will require multiple operations using a "carry" for adding or a "borrow" for subtracting. ,-----. | ADC | -- Add Memory to Acc. '-----' with Carry A+M+C -> A The ADC instruction adds a value in memory to the accumulator. The value of the carry bit (1 or 0) is also added in. The result is stored in the accumulator and the following flags are effected: the carry bit is set if the sum exceeds 255 for a binary add or 99 for a decimal add; the negative flag is set if the seventh bit of the accumulator is on; the overflow flag is set if bit 7 was changed due to the sum exceeding +127 or -128; the zero flag is set if the result is zero. This instruction, like the LDA instruction, handles all of the major addressing modes. Also, the addition may be either binary or binary-coded-decimal and is chosen by the following one-byte commands: ,-----. | CLD | -- Clear Decimal Mode '-----' (return to binary mode) 0 -> D ,-----. | SED | -- Set Decimal Mode '-----' 1 -> D Normally, the binary mode is assumed and all operations are done with binary numbers. The decimal mode is used in special cases and should be cleared after all decimal operations. Also, the carry flag should be cleared before an addition routine to disregard a previous carry. ,-----. | CLC | -- Clear Carry Flag '-----' 0 -> C LDA #$4A ;Add the number hex 4A CLC ;(without a carry) ADC $2C00 ;to the value in memory ;location hex 2C00 STA $2C00 ;and put the sum back ;into memory. Adding two 2-byte numbers: LDA LOW1 ;Load lower order byte ;of first number. CLC ;Clear the Carry flag. ADC LOW2 ;Add LOW1 to the lower ;byte of 2nd number. STA LOW3 ;Store low half of the ;sum at LOW3. LDA HIGH1 ;Get the higher-order ;byte of 1st number. ADC HIGH2 ;Add to the high byte ;of 2nd number plus the ;carry from the low sum. STA HIGH3 ;Keep the high half of ;the sum at HIGH3. ,-----. | SBC | -- Subtract Memory from '-----' Acc. with Borrow. A-M-(1-C) -> A The SBC instruction subtracts a value in memory from the accumulator and takes away a borrow left from the previous operation. The borrow is actually the lack of a carry! Therfore, the carry must be set before a subtraction operation. This command has the same addressing modes and flag changes as the ADC instruction. If the result of the subtraction is less than zero, the carry flag is cleared to signify a borrow. ,-----. | SEC | -- Set Carry Flag '-----' (Clear the Borrow) 1 -> C LDA TIME ;Decrease the value in SEC ;memory location 'TIME' SBC #60 ;by the number 60. STA TIME LDA LOW1 ;Subtract two 16-bit SEC ;numbers saved at SBC LOW2 ;HIGH1,LOW1 STA LOW3 ;and HIGH2,LOW2 LDA HIGH1 ;And save the results SBC HIGH2 ;at HIGH3,LOW3. STA HIGH3 2.1.4 Increment and Decrement The Increment and Decrement instructions add or subtract one from a value in memory or an index register. These instructions are very valuable when used inside loops. The only way to DEC or INC directly with the accumulator is to use the ADC or SBC. ,-----. | INC | -- Increment Memory by One '-----' M+1 -> M ,-----. | DEC | -- Decrement Memory by One '-----' M-1 -> M The INC instruction adds 1 to the contents of a memory location and the DEC instruction subtracts one from memory. These commands operate directly on memory without effecting the contents of the registers. Only the zero and negative flags are effected by the new value in memory. LDA #10 INC COUNT ;Add one to the value ;at Location 'COUNT'. ;The accumulator still equals 10. ,-----. | INX | -- Increment X-Reg. by One '-----' X+1 -> X ,-----. | DEX | -- Decrement X-Reg. by One '-----' X-1 -> X ,-----. | INY | -- Increment Y-Reg. by One '-----' Y+1 -> Y ,-----. | DEY | -- Decrement Y-Reg. by One '-----' Y-1 -> Y These instructions have the same effect as the INC and DEC commands but work directly on the index registers. We will see how these one-byte commands are very useful in controlling loops. LDA #$FF DEX ;Subtract one from X-reg. INY ;Add one to Y-Register. ;The is unchanged. 2.1.5 Logical Operations The binary logic operations are used to test individual bits for decision making purposes. Also, they can change certain bits of a byte without effecting the others. ,-----. | AND | -- "AND" Memory with Acc. '-----' A & M -> A The logical "AND" operation compares each bit in memory with the corresponding bit in the accumulator and stores the results back in the accumulator. If both bits are a "1", the answer will be a "1"; any other combination gives a zero for that bit. The negative and zero flags are effected by the results. The following example shows how one bit in memory is tested to make a decision: LEFT=4 ;The number LEFT is a LDA #LEFT ;"mask" for bit 3. ;(4=0000 0100 binary) AND STICK0 ;Check if the stick ;is in that position. BEQ LEFTP0 ;...if it is, then go ;to the instruction ;labeled 'LEFTP0'. We will discuss branch instructions in the session 3. ,-----. | ORA | -- "OR" Memory with Acc. '-----' A or M -> A The binary "OR" instruction returns a zero to the bits of the accumulator if both corresponding bits are "0"; any other combination gives a "1". Again, this instruction effects the negative and zero flags. This example sets a single bit in memory without changing the bits others in that location: LDA #1 ;Make only the desired ; bit a one... ORA CNTRB ;Set bit 1 in memory ;at location 'CNTRB' STA CNTRB ;and store the result ;back there. ,-----. | EOR | -- "Exclusive-OR" Memory '-----' with Accumulator A V M -> A The "Exclusive-Or" function compares each bit in memory with the accumulator, returns a "1" if they are different (0 and 1), or returns a "0" if they are the same (1-1 or 0-0). This instruction can be used to complement a number by comparing with binary 1111 1111. LDA DATA ;Get the number stored ;at location 'DATA'. EOR #$FF ;Complement it. ;($FF= binary 1111 1111.) STA DATA ;And put it back. 2.2 Assembly Language Format Please review the details invloved in entering assembly language lines of code. Unlike BASIC, Assembly programs have distinct "fields" on a line that are reserved for different aspects of a line of instruction. Read some programs to get used to the format! Read: ATARI ASM/ED Manual, p. 8-10 And other references covering this. 2.3 ATARI ASM/ED Editor Commands Become familiar with the Editor. The NUM command gives line numbers automatically when entering code. The REN will renumber the program lines. Note that the line numbers are needed only for editor purposes and are not used in the program as in BASIC. The LIST and ENTER commands are used in the Editor to store and retrieve the source code. The file is not an executable program but needs to be converted to the actual machine code. The ASM or assemble command does this for you. By default, it takes your source code in memory, assembles it into memory and sends a listing to the screen. There are many variations of this command to allow assembling to and from disk with the listing sent to your printer,etc. READ: ATARI ASM/ED Manual Ch.3 and p. 25-26.