Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Beginning VB.NET Databases

Authors: Thearon Willis

1st Edition

1594864217, 978-1594864216

More Books

Students also viewed these Databases questions

Question

How flying airoplane?

Answered: 1 week ago