My university task is to make code that will sort(from smallest to largest) 10 data(numbers) from memory stored at adresses from 50H to 60H. I've been working on this for 5 hours and didn't succeeded.
I think my side of logic is good but somehow JP instruction is always done.
This is text representation of what did I(line by line):
I loaded HL register to adress 50H, then loaded accumulator A with data in memory at adress 50H, I increased HL register by one so it points to 51H, loaded register B with data at 51H. Put AND A to reset carry flag. Subtracted A-B regster, decreased HL register so it points to adress 50H.Loaded A again with data at 50H(because after subtraction register A was overwritten,but I got carry flag), increased HL (51H),
then
Exclamation this is where my program dies, line JP C, MEM should jump to MEM spot when carry flag is set to 1 but for me it always jumps (regardless of carry flag)Exclamation
at MEM:
load adress 51H with data at A register,decreased HL(50H) and load 50H with B register
Basicly at MEM I switched data from 50H to 51H and from 51H to 50H because subtraction showed carry flag.
Again, my problem is whether subtraction is positive or negative (carry flag set to 1 OR 0) I have execution of MEM subprogram.
Code:
LD HL,50H
LD A,(HL)
INC HL
LD B,(HL)
AND A
SBC A,B
DEC HL
LD A,(HL)
INC HL
JP C, MEM
MEM:
LD (HL),A
DEC HL
LD (HL),B
HALT
END
This is only the beginning of the program it's not complete program for sorting.
I thought of loading 2 data first then comparing them and putting them in memory sorted then loading next data and comparing them with previous two and sending it on appropirate position.
Is that what bubble sort does?
The first iteration will give you the smallest number in the lot ..
The second iteration ( 9 numbers to deal with ) will give you the second smallest number .. and so on ..
The last iteration, which in fact will compare only 2 numbers, will give you the biggest number in the lot ..
I made program which out of 10 numbers in memory finds the smallest one.
i put that number on first adress (50H) and the first number to put on it's place.
I used adress 70H to store adress from smallest number among those 10 numbers
here's the code:
LD HL,50H
LD D,9
LOOP1:
LD A,(HL)
INC HL
LOOP:
LD B,(HL)
CP B
CALL NC,FLAG0
CALL C,FLAG1
DEC D
JR NZ,LOOP
LD HL,50H
LD C,(HL)
LD HL,(70H)
LD (HL),C
LD HL,50H
LD (HL),A
HALT
FLAG0:
LD A,B
LD (70H),HL
INC HL
LD B,(HL)
RET
FLAG1:
INC HL
LD B,(HL)
RET
NEXT STEP IS TO DO THE SAME PROCEDURE BUT I MUST EXCLUDE FIRST NUMBER ON ADRESS 50H BECAUSE IT IS THE SMALLEST SO I DON'T WANT IT TO BE IN SEARCH AGAIN.
THAT MEANS SECOND LINE MUST POINT TO 8 AND STARTING ADRESS MUST BE 51H (FIRST TWO LINES IN CODE).
PROBLEM IS THAT I DON'T KNOW HOW TO DECREASE THEM AT THE BEGINNING OF A CODE AND AT THE END WHERE IT WILL PUT SECOND SMALLEST NUMBER ON THE SAME ADRESS AS FIRST ONE. I COULD DO IT MANUALLY BUT IT'S NOT USEFUL.
Hammer111:
Please test this sub-routine, I haven't try it
Code:
; Memory area 50h until 60h (17 bytes)
BubbleSort:
LD DE,0050h
Scan:
LD A,(DE)
PUSH DE
POP HL
INC HL ; HL = DE + 1
Compare:
LD B,(HL)
CP A,B ; A => B
JR NC,NoSwap ; yes, don't swap
LD C,B
LD B,A
LD A,C ; Swap A : B
LD (DE),A ; Save to top most location
NoSwap:
INC HL ; Next byte to compare
LD A,61h
CP A,L ; end of array?
JR NZ,Compare ; not yet, next compare
INC DE ; Next top most location
LD A,60h
CP A,E ; end of scan?
JR NZ,Scan ; not yet, next Scan
RET
Acumulator is stuck with the adress 61H in your code.
I made code using some of your lines which helped me.
The only thing is that I don't know how to stop loop at last line (JR LOOP), I want it to stop when it reaches adress 5AH
I tried putting this instead of JR LOOP line
LD A,5AH
CP L
JR NZ LOOP
it works for a few cycles (for 3 or 4 numbers) and then it stops (it stops somewhere around 54H
Code:
;load adress 50H
LD DE,50H
LOOP:
LD A,(DE)
PUSH DE
POP HL
COMPARE:
INC HL
LD B,(HL)
CP B ; a>=b
CALL NC,SWAP ;if carry=0 switch data
LD C,A
LD A,5AH
CP L
LD A,C
CALL NZ,COMPARE
JP SWAPNO
SWAP:
PUSH HL
POP IX
LD C,A
LD A,B
LD B,C
RET
SWAPNO:
PUSH DE
POP HL
LD C,(HL)
LD (DE),A
PUSH IX
POP HL
LD L,H
LD H,0
LD (HL),C
INC DE
JP LOOP
HALT
BubbleSort:
LD DE,0050h
Scan:
PUSH DE
POP HL
INC HL ; HL = DE + 1
Compare:
LD A,(DE)
LD B,(HL)
CP B ; A < B
JR C,NoSwap ; yes, don't swap
LD C,B
LD B,A
LD A,C
LD (DE),A ; Save to top most location
LD (HL),B
NoSwap:
INC HL ; Next byte to compare
LD A,061h
CP L ; end of array?
JR NZ,Compare ; not yet, next compare
INC DE ; Next top most location
PUSH DE
POP DE
LD A,060h
CP E ; end of scan?
JR NZ,Scan ; not yet, next Scan
RET
off topic:
if you liked your z8 maybe you will be interested in learning the zilog microcontrollers family which have a free c compiler i think after you wrote in assembly you will find programming in c a breath