8085 Assembly Language Programs

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 29

Assembly language programs

8085
8085 program to find maximum and minimum of 10 numbers

In CMP instruction:
If Accumulator > Register then carry and zero
flags are reset
If Accumulator = Register then zero flag is set
If Accumulator < Register then carry flag is set

Assumption – List of numbers from 2050H to


2059H and output at 2060H and 2061H.

03/06/2024 04:24 PM 2
Algorithm
• Maximum number is stored in B register and minimum in C
register
• Load counter in D register
• Load starting element in Accumulator, B and C register
• Compare Accumulator and B register
• If carry flag is not set then transfer contents of Accumulator to B.
Else, compare Accumulator with C register, if carry flag is set
transfer contents of Accumulator to C
• Decrement D register
• If D>0 take next element in Accumulator and go to point 4
• If D=0, store B and C register in memory
• End of program
03/06/2024 04:24 PM 3
Program
Load starting
2000H LXI H, 2050H
address of list
2003H MOV B, M Store maximum
2004H MOV C, M Store minimum
Counter for 10
2005H MVI D, 0AH
elements
Retrieve list
2007H LOOP MOV A, M element in
Accumulator
Compare element
2008H CMP B with maximum
number

03/06/2024 04:24 PM 4
Jump to MIN if not
2009H JC MIN
maximum

Transfer contents
200CH MOV B, A
of A to B as A > B

Compare element
200DH MIN CMP C with minimum
number

Jump to SKIP if not


200EH JNC SKIP
minimum

Transfer contents
2011H MOV C, A of A to C if A <
minimum

2012H SKIP INX H Increment memory

03/06/2024 04:24 PM 5
Decrement
2013H DCR D
counter

Jump to LOOP if D
2014H JNZ LOOP
>0

Load address to
2017H LXI H, 2060H
store maximum

Move maximum to
201AH MOV M, B
2060H

Increment
201BH INX H
memory

Move minimum to
201CH MOV M, C
2061H

201DH HLT Halt

03/06/2024 04:24 PM 6
Explanation
• One by one all elements are compared with B and C
register.
• Element is compared with maximum, if it greater
than maximum then it is stored in B register. Else, it
is compared with minimum and if it is less than
minimum then it stored in C regiter.
• Loop executes 10 number of times.
• At the end of 10 iterations, maximum and minimum
are stored at 2060H and 2061H respectively.

03/06/2024 04:24 PM 7
SMALLEST NUMBER IN A DATA ARRAY

Algorithm
1) Load the address of the first element of the array in HL pair
2) Move the count to B – reg.
3) Increment the pointer
4) Get the first data in A – reg.
5) Decrement the count.
6) Increment the pointer
7) Compare the content of memory addressed by HL pair with that of A - reg.
8) If carry = 1, go to step 10 or if Carry = 0 go to step 9
9) Move the content of memory addressed by HL to A – reg.
10) Decrement the count
11) Check for Zero of the count. If ZF = 0, go to step 6, or if ZF = 1 go to next step.
12) Store the smallest data in memory.
13) Terminate the program.

03/06/2024 04:24 PM 8
Program
LXI H,4200 Set pointer for array
MOV B,M Load the Count
INX H Set 1st element as largest data
MOV A,M
DCR B Decremented the count
LOOP: INX H
CMP M If A- reg < M go to AHEAD
JC AHEAD
MOV A,M Set the new value as smallest
AHEAD:DCR B
JNZ LOOP Repeat comparisons till count = 0
STA 4300 Store the largest value at 4300
HLT
03/06/2024 04:24 PM 9
Largest number in an array

03/06/2024 04:24 PM 10
03/06/2024 04:24 PM 11
03/06/2024 04:24 PM 12
Sort an array in ascending order

03/06/2024 04:24 PM 13
03/06/2024 04:24 PM 14
03/06/2024 04:24 PM 15
03/06/2024 04:24 PM 16
Sort an array in ascending order
Algorithm:
1. Initialize HL pair as memory pointer
2. Get the count at 4200 into C - register
3. Copy it in D - register (for bubble sort (N-1) times
required)
4. Get the first value in A - register
5. Compare it with the value at next location
6. If they are out of order, exchange the contents of A - register
and Memory
7. Decrement D - register content by 1
8. Repeat steps 5 and 7 till the value in D- register become
zero
9. Decrement C - register content by 1
10. Repeat steps 3 to 9 till the value in C - register becomes
03/06/2024 04:24 PM 17
zero
Program
LXI H,5000 ;Set pointer for array
MOV C,M ;Load the Count
DCR C ;Decrement Count
REPEAT: MOV D,C
LXI H,5001
LOOP: MOV A,M ;copy content of memory location to Accumulator
INX H
CMP M
JC SKIP ;jump to skip if carry generated
MOV B,M ;copy content of memory location to B - Register
MOV M,A ;copy content of Accumulator to memory location
DCX H ;Decrement content of HL pair of registers
MOV M,B ;copy content of B - Register to memory location
INX H ;Increment content of HL pair of registers
SKIP: DCR D ;Decrement content of Register - D
JNZ LOOP ;jump to loop if not equal to zero
DCR C ;Decrement count
JNZ REPEAT ;jump to repeat if not equal to zero
HLT ;Terminate Program

03/06/2024 04:24 PM 18
Result
Input:
Data 0: 05H in memory location 5000 -->array size
Data 1: 05H in memory location 5001
Data 2: 04H in memory location 5002
Data 3: 03H in memory location 5003
Data 4: 02H in memory location 5004
Data 5: 01H in memory location 5005

Output:
Data 0: 05H in memory location 5000 --> array size
Data 1: 01H in memory location 5001
Data 2: 02H in memory location 5002
Data 3: 03H in memory location 5003
Data 4: 04H in memory location 5004
Data 5: 04:24
03/06/2024 05HPMin memory location 5005 19
Sort an array in descending order
START:MVI B, 00 ; Flag = 0
LXI H, 4150 ; Count = length of array
MOV C, M
DCR C ; No. of pair = count -1
INX H ; Point to start of array
LOOP:MOV A, M ; Get kth element
INX H
CMP M ; Compare to (K+1) th element
JNC LOOP 1 ; No interchange if kth >= (k+1) th
MOV D, M ; Interchange if out of order
MOV M, A ;
DCR H
MOV M, D
INX H
MVI B, 01H ; Flag=1
LOOP 1:DCR C ; count down
JNZ LOOP ;
DCR B ; is flag = 1?
JZ START ; do another sort, if yes
03/06/2024HLT
04:24 PM ; If flag = 0, step execution 20
To add numbers in an array

Problem – Write an assembly language program


to add hexadecimal numbers stored in
continuous memory or in an array.
Assumption – Suppose the size of the array is
stored at memory location 2050 and the base
address of the array is 2051. The sum will be
stored at memory location 3050 and carry will
be stored at location 3051.

03/06/2024 04:24 PM 21
Algorithm
• Load the base address of the array in HL
register pair.
• Use the size of the array as a counter.
• Initialise accumulator to 00.
• Add content of accumulator with the content
stored at memory location given in HL pair.
• Decrease counter on each addition.

03/06/2024 04:24 PM 22
Program
2000 LDA 2050 A <- [2050]
2003 MOV B, A B <- A
2004 LXI H, 2051 H <- 20 and L <- 51
2007 MVI A, 00 A <- 00
2009 MVI C, 00 C <- 00
200B ADD M A <- A+M
200C INR M M <- M+1
200D JNC 2011
2010 INR C C <- C+1
2011 DCR B B <- B-1
2012 JNZ 200B
2015 STA 3050 3050 <- A

03/06/2024 04:24 PM 23
2018 MOV A, C A <- C
2019 STA 3051 3051 <- A

201C HLT
Terminates the program

03/06/2024 04:24 PM 24
Explanation –

LDA 2050: load accumulator with content of location 2050


MOV B, A: copy contents of accumulator to register B
LXI H, 2051: store 20 to H register and 51 to L register
MVI A, 00: store 00 to accumulator
MVI C, 00: store 00 to register C
ADD M: add accumulator with the contents of memory location given in HL register pair
INR M: increase M by 1
JNC 2011: if not carry, jump to location 2011 otherwise to the location given in PC
INR C: increase content of register C by 1
DCR B: decrease content of register B by 1
JNZ 200B: if not zero, jump to location 200B otherwise to the location given in PC
STA 3050: store contents of accumulator to memory location 3050
MOV A, C: copy contents of register C to accumulator
STA 3051: store contents of accumulator to memory location 3051
HLT: terminates the program
03/06/2024 04:24 PM 25
Square of a Number Using Look Up Table

Algorithm
1. Initialize HL pair to point Look up table.
2. Get the data .
3. Check whether the given input is less than 9.
4. If yes go to next step else halt the program.
5. Add the desired address with the
accumulator content.
6. Store the result
03/06/2024 04:24 PM 26
Program and Result
LXI H,5000 ;Initialsie Look up table address
LDA 5050 ;Get the data
CPI 0A ;Check input > 9
JC AFTER ;if yes error
VI A,FF ;Error Indication
STA 5051
HLT
AFTER: MOV C,A ;Add the desired Address
MVI B,00
DAD B
MOV A,M
STA 5051 ;Store the result
HLT ;Terminate the program
03/06/2024 04:24 PM 27
LOOKUP TABLE
5000 01
5001 04
5002 09
5003 16
5004 25
5005 36
5006 49
5007 64
5008 81
03/06/2024 04:24 PM 28
Result
Input:
Data: 05H in memory location 5050
Output:
Data: 25H (Square of 5) in memory location 5051

Input:
Data: 11H in memory location 5050
Output:
Data: FFH (Error Indication) in memory location 5051
03/06/2024 04:24 PM 29

You might also like