In: Computer Science
Suppose you have the following set of hexadecimal values: $20, $25, $40, $50, $12. Write a segment of program to find the minimum and maximum values of the set. Answer must be written in assembly.
For
implementation of given problem we use CMP instruction which
compare two operand by subtracting them
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
Here List of numbers store from 2000H to 2004H and maximum number is at 2010H and minimum number is at 2011H.
It follow the following step for finding the minimum and maximum number from given number.
1. In B register maximum number is stored and in C register minimum number is stored.
2. Load counter =number of element in D register
3. Load starting element in Accumulator which is 2000h, B and C register
4. Compare Accumulator and B register
5. Transfer contents of Accumulator to B If carry flag is not set else, compare Accumulator with C register, if carry flag is set transfer contents of Accumulator to C
6. Decrement counter in D register
7. If D>0 take next element in Accumulator and go to point 4
8. If D=0, store B to 2010h and C register in memory 2011h
9. End of program
Program:
ADDRESS |
LABEL |
INSTRUCTION |
COMMENT |
2000H |
LXI H, 2000H |
Load starting address of list which is 2000h |
|
2003H |
MOV B, M |
Store maximum in register B |
|
2004H |
MOV C, M |
Store minimum in register C |
|
2005H |
MVI D, 05H |
Counter for 5 elements set register D=5 |
|
2007H |
LOOP: |
MOV A, M |
Retrieve list element in Accumulator |
2008H |
CMP B |
Compare element with maximum number |
|
2009H |
JC MIN |
Jump to MIN if not maximum |
|
200CH |
MOV B, A |
Transfer contents of A to B as A > B |
|
200DH |
MIN: |
CMP C |
Compare element with minimum number |
200EH |
JNC SKIP |
Jump to SKIP if not minimum |
|
2011H |
MOV C, A |
Transfer contents of A to C if A < minimum |
|
2012H |
SKIP: |
INX H |
Increment memory to next address which is 2001h |
2013H |
DCR D |
Decrement counter |
|
2014H |
JNZ LOOP |
Jump to LOOP if D > 0 |
|
2017H |
LXI H, 2010H |
Load address 2010h to store maximum |
|
201AH |
MOV M, B |
Move maximum to 2010H |
|
201BH |
INX H |
Increment memory so next address is 2011h |
|
201CH |
MOV M, C |
Move minimum to 2011H |
|
201DH |
HLT |
Halt |
Output:
If at any point you find any difficulty feel free ask me.