ORG 0x0000 ; Set the program counter to 0x0000
START:LXI H, 0x4000 ; Load the starting address of the array into the H-L pairMVI A, 0x00 ; Initialize the accumulator to 0MOV B, #5 ; Initialize the loop counter to 5SUM_LOOP:MOV C, M ; Load the next number from memory into the C registerADD C ; Add the number to the accumulatorINX H ; Increment the memory addressDCR B ; Decrement the loop counterJNZ SUM_LOOP ; Jump to SUM_LOOP if the loop counter is not zeroSTA 0x4005 ; Store the sum in memory location 0x4005HLT ; Halt the programEND START ; End of program
Explanation:
The program starts by loading the starting address of the array (0x4000) into the H-L pair using the LXI H instruction.
The MVI A instruction is used to initialize the accumulator to 0.
The MOV B instruction is used to initialize the loop counter to 5.
The loop starts with the label SUM_LOOP.
The MOV C instruction is used to load the next number from memory into the C register.
The ADD C instruction adds the number to the accumulator.
The INX H instruction increments the memory address, moving to the next number in the array.
The DCR B instruction decrements the loop counter.
The JNZ SUM_LOOP instruction jumps back to the SUM_LOOP label if the loop counter is not zero.
After the loop completes, the STA instruction is used to store the sum in memory location 0x4005.
Finally, the program halts using the HLT instruction.
Note: This program assumes that the numbers are stored in unsigned form. If the numbers are signed, they will need to be converted to two's complement form before being added.
0 Comments