Currently trying to make a program that bubble sorts several numbers either in ascending or descending order, where type of order is decided by the CV in 00h. So, i just can't pinpoint what's the reason for this problem, because when i try to compile the code it says:"Label STARTASC is not defined. Also check the label definition, there may be some issues on that line." Here is code:
mvi H,00
MOV A, M ; Load control value into accumulator
CPI 00H ; Compare with 00
JZ STARTDESC ; If equal, jump to descending sort
JMP STARTASC ; Otherwise, jump to ascending sort
STARTDESC: LXI H, 2500H ;Load the size of the array(number)
MVI B, 00H ;Set register B to zero
MOV C, M ;Set register C to the number of array's elements
DCR C ;Decrease C
INX H ;Increment the pointer to access the array's element
CHECKDESC:
MOV A, M ;Store array's elements in ACC
INX H ;Increment the pointer to access the next array's element
CMP M ;Compares ACC with the next element
JNC NEXTDESC ;Jumps to NEXTDESC if ACC value is less
MOV D, M ;Swaps the elements that were being compared
MOV M, A ;...
DCX H
MOV M, D ;...
INX H
MVI B, 01H ;Stores 1 in register B, if the swap is made
NEXTDESC:
DCR C ;Decrement register C for the next iteration
JNZ CHECKDESC ;If c>0, then jumps to CHECKDESC
MOV A, B ;Moves contents of register B to the ACC
CPI 01H ;Compares ACC with 01H(1)
JZ STARTDESC ;If B=1, then jumps to the start of the loop
HLT ;If not, then the sorting is done and program stops
STARTASC: LXI H, 2500H ;Load the size of the array(number)
MVI B, 00H ;Set register B to zero
MOV C, M ;Set register C to the number of array's elements
DCR C ;Decrease C
INX H ;Increment the pointer to access the array's element
CHECKASC:
MOV A, M ;Store array's elements in ACC
INX H ;Increment memory to access next element
CMP M ;Compares ACC with next element
JC NEXTASC ;Jump to NEXTASC, if ACC value is less then
JZ NEXTASC ;or Jump to NEXTASC, if ACC value is equal
MOV D, M ;Swaps the elements that were being compared
MOV M, A ;...
DCX H
MOV M, D ;...
INX H
MVI B, 01H ;Stores 1 in register B, if the swap is made
NEXTASC:
DCR C ;Decrement register C for the next iteration
JNZ CHECKASC ;If c>0, then jumps to CHECKDESC
MOV A, B ;Moves contents of register B to the ACC
CPI 01H ;Compares ACC with 01H(1)
JZ STARTASC ;If B=1, then jumps to the start of the loop
HLT ;If not, then the sorting is done and program stops
So just want to know why is this happening.
It might be a platform issue. Some platforms, such as this online simulator, do not correctly parse labels on empty lines. Thus, it now compiles smoothly by simply changing the following:
to