ILLUSTRATIVE PROGRAM 2: ADDITION WITH CARRY
The following program adds the number of bytes stored in memory and counts the number of carries generated. The maximum sum can be up to 16-bit.
Problem Statement
Add the following ten data bytes stored in memory with the starting address INBUF (Input Buffer). Store the sum in two memory locations; the low-order byte of the sum should be stored in OUTBUF and the high-order byte in OUTBUF + 1.
Data (H): A2; 37, 4F, 97, 22, 6B, 75, 8E, 9A, C7.
Problem Analysis
This problem is similar to Example 8.11 and can be very easily analyzed in terms of the blocks shown in the generalized flowchart in Figure.
1. In the initialization block, we need to set up a counter to count ten bytes, a memory pointer for INBUF, and registers to save the partial sum and carries. We use the accumulator for addition. The memory pointer for OUTBUF is not necessary until the data processing is completed: thus, the memory pointer used for INBUF can also be used for OUTBUF.
2. In this problem, the data processing block needs to be expanded because of the carries. Whenever a carry is generated after an addition, the carry register Will be incremented; thus, the high-order byte of the sum Will be saved in the carry register, and the low-order byte will be in the accumulator.
Program Description and Execution
The comments written in the program (see Figure) explain the function of each instruction, and the blocks drawn around the instructions show the sequence of execution. However, Figure should not be used or viewed as an illustration of a flowchart.
In the initialization block, the accumulator and register C are cleared for use in arithmetic operations; otherwise, residual data would cause erroneous results. However, register D need not be specifically cleared because the first load inĀ ruction replaces its residual data.
This program has two types of loops; one loop repeats the addition-related instructions if the counter is not zero, and the second loop skips the carry counter if there is no carry. The instruction ADC (Add with Carry) is inappropriate for this problem; this instruction is used for 16-bit addition (see Appendix A).
In the output block, the HL register is used again as a memory pointer for the output buffer memory. After all bytes have been added, the low-order byte of the result, which is in the accumulator, is stored in the memory location OUTBUF, and the high-order byte (carries) in register C is stored in the next memory location OUTBUF + 1.
Program Assembly (Assembly of ADDITION.ASM)
This section shows an illustration of the listing file of the program assembled using an assembler, This listing illustrates.how to store data using the Define Byte (DEFB) pseudo-op.