Answered step by step
Verified Expert Solution
Question
1 Approved Answer
In memory, an array is stored in a contiguous block of memory locations. The start of the array is specified by the base address. The
In memory, an array is stored in a contiguous block of memory locations. The start of the array is specified by the base address. The i-th element of the array is stored in memory location base_address + (i size_of_data_type), where "size_of_data_type is the number of memory locations required to store a single element. For example, say we had an array B of 32-bit bit vectors stored in LC-3 memory starting at memory location x3100. First, note that the size_of_data_type = 2 because we need two memory locations to store a single 32-bit bit vector in the LC-3. Therefore, B[O] will be contained in memory location x3100, B[1] will be contained in memory location x3102, B[2] will be contained in memory location x3104, and so on. In general, the i-th element of B will be stored in memory location x3100 + 2i. Your Job: Write a program in LC-3 assembly language to sort an array NUMBERS of 2's complement integers. Your program should assume that the base address of NUMBERS is stored in memory location x3100 and the number of elements n in the array is stored in memory location x3101. Each memory location contains a single 2's complement integer. That is, each memory location contains a single element of the array. Your program should sort the array of 2's complement integers in descending order (largest-to-smallest) and store the result back in memory starting at the base address of the array. Your program should start at memory location x3000. Example The base address and the number of elements n in memory: Location Contents x 3100 x32F0 x3101 x0010 Therefore, there is an array in memory starting at memory location x32F0 and has 16 elements. Here are the memory locations before sorting: Location Contents x32F0 xFFFF x32F1 x0062 x32F2 x0A73 x 32 F3 x006C x32F4 x0070 x32F5 x0001 x32F6 x0063 x32F7 x0065 x32F8 x0062 x32 F9 x0073 x32 FA x006E x 32 FB x006B x 32 FC xFF76 x 32 FD x0F7A x 32 FE x0068 x32FF x006D Memory locations x32F0 through x32FF after sorting: Location Contents x32F0 x0F7A x32F1 X0A73 x 32 F2 x0073 x32 F3 x0070 x32F4 x006E x32F5 x006D x32F6 x006C x32F7 x006B x32F8 x0068 x32F9 x0065 x 32 FA x0063 x 32 FB x0062 x 32 FC x0062 x 32 FD x0001 x 32 FE xFFFF x32FF xFF76 File name: Write your program in a file called sort.asm. Hint 1: How would you find the largest 2's complement integer in the array? Hint 2: If you swap the largest 2's complement integer in the array with the first 2's complement integer in the array, you now have to sort an array with n - 1 elements (starting from one memory location after the base address). Hint 3: This number involves comparing numbers, just like Programming Lab 1. Make sure the way you compare numbers does not result in overflow. Note: There are multiple ways to solve this problem. If you have a solution that doesn't rely on the hints above, then great! If you are struggling to start, the hints above are designed to help Test Case To help you get started, we are providing you with a simple test case. Once you have code that works on this test case, make your own (more difficult) test cases to ensure your program works. The test case is an array with 5 elements and the base address is x3200: Location Contents x3100 x 3200 x3101 x0005 Here is the array starting at memory location x3200: Location Contents x 3200 x0002 x3201 x0001 x3202 x0004 x3203 x0003 x3204 x0005 After sorting, the array should look like: Location Contents x 3200 x0005 x3201 x0004 x3202 x0003 x3203 x0002 x3204 x0001 Here is the code you need to paste into the LC-3 simulator to get the above test case into memory. You can paste this below your program. ORIG X3100 FILL X3200 FILL x0005 END .ORIG x3200 FILL x0002 . FILL x0001 .FILL X0004 .FILL x0003 . FILL x0005 .END In memory, an array is stored in a contiguous block of memory locations. The start of the array is specified by the base address. The i-th element of the array is stored in memory location base_address + (i size_of_data_type), where "size_of_data_type is the number of memory locations required to store a single element. For example, say we had an array B of 32-bit bit vectors stored in LC-3 memory starting at memory location x3100. First, note that the size_of_data_type = 2 because we need two memory locations to store a single 32-bit bit vector in the LC-3. Therefore, B[O] will be contained in memory location x3100, B[1] will be contained in memory location x3102, B[2] will be contained in memory location x3104, and so on. In general, the i-th element of B will be stored in memory location x3100 + 2i. Your Job: Write a program in LC-3 assembly language to sort an array NUMBERS of 2's complement integers. Your program should assume that the base address of NUMBERS is stored in memory location x3100 and the number of elements n in the array is stored in memory location x3101. Each memory location contains a single 2's complement integer. That is, each memory location contains a single element of the array. Your program should sort the array of 2's complement integers in descending order (largest-to-smallest) and store the result back in memory starting at the base address of the array. Your program should start at memory location x3000. Example The base address and the number of elements n in memory: Location Contents x 3100 x32F0 x3101 x0010 Therefore, there is an array in memory starting at memory location x32F0 and has 16 elements. Here are the memory locations before sorting: Location Contents x32F0 xFFFF x32F1 x0062 x32F2 x0A73 x 32 F3 x006C x32F4 x0070 x32F5 x0001 x32F6 x0063 x32F7 x0065 x32F8 x0062 x32 F9 x0073 x32 FA x006E x 32 FB x006B x 32 FC xFF76 x 32 FD x0F7A x 32 FE x0068 x32FF x006D Memory locations x32F0 through x32FF after sorting: Location Contents x32F0 x0F7A x32F1 X0A73 x 32 F2 x0073 x32 F3 x0070 x32F4 x006E x32F5 x006D x32F6 x006C x32F7 x006B x32F8 x0068 x32F9 x0065 x 32 FA x0063 x 32 FB x0062 x 32 FC x0062 x 32 FD x0001 x 32 FE xFFFF x32FF xFF76 File name: Write your program in a file called sort.asm. Hint 1: How would you find the largest 2's complement integer in the array? Hint 2: If you swap the largest 2's complement integer in the array with the first 2's complement integer in the array, you now have to sort an array with n - 1 elements (starting from one memory location after the base address). Hint 3: This number involves comparing numbers, just like Programming Lab 1. Make sure the way you compare numbers does not result in overflow. Note: There are multiple ways to solve this problem. If you have a solution that doesn't rely on the hints above, then great! If you are struggling to start, the hints above are designed to help Test Case To help you get started, we are providing you with a simple test case. Once you have code that works on this test case, make your own (more difficult) test cases to ensure your program works. The test case is an array with 5 elements and the base address is x3200: Location Contents x3100 x 3200 x3101 x0005 Here is the array starting at memory location x3200: Location Contents x 3200 x0002 x3201 x0001 x3202 x0004 x3203 x0003 x3204 x0005 After sorting, the array should look like: Location Contents x 3200 x0005 x3201 x0004 x3202 x0003 x3203 x0002 x3204 x0001 Here is the code you need to paste into the LC-3 simulator to get the above test case into memory. You can paste this below your program. ORIG X3100 FILL X3200 FILL x0005 END .ORIG x3200 FILL x0002 . FILL x0001 .FILL X0004 .FILL x0003 . FILL x0005 .END
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started