Why isn't my selection sort HLA code correctly sorting out an array with labelled switches?

20 Views Asked by At

Selection sort using labels does not sort out array in HLA.

The code below is supposed to sort an array with each switch in the array being printed. The code is supposed to be in the style of "pure assembly" (this is an assignment). I have tried changing the way ebx and ecx change values but did not have success. I am also thinking that my fundamental knowledge is what is holding me back on this assignment. Any and all help is greatly appreciated.

program Project5;
#include( "stdlib.hhf" );
#include( "stdio.hhf" );

const
    NumElements: int32 := 15;

static
    DataToSort: uns32 [NumElements] := [62, 20, 97, 22, 58, 12, 45, 10, 78, 13, 27, 54, 50, 40, 35];
    lastIndex:  int32 := 14;
    Index:      int32;
    temp:       int32;

begin Project5;

    stdout.put( "Selection Sort, Number of Array NumElements: ", NumElements, nl );

    // Print original array elements
    stdout.put( "Original Array Elements:", nl );
    mov( 0, ebx );                 // Initialize ebx (Index) to 0
    
Print_Array_Elements:
    cmp(ebx, lastIndex);
    jg End_Print_Array_Elements;
    stdout.put(DataToSort[ebx*4]: 4);
    inc(ebx); 
    jmp Print_Array_Elements;
    
End_Print_Array_Elements:
stdout.put(nl,nl);

jmp Loop_while;

    Loop_while:
    cmp(lastIndex, 0);
    je End_loop_start;
    mov(0, Index);
    mov(0, eax);
    jmp Loop_For;

    Loop_For:
    cmp(eax, lastIndex);
    jg Copy_to_registers;
    mov(Index, ebx);
    mov(DataToSort[ebx*4], ecx);
    cmp (DataToSort[eax*4], ecx);
    jg Loop_If_Condition;
    jmp Loop_For_Condition;
    
    Loop_If_Condition:
        mov(eax,Index);
    jmp Loop_For_Condition;
    
    Loop_For_Condition:
    inc(eax);
    jmp Copy_to_registers;
    
    Copy_to_registers:
        mov(lastIndex, ecx);
    mov(Index, ebx);
    jmp startNestLoop1;

    startNestLoop1:
    mov(DataToSort[ecx * 4], temp);
    mov(DataToSort[ebx * 4], DataToSort[ecx * 4]);
    mov(temp, DataToSort[ebx * 4]);
    dec(lastIndex);
    cmp(ebx, 0);
    jne nest_Loop_initiation;
    jmp startNestLoop2;
    
    nest_Loop_initiation:
    mov(0,ebx);
    jmp startNestLoop2;
    
    startNestLoop2:
    stdout.put(DataToSort[ebx*4]: 4);
    inc(ebx);
    cmp (ebx, 15);
    jge endNestLoop2;

    jmp startNestLoop2;
    
    endNestLoop2:
    stdout.put(nl);
    jmp Loop_while;

End_loop_start:
end Project5;
0

There are 0 best solutions below