Skip to content

Commit

Permalink
Makefile, linker script and quick sort in C.
Browse files Browse the repository at this point in the history
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
yqszxx committed Aug 25, 2019
1 parent 638a64c commit 980b528
Show file tree
Hide file tree
Showing 5 changed files with 147 additions and 0 deletions.
29 changes: 29 additions & 0 deletions Makefile
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 $<
4 changes: 4 additions & 0 deletions build/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
*
*/
!.gitignore

76 changes: 76 additions & 0 deletions cprog.c
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]);
}
}

13 changes: 13 additions & 0 deletions link.ld
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) }
}

25 changes: 25 additions & 0 deletions sprog.s
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

0 comments on commit 980b528

Please sign in to comment.