Selection sort in LC3 assembly
I'm trying to code selection sort in LC3 assembly, but after hours of trying, I cannot seem to figure out how to do it. The pseudocode I have written to model my selection sort algorithm is:
for i = 0 to ARRAY.LENGTH  1 > //iterate through every position in array
min_idx = i //find element that will move to position i
for j = i + 1 to ARRAY.LENGTH >
if ARRAY_ADDRESS[j] < ARRAY_ADDRESS[min_idx] >
min_idx = j
swap(ARRRAY_ADDRESS[i], ARRAY_ADDRESS[min_idx])
The array to be sorted is located at ARRAY_ADDRESS somewhere in memory and the the length of the array is stored in the value ARRAY.LENGTH.
Any suggestions or clues where to start? I've written my assembly but it does not seem to work and I have no clue why. Thanks! Here's a link to the text document: https://textuploader.com/dnkrt
1 answer

This is an honor code violation.
See also questions close to this topic

multi level sorting of numerical and string values
I want to do a multi level sorting of floating values using awk on a data like below:
store:LA400.68
pens200.34
reynolds110.34
butterflow90.00
trimexNA
copies110.34
classmate110.34
pencil90.00
HB44.5
classmate45.5the numerical value is the amount of available stock.
the sorted result should be like
store:LA400.68
pencil90.00
HB44.5
classmate45.5
copies110.34
classmate110.34
pens200.34
butterflow90.00
reynolds110.34
trimexNAin ascending order first based on the product and in product based on the brand with NA value in the last.
i tried picking up the values of $2 first with respect to store(as there are multiple stores) then appending the value of product after the store value and in the last the value of brand and stored the same in an array.its is looking something like:
400.68
400.68:200.34
400.68:200.34:110.34
400.68:200.34:90.00
400.68:200.34:NAusing asort on this array is not displaying the required result.
{ match($0, /^ */);
offset = RLENGTH;
if (offset == 1) { items[NR] = $2 }
else if (offset > prev_ofst) { items[NR] = items[NR1]":"$2 }
else if (offset < prev_ofst) {
prev_item = items[NR1];
gsub("(\:[^:]+\:[^:]+)$", "", prev_item);
items[NR] = prev_item":"$2;
}
else {
prev_item = items[NR1];
gsub("(\:[^:]+)$", "", prev_item);
items[NR] = prev_item" "$2;
}
prev_ofst = offset;
print items[NR];
}
END{
asort(items);
for (i = 1; i <= NR; i++) {
gsub("[^:]+\:", "", items[i]);
print items[i];
}
} 
getting ValueError: invalid literal for int() with base 10: 'log' in python
I want to sort this list in a way that .log should be the first file and .gz file should be in a descending order
my_list = [ '/abc/a.log.1.gz', '/abc/a.log', '/abc/a.log.gz', '/abc/a.log.30.gz', '/abc/a.log.2.gz', '/abc/a.log.5.gz', '/abc/a.log.3.gz', '/abc/a.log.6.gz', '/abc/a.log.4.gz', '/abc/a.log.12.gz', '/abc/a.log.10.gz', '/abc/a.log.8.gz', '/abc/a.log.14.gz', '/abc/a.log.29.gz' ]
Expected Result:
my_list = ['/abc/a.log', '/abc/a.log.gz', '/abc/a.log.30.gz', '/abc/a.log.29.gz', '/abc/a.log.29.gz', '/abc/a.log.14.gz', '/abc/a.log.12.gz', '/abc/a.log.10.gz', '/abc/a.log.8.gz', '/abc/a.log.6.gz', '/abc/a.log.5.gz', '/abc/a.log.4.gz', '/abc/a.log.3.gz', '/abc/a.log.2.gz' '/abc/a.log.1.gz']
My solution: import os
def get_sort_keys(filepath): split_file_path = os.path.splitext(filepath) sort_key = (split_file_path[1], *os.path.splitext(split_file_path[0])) return (sort_key[0], sort_key[1], int(sort_key[2].strip(".")) if sort_key[2] else 0) print(sorted(my_list, key=get_sort_keys, reverse=True))
Getting error:
ValueError: invalid literal for int() with base 10: 'log'

Pandas sort multiple columns independently
I've been struggling to sort the entire columns of my df, however, my code seems to be working for solely the first column ('Name') and shuffles the rest of the columns based upon the first column as shown here:
Index Name Age Education Country 0 W 2 BS C 1 V 1 PhD F 2 R 9 MA A 3 A 8 MA A 4 D 7 PhD B 5 C 4 BS C
df.sort_values(by=['Name', 'Age', 'Education', 'Country'],ascending=[True,True, True, True])
Here's what I'm hoping to get:
Index Name Age Education Country 0 A 1 BS A 1 C 2 BS A 2 D 4 MA B 3 R 7 MA C 4 V 8 PhD C 5 W 9 PhD F
Instead, I'm getting the following:
Index Name Age Education Country 3 A 8 MA A 5 C 4 BS C 4 D 7 PhD B 2 R 9 MA A 1 V 1 PhD F 0 W 2 BS C
Could you please shed some light on this issue. Many thanks in advance. Cheers, R.

Assembler bit logical operations?
I am trying to deal with the assembler, it does not work. Here are my questions:
What is the encoding of xor $32, %AL?
What instruction is encoded below?
00010101 00111001 00001000

assembly program for Fibonacci
I just got a question about the assembly program for Fibonacci sequence. The question is as following : The Fibonacci sequence
F
is defined asF(1) = F(2) = 1
and forn ≥ 2
,F(n + 1) = F(n) + F(n − 1)
i.e., the(n + 1)th
value is given by the sum of thenth
value and the(n − 1)th
value. Write an assembly program typical of RISC machines for computing the
kth
valueF(k)
, wherek
is a natural number greater than2
loaded from a memory locationM
, and storing the result at memory locationM
.
I received the answer of following:
LOAD r2, M LOAD r0, #1 LOAD r1, #1 4: SUB r2, r2, #1 ADD r3, r0, r1 LOAD r0, r1 LOAD r1, r3 BNE 4, r2, #2 // jump to instruction 4 if r2 is not equal to 2 STOR M, r1
where # indicates immediate addressing and BNE stands for "branch if not equal".
I do not understand why... Can anyone please explain it to me?
 Write an assembly program typical of RISC machines for computing the

cant understand why certain variable is being altered
I've been tasked with creating a program that will list composite numbers within a user identified range. To determine if a number is composite I will be dividing it and checking for a remainder of zero. My actual problem is trying to print the variable called "current" in my code. current is initialized to 3 and then incremented every loop, so I expect the number 4 to be printed first but 2 prints first. How is this possible, current never even gets to 2, it only increases from 3.
mov ecx, terms trial: inc current mov eax, current cdq mov ebx, 2 div ebx cmp edx, 0 je composite cmp edx, 0 jg below composite: mov edx, OFFSET current call WriteDec call CrLf below: loop trial
If I input 9 I expect 4, 6 and 8 to print because these all leave a remainder of 0 when divided by 2. instead I get 2, 3, 4, 5 and 6 printed.

How to enhanced selection sort code using java
Hi pro programmers I have an problem that need to enhance a selection sort with ms duration but I don't have any idea how to start/code it.
here is the text that give to us. Thank you in advanced pro programmers
class SelectionSort { void sort(int arr[]) { int n = arr.length; // One by one move boundary of unsorted subarray for (int i = 0; i < n1; i++) { // Find the minimum element in unsorted array int min_idx = i; for (int j = i+1; j < n; j++) if (arr[j] < arr[min_idx]) min_idx = j; // Swap the found minimum element with the first // element int temp = arr[min_idx]; arr[min_idx] = arr[i]; arr[i] = temp; } } // Prints the array void printArray(int arr[]) { int n = arr.length; for (int i=0; i<n; ++i) System.out.print(arr[i]+" "); System.out.println(); } // Driver code to test above public static void main(String args[]) { SelectionSort ob = new SelectionSort(); int arr[] = {64,25,12,22,11}; ob.sort(arr); System.out.println("Sorted array"); ob.printArray(arr); }
}

Selection sort algorithm only works when an array without 0 is passed
I am trying to implement a selection sort algorithm. For some reason it only works when I pass an array which does not contain the value 0. I can't seem to figure out why it's doing this.
Here is my solution:
function selectionSort(array) { let smallestItem = null; let smallestItemIndex = null; for(i = 0 ; i < array.length ; i++){ smallestItemIndex = i; for(j = i + 1 ; j < array.length ; j++){ if(array[smallestItemIndex] > array[j]){ smallestItem = array[j]; smallestItemIndex = j; } } if(smallestItem){ let temp = array[i]; array[i] = smallestItem; array[smallestItemIndex] = temp; } smallestItem = null; }

Selection sort in a singly linked list (swapping values)
I have written a Java code to use selection sort on a singly linked list (swapping the values) but I don't see any output in the code. For some reason, the while loop in the selection sort function doesn't end. Please help me know where I am wrong.
public class SingleLinkedList { Node head; static class Node { int val; Node next; Node (int v) { val = v; next = null; } } public void traverse() { Node n = head; while(n!= null) { System.out.println(n.val + " "); n = n.next; } System.out.println(" "); } public void selectionSort() { Node current = head; Node current2; Node min; while(current!= null) { min = current; current2 = current.next; while(current2.next != null) { if(min.val<current2.val) { min = current2; current2 = current2.next; } } Node tmp = new Node(current.val); min.val = current.val; min.val = tmp.val; current = current.next; } } public static void main(String[] args) { SingleLinkedList sll = new SingleLinkedList(); sll.head = new Node(11); Node two = new Node (6); Node three = new Node (1); Node four = new Node (2); Node five = new Node (4); Node six= new Node (9); Node seven = new Node (13); Node eight = new Node (8); Node nine = new Node (15); Node ten= new Node (7); Node eleven = new Node (3); Node twelve = new Node (5); Node thirteen = new Node (10); Node fourteen = new Node (12); Node fifteen = new Node(14); sll.head.next = two; two.next = three; three.next = four; four.next = five; five.next = six; six.next = seven; seven.next=eight; eight.next= nine; nine.next = ten; ten.next = eleven; eleven.next = twelve; twelve.next = thirteen; thirteen.next = fourteen; fourteen.next = fifteen; //fifteen.next = null; sll.traverse(); sll.selectionSort(); sll.traverse(); } }
Expected result should be all the numbers sorted in ascending order 1 to 15 but the actual output is not shown ( the while loop must be running indefinitely). Only the unsorted values are printed but not the sorted values after calling the sort function on the linked list. Surely must be a because the sort function runs in an infinite loop Debug result

How to set a value out of PC relative addressing range in machine code
I'm working on an exercise that requires some access to far away memory relative to my starting location. My program starts at x3000 and needs to store some values at x4000. I'm very lost on how to reference x4000 as a value purely in machine code.
I understand that PC relative addressing modes have a range of 256 to 255, and that indirect addressing modes are the way I want to go. However, in all the examples I've found in my research into this, there is a convenient location like x3010 or x3100 that already has the value x4000 assigned to it, so it's simple to use LDI or STI instructions to get where I want to go, if that makes sense.
For example, in this video https://www.youtube.com/watch?v=359TeV9UvM8&t=26s , the state of the machine begins with x3010, x3011, and x3012 already containing the address of far away memory. I want to get to this state.
So, purely in machine code, how can I set the value of a register or memory location to that of a far away memory location so that I can access it via indirect addressing methods?

Understanding lc3 program
Im trying to understand this code . I know he suppose to print "ABCDEFG" or "ABCDFGH" (according to 2 options) but i cannot understand why it heppens.
My problem is that why in x3001 R1=R0+0 While R0 is not given in front and the program workd. Thanks.
.ORIG x3000 LEA R1, TESTOUT ADD R1, R0, #0 BACK_1 LDR R0, R1, #0 BRz NEXT_1 TRAP x21 ADD R1, R1, #1 BRnzp BACK_1 NEXT_1 LEA R1, TESTOUT BACK_2 LDR R0, R1, #0 BRz NEXT_2 JSR SUB_1 ADD R1, R1, #1 BRnzp BACK_2 NEXT_2 TRAP x25 SUB_1 ADD R0, R0, #5 / ADD R0, R0, #3 K LDI R2, DSR BRzp K STI R0, DDR RET DSR .FILL xF3FC DDR .FILL xF3FF TESTOUT .STRINGZ "ABC" .END

How to fix "Offset out of range" error using LEA instruction in LC3 Assembly
I'm just starting to learn LC3 Assembly for my machine organization class. I need to write a small program in LC3 assembly to calculate the sum of a list of positive numbers that is terminated by a zero. My program need to start at location x3000 and my data (the list of numbers) should start at x4000. I wrote this:
.ORIG x3000 AND R1,R1,x0 ;clear R1 to be used for the sum LEA R2,x4000 ;load the starting address of the data LDR R3,R2,x0 ;load the next number to be added LOOP ADD R1,R1,R3 ;add the next number to the sum ADD R2,R2,x1 ;increment the pointer LDR R3,R2,x0 ;load next number to be added BRp LOOP ;loop if next number is positive HALT .END
But I get this error:
at line 3: while parsing the offset for a LEA: offset 16384 is out of range; it must fit into 9 bits, so it should be between 256 and 255, inclusive
I'm having a hard time understanding exactly how I'm supposed to use the LEA instruction to get it to do what I want. How should I change it?
Edit 3:
.ORIG x3000 AND R1,R1,x0 ;clear R1 to be used for the sum LD R2,NUMBERS ;load the starting address of the data LDR R3,R2,x0 ;load the next number to be added LOOP ADD R1,R1,R3 ;add the next number to the sum ADD R2,R2,x1 ;increment the pointer LDR R3,R2,x0 ;load next number to be added BRp LOOP ;loop if next number is positive HALT NUMBERS .FILL x4000 .END