Q Write a Program to add two numbers ?
A let us say the two numbers are at locations 2000 and 2001, and store the result in 2002 Solution - 1: Brute force

MVI C, 00H       ; we are setting carry flag as 0, we can skip it but need to check two conditions if we haven't done it
LDA 2000H        ; load data of 2000H address in A register
MOV B, A         ; move data of B to A
 
LDA 2001H        ; load data of 2001H address in A register
ADD B            ; Add A + B
 
JNC SKIP         ; Jump if not Carry - Jump if carry is 0
INR C            ; Set carry to 1, by incrementing
 
SKIP: STA 2002H  ; store result into A register
	  MOV A, C   ; move value of C (carry) into A Register
	  STA 2003H  ; Store carry presnt in A register into C register
HALT

Solution - 2: Optimized Code using M

MVI C, 00H       ; we are setting carry flag as 0, we can skip it but need to check two conditions if we haven't done it
LXI H, 2000H     ; loads value of 2000H into HL pair
MOV A, M         ; moving values of A into M
 
INX H            ; Incrementing HL pair to input second number
ADD M            ; A + M
JNC SKIP         ; Jump if not Carry - Jump if carry is 0
INR C            ; Set carry to 1, by incrementing
 
SKIP: INX H      ; Increment value in HL pair
	  MOV M, C   ; Moving value of carry into M
HALT

Q Write an Program to transfer data from source block to destination block ?
A Considering Source block as 2000 - 2009(storing in BC Pair) and destination block as 3000 - 3009 (storing in DE Pair)

LXI B, 2000H     ; Source Address
LXI D, 3000H     ; Destination Address
MVI L, 0AH       ; count = 10
 
; loop
BACK: LDAX B     ; Load A from the memory location pointed by BC
	  STAX D     ; Load DE Pair data into A
	  INX B      ; Incrementing B pointer
	  INX D      ; Incrementing D pointer
	  DCR L      ; Decrementing L
	  JNZ BACK
HALT

Q Write an program for Inverted Block transfer ?
A Considering Source block as 2000 - 2009(storing in BC Pair) and destination block as 3000 - 3009 (storing in DE Pair). we need to transfer data of 2000 to 3009, 2001 to 3008 and so on

LXI B, 2000H     ; Source Address
LXI D, 3009H     ; Destination Address
MVI L, 0AH       ; count = 10
 
; loop
BACK: LDAX B     ; Load A from the memory location pointed by BC
	  STAX D     ; Load DE Pair data into A
	  INX B      ; Incrementing B pointer
	  DCX D      ; Decrementing D pointer
	  DCR L      ; Decrementing L
	  JNZ BACK
HALT

Q Write an Program to transfer data from source block to destination block without using third block ?
A Considering Source block as 2000 - 2009(storing in BC Pair) and destination block as 3000 - 3009 (storing in DE Pair)

; Algorithm: Move data of 2000H (say P) into A, Move the data from A into H, Move data from 3000H (say Q) into A, Move data from A into P, Move data from H into Q and loop the same
LXI B, 2000H     ; Source Address
LXI D, 3009H     ; Destination Address
MVI L, 0AH       ; count = 10
 
BACK: LDAX B     ; A = P
	  MOV H, A   ; H = A
	  LDAX D     ; H = Q
	  STAX B     ; BC = Q
	  MOV A, H   ; A = P
	  STAX D     ; DE = P
	  INX B      ; Incrementing B pointer
	  DCX D      ; Decrementing D pointer
	  DCR L      ; Decrementing L
	  JNZ BACK
HALT

Q Write a program to add a series of numbers from 2000H and store them in the immediate location after the series
A

; Algorithm: Initialise H with starting register, C with count, A(sum) with 00, B(carry) with 00
LXI H, 2000H     ; Starting Address
MVI C, 0AH       ; Initialise C with 0
MVI A, 00H       ; Initialise A with 0
MVI B, 00H       ; Initialise B with 0
 
BACK: ADD M      ; A + M
      JNC SKIP   ; Jump if not Carry
      INR B      ; Incrementing next address as carry occured
 
SKIP: INX H      ; Incrementing H Pointer
      DCR C      ; Decrement C
      JNZ BACK   ; If not zero jump
MOV M, A
INX M
MOV M, B
HALT

Q Write a program to determine the highest in the series of numbers store from location 2000H to 2009 and store the result after the series
A

; Algorithm: intialise an variable with 0 and loop on series and compare them with variable and if found more swap them
LXI H, 2000H     ; Starting Address
MVI C, 0AH       ; Initialise C with 0
MVI A, 00H       ; Initialise A with 0
 
MVI C, 0AH
MVI A, 00H
BACK: CMP M      ; Compare M - A
      JNC SKIP
	  MOV A, M
SKIP: INX H
	  DCR C
	  JNZ BACK
MOV M, A
HALT

Q Write the program to find even and odd number from a series of n Number and store the result after the series
A For Even numbers: LSB = 1, For Odd numbers: LSB = 0. For this we use Rotate Instructions and check carry

LXI H, 2000H     ; Starting Address
MVI C, 0AH       ; Initialise C with 0
MVI D, 00H       ; Initialise A with 0 (even count)
MVI B, 00H       ; Initialise A with 0 (odd count)
 
BACK: MOV A, M
      RRC        ; Rotate Right Carry
      ; if - else
      JC ODD     ; If number is odd
      INR D      ; If not odd increment D and skip
      JMP SKIP
ODD:  INR B      ; If odd increment B
SKIP: INX H
      DCR C
      JNZ BACK
Mov M, D
INX H
MOV M, B
HALT

Q Write a Program to find the number of 1's in the given number
A Rotate number to right and check carry flag

LDA A, 2000H     ; Starting Value
MVI C, 0AH       ; Initialise C with 0 (carry)
MVI B, 00H       ; Initialise B with 0 (no.of one's)
 
BACK: RRC        ; Rotate Right Carry
      JNC SKIP
	  INR B
SKIP: DCR C
	  JNZ BACK
MOV A, B
STA 2001H
HALT