Sorting in ascending order using 8085.

Pragyaditya Das picture Pragyaditya Das · Jan 30, 2015 · Viewed 10.3k times · Source

So, here in the following code, I am writing a code to sort numbers in ascending order.

start: nop

MVI B, 09 ; Initialize counter     
LXI H, 2200H  ;Initialize memory pointer
MVI C, 09H; Initialize counter 2
BACK: MOV A, M ;Get the number
INX H ;Increment memory pointer
CMP M; Compare number with next number
JC SKIP;If less, don't interchange
JZ SKIP; If equal, don't interchang
MOV D, M
MOV M, A
DCX H
MOV M, D
INX H ;Interchange two numbers
DCR C  ; Decrement counter 2
JNZ BACK ;If not zero, repeat
DCR B ; Decrement counter 1
JNZ START
HLT ; Terminate program execution

This was that was taught in class. When I try running the code in GNUSim, I get errors like :

1. Line 9: Undefined symbol.
2. Line 9: Invalid operand or symbol.Check whether operands start with a 0. Like a0H should be 0a0H.

Can somebody help?

Answer

andriyleu picture andriyleu · Jun 22, 2015

In 8085 (js8085) I'd do it the next way (using bubble sort):

@begin 0100
@next 0100
MVI A 00
MVI B 00
MVI C 00
MVI D 00
MVI E 00
MVI H 00
MVI L 00
IN 00
out 00
DCR A 
out 06
bubble: in 06
cmp c
jz finished
inr e
ldax b
mov h,a
ldax d
cmp h
jc change;
comprobation: in 00
cmp e
jz semi-fin
call bubble
semi-fin: inr c
mov a,c
mov e,c
call bubble
change: stax b
mov a,h
stax d
call comprobation
finished: hlt

In the port 00 you got the number of elements you have and the the elements themselves are starting from the position 0000 to the number of elements - 1.