Arithmetic And Logical Operations

Most arithmetic and logical operations (addition, subtraction, AND, OR, EXCLUSIVE OR, and comparison) can be performed only between the accumulator and an 8-bit register, a byte of immediate data, or a byte of data in memory addressed through register pair HL or via indexing. Note that arithmetic and logical instructions do not allow direct addressing. If a result is produced (comparison does not produce any), it replaces the operand in the accumulator.

Examples

1. Logically OR the accumulator with register C.

OR C

OR C logically ORs register C with the accumulator and places the result in the accumulator. The programmer only has to specify one operand; the other operand and the destination of the result are always the accumulator.

2. Add register B to the accumulator.

ADD A,B

ADD A,B adds register B to the accumulator (register A) and places the result in the accumulator. In the instructions ADC, ADD, and SBC, the programmer must specify both operands. The reason is that the Z80 also has the instructions ADC HL (add register pair to HL with carry), ADD HL (add register pair to HL), ADD xy (add register pair or index register to index register), and SBC HL (subtract register pair from HL with borrow). Note the inconsistency here: Both operands must be specified in ADC, ADD, and SBC, but only one operand in SUB; furthermore, the Z80 has an ADD xy instruction, but no ADC xy or SBC xy instruction. Since the 16-bit arithmetic instructions are mainly intended for addressing, we will discuss them later.

3. Logically AND the accumulator with the binary constant BICON. AND BICON

Immediate addressing is the default mode; no special operation code or designation is necessary.

4. Logically OR the accumulator with the data at the address in register pair HL. OR CHL)

Parentheses indicate a reference to the contents of a memory address. Other operations require more than one instruction. Some typical examples are: • Add memory locations OPERl and OPER2, place sum in memory location SUM.

ADD A,B

:GET FIRST OPERAND :GET SECOND OPERAND SAVE SUM

:GET FIRST OPERAND ! ADD SECOND OPERAND SAVE SUM

We can shorten the second alternative considerably if the operands and the sum occupy consecutive memory addresses. For example, if OPER2 = OPERl + l and SUM = OPER2 + l, we have

LD HL,OPERl

INC HL

INC HL

; GET FIRST OPERAND ? ADD SECOND OPERAND ; SAVE SUM

Add a constant (VALUE) to memory location OPER.

LD HL,OPER

ADD A,VALUE

If VALUE = 1, we can shorten the second alternative to

You can use DEC (HL) similarly without changing the accumulator, but both DEC (HL) and INC (HL) affect all the flags except Carry.

0 0

Post a comment

  • Receive news updates via email from this site