Question: I have this exercise Write a program in Hack assembly to sort a given array in - place in ascending order ( smallest to largest

I have this exercise
Write a program in Hack assembly to sort a given array in-place in ascending order (smallest to largest).
You may implement any sorting algorithm but should aim for a complexity of O(n2) or better.
Complete the code in ArrSort.asm
Inputs:
R1 contains the RAM address of the first element in the array
R2 contains the length of the array
Outputs:
Write your True (-1) to R0 when your program finishes.
The correctly sorted array should replace the original array in its location.
I have this code for the ArrSort.asm file:
@R1
A=M // getting the address of the first element @20
D=M
@R2
M=M-1
D=M
@LENG
M=D
(FIRST_LOOP)
@R1
A=M //(A=20)
(SECOND_LOOP)
@R1
A=M // getting the address of the first element @20
D=M // D=20(trying to get the value at memory address 20 which is 2)
A=A+1//A=21
D=M-D // Compare the current element with the next element
// If D is positive or zero, the current element is greater or equal to the next element
@GREATER
D;JGE
// Swap the current element with the next element
@SWAP
D;JLT // less than zero
(SWAP)
// Swap the elements in memory
@R1
A=M
D=M
@TEMP
M=D // TEMP stores 2
@R1
A=M+1
D=M // D=1
A=A-1
M=D //2 is replace with one
@TEMP
D=M // TEMP stores 2
@R1
A=M+1
M=D
@GREATER
//0;JMP
(GREATER)
@R1
M=M+1
@LENG
M=M-1
D=M
@SECOND_LOOP
D;JGT
@R1
M=M+1
@R2
M=M-1
D=M
@FIRST_LOOP
D;JGT
@END
D;JEQ
(END)
@R0
M=-1
(END1)
@END1
0;JMP
I need to pass the following tests:
Positive Integers
Duplicates
Zeros
Array Size/Location
Negative Integers
Mixed Integers
Edge Cases
but I have not been able to pass the test with code given above
As well it seems it is rebundant and it needs to improve the performance.
I need help improving the code please

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!