2. ARRANGE AN ARRAY OF DATA IN
ASCENDING ORDER
Dr. N. ANURADHA
ASSISTANT PROFESSOR OF PHYSICS
BON SECOURS COLLEGE FOR WOMEN,
THANJAVUR
3. ARRANGE AN ARRAY OF DATA IN ASCENDING ORDER
BOOK : FUNDAMENTALS OF MICROPROCESSOR AND MICROCONTROLLER
AUTHOR : B. RAM
MEMORY
ADDRESS
MACHINE
CODES
LABELS
MNEMONICS
& OPERANDS
COMMENTS
2000 21,00,25 LXI H, 2500 Address of count for number of passes in H-L pair
2003 4E MOV C, M Count for number of passes in register C
2004 21,00,25 BACK LXI H, 2500 Address of count for number of comparisons in H-L pair
2007 56 MOV D, M Count for number of comparisons in register D
2008 23 INX H Address of 1st number in H-L pair
2009 7E MOV A, M 1st number in accumulator
200A 23 LOOP INX H Address of next number
200B 46 MOV B, M Next number in B Register
200C B8 CMP B Compare next number with previous number in Accumulator
200D D2,16,20
JNC AHEAD No Carry, larger number in accumulator. Go to the label AHEAD
Carry present , go to the next instruction.
2010 2B DCX H Decrement the content of H-L Pair
2011 77 MOV M, A Place smaller of the two compared numbers in memory
2012 78 MOV A, B Place greater of the two compared numbers in accumulator
2013 C3,18,20 JMP GO Unconditional Jump
2016 2B AHEAD DCX H Decrement the content of H-L pair
2017 70 MOV M, B Place smaller of the two compared numbers in memory
2018 23 GO INX H Increment the content of H-L Pair
2019 15 DCR D Decrease the count for comparisons
201A C2,0A,20 JNZ LOOP No Zero(D # 0), Go to the label LOOP
201D 77 MOV M,A Place greatest number after a pass in the memory
201E 0D DCR C Decrease the count for passes
201F C2,04,20 JNZ BACK No Zero(C # 0), Go to the label BACK
2022 76 HLT Stop
4. EXAMPLE 1 : DATA INPUT
2500 = 04
2501 = 60 ( Data β 1 ) 2504 = 15 ( Data β 4 )
2502 = 40 ( Data β 2 ) 2505 = 25 ( Data β 5 )
2503 = 50 ( Data β 3 )
LABEL
MNEMONICS
& OPERANDS STEP : 1 STEP : 2 STEP : 3 STEP : 4
LXI H, 2500 H-L = 2500 = 04
MOV C, M C = 04
BACK LXI H, 2500 H-L = 2500 = 04
MOV D, M D = 04
INX H H-L = 2501 = 60
MOV A, M A = 60
LOOP INX H H-L = 2502 = 40 H-L = 2503 = 50 H-L = 2504 = 15 H-L = 2505 = 25
MOV B, M B = 40 B = 50 B = 15 B = 25
CMP B A - B = 60 - 40, A - B = 60 - 50, A - B = 60 - 15, A - B = 60 - 25,
JNC AHEAD
No carry, Go to the
label AHEAD
No carry, Go to the
label AHEAD
No carry, Go to the
label AHEAD
No carry, Go to the
label AHEAD
DCX H
MOV M, A
MOV A, B
JMP GO
AHEAD DCX H H-L = 2501 H-L = 2502 H-L = 2503 H-L = 2504
MOV M, B 2501 = 40 2502 = 50 2503 = 15 2504 = 25
GO INX H H-L = 2502 H-L = 2503 H-L = 2504 H-L = 2505
DCR D D = 03 D = 02 D = 01 D = 00
JNZ LOOP
No zero, go to the
label LOOP
No zero, go to the
label LOOP
No zero, go to the
label LOOP
Zero, go to the next
instruction
MOV M,A 2505 = 60
DCR C C=03
JNZ BACK
No zero, go to the
label BACK
HLT
CYCLE - 1
5. AFTER THE CYCLE 1
2501 = 40 ( Data β 1 ) 2504 = 25 ( Data β 4 )
2502 = 50 ( Data β 2 ) 2505 = 60 ( Data β 5 )
2503 = 15 ( Data β 3 )
LABEL
MNEMONICS
& OPERANDS STEP : 1 STEP : 2 STEP : 3 STEP : 4
LXI H, 2500
MOV C, M
BACK LXI H, 2500 H-L = 2500 = 04
MOV D, M D = 04
INX H H-L = 2501 = 40
MOV A, M A = 40
LOOP INX H H-L = 2502 = 50 H-L = 2503 = 15 H-L = 2504 = 25 H-L = 2505 = 60
MOV B, M B = 50 B = 15 B = 25 B = 60
CMP B A - B = 40 - 50, A-B = 50 - 15 A-B = 50-25 A-B = 50 -60
JNC AHEAD
Carry present, go to
the next instruction
No carry, Go to the
label AHEAD
No carry, Go to the
label AHEAD
Carry present, go to the
next instruction
DCX H H-L = 2501 H-L = 2504
MOV M, A 2501 = 40 2504 = 50
MOV A, B A = 50 A = 60
JMP GO
AHEAD DCX H H-L = 2502 H-L = 2503
MOV M, B 2502 = 15 2503 = 25
GO INX H H-L = 2502 H-L = 2503 H-L = 2504 H-L = 2505
DCR D D = 03 D = 02 D = 01 D=00
JNZ LOOP
No zero, go to the
label LOOP
No zero, go to the
label LOOP
No zero, go to the
label LOOP
Zero, go to the next
instruction
MOV M,A 2505 = 60
DCR C C=02
JNZ BACK
No zero, go to the
label BACK
HLT
CYCLE - 2
6. AFTER THE CYCLE 2
2501 = 40 ( Data β 1 ) 2504 = 50 ( Data β 4 )
2502 = 15 ( Data β 2 ) 2505 = 60 ( Data β 5 )
2503 = 25 ( Data β 3 )
LABEL
MNEMONICS
& OPERANDS STEP : 1 STEP : 2 STEP : 3 STEP : 4
LXI H, 2500
MOV C, M
BACK LXI H, 2500 H-L = 2500 = 04
MOV D, M D = 04
INX H H-L = 2501 = 40
MOV A, M A = 40
LOOP INX H H-L = 2502 = 15 H-L = 2503 = 25 H-L = 2504 =50 H-L=2505
MOV B, M B = 15 B=25 B=50 B=60
CMP B A-B = 40 β 15 A-B = 40 -25 A-B = 40-50 A-B=50-60
JNC AHEAD
No carry, Go to the
label AHEAD
No carry, Go to the
label AHEAD
Carry present, go to
the next instruction
Carry present, go to
the next instruction
DCX H H-L=2503 H-L = 2504
MOV M, A 2503=40 2504 = 50
MOV A, B A=50 A=60
JMP GO
AHEAD DCX H H β L = 2501 H-L = 2502
MOV M, B 2501 = 15 2502 =25
GO INX H H-L = 2502 H-L =2503 H-L=2504 H-L = 2505
DCR D D = 03 D =02 D=01 D=00
JNZ LOOP
No zero, go to the
label LOOP
No zero, go to the
label LOOP
No zero, go to the
label LOOP
Zero, go to the next
instruction
MOV M,A 2505=60
DCR C C=01
JNZ BACK
No zero, go to the
label BACK
HLT
CYCLE - 3
7. AFTER THE CYCLE 3
2501 = 15 ( Data β 1 ) 2504 = 50 ( Data β 4 )
2502 = 25 ( Data β 2 ) 2505 = 60 ( Data β 5 )
2503 = 40( Data β 3 )
LABEL
MNEMONICS
& OPERANDS STEP : 1 STEP : 2 STEP : 3 STEP : 4
LXI H, 2500
MOV C, M
BACK LXI H, 2500 H-L = 2500 = 04
MOV D, M D = 04
INX H H-L = 2501 = 15
MOV A, M A = 15
LOOP INX H H-L = 2502 = 25 H-L = 2503 = 40 H-L = 2504 =50 H-L=2505
MOV B, M B = 25 B=40 B=50 B=60
CMP B A-B = 15 β 25 A-B = 25 -40 A-B = 40-50 A-B=50-60
JNC AHEAD
Carry present, go to
the next instruction
Carry present, go to
the next instruction
Carry present, go to
the next instruction
Carry present, go to
the next instruction
DCX H H-L = 2501 H β L = 2502 H-L=2503 H-L = 2504
MOV M, A 2501 = 15 2502 = 25 2503=40 2504 = 50
MOV A, B A = 25 A = 40 A=50 A=60
JMP GO
AHEAD DCX H
MOV M, B
GO INX H H-L = 2502 H-L =2503 H-L=2504 H-L = 2505
DCR D D = 03 D =02 D=01 D=00
JNZ LOOP
No zero, go to the
label LOOP
No zero, go to the
label LOOP
No zero, go to the
label LOOP
Zero, go to the next
instruction
MOV M,A 2505=60
DCR C C=00
JNZ BACK
Zero, go to the next
instruction
HLT HLT
CYCLE - 4
8. DATA INPUT
2500 = 04
2501 = 60 ( Data β 1 )
2502 = 40 ( Data β 2 )
2503 = 50 ( Data β 3 )
2504 = 15 ( Data β 4 )
2505 = 25 ( Data β 5 )
THE FINAL RESULT OF THE PROGRAM
2501 = 15
2502 = 25
2503 = 40
2504 = 50
2505 = 60