Sunday, 15 January 2012

How to ADD two 16 Bit Number

Suppose we have to add two HEX numbers viz:
4351 & 3211.
First take the LSB of two number and ADD them. Then take the MSB of thw numbers and add them with CARRY.


CODE:

MVI A,51H
MVI B,11H
ADD B
STA 5000H
MVI A,43H
MVI B,32H
ADC B
STA 5002H
HLT



When this program will be executed then LSB will be saved at location 5000H and MSB will be saved at 5002H location

Saturday, 15 October 2011

To find factorial of a number.

To find factorial of a number we can use the same procedure as in C++ programming.


CODE:



MVI B,05
MOV C, B
DCR C
LABEL2: MOV E, C
SUB A
LABEL1: ADD B
DCR E
JNZ LABEL1
MOV B, A
DCR C
JNZ LABEL2
STA 00H
HLT




Screen Shot:



To multiply two numbers.

To multiply two numbers we can use multiple addition of that number up to the second number.

CODE:



MVI D,3
MVI B,2
MVI A,00
LABEL: ADD D
DCR B
JNZ LABEL
HLT




Screen Shot:

Friday, 14 October 2011

How to add two numbers without using ADD command?

To add two numbers without using ADD command first we need to understand the basics of addition. The basic of addition is that we increment the first number by 1 upto second number to reach the answer of their additon. The same procedure will be followed here in this program

CODE
MVI A,08
MVI B,54
LABEL: INR A
DCR B
JNZ LABEL
HLT

Screen shot of the program ran in simulator is here:




Monday, 10 October 2011

8085 Architecture


How to count number of 1's in a byte?

To count the number of 1's in a byte[8 bits] the technique is as follows:-
We use RAR instruction to rotate the D0 bit via CARRY flag and check the status of carry flag for 1. And we count the number of times the CARRY flag is set[ie equal to 1].

Code-
                   MVI A,FFH                                  ; Load A with Immediate data as FF
                   MVI B,08                                      ; Initialize B with 08 for counting
 LABEL2: RAR                                              ; Rotate Right through CARRY
                  JC LABEL1                                  ; Check for CARRY flag
                  DCR B                                          ; Decrement counter
                  JNZ LABEL2                               ; Check for number of counting
                  LABEL1: INR C                          ; Increment C for number of 1's
                  DCR B                                          ;
                  JNZ LABEL2                               ;
                  HLT

Monday, 3 October 2011

Table of 2

In this program the starting address is taken as 2001H.
The table of 2 {2,4,6,8,.......,20} would be saved in memory location starting from 2001H location.
CODE: 




               LXI H,2001H                                     ; Make H/L pair as pointer to location 2001H
               MVI A,00H                                        ;
               MVI  B,10                                          ; Taking Register B as a counter for table
LABEL: ADI 02                                               ; Add 02 as immediate value toRregister A
               MOV M,A                                         ; Save value of Register A to memory location pointed by H
               INX H                                                ; Increment the address pointed by Register H
               DCR B                                               ; Decrement the B
               JNZ LABEL                                      ; Check for ZERO  and Jump to LABEL


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