Square of a number using a look-up table (assembly language program)
Problem statement: Write down an assembly language program to find the square of the numbers stored as an array starting at memory location C100H. Store the result as an array starting at memory location C200H. Assume that there are five numbers in the array.
Solution:
how to solve
The following program uses the look-up table concept. Instead of finding the squares by arithmetic operations, the calculated values of squares of 0 to 9 are stored in memory locations.
These memory locations are accessed using their addresses, the last digits of which are from zero to nine.
The program given in the table uses three register pairs and the indirect addressing mode for data transfer.
Look-up table for squares of numbers 0 to 9
The look-up table for squares of numbers 0 to 9 is given below:
Memory Address(H) | Decimal number | Square(H) |
C300 | 0 | 00 |
C301 | 1 | 01 |
C302 | 2 | 04 |
C303 | 3 | 09 |
C304 | 4 | 10 |
C305 | 5 | 19 |
C306 | 6 | 24 |
C307 | 7 | 31 |
C308 | 8 | 40 |
C309 | 9 | 51 |
Input Data:
Memory Address(H) | Data(H) |
C100 | 02 |
C101 | 05 |
C102 | 07 |
C103 | 01 |
C104 | 09 |
PROGRAM
Memory Address(H) | Machine Code(H) | Label | Mnemonics(Instruction) | Comments |
C000 C001 C002 | 21 00 C3 | LXI H C300H | Load the register pair H-L with the address C300H; Initialize the look-up table pointer | |
C003 C004 C005 | 11 00 C1 | LXI D C100H | Initialize the source memory array pointer | |
C006 C007 C008 | 01 00 C2 | LXI B C200H | Initialize the destination memory array pointer | |
C009 | 1A | LOOP | LDAX D | Get a number from the source array. |
C00A | 6F | MOV L, A | Move the number to register L. | |
C00B | 7E | MOV A, M | Load the square in the accumulator. | |
C00C | 02 | STAX B | Store the result in the destination memory location. | |
C00D | 13 | INX D | Increment the source memory pointer. | |
C00E | 03 | INX B | Increment the destination memory pointer. | |
C00F | 7B | MOV A, E | Move the lower-order byte of the source memory location to the accumulator. | |
C010 C011 | FE 05 | CPI 05H | Compare it with 05H | |
C012 C013 C014 | C2 09 C0 | JNZ LOOP | If it is not equal to 05H, repeat the process. | |
C015 | 76 | HLT | If it is equal to 05H, terminate program execution. |
RESULT
Memory Address(H) | Data(H) |
C200 | 04 |
C201 | 19 |
C202 | 31 |
C203 | 01 |
C204 | 51 |