Sunday 25 September 2011

How to exchange the content of two memory location.


1. Using XCHG operation

Instruction                                           Comment

LXI       H, D000 H                          ; HL pointing to D000 H
LXI       D, D001 H                          ; DE pointing to D001 H
MOV    B, M                                   ; B  (HL) (first byte)
LDAX    D                                       ; Load second byte in ACC.
XCHG                                              ; Exchange DE and HL pair contents.
MOV    M, B                                   ; Store first byte in second location
STAX    D                                        ; Store second byte in first location.
HLT                                                 ; Stop


Foot note: 
The XCHG memonic can only be used for exchanging contents of DE and HL pair. So for exchanging the contents of any two memory location its better to make DE and HL as memory pointer.



2. Without using XCHG operation.

Instruction                                                    Comment

LXI H,D000H                                            ; HL pointing to D000H
LXI B,D001H                                            ; BC pointing to D001H
MOV D,M                                                 ; Move the contents of M to D
LDAX B                                                     ; Move the contents of BC to Accumulator
MOV M,A                                                 ; Move the contents of  BC to HL  
STAX D                                                     ; Move the contents of HL to BC
HLT                                                           ; Stop