-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Makefile, linker script and quick sort in C.
cprog.c contains quick sort implementation ripped from https://www.geeksforgeeks.org/quick-sort/ sprog.s contains a program printing an array.
- Loading branch information
Showing
5 changed files
with
147 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
TOOLCHAIN=/opt/riscv64-unknown-elf-gcc-8.2.0-2019.05.3-x86_64-linux-ubuntu14/bin/riscv64-unknown-elf- | ||
EMULATOR=/mnt/hgfs/Exchange/go_build_main_go | ||
|
||
build/sprog.o: sprog.s | ||
$(TOOLCHAIN)as -march=rv32i -mabi=ilp32 -o $@ $< | ||
|
||
build/sprog: build/sprog.o link.ld | ||
$(TOOLCHAIN)ld -m elf32lriscv -o $@ -T link.ld $< | ||
|
||
sdebug: build/sprog | ||
$(EMULATOR) $< | ||
|
||
srun: build/sprog | ||
$(EMULATOR) $< 2>/dev/null | ||
|
||
sdump: build/sprog | ||
$(TOOLCHAIN)objdump -d $< | ||
|
||
build/cprog: cprog.c link.ld | ||
$(TOOLCHAIN)gcc -march=rv32i -mabi=ilp32 -static -mcmodel=medany -fvisibility=hidden -nostdlib -nostartfiles -o $@ -T link.ld $< | ||
|
||
cdebug: build/cprog | ||
$(EMULATOR) $< | ||
|
||
crun: build/cprog | ||
$(EMULATOR) $< 2>/dev/null | ||
|
||
cdump: build/cprog | ||
$(TOOLCHAIN)objdump -d $< |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
* | ||
*/ | ||
!.gitignore | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
extern int PRINT; | ||
extern int DONE; | ||
|
||
#define N 16 | ||
int arr[] = {0xF, 0xA, 0xB, 0xE, | ||
0x9, 0x1, 0x8, 0x4, | ||
0x6, 0x2, 0x5, 0x7, | ||
0x3, 0xC, 0x0, 0xD}; | ||
|
||
void printArray(); | ||
void quickSort(int low, int high); | ||
void print(int n); | ||
|
||
// Driver program to test above functions | ||
int main() { | ||
printArray(); | ||
print(0x66666666); | ||
quickSort(0, N - 1); | ||
printArray(); | ||
DONE = 0; | ||
} | ||
|
||
void print(int n) { | ||
PRINT = n; | ||
} | ||
|
||
void swap(int* a, int* b) { | ||
int t = *a; | ||
*a = *b; | ||
*b = t; | ||
} | ||
|
||
/* This function takes last element as pivot, places | ||
the pivot element at its correct position in sorted | ||
array, and places all smaller (smaller than pivot) | ||
to left of pivot and all greater elements to right | ||
of pivot */ | ||
int partition (int low, int high) { | ||
int pivot = arr[high]; // pivot | ||
int i = (low - 1); // Index of smaller element | ||
|
||
for (int j = low; j <= high - 1; j++) { | ||
// If current element is smaller than or | ||
// equal to pivot | ||
if (arr[j] <= pivot) { | ||
i++; // increment index of smaller element | ||
swap(&arr[i], &arr[j]); | ||
} | ||
} | ||
swap(&arr[i + 1], &arr[high]); | ||
return (i + 1); | ||
} | ||
|
||
/* The main function that implements QuickSort | ||
arr[] --> Array to be sorted, | ||
low --> Starting index, | ||
high --> Ending index */ | ||
void quickSort(int low, int high) { | ||
if (low < high) { | ||
/* pi is partitioning index, arr[p] is now | ||
at right place */ | ||
int pi = partition(low, high); | ||
|
||
// Separately sort elements before | ||
// partition and after partition | ||
quickSort(low, pi - 1); | ||
quickSort(pi + 1, high); | ||
} | ||
} | ||
|
||
void printArray() { | ||
for (int i = 0; i < N; i++) { | ||
print(arr[i]); | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
SECTIONS | ||
{ | ||
. = 0; | ||
.text : { | ||
*(.text) | ||
PROVIDE(PRINT = 0xFFF8); | ||
PROVIDE(DONE = 0xFFFC); | ||
} | ||
/* . = 0x1000;*/ | ||
.data : { *(.data) } | ||
.bss : { *(.bss) } | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
.equ N, 16 | ||
|
||
.text | ||
.global _start | ||
.global PRINT | ||
|
||
_start: | ||
la t4, a # t4 holds the base address of array a | ||
la t5, a | ||
addi t5, t5, 64 # t5 holds the address of last enelemt in a | ||
la t6, PRINT # t6 holds 0xFFFC for print, for done use 4(t6) | ||
call printArray | ||
sw zero, 4(t6) | ||
|
||
printArray: | ||
mv t0, t4 # t0 is the pointer of current element | ||
.loop: | ||
lw t2, 0(t0) # load the element | ||
sw t2, 0(t6) # print | ||
addi t0, t0, 4 # move pointer to next element | ||
blt t0, t5, .loop # loop until the last element | ||
ret | ||
|
||
.data | ||
a: .word 0xf, 0xa, 0xb, 0xe, 0x9, 0x1, 0x8, 0x4, 0x6, 0x2, 0x5, 0x7, 0x3, 0xc, 0x0, 0xd |