0% found this document useful (0 votes)
3 views9 pages

Lab 0

Cnfbgkhvvb

Uploaded by

Linh N.N
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views9 pages

Lab 0

Cnfbgkhvvb

Uploaded by

Linh N.N
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Lab 06.

Array and Pointer


Hồ Nhật Triều -20236003
Assignment 1:
Task: Find the maximum prefix sum in an array.

Here is the output of program:


t0= 0x00000005 (5 index)
The loop increments t0 with each iteration as it processes each element of the array.
Once it finishes processing the last element (A[4]), t0 is incremented to 5, indicating that
the program has gone through all 5 elements of the array.
At this point, the loop terminates, because t0 == a1 (where a1 = 5, the number of elements
in the array).
t1 = 0x00000002 (Running sum)
As the program processes each element in the loop, it adds that element to t1. So, t1
accumulates the sum as it moves through the array.
After the final element -4 is added to the running sum, the total is 2. This value is stored in
t1 at the end of the program.
s0 = 0x00000004 (Prefix length)
In this array, the maximum prefix sum is found when the first 4 elements are considered (2,
-3, 2, 5). The sum of these 4 elements is 6:
2 + (-3) + 2 + 5 = 6
As a result, when the program detects this maximum sum, s0 is updated to 4, indicating
that the maximum sum involves the first 4 elements.
The final element, -4, reduces the running sum to 2, which is lower than the maximum
sum, so s0 remains at 4.
s1 =0x00000006 (Maximum prefix sum)
When the program processes the first 4 elements (2, -3, 2, 5), the sum reaches 6. Since 6 is
larger than the initial value of s1 (which starts as the smallest possible integer,
0x80000000), s1 is updated to 6.
Assignment 2:

Main Sorting Logic:


• The selection sort algorithm iteratively finds the largest element in the unsorted
portion of the array.
• The largest element is swapped with the last element of the unsorted portion.
Max Procedure:
• The max subroutine finds the maximum value in the unsorted part of the array by
iterating from the first element (a0) to the last element (a1).
• The values of the maximum element are stored in registers s0 (address) and s1
(value).
Print Array Subroutine:
• After each sorting pass, the print_array subroutine prints the current state of the
array.
• The array is printed element by element, with a space separating each number,
followed by a newline.
Here is the output of the program:

Assignment 3:

As we can see in the data segment, Memory Locations Before Sorting (Initial Array)
• 0x10010000: 0x00000007 → 7
• 0x10010004: 0xFFFFFFFE → -2 (as a signed integer)
• 0x10010008: 0x00000005 → 5
• 0x1001000C: 0x00000001 → 1
• 0x10010010: 0x00000006 → 6
This represents the original unsorted array: [7, -2, 5, 1, 6].

Memory Locations After Sorting


Once the program finishes running, the values in these memory locations should change to
reflect the sorted array. Since Bubble Sort sorts the array in ascending order, the memory
values should represent the sorted array [-2, 1, 5, 6, 7].
Here’s what you should expect in the Data Segment after sorting:
• 0x10010000: 0xFFFFFFFE → -2
• 0x10010004: 0x00000001 → 1
• 0x10010008: 0x00000005 → 5
• 0x1001000C: 0x00000006 → 6
• 0x10010010: 0x00000007 → 7

Program Output: The program sorts the input array. In this case, it sorts [7, -2, 5, 1, 6] into [-2, 1,
5, 6, 7].

Data Segment After Sorting: Check memory starting at 0x10010000. After sorting, these memory
locations will hold the sorted array values.

Registers: As the program runs, registers like t4 and t5 will show which elements are being
compared and swapped. You can track the sorting process by looking at these registers.
Assignment 4:
Key Sections:

1. Data Section:
o Strings like "Iteration", space, newline are stored.
o The array A (to be sorted) is defined.
2. Main Section:
o Initializes pointers to the array (a0, a1, a2), iteration counter (a3), and a flag (t5 to
track if sorting is complete).
o Jumps to the insertion_sort section.
3. Insertion Sort:
o Compares and inserts elements like in the insertion sort algorithm.
o Inner while loop shifts elements to the right if necessary.
o The sorted element is inserted back when correct.
4. Print Phase:
o After every iteration, the array is printed.
o Each element is printed followed by a space. After printing the whole array, a
newline is printed.
o The program checks if sorting is complete; if yes, it ends, otherwise it continues
sorting.

Here is the output of the program:


The output shows the process of executing the insertion sort algorithm on array A, with each step
printing the array after sorting:

• Iteration 1: Element 3 is inserted before 9. Result: 3 9 7 1 5 8 2.


• Iteration 2: Element 7 is inserted between 3 and 9. Result: 3 7 9 1 5 8 2.
• Iteration 3: No change, as 9 is already in place.
• Iteration 4: Element 1 is inserted at the start. Result: 1 3 7 9 5 8 2.
• Iteration 5: Element 5 is inserted between 3 and 7. Result: 1 3 5 7 9 8 2.
• Iteration 6: Element 8 is inserted between 7 and 9. Result: 1 3 5 7 8 9 2.
• Iteration 7: Element 2 is inserted between 1 and 3. Final result: 1 2 3 5 7 8 9.

In the end, the array is fully sorted.

You might also like