Why does this code not work with Bubble sort

33 views Asked by At

These variable has ALREADY been defined:

  int i, k, Done;     int A[];                // Array to be sorted   int N;          // contains the length of the array

Write the following Bubble Sort algorithm in ARM assembler

   Done = 0;             // 0 represents false
   k = 1;

   while (Done == 0) 
   {  
      Done = 1;          // 1 represents true.

      for (i = 0; i < N-k; i++)
      { 
         if (A[i] > A[i+1])
         { 
   Swap A[i] and A[i+1];  // Use registers to swap value
             Done = 0;            // Inidcate not sorted
   }    }
      k = k + 1;
   }

NOTE: All variables used in the program have ALREADY been defined for you DO NOT define any new variables in the code. ============================================================= */

        .global main, Stop, CodeEnd, DataStart, DataEnd

   main:
    MOV r4, #0
    MOV r5, #1
    LDR r7, =N

OuterLoop:
    MOV r4, #1

    MOV r6, #0

InnerLoop:
    LDR r8, =N
    SUB r8, r8, r5
    CMP r6, r8
    BGE UpdateK

    LDR r9, =A

    MOV r10, r6
    LSL r10, r10, #2
    ADD r9, r9, r10

    LDR r11, [r9]
    ADD r10, r6, #1
    LSL r10, r10, #2
    ADD r9, r9, r10

    LDR r12, [r9]

    CMP r11, r12
    BLE NoSwap

    STR r12, [r9, #-4]
    STR r11, [r9]

    MOV r4, #0

NoSwap:
    ADD r6, r6, #1
    B InnerLoop

UpdateK:
    ADD r5, r5, #1
    MOV r6, #0
    B OuterLoop
     



Stop:
    nop

CodeEnd:


/*********************************************************************
   DO NOT make any changed to the code below this line

   The include file "variables.h" contains the variable definitions for

        int i, k, Done;
        int A[];                // Array to be sorted
        int N;                  // contains the length of the array
 *********************************************************************/

#include "variables.h"

.end

This code compiles successfully but the array does not change after i run it.

0

There are 0 answers