Skip to main content

Program to calculate the sum of the series of numbers

Statement: Find the sum of the series of numbers. The length of the series is given in the memory location 2000H and the series itself begins from memory location 2001H. 

A). If the sum is an 8-bit number, store the sum at memory location 2005H. 

B). If the sum is a 16-bit number, store the sum at memory locations 2005H and 2006H.

A). 
EXAMPLE:

(2000H) = 04H
(2001H) = 30H
(2002H) = 15H
(2003H) = 13H
(2004H) = 33H

Result = 30H + 15H + 13H + 33H = 8BH
Therefore, (2005H) = 8BH

FLOW CHART:


PROGRAM:

              LDA 2000H          ; Get the length of the series
              MOV C, A            ; Initialise counter
              SUB A                  ; Make Sum = 0
              LXI H, 2001H      ; Initialise pointer
BACK: ADD M                 ; Sum = Sum + data
             INX H                    ; Increment pointer
             DCR C                   ; Decrement counter
             JNZ BACK            ; If counter is not equal to zero, repeat
             STA 2005H            ; Store result
             HLT                        ; Terminate program execution

B). 
EXAMPLE:

(2000H) = 04H
(2001H) = 62H
(2002H) = 52H
(2003H) = 3BH
(2004H) = 9AH

Result = 62H + 52H + 3BH + 9AH = 0189H
Therefore, (2005H) = 89H and (2006H) = 01H

FLOW CHART:


PROGRAM:

              LDA 2000H                 ; Get the length of the series
              MOV C, A                    ; Initialise counter
              LXI H, 2001H              ; Initialise pointer
              SUB A                          ; Make Sumlow = 0
              MOV B, A                    ; Make Sumhigh = 0
 BACK: ADD M                        ; Sum = Sum + data
              JNC SKIP                     ; If no carry,then move to SKIP
              INR B                           ; Add carry to higher byte of sum
   SKIP: INX H                           ; Increment pointer
              DCR C                          ; Decrement counter
              JNZ BACK                   ; If counter is not equal to zero, repeat
              STA 2005H                   ; Store lower byte of result
              MOV A, B                    ; Move higher byte of result
              STA 2006H                   ; Store higher byte of result
              HLT                               ; Terminate program execution

Comments

Popular posts from this blog

Program to pack the two unpacked BCD numbers

Statement: Pack the two unpacked BCD numbers stored in memory locations 2000H and 2001H and store the result in memory location 2002H. The least significant digit is stored at 2000H. Example: (2000H) = 04H (2001H) = 09H Result = (2002H) = 94H Program: LDA 2001H                        ; Obtain the most significant BCD digit RLC                                    ; Rotate left  RLC                                    ; Rotate left  RLC                                    ; Rotate left  RLC                                    ; Rotate left...

Program to exchange the contents of two memory locations

Statement: Exchange the contents of memory locations 2000H and 2001H. Example:  Initially, (2000H) = 34H (2001H) = 20H After exchanging, (2000H) = 20H (2001H) = 34H Program 1 using direct addressing instructions: LDA 2000H               ; Get the contents of location 2000H into accumulator MOV B, A                  ; Move the contents of accumulator into register B LDA 2001H                ; Get the contents of location 2001H into accumulator STA 2000H                 ; Store the contents of accumulator into location 2000H MOV A, B                  ; Move the contents of register B into accumulator STA 2001H                 ; Store the contents of accumulator into location 2001H HLT  ...

Stack Operations Instructions (Part-II) - SPHL and XTHL

SPHL: This instruction copies the contents of HL register pair to the stack pointer (SP). It means that the stack pointer will now point to the memory location whose address was given in the HL register pair. No flags are affected. It is a one byte instruction. Register addressing mode is used. Example: If HL = 2030H, then SPHL ; This instruction will copy 2030H into SP. So SP will point to the memory location 2030H. XTHL: This instruction exchanges the contents of the memory location pointed by stack pointer with the contents of the L register and the contents of the next memory location with the contents of H register. This instruction does not alter the contents of the stack pointer. No flags are affected. It is a one byte instruction. Register Indirect addressing mode is used. Example: If HL = 5601H, SP = 2000H, (2000H) = 30H and (2001H) = 20H, then XTHL ; This instruction will exchange the contents of the memory location 2000H i.e. 3...