The following code is the bubble sort algorithm that you must convert to an assembler implementation. The algorithm must sort the contents of the A array into ascending order. You should make use of mnemonic names in your program to represent variables and locations within the code to branch to when executing a jump instruction.
A couple of tips that might be useful as you develop your program:
Comparisons:
Your program will need to compare two numbers to determine if one is larger than the other. In assembler, we do not have an 'if' expression that allows us to test the equality or inequality of two variables. The way that this capability can be implemented is by subtracting the second number from the first number and then jumping based upon the result. For example, if A and B are our two numbers then the rules for A-B are as follows:
If A-B = 0, then A=B.
If A-B > 0, then A > B.
If A-B < 0,="" then="" a=""><>
If the result of the operation is 0 then we can execute a JEQ which means jump if the output of the operation is equal to zero (see the jump table below).
ATTACHED- RTY
In the algorithm, we need to be able to test if a variable is less than, greater than, or equal to either another variable or a constant value. Each of these tests can be accomplished by subtracting the two numbers and using the appropriate jump instructions such as:
JLT - jump less than 0
JGT - jump greater than 0
JEQ - jump equal to 0
For this exercise, you should develop your solution to the exercise and test it using the CPU emulator. Your completed assignment should be shared with your peers by posting it to the unit forum.
//Bubble Sort Algorithm in Pseudocode
A[0] =5
A[1]=4
A[2]=3
A[3]=2
A[4]=1
A[5]=0
int j;
flag = 1; // set flag to true to begin first pass
int temp; //holding variable
while ( flag == 1)
{
flag= false; //set flag to false awaiting a possible swap
for( j=0; j < 6; ="" j++="">
{
if (A[ j ] > A[j+1] ) // test for ascending sort
{
temp = A[ j ]; //swap elements
A[ j ] = A[ j+1 ];
A[ j+1 ] = temp;
flag = 1; //shows a swap occurred
}
}
}
Extracted text: 12 Macmonie Ellect (ond < 0)="" (aa="0)" (oet=""> 0) No jump If ent >0 jump If out = 0 jump If out 20 jump If ent