Header Ads Widget

Ticker

6/recent/ticker-posts

Write an Assembly Language Program to find the sum of 5 numbers using loop.

 ORG 0x0000          ; Set the program counter to 0x0000


START:
    LXI H, 0x4000   ; Load the starting address of the array into the H-L pair
    MVI A, 0x00     ; Initialize the accumulator to 0
    MOV B, #5       ; Initialize the loop counter to 5

SUM_LOOP:
    MOV C, M        ; Load the next number from memory into the C register
    ADD C           ; Add the number to the accumulator
    INX H           ; Increment the memory address
    DCR B           ; Decrement the loop counter
    JNZ SUM_LOOP    ; Jump to SUM_LOOP if the loop counter is not zero

    STA 0x4005      ; Store the sum in memory location 0x4005
    HLT             ; Halt the program

    END 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.

Post a Comment

0 Comments