8085 program to search a number in an array of n numbers
Problem – Write an assembly language program in 8085 to search a given number in an array of n numbers. If number is found, then store F0 in memory location 3051 otherwise store 0F in 3051.
Assumptions – Count of elements in an array is stored at memory location 2050. Array is stored from starting memory address 2051 and number which user want to search is stored at memory location 3050.
Examples –
Algorithm –
- Make the memory pointer points to memory location 2050 by help of LXI H 2050 instruction
- Store value of array size in register C
- Store number to be search in register B
- Increment memory pointer by 1 so that it points to next array index
- Store element of array in accumulator A and compare it with value of B
- If both are same i.e. if ZF = 1 then store F0 in A and store the result in memory location 3051 and go to step 9
- Otherwise store 0F in A and store it in memory location 3051
- Decrement C by 01 and check if C is not equal to zero i.e. ZF = 0, if true go to step 3 otherwise go to step 9
- End of program
Program –
MEMORY ADDRESS | MNEMONICS | COMMENT |
---|---|---|
2000 | LXI H 2050 | H <- 20, L <- 50 |
2003 | MOV C, M | C <- M |
2004 | LDA 3050 | A <- M[3050] |
2007 | MOV B, A | B <- A |
2008 | INX H | HL <- HL + 0001 |
2009 | MOV A, M | A <- M |
200A | CMP B | A – B |
200B | JNZ 2014 | Jump if ZF = 0 |
200E | MVI A F0 | A <- F0 |
2010 | STA 3051 | M[3051] <- A |
2013 | HLT | END |
2014 | MVI A 0F | A <- 0F |
2016 | STA 3051 | M[3051] <- A |
2019 | DCR C | C <- C – 01 |
201A | JNZ 2008 | Jump if ZF = 0 |
201D | HLT | END |
Explanation – Registers used A, B, C, H, L and indirect memory M:
- LXI H 2050 – initialize register H with 20 and register L with 50
- MOV C, M – assign content of indirect memory location, M which is represented by registers H and L to register C
- LDA 3050 – loads the content of memory location 3050 in accumulator A
- MOV B, A – move the content of A in register B
- INX H – increment HL by 1, i.e. M is incremented by 1 and now M will point to next memory location
- MOV A, M – move the content of memory location M in accumulator A
- CMP B – subtract B from A and update flags of 8085
- JNZ 2014 – jump to memory location 2014 if zero flag is reset i.e. ZF = 0
- MVI A F0 – assign F0 to A
- STA 3051 – stores value of A in 3051
- HLT – stops executing the program and halts any further execution
- MVI A 0F – assign 0F to A
- STA 3051 – stores value of A in 3051
- DCR C – decrement C by 01
- JNZ 2008 – jump to memory location 2008 if zero flag is reset
- HLT – stops executing the program and halts any further execution
Please Login to comment...