How would you translate the following from Java to RISC-V?
static int Y1[] = {13, 101, 79, 23, 154, 4, 11, 38, 88, 45, 17,
94, 62, 1};
static int lenY1 = 14;
public static void main(String[] args) {
quickSort(Y1,lenY1);
}
public static void quickSort(int[] x, int n) {
qSort(x,0,n-1);
}
Below is the detailed step by step translation of QuickSort from Java program to RISC-V.
# In Java we have quicksort(int[] x, lo, hi)
# In RISC-V here we used x as a1
# lo as a2
# hi as a3
quicksort:
blt a3, a2, quicksort_exit # if (lo >= hi) we just return
# save stuff in the stack
addi sp, sp, -32
sd ra, 0(sp)
sd s10, 8(sp) # s10 is going to hold lo
sd s11, 16(sp) # s11 is going to hold hi
sd s9, 24(sp) # s9 is going to hold the pivot
# hold lo and hi
mv s10, a2 # s10 <- lo
mv s11, a3 # s11 <- hi
jal ra, qSort # call qSort
mv s9, a0 # save the pivot on s9
# s9 = pivot
# s10 = lo
# s11 = hi
# recursively call quicksort on both subarrays
addi a3, s9, -1 # hi = pivot (-1)
mv a2, s10 # lo = lo
jal ra, quicksort # quicksort(a1, lo, pivot-1)
addi a2, s9, 1 # lo = pivot (+1)
mv a3, s11 # hi = hi
jal ra, quicksort
# load stuff back from the stack
ld ra, 0(sp)
ld s10, 8(sp)
ld s11, 16(sp)
ld s9, 24(sp)
addi sp, sp, 32
quicksort_exit:
ret
# In Java we have qSort(*arr, lo, hi) -> pivot
# In RISC-V here we used arr as a1
# lo as a2
# hi as a3
# pivot <- a0 (return value)
qSort:
# save stuff in the stack
addi sp, sp, -24
sd ra, 0(sp)
sd s10, 8(sp)
sd s11, 16(sp)
# init pivot to high (a3)
add t0, a1, a3
lbu t0, 0(t0)
addi t2, a2, -1 # (i) index of the smaller element => t2 =
low - 1
mv t6, a2 # t6 = j = low
addi t5, a3, -1 # t5 = high-1
qSort_forloop:
bgt t6, t5, qSort_forloop_end # if t6 > t5 then
qSort_forloop_end
add s11, a1, t6 # s11 = *arr[j]
lbu t1, 0(s11) # t1 = *(arr[j])
bgtu t1, t0, qSort_forloop_inner_skip # if t1>t0 skip (if
arr[j]>pivot)
addi t2, t2, 1 # i++
add s10, a1, t2 # s10 = *arr[t2] = *arr[i]
lbu t3, 0(s10) # t3 = *(arr[i])
sb t3, 0(s11) # arr[j] = t3
sb t1, 0(s10) # arr[i] = t1
qSort_forloop_inner_skip:
addi t6, t6, 1 # j++
j qSort_forloop
qSort_forloop_end:
addi a0, t2, 1 # write return value as i+1
# swap(&arr[i+1], &arr[high])
add s10, a1, a0 # s10 = *arr[i+1]
add s11, a1, a3 # s11 = *arr[high]
lbu t2, 0(s10) # t2 = *s10
lbu t3, 0(s11) # t3 = *s11
sb t2, 0(s11)
sb t3, 0(s10)
# load stuff back from the stack
ld ra, 0(sp)
ld s10, 8(sp)
ld s11, 16(sp)
addi sp, sp, 24
qSort_bail:
ret
How would you translate the following from Java to RISC-V? static int Y1[] = {13, 101,...
please evaluate the following code. this is JAVA a. class Car { public int i = 3; public Car(int i) { this.i = i; } } ... Car x = new Car(7), y = new Car(5); x = y; y.i = 9; System.out.println(x.i); b. class Driver { public static void main(String[] args) { int[] x = {5, 2, 3, 6, 5}; int n = x.length; for (int j = n-2; j > 0; j--) x[j] = x[j-1]; for (int j...
Java, how would i do this
public static void main(String[] args) { int n = 3; int result; result = factorial(n); + public static int factorial(int n) public static int factorial(int n) n = 3 { returns 3* 2 * 1 { if (n == 1) return n; else return (n * factorial(n-1)); if (n == 1) return n; else return (3 * factorial(3-1)); ܢܟ } public static int factorial(int n) n = 2 public static int factorial(int n) returns...
Java Inner class class Outer { private int x=10; private static int y=20 ; publics void M1( ) { int z=30; class Inner { public void M2() { Sytem.out.println(“sum: ”+ (x+y+z)); } } Inner i=new Inner(); i.M2() ; /// first call i.M2(); // second call i.M2(); // third call } // end of M1 publics static void main(String[] args) { Outer O = new...
1. In java (What is the output of the code segment)? public static void Main(){ int x = 38; int y = 45; int z = DoIt(ref x, ref y); PRINTLINE(x + “ “ + y + “ “ + z); // PRINTLINE 1 } static int DoIt(ref int a, ref int b) { a /= 6; b /= 8; PRINTLINE(a + “ “ + b); // PRINTLINE 2 return (a + b); } 2....
Complete the following Java program by writing the missing methods. public class 02 { public static void main(String[] args) { Scanner scan = new Scanner(System.in); System.out.println("Enter a number: int n = scan.nextInt(); if (isOdd (n)) { //Print n to 1 System.out.println("Print all numbers from "+n+" to 1"); printNumToOne (n); } else { System.out.println(n + " is //End of main()
Java
Do 61a, 61b, 61c, 61d. Show Output and Code.
public class Employee { private int id; private String name; private int sal; public Employee(int id, String name, int sal) { super(); this.id = id; this.name = name; this.sal = sal; } public int getid) { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; public void setName(String name) { this.name = name; } public int get Sall) { return sal;...
What is printed by running the following code? public static void main(String[] args) { int[] nums = {2, 3, 4}; int n = 5; changeMe1(n, nums); System.out.print( n ); System.out.print(nums[0]); changeMe2(n, nums); System.out.print( n ); System.out.print(nums[0]); } public static void changeMe1(int number, int[] list) { number++; list[0]++; } public static void changeMe2(int number, int[] list) { number = 9; list = new int[1]; list[0] = 99; }
Given the following Java code: public static void main (String[] args) { int num; System.out.println("Enter a number"); num = scan.nextInt(); <-first input statement while (num != 0) { System.out.println ("The number is: " + num); System.out.println("Enter a number"); num = scan.nextInt(); } //End While } //End Module The first input statement shown above is called the:
1) Consider the following Java program: 1 public class HelloWorld { 2 // My first program! 3 public static void main(String[] args) { 4 System.out.println("Hello, World!"); 5 } 6 } What is on line 1? a. a variable declaration b. a statement c. a method (subroutine) definition d. a comment e. a class definition 2) Which one of the following does NOT describe an array? a. It can be used in a for-each loop. b. It has a numbered sequence...
Java i know that result will be 415. Can you explain how it works. step by step please. thanks public static void whatsPrinted(int A[]) { for (int i=1; i<A.length; i++) { A[i]=A[i-1]*2+1; } } public static void main(String args[]) { int A[] = {12,3,8,9,7,11}; whatsPrinted(A); System.out.println(A[A.length-1]); }