Saturday, April 23, 2011

Write Assembly language program To subtract 2 16-bit BCD numbers


; HL = 2435
; DE = 6578


LHLD 6578H ;
XCHG ; DE = 6578
LHLD 2435H ; HL = 2435
MOV A,L
SUB E ;
DAA ;
STA 2000H ;
MOV A,H ;
SBB D ;
DAA ;
STA 2001H ;
HLT ;

Write Assembly language program To subtract 2 8-bit BCD numbers


; 2000H = 24
; 2001H = 43


LDA 2000H ;
MOV B,A ;
LDA 2001H ;
SUB B ;
DAA ;
STA 2002H ;
HLT ;

Write Assembly language program To add 2 16-bit BCD numbers


; HL = 2435
; DE = 6578
; METHOD 1


LHLD 6578H ;
XCHG ; DE = 6578
LHLD 2435H ; HL = 2435
MOV A,L
ADD E ;
DAA ;
STA 2000H ;
MOV A,H ;
ADC D ;
DAA ;
STA 2001H ;
HLT ;

Write Assembly language program To add 2 8-bit BCD numbers


; 2000H = 78
; 2001H = 34
; 2002H = 78 + 34 = 112
; METHOD 1


LXI H, 2000H ;
MOV A,M ;78
INX H ;34
ADD M ;78 + 34
DAA ;112
INX H ;
MOV M,A ;STORE 112
HLT ;

Write Assembly language program for 2-Digit BCD to Binary conversion


; NEED TO USE CONCEPT OF POSITIONAL 
; WEIGHTING
; EXAMPLE: 34
; 34 = 3X0A + 4
; 34 = 1E + 4
; 34 = 22
; 2000H = 34H
;
;
LDA 2000H ;
ANI F0 ; A = 30
RRC ; MAKE MSB LSB
RRC
RRC
RRC
MVI B, 0AH ; B = 0A
CONTI ADD A ;
DCR B ;
JNZ CONTI ;
MOV B, A ; B = 1E = A
LDA 2000H ;
ANI 0F ; A = 4
ADD B ; A = 22
STA 2001H ; STORE 22
HLT

Tuesday, April 12, 2011

Write a Subroutine for 8085 to generate delay 0f 10ms(assume .333us clock cycle)


; REQUIRED T STATES
; = 10ms/.333us
; = 30030 T STATES
; SO WE CAN SAY VALUE OF COUNT HERE WILL BE HIGH
; THEREFORE TAKING A REGISTER PAIR AS A COUNTER


LXI B, COUNT ; 10T LOWER ADDRESS IN B HIGHER IN C
L1 DCX B ; 6T
MOV A,B ; 4T MOVE LOWER ADDRESS IN A
ORA C ; 4T SEE IF BOTH A AND C ARE ZERO
JNZ L1 ; 10/7T
RET ; 10T
; Td = 10 + COUNT X ( 6 + 4 + 4 + 10) + 10 - 3 = 30030
; 24COUNT = 30030 - 17 = 30013
; COUNT = 1250(DEC) = 04E2(HEX)

Write a Subroutine for 8085 to generate delay 0f 100us(assume 320ns clock cycle)



; clock cycle given is 320ns
; therefore required T-states = 100us/320ns
;      = 312.5 Tstates


MVI C, COUNT ; 7 T STATES
L1 DCR C ; 4T
JNZ L1 ; 10/7 T
RET ; 10 T REMEMBER A SUBROUTINE IS TO BE MADE


; Td = 7(MVI C, COUNT) + (COUNT X (4+10)) + 10(RET) - 3(LAST JUMP WILL NOT OCCUR) = 312.5
; THEREFORE 14COUNT = 297.5
; COUNT = 21.32 = 21(DEC) = 15(HEX)

Monday, April 4, 2011

Program to find mINImum number in an array




LXI H, 2000H ; INITIALIZE STARTING ADDRESS
MVI C, 10H ; SIZE OF ARRAY IS 10
XRA A ;
BACK CMP M ; COMPARE WITH MEMORY
JC SKIP ;
MOV A, M ; IF NUMBER< MAX INTERCHANGE
SKIP INX H ;
DCR C ;
JNZ BACK ;
STA 1FFFH ;
HLT ;

Calculate sum of series of even numbers


; MEANS ADD ONLY EVEN NUMBERS IN AN ARRAY
;
;
;


MVI C, 05H ; SIZE OF ARRAY
LXI H, 2000H ; START ARRAY
MVI B, 00H ; SUM =0
BACK MOV A, M ;
ANI 01H ; MASK BITS D1 TO D7 LEAVING D0 ALONE
JNZ SKIP ;
ADD M ; ADD EVEN NUMBER
MOV B,A ;
SKIP INX H ;
DCR C ;
JNZ BACK ;
STA 1FFFH ;
HLT ;

Count the number of 1's in register


; EXAMPLE H = 0101 0010 = 52 NUMBER OF 1'S =3
;
;


MVI C, 00H ; nUMBER OF 1'S
MVI B, 08H ; AS THERE ARE ONLY 8 BITS IN REGISTER
LXI H, 2000H ;
MOV A, M ;
L2 RAR ; ROTATE THROUGH CARRY
JNC L1 ; IF CARRY 1 CONTINUE
INR C ;
L1 DCR B ;
JNZ L2 ;
MOV A, C ;
STA 1FFFH ;
HLT

Program to find number of negative numbers in array


I am personally not able to understand this particular program as it uses MSB instead of sign flag. This is direct rip off untested version from a book.
--------------------
        MVI C, 05H ; SIZE OF ARRAY
MVI B, 00H ; COUNT OF NEGATIVE NUMBERS
LXI H, 2000H ; LOAD STARTING ADDRESS
BACK MOV A, M ;
ANI 80H ;
JZ SKIP ; CHECK IF MSB = 1 IF MSB = 1 THEN NUMBER IS NEGATIVE
INR B ;
SKIP INX H ;
DCR C ;
JNZ BACK ;
STA 1FFFH ;
HLT ;

Program to find maximum number in an array


        LXI H, 2000H ; INITIALIZE STARTING ADDRESS
MVI C, 10H ; SIZE OF ARRAY IS 10
XRA A ;
BACK CMP M ; COMPARE WITH MEMORY
JNC SKIP ;
MOV A, M ; IF NUMBER> MAX INTERCHANGE
SKIP INX H ;
DCR C ;
JNZ BACK ;
STA 1FFFH ;
HLT ;

Program to sort the numbers in DEscending order


        MVI B, 09H   ; SIZE OF ARRAY
START   LXI H, 2000H ; INITIALIZE MEMORY POINTER
MVI C, 09H   ; INNER LOOP COMPARE ONE TERM WITH 9 TERMS
BACK MOV A, M     ; GET NUMBER IN ACCUMULATOR
INX H     ; INCREMENT MEMORY POINTER
CMP M     ; COMPARE
JNC SKIP     ; IF A>M DONT INTERCHANGE
JZ SKIP     ; IF A=M DONT INTERCHANGE
MOV D, M     ; OTHERWISE SWAP
MOV M, A     ;
DCX H        ; INTERCHANGE NUMBERS
MOV M, D     ; 
INX H        ; INCREMENT TO NEXT MEMORY
SKIP DCR C     ; DCR C
JNZ BACK     ; TILL C!=0 CONTINUE
DCR B     ; DCREMENT OUTSIDE COUNTER
JNZ START    ; TILL B!=0
HLT     ;

Program to sort the numbers in ascending order


        MVI B, 09H   ; SIZE OF ARRAY
START   LXI H, 2000H ; INITIALIZE MEMORY POINTER
MVI C, 09H   ; INNER LOOP COMPARE ONE TERM WITH 9 TERMS
BACK MOV A, M     ; GET NUMBER IN ACCUMULATOR
INX H     ; INCREMENT MEMORY POINTER
CMP M     ; COMPARE
JC SKIP     ; IF A<M DONT INTERCHANGE
JZ SKIP     ; IF A=M DONT INTERCHANGE
MOV D, M     ; OTHERWISE SWAP
MOV M, A     ;
DCX H        ; INTERCHANGE NUMBERS
MOV M, D     ; 
INX H        ; INCREMENT TO NEXT MEMORY
SKIP DCR C     ; DCR C
JNZ BACK     ; TILL C!=0 CONTINUE
DCR B     ; DCREMENT OUTSIDE COUNTER
JNZ START    ; TILL B!=0
HLT     ;

Unpack the packed bcd numbers


; GIVEN
; 2000H = 42
; 2001H = 04
; 2002H = 02


LDA 2000H ; LOAD BCD NUMBER IN ACCUMULATOR
ANI F0H   ; MASK LOWER BYTE TO GET A = 40
RRC  ;
RRC  ;
RRC  ;
RRC  ; A = 04
STA 2001H ; STORE 04
LDA 2000H ; LOAD BCD NUMBER
ANI 0FH  ; MASK HIGHER BYTE TO GET A = 02
STA 2002H ; STORE 02
HLT  ;

Pack the 2 unpacked bcd numbers


; GIVEN
; 2000H = 02H
; 2001H = 04H
; 2002H = 42H


LDA 2001H ; LOAD MSB IN ACCUMULATOR 04H
RLC  ;
RLC  ;
RLC  ;
RLC  ; A = 40H
ANI F0H   ; MAKE LSB ZERO
MOV B, A  ; TEMPORARY STORAGE
LDA 2000H ; LOAD LEAST SIGINIFICANT BIT
ADD B     ; ADD 40 AND 02
STA 2002H ; STORE RESULT 
HLT  ;

Program to mask higher nibble


; A = 32
; LOGICALLY AND WITH 0FH
; A = 02


MVI A, 32 ;
ANI 0FH  ;
STA 2000H ;
HLT  ;

Program to mask lower nibble


; A = 32
; LOGICALLY AND WITH F0H 
; A = 30


MVI A, 32H ;
ANI F0H    ;
STA 2000H  ;
HLT   ;

Find 2's complement of number


; 32 = 0011 0010
; 32'S COMPLEMENT
; 1100 1101 = CD
; 2's COMPLEMENT IS 1100 1101 + 1 = 1100 1110 = CE
LDA 2000H ; LOAD THE NUMBER
CMA  ; COMPLEMENT THE NUMBER
ADI 01H   ;
STA 2001H ; STORE THE COMPLEMENT
HLT  ;

Find 1's complement of number


; 32 = 0011 0010
; 32'S COMPLEMENT
; 1100 1101 = CD


LDA 2000H ; LOAD THE NUMBER
CMA  ; COMPLEMENT THE NUMBER
STA 2001H ; STORE THE COMPLEMENT
HLT  ;

Program to Subtract 2 16 bit numbers


; 2000H = 21H
; 2001H = 12H
; 2002H = 21H
; 2003H = 43H
; RESULT = 1221H - 4321H
; = 3100H
; 2004H = 00H LOWER 2 BYTES
; 2005H = 31H HIGHER 2 BYTES


LHLD 2000H ; LOAD FIRST 16 BIT INTO HL PAIR H=12H L=21H
XCHG   ; SAVE IT IN DE PAIR
LHLD 2002H ; LOAD SECOND 16 BIT INTO HL PAIR H=21H L=43H
MOV A, E   ; LOAD LOWER BYTE OF FIRST NUMBER IN ACCUMULATOR
SUB L   ; SUBTRACT 2 LOWER BYTES
MOV L, A   ; STORE RESULT
MOV A, D   ; LOAD HIGHER BYTE INTO ACCUMULATOR
SBB H   ; SUBTRACT HIGHER BYTE OF SECOND NUMBER WITH BORROW OF PREVIOUS SUBTRACTION
MOV H, A   ; A = A - H - CY
SHLD 2004H ; STORE RESULT
HLT   ;

Program to Add 2 16 bit number

This is done using 8 bit registers
--------------------------------

; 2000H = 21H
; 2001H = 12H
; 2002H = 21H
; 2003H = 43H
; RESULT = 1221H + 4321H
; = 5542H
; 2004H = 42H LOWER 2 BYTES
; 2005H = 55H HIGHER 2 BYTES
; METHOD 1


LHLD 2000H ;LOAD FIRST 16 BIT INTO HL PAIR H=12H L=21H
XCHG    ; SAVE IT IN DE PAIR
LHLD 2002H ; LOAD SECOND 16 BIT INTO HL PAIR H=21H L=43H
MOV A, E   ; LOAD LOWER BYTE OF FIRST NUMBER IN ACCUMULATOR
ADD L    ;ADD TWO LOWER BYTES
MOV L, A   ; STORE RESULT IN L. L=42H
MOV A, D   ; LOAD HIGHER BYTE INTO ACCUMULATOR
ADC H    ; ADD 2 HIGHER BYTES ALONG WITH CARRY FROM THE LOWER BYTES
MOV H, A   ;
SHLD 2004H ;
HLT    ;
------------------------------

; METHOD 2
LHLD 2000H ; LOAD FIRST 16 BIT INTO HL PAIR H=12H L=21H
XCHG   ; SAVE IT IN DE PAIR
LHLD 2002H ; LOAD SECOND 16 BIT INTO HL PAIR H=21H L=43H
DAD D   ; ADD 2 16 BIT NUMBERS
SHLD 2004H ; STORE 16 BIT RESULT
HLT   ;

-----------------------------------