COMP1036 Coursework Part II (25 marks)
Coursework Part II
项目类别:计算机
Write a program in Hack Assembly Language that sorts an array of integers in ascending or descending order. The
unsorted array contains 5 or more elements, located at a range of memory locations starting from RAM[50]. The integers
in the array can be positive, negative, or zero.
The program should allow you to sort either the entire array or a portion of it. The number of elements to be sorted is
determined by the integers stored in RAM[0] and RAM[1], as described below:
Read two input values, X and Y, from RAM[0] and RAM[1], respectively, and output the computed result, Z, to RAM[2].
X and Y can be positive or negative. The program should function correctly regardless of whether X < Y, X > Y, or X =
Y.
Different rules will be applied based on whether X and Y are even or odd integers, as follows:
(1)
IF both X and Y are even integers, THEN Z is the sum of all even integers between X and Y (inclusive).
(2)
IF both X and Y are odd integers, THEN Z is the sum of all odd integers between X and Y (inclusive).
(3)
IF one of X or Y is odd and the other is even, THEN Z is the sum of all integers between X and Y (inclusive).
(4)
IF X = Y, THEN Z = X or Z = Y.
(5)
IF Z is positive, THEN sort the array in ascending order.
(6)
IF Z is negative, THEN sort the array in descending order.
(7)
IF Z is zero, THEN no sorting should be done.
Example 1:
Given RAM[0] = X = -4; RAM[1] = Y = 2
The range of integers between X and Y (inclusive) is [-4, -3, -2, -1, 0, 1, 2].
Applying Rule (1): RAM[2] = Z = (-4) + (-2) + 0 + 2 = -4.
Applying Rule (6) for Z = -4: Sort the first 4 elements of the array in descending order.
Example 2:
Given RAM[0] = X = -5; RAM[1] = Y = 5
The range of integers between X and Y (inclusive) is [-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5].
Applying Rule (2): RAM[2] = Z = (-5) + (-3) + (-1) + 1 + 3 + 5 = 0.
Applying Rule (7) for Z = 0: No sorting should be done.
Example 3:
Given RAM[0] = X = 2; RAM[1] = Y = 3
The range of integers between X and Y (inclusive) is [2, 3].
Applying Rule (3): RAM[2] = Z = 2 + 3 = 5.
Applying Rule (5) for Z = 5: Sort the first 5 elements of the array in ascending order.
Example 4:
Given RAM[0] = X = 3; RAM[1] = Y = 3
The range of integers between X and Y (inclusive) is [3].
Applying Rule (4): RAM[2] = Z = 3.
Applying Rule (5) for Z = 3: Sort the first 3 elements of the array in ascending order.
留学ICU™️ 留学生辅助指导品牌
在线客服 7*24 全天为您提供咨询服务
咨询电话(全球): +86 17530857517
客服QQ:2405269519
微信咨询:zz-x2580
关于我们
微信订阅号
© 2012-2021 ABC网站 站点地图:Google Sitemap | 服务条款 | 隐私政策
提示:ABC网站所开展服务及提供的文稿基于客户所提供资料,客户可用于研究目的等方面,本机构不鼓励、不提倡任何学术欺诈行为。