accumulator and memory reference instructions - part 2

22
BRANCH INSTRUCTIONS • The major use of the condition codes is with CONDITIONAL BRANCH instructions. As explained earlier, a BRANCH instruction directs the PC to another address. Instruction Mnemonic Op Code CC Branch Always BRA 20 — — Branch if carry SET BCS 25 C = 1 Branch if carry CLEAR BCC 24 C = 0 Branch if zero BEQ 27 Z = 1 Branch if not zero BNE 26 Z = 0 Branch if minus BMI 2B N

Upload: karimovaraikhanovna

Post on 08-Nov-2015

228 views

Category:

Documents


1 download

DESCRIPTION

microprocessor

TRANSCRIPT

BRANCH INSTRUCTIONS

BRANCH INSTRUCTIONSThe major use of the condition codes is with CONDITIONAL BRANCH instructions. As explained earlier, a BRANCH instruction directs the PC to another address.Instruction Mnemonic Op Code CC Branch Always BRA 20 Branch if carry SET BCS 25 C = 1Branch if carry CLEAR BCC 24C = 0Branch if zero BEQ 27 Z = 1Branch if not zero BNE 26 Z = 0Branch if minus BMI 2B N = 1Branch if plus BPL 2A N = 0Branch if overflow SET BVS 29V = 1Branch if overflow CLEAR BVC 28V = 0BRANCH INSTRUCTIONSThe instruction BRA, is an unconditional branch. The program always jumps or branches when the BRA (Branch Always) instruction occurs. The remaining instructions are conditional branches; they take the branch only if the condition codes are set properly.Each branch instruction is accompanied by an offset, a number that is added to the current value of the Program Counter to determine where the P will go if it branches.Branch instructions skip over a part of the program to a location that is relative to the program counter.Example 11At a point in a program the H, I, and C bits of the CCR should be SET and theN, V, and Z bits should be CLEAR. Write a sequence of instructions to set thebits accordingly.

SOLUTION

The CCR should look like this11110001lt can be forced into this configuration by the following instructions: Code Mnemonic Comments 86 F1 LDAA #$F1 Load desired contents of CCR into A06 TAP Transfer A to CCRCalculating OffsetsA BRANCH instruction for the 6800 consists of the BRANCH op code in one byte and an 8-bit offset in the following byte. The offset is a 2s-complement number that is added to the address.If N is the location of the branch instruction and T is the target address (the address the program will jump to), the following formula can be used to calculate the offset:Offset: T (N + 2) Note that both forward and backward branches can be accommodated. Backward branches have negative offsets.Example 12The op code of the branch instruction is in $011A. What should the offset be if the program must jump to $0150?

SOLUTIONFrom the equation we obtain$011A + 2 = $011COffset = $0150 - $011C = $34Thus the program segment would be as follows:011A XX (branch op code)011B 34 (offset)Example 13The op code of the branch instruction is in $011A. What should the offset be if the program must jump to $00EA?

SOLUTIONFrom the formula $011A + 2 = $011COffset = $00EA - $011C = $FFCEFor an 8-bit negative offset the $FF is discarded and the program is as follows:011A XX (branch op code)011B CE (offset)Note that the offset is a negative number ($CE) and the program branches backward from $11C to $EA.Long and Short BranchesThe maximum positive number that can be represented in one byte is 7F16 (or 12710), and the most negative number is 8016 (or -12810). Thus the program can branch forward no more than 127 bytes or backward no more than 128 bytes from where it would be if it did not branch (i.e., the start of the next instruction). This range is often sufficient because the destination of most branches is usually nearby.BCD ADDITION AND THE H BITMost people prefer to communicate with their computers using ordinary decimal numbers. In applications that deal with money such as cash registers, it is preferable to keep the numbers in decimal form rather than converting them to binary or hex. The use of the H bit of the CCR and the DAA (Decimal Adjust Accumulator) instruction makes decimal arithmetic possible.Expressing Numbers in BCDDecimal numbers are usually entered into a computer in BCD (Binary-Coded Decimal) form. The BCD code uses 4 binary bits called a decade to represent a single decimal digit (0 to 9). Because numbers greater than 9 are not used, the numbers from 10 to 15, which are also possible with 4-bit representation should never appear in a BCD output.Expressing Numbers in BCD

Example 13Express the decimal number 630910 in BCD form.

SOLUTIONFrom the code conversion table, we find that6 = 01103 = 00110 = 00009 = 1001The number 6309 is expressed by stringing these bits together:(630910):0110 0011 0000 1001Adding BCD NumbersBecause each 6800 memory location contains 8 bits and each BCD decade contains 4 bits, it is natural to store two BCD digits in a single memory location. This is sometimes called packing or packed BCD.Addition and subtraction of BCD numbers is possible, but because all addition and subtraction instructions in the 6800 assume binary numbers, the binary results must be manipulated to convert them to BCD. This is done by using the H bit and the DAA instruction.Adding BCD NumbersThe H bit is changed only by addition instructions. It is SET when the addition produces a carry out of bit position 3 and into bit position 4. For BCD numbers the H bit is SET whenever the sum of the two digits, plus carry, is equal to or greater than (16)10Example 14Accumulators A and B contain the decimal numbers 48 and 79, respectively. They are added by an ADD accumulator (ABA) instruction. What is the result, and what are the conditions of the C and H bits after the addition?

SOLUTIONThe 6800 adds 48 and 79 as though they were hex digits, placing the sum, C1, in accumulator A. At the end of the addition the H bit is SET (because the sum of 8 and 9 produces a carry), but the carry bit is CLEAR because the sum of the two most significant digits is less than 16.DAA InstructionThe result of Example 14 (48 + 79 = C1) is unsatisfactory if decimal arithmetic is being used. This instruction must be followed by a Decimal Adjust Accumulator (DAA) instruction to convert the hex result to the correct BCD result.lt examines four parts of the result:1. The lower half-byte2. The upper halfbyte3. The H bit4. The C bitExample 15What happens if a DAA instruction is added to Example 14?

SOLUTIONln Example 14, the sum was C1. The DAA notes the following:1. The lower half-byte is 0-3.2. The H bit is SET.3. The upper half-byte is A-F.4. The C bit is CLEAR.The DAA adds 66 to the result and SETs the C bit. After the DAA, A contains C1 + 66 = 127 and the carry bit is SET, which indicates a carry (a weight of 100 in decimal arithmetic). Therefore, the BCD sum is 127, which is correct.Example 16The decimal numbers 2948 and 4957 are in locations $20, $21 and $22, $23, respectively. Write a program to add them and store the BCD result in locations $24 and $25.

ADDR OBJECT MNEMONICS COMMENTSC000 96 21 LDA $21 Lead LS byte of AugendC002 9B 23 ADDA $23 Add LS byte of AddendC004 19 DAA Adjust for half carry, if anyC005 97 25 STAA $25 Store results in 25C007 96 20 LDAA $20 Load MS byte of Augend in AC009 99 22 ADCA $22 Add W-C MS byte of AddendCOOB 19 DAA Adjust resultCOOC 97 24 STAA $24 Store MS byteCOOE DE 24 LDX $24 Load 16-bit answer into XSubtracting BCD NumbersBCD subtraction in the 6800 P can be accomplished by complementation. Subtraction by complementation works well for decimal numbers. ln subtraction by complementation, the subtrahend must be replaced by its 9s complement, which is obtained by taking each decimal digit and replacing it with the difference between itself and 9.Subtracting BCD NumbersDecimal subtraction can be performed by using the following procedure:1. Take the 9s complement of the subtrahend and add it to the minuend.2. Remove the most significant 1 and add it to the least significant digit. This is known as an end-around-carry.Example 17Subtract 19,307 from 28,652.

SOLUTIONThe 9s complement of 19,307 is 80,692. Adding this to the minuend, we obtain(minuend) 28,652 (original number)(subtrahend) 80,692 (9s complement)109,344Removing the most significant t and adding it to the least significant digit yields

This is the correct result.

Subtracting BCD NumbersIn the 6800 the H bit is not set by subtraction instructions.The 6800 can be programmed for BCD subtraction as follows:1. Load accumulator A with 99.2. Subtract the subtrahend (to obtain the 9s complement).3. Add 1 (to make the 10s complement).4. Add the minuend (BCD addition).5. Decimal adjust the accumulator.Example 18Subtract 35 from 82 using the method just described. Assume that B2 is in $20 and 35 is in $22.

SOLUTIONThe correct answer, 47, appears in A at the end of the program. Some adjustments are required in the program if multiple-byte subtraction or the possibility of negative results are to be allowed.

ADDR OBJECT MNEMOMCS COMMENTSC000 86 99 LDAA #$99 Make 9s ComplementC002 90 22 SUBA $22 Get SubtrahendC004 4C INCA Make 10s ComplementC005 9B 20 ADDA $20 Add other numberC007 19 DAA Decimal Adjust