Data update
This commit is contained in:
parent
4bb20c9b71
commit
cbaf4c4b64
12390 changed files with 318560 additions and 27248 deletions
|
|
@ -0,0 +1,9 @@
|
|||
generic
|
||||
type Element_Type is private;
|
||||
type Index_Type is (<>);
|
||||
type Collection_Type is array(Index_Type range <>) of Element_Type;
|
||||
with function "<"(Left, Right : Element_Type) return Boolean is <>;
|
||||
|
||||
package Mergesort is
|
||||
function Sort(Item : Collection_Type) return Collection_Type;
|
||||
end MergeSort;
|
||||
|
|
@ -0,0 +1,62 @@
|
|||
package body Mergesort is
|
||||
|
||||
-----------
|
||||
-- Merge --
|
||||
-----------
|
||||
|
||||
function Merge(Left, Right : Collection_Type) return Collection_Type is
|
||||
Result : Collection_Type(Left'First..Right'Last);
|
||||
Left_Index : Index_Type := Left'First;
|
||||
Right_Index : Index_Type := Right'First;
|
||||
Result_Index : Index_Type := Result'First;
|
||||
begin
|
||||
while Left_Index <= Left'Last and Right_Index <= Right'Last loop
|
||||
if Left(Left_Index) <= Right(Right_Index) then
|
||||
Result(Result_Index) := Left(Left_Index);
|
||||
Left_Index := Index_Type'Succ(Left_Index); -- increment Left_Index
|
||||
else
|
||||
Result(Result_Index) := Right(Right_Index);
|
||||
Right_Index := Index_Type'Succ(Right_Index); -- increment Right_Index
|
||||
end if;
|
||||
Result_Index := Index_Type'Succ(Result_Index); -- increment Result_Index
|
||||
end loop;
|
||||
if Left_Index <= Left'Last then
|
||||
Result(Result_Index..Result'Last) := Left(Left_Index..Left'Last);
|
||||
end if;
|
||||
if Right_Index <= Right'Last then
|
||||
Result(Result_Index..Result'Last) := Right(Right_Index..Right'Last);
|
||||
end if;
|
||||
return Result;
|
||||
end Merge;
|
||||
|
||||
----------
|
||||
-- Sort --
|
||||
----------
|
||||
|
||||
function Sort (Item : Collection_Type) return Collection_Type is
|
||||
Result : Collection_Type(Item'range);
|
||||
Middle : Index_Type;
|
||||
begin
|
||||
if Item'Length <= 1 then
|
||||
return Item;
|
||||
else
|
||||
Middle := Index_Type'Val((Item'Length / 2) + Index_Type'Pos(Item'First));
|
||||
declare
|
||||
Left : Collection_Type(Item'First..Index_Type'Pred(Middle));
|
||||
Right : Collection_Type(Middle..Item'Last);
|
||||
begin
|
||||
for I in Left'range loop
|
||||
Left(I) := Item(I);
|
||||
end loop;
|
||||
for I in Right'range loop
|
||||
Right(I) := Item(I);
|
||||
end loop;
|
||||
Left := Sort(Left);
|
||||
Right := Sort(Right);
|
||||
Result := Merge(Left, Right);
|
||||
end;
|
||||
return Result;
|
||||
end if;
|
||||
end Sort;
|
||||
|
||||
end Mergesort;
|
||||
|
|
@ -0,0 +1,19 @@
|
|||
with Ada.Text_Io; use Ada.Text_Io;
|
||||
with Mergesort;
|
||||
|
||||
procedure Mergesort_Test is
|
||||
type List_Type is array(Positive range <>) of Integer;
|
||||
package List_Sort is new Mergesort(Integer, Positive, List_Type);
|
||||
procedure Print(Item : List_Type) is
|
||||
begin
|
||||
for I in Item'range loop
|
||||
Put(Integer'Image(Item(I)));
|
||||
end loop;
|
||||
New_Line;
|
||||
end Print;
|
||||
|
||||
List : List_Type := (1, 5, 2, 7, 3, 9, 4, 6);
|
||||
begin
|
||||
Print(List);
|
||||
Print(List_Sort.Sort(List));
|
||||
end Mergesort_Test;
|
||||
|
|
@ -0,0 +1,19 @@
|
|||
(let ((fixpoint-combinator
|
||||
(lambda (g)
|
||||
(let ((inner (lambda (x) (g (x x)))))
|
||||
(inner inner))))
|
||||
(sortmerge (lambda (list pred x)
|
||||
(list
|
||||
(fixpoint-combinator
|
||||
(lambda (recur head tail x)
|
||||
((pred head (x t))
|
||||
;; these (lambda (p) ...) are inlined cons calls
|
||||
(lambda (p) (p head (tail recur x)))
|
||||
(lambda (p) (p (x t) (lambda (p) (p head tail)))))))
|
||||
(lambda (p) (p x nil))))))
|
||||
(lambda (pred list)
|
||||
(list
|
||||
(fixpoint-combinator
|
||||
(lambda (recur head tail z)
|
||||
(sortmerge (tail recur z) pred head)))
|
||||
nil)))
|
||||
|
|
@ -0,0 +1,293 @@
|
|||
IDENTIFICATION DIVISION.
|
||||
PROGRAM-ID. MERGESORT.
|
||||
AUTHOR. DAVE STRATFORD.
|
||||
DATE-WRITTEN. APRIL 2010.
|
||||
INSTALLATION. HEXAGON SYSTEMS LIMITED.
|
||||
******************************************************************
|
||||
* MERGE SORT *
|
||||
* The Merge sort uses a completely different paradigm, one of *
|
||||
* divide and conquer, to many of the other sorts. The data set *
|
||||
* is split into smaller sub sets upon which are sorted and then *
|
||||
* merged together to form the final sorted data set. *
|
||||
* This version uses the recursive method. Split the data set in *
|
||||
* half and perform a merge sort on each half. This in turn splits*
|
||||
* each half again and again until each set is just one or 2 items*
|
||||
* long. A set of one item is already sorted so is ignored, a set *
|
||||
* of two is compared and swapped as necessary. The smaller data *
|
||||
* sets are then repeatedly merged together to eventually form the*
|
||||
* full, sorted, set. *
|
||||
* Since cobol cannot do recursion this module only simulates it *
|
||||
* so is not as fast as a normal recursive version would be. *
|
||||
* Scales very well to larger data sets, its relative complexity *
|
||||
* means it is not suited to sorting smaller data sets: use an *
|
||||
* Insertion sort instead as the Merge sort is a stable sort. *
|
||||
******************************************************************
|
||||
|
||||
ENVIRONMENT DIVISION.
|
||||
CONFIGURATION SECTION.
|
||||
SOURCE-COMPUTER. ICL VME.
|
||||
OBJECT-COMPUTER. ICL VME.
|
||||
|
||||
INPUT-OUTPUT SECTION.
|
||||
FILE-CONTROL.
|
||||
SELECT FA-INPUT-FILE ASSIGN FL01.
|
||||
SELECT FB-OUTPUT-FILE ASSIGN FL02.
|
||||
|
||||
DATA DIVISION.
|
||||
FILE SECTION.
|
||||
FD FA-INPUT-FILE.
|
||||
01 FA-INPUT-REC.
|
||||
03 FA-DATA PIC 9(6).
|
||||
|
||||
FD FB-OUTPUT-FILE.
|
||||
01 FB-OUTPUT-REC PIC 9(6).
|
||||
|
||||
WORKING-STORAGE SECTION.
|
||||
01 WA-IDENTITY.
|
||||
03 WA-PROGNAME PIC X(10) VALUE "MERGESORT".
|
||||
03 WA-VERSION PIC X(6) VALUE "000001".
|
||||
|
||||
01 WB-TABLE.
|
||||
03 WB-ENTRY PIC 9(8) COMP SYNC OCCURS 100000
|
||||
INDEXED BY WB-IX-1
|
||||
WB-IX-2.
|
||||
|
||||
01 WC-VARS.
|
||||
03 WC-SIZE PIC S9(8) COMP SYNC.
|
||||
03 WC-TEMP PIC S9(8) COMP SYNC.
|
||||
03 WC-START PIC S9(8) COMP SYNC.
|
||||
03 WC-MIDDLE PIC S9(8) COMP SYNC.
|
||||
03 WC-END PIC S9(8) COMP SYNC.
|
||||
|
||||
01 WD-FIRST-HALF.
|
||||
03 WD-FH-MAX PIC S9(8) COMP SYNC.
|
||||
03 WD-ENTRY PIC 9(8) COMP SYNC OCCURS 50000
|
||||
INDEXED BY WD-IX.
|
||||
|
||||
01 WF-CONDITION-FLAGS.
|
||||
03 WF-EOF-FLAG PIC X.
|
||||
88 END-OF-FILE VALUE "Y".
|
||||
03 WF-EMPTY-FILE-FLAG PIC X.
|
||||
88 EMPTY-FILE VALUE "Y".
|
||||
|
||||
01 WS-STACK.
|
||||
* This stack is big enough to sort a list of 1million items.
|
||||
03 WS-STACK-ENTRY OCCURS 20 INDEXED BY WS-STACK-TOP.
|
||||
05 WS-START PIC S9(8) COMP SYNC.
|
||||
05 WS-MIDDLE PIC S9(8) COMP SYNC.
|
||||
05 WS-END PIC S9(8) COMP SYNC.
|
||||
05 WS-FS-FLAG PIC X.
|
||||
88 FIRST-HALF VALUE "F".
|
||||
88 SECOND-HALF VALUE "S".
|
||||
88 WS-ALL VALUE "A".
|
||||
05 WS-IO-FLAG PIC X.
|
||||
88 WS-IN VALUE "I".
|
||||
88 WS-OUT VALUE "O".
|
||||
|
||||
PROCEDURE DIVISION.
|
||||
A-MAIN SECTION.
|
||||
A-000.
|
||||
PERFORM B-INITIALISE.
|
||||
|
||||
IF NOT EMPTY-FILE
|
||||
PERFORM C-PROCESS.
|
||||
|
||||
PERFORM D-FINISH.
|
||||
|
||||
A-999.
|
||||
STOP RUN.
|
||||
|
||||
B-INITIALISE SECTION.
|
||||
B-000.
|
||||
DISPLAY "*** " WA-PROGNAME " VERSION "
|
||||
WA-VERSION " STARTING ***".
|
||||
|
||||
MOVE ALL "N" TO WF-CONDITION-FLAGS.
|
||||
OPEN INPUT FA-INPUT-FILE.
|
||||
SET WB-IX-1 TO 0.
|
||||
|
||||
READ FA-INPUT-FILE AT END MOVE "Y" TO WF-EOF-FLAG
|
||||
WF-EMPTY-FILE-FLAG.
|
||||
|
||||
PERFORM BA-READ-INPUT UNTIL END-OF-FILE.
|
||||
|
||||
CLOSE FA-INPUT-FILE.
|
||||
|
||||
SET WC-SIZE TO WB-IX-1.
|
||||
|
||||
B-999.
|
||||
EXIT.
|
||||
|
||||
BA-READ-INPUT SECTION.
|
||||
BA-000.
|
||||
SET WB-IX-1 UP BY 1.
|
||||
MOVE FA-DATA TO WB-ENTRY(WB-IX-1).
|
||||
|
||||
READ FA-INPUT-FILE AT END MOVE "Y" TO WF-EOF-FLAG.
|
||||
|
||||
BA-999.
|
||||
EXIT.
|
||||
|
||||
C-PROCESS SECTION.
|
||||
C-000.
|
||||
DISPLAY "SORT STARTING".
|
||||
|
||||
MOVE 1 TO WS-START(1).
|
||||
MOVE WC-SIZE TO WS-END(1).
|
||||
MOVE "F" TO WS-FS-FLAG(1).
|
||||
MOVE "I" TO WS-IO-FLAG(1).
|
||||
SET WS-STACK-TOP TO 2.
|
||||
|
||||
PERFORM E-MERGE-SORT UNTIL WS-OUT(1).
|
||||
|
||||
DISPLAY "SORT FINISHED".
|
||||
|
||||
C-999.
|
||||
EXIT.
|
||||
|
||||
D-FINISH SECTION.
|
||||
D-000.
|
||||
OPEN OUTPUT FB-OUTPUT-FILE.
|
||||
SET WB-IX-1 TO 1.
|
||||
|
||||
PERFORM DA-WRITE-OUTPUT UNTIL WB-IX-1 > WC-SIZE.
|
||||
|
||||
CLOSE FB-OUTPUT-FILE.
|
||||
|
||||
DISPLAY "*** " WA-PROGNAME " FINISHED ***".
|
||||
|
||||
D-999.
|
||||
EXIT.
|
||||
|
||||
DA-WRITE-OUTPUT SECTION.
|
||||
DA-000.
|
||||
WRITE FB-OUTPUT-REC FROM WB-ENTRY(WB-IX-1).
|
||||
SET WB-IX-1 UP BY 1.
|
||||
|
||||
DA-999.
|
||||
EXIT.
|
||||
|
||||
******************************************************************
|
||||
E-MERGE-SORT SECTION.
|
||||
*===================== *
|
||||
* This section controls the simulated recursion. *
|
||||
******************************************************************
|
||||
E-000.
|
||||
IF WS-OUT(WS-STACK-TOP - 1)
|
||||
GO TO E-010.
|
||||
|
||||
MOVE WS-START(WS-STACK-TOP - 1) TO WC-START.
|
||||
MOVE WS-END(WS-STACK-TOP - 1) TO WC-END.
|
||||
|
||||
* First check size of part we are dealing with.
|
||||
IF WC-END - WC-START = 0
|
||||
* Only 1 number in range, so simply set for output, and move on
|
||||
MOVE "O" TO WS-IO-FLAG(WS-STACK-TOP - 1)
|
||||
GO TO E-010.
|
||||
|
||||
IF WC-END - WC-START = 1
|
||||
* 2 numbers, so compare and swap as necessary. Set for output
|
||||
MOVE "O" TO WS-IO-FLAG(WS-STACK-TOP - 1)
|
||||
IF WB-ENTRY(WC-START) > WB-ENTRY(WC-END)
|
||||
MOVE WB-ENTRY(WC-START) TO WC-TEMP
|
||||
MOVE WB-ENTRY(WC-END) TO WB-ENTRY(WC-START)
|
||||
MOVE WC-TEMP TO WB-ENTRY(WC-END)
|
||||
GO TO E-010
|
||||
ELSE
|
||||
GO TO E-010.
|
||||
|
||||
* More than 2, so split and carry on down
|
||||
COMPUTE WC-MIDDLE = ( WC-START + WC-END ) / 2.
|
||||
|
||||
MOVE WC-START TO WS-START(WS-STACK-TOP).
|
||||
MOVE WC-MIDDLE TO WS-END(WS-STACK-TOP).
|
||||
MOVE "F" TO WS-FS-FLAG(WS-STACK-TOP).
|
||||
MOVE "I" TO WS-IO-FLAG(WS-STACK-TOP).
|
||||
SET WS-STACK-TOP UP BY 1.
|
||||
|
||||
GO TO E-999.
|
||||
|
||||
E-010.
|
||||
SET WS-STACK-TOP DOWN BY 1.
|
||||
|
||||
IF SECOND-HALF(WS-STACK-TOP)
|
||||
GO TO E-020.
|
||||
|
||||
MOVE WS-START(WS-STACK-TOP - 1) TO WC-START.
|
||||
MOVE WS-END(WS-STACK-TOP - 1) TO WC-END.
|
||||
COMPUTE WC-MIDDLE = ( WC-START + WC-END ) / 2 + 1.
|
||||
|
||||
MOVE WC-MIDDLE TO WS-START(WS-STACK-TOP).
|
||||
MOVE WC-END TO WS-END(WS-STACK-TOP).
|
||||
MOVE "S" TO WS-FS-FLAG(WS-STACK-TOP).
|
||||
MOVE "I" TO WS-IO-FLAG(WS-STACK-TOP).
|
||||
SET WS-STACK-TOP UP BY 1.
|
||||
|
||||
GO TO E-999.
|
||||
|
||||
E-020.
|
||||
MOVE WS-START(WS-STACK-TOP - 1) TO WC-START.
|
||||
MOVE WS-END(WS-STACK-TOP - 1) TO WC-END.
|
||||
COMPUTE WC-MIDDLE = ( WC-START + WC-END ) / 2.
|
||||
PERFORM H-PROCESS-MERGE.
|
||||
MOVE "O" TO WS-IO-FLAG(WS-STACK-TOP - 1).
|
||||
|
||||
E-999.
|
||||
EXIT.
|
||||
|
||||
******************************************************************
|
||||
H-PROCESS-MERGE SECTION.
|
||||
*======================== *
|
||||
* This section identifies which data is to be merged, and then *
|
||||
* merges the two data streams into a single larger data stream. *
|
||||
******************************************************************
|
||||
H-000.
|
||||
INITIALISE WD-FIRST-HALF.
|
||||
COMPUTE WD-FH-MAX = WC-MIDDLE - WC-START + 1.
|
||||
SET WD-IX TO 1.
|
||||
|
||||
PERFORM HA-COPY-OUT VARYING WB-IX-1 FROM WC-START BY 1
|
||||
UNTIL WB-IX-1 > WC-MIDDLE.
|
||||
|
||||
SET WB-IX-1 TO WC-START.
|
||||
SET WB-IX-2 TO WC-MIDDLE.
|
||||
SET WB-IX-2 UP BY 1.
|
||||
SET WD-IX TO 1.
|
||||
|
||||
PERFORM HB-MERGE UNTIL WD-IX > WD-FH-MAX OR WB-IX-2 > WC-END.
|
||||
|
||||
PERFORM HC-COPY-BACK UNTIL WD-IX > WD-FH-MAX.
|
||||
|
||||
H-999.
|
||||
EXIT.
|
||||
|
||||
HA-COPY-OUT SECTION.
|
||||
HA-000.
|
||||
MOVE WB-ENTRY(WB-IX-1) TO WD-ENTRY(WD-IX).
|
||||
SET WD-IX UP BY 1.
|
||||
|
||||
HA-999.
|
||||
EXIT.
|
||||
|
||||
HB-MERGE SECTION.
|
||||
HB-000.
|
||||
IF WB-ENTRY(WB-IX-2) < WD-ENTRY(WD-IX)
|
||||
MOVE WB-ENTRY(WB-IX-2) TO WB-ENTRY(WB-IX-1)
|
||||
SET WB-IX-2 UP BY 1
|
||||
ELSE
|
||||
MOVE WD-ENTRY(WD-IX) TO WB-ENTRY(WB-IX-1)
|
||||
SET WD-IX UP BY 1.
|
||||
|
||||
SET WB-IX-1 UP BY 1.
|
||||
|
||||
HB-999.
|
||||
EXIT.
|
||||
|
||||
HC-COPY-BACK SECTION.
|
||||
HC-000.
|
||||
MOVE WD-ENTRY(WD-IX) TO WB-ENTRY(WB-IX-1).
|
||||
SET WD-IX UP BY 1.
|
||||
SET WB-IX-1 UP BY 1.
|
||||
|
||||
HC-999.
|
||||
EXIT.
|
||||
|
|
@ -0,0 +1,37 @@
|
|||
function merge(sequence left, sequence right)
|
||||
sequence result
|
||||
result = {}
|
||||
while length(left) > 0 and length(right) > 0 do
|
||||
if compare(left[1], right[1]) <= 0 then
|
||||
result = append(result, left[1])
|
||||
left = left[2..$]
|
||||
else
|
||||
result = append(result, right[1])
|
||||
right = right[2..$]
|
||||
end if
|
||||
end while
|
||||
return result & left & right
|
||||
end function
|
||||
|
||||
function mergesort(sequence m)
|
||||
sequence left, right
|
||||
integer middle
|
||||
if length(m) <= 1 then
|
||||
return m
|
||||
else
|
||||
middle = floor(length(m)/2)
|
||||
left = mergesort(m[1..middle])
|
||||
right = mergesort(m[middle+1..$])
|
||||
if compare(left[$], right[1]) <= 0 then
|
||||
return left & right
|
||||
elsif compare(right[$], left[1]) <= 0 then
|
||||
return right & left
|
||||
else
|
||||
return merge(left, right)
|
||||
end if
|
||||
end if
|
||||
end function
|
||||
|
||||
constant s = rand(repeat(1000,10))
|
||||
? s
|
||||
? mergesort(s)
|
||||
|
|
@ -0,0 +1,19 @@
|
|||
def mergesort(x) {
|
||||
return x if x.size() <= 1
|
||||
def left = mergesort(x.subList(0,x.size()/2))
|
||||
def right = mergesort(x.subList(x.size()/2))
|
||||
return left + right if left[-1] <= right[0]
|
||||
return merge(left, right)
|
||||
}
|
||||
|
||||
def merge(left, right) {
|
||||
def result = []
|
||||
while (left.size() > 0 && right.size() > 0) {
|
||||
[result <<= left[0], left = left.subList(1)] and continue if left[0] < right[0]
|
||||
[result <<= right[0], right = right.subList(1)]
|
||||
}
|
||||
result += left; result += right
|
||||
}
|
||||
|
||||
def numbers = 10.map{ random(20) }
|
||||
println "Numbers: $numbers, sorted: ${mergesort(numbers)}"
|
||||
|
|
@ -0,0 +1,37 @@
|
|||
require "table2"
|
||||
|
||||
local function merge(left, right)
|
||||
local result = {}
|
||||
while #left > 0 and #right > 0 do
|
||||
if left[1] <= right[1] then
|
||||
result:insert(left[1])
|
||||
left = left:slice(2)
|
||||
else
|
||||
result:insert(right[1])
|
||||
right = right:slice(2)
|
||||
end
|
||||
end
|
||||
if #left > 0 then result:join(left) end
|
||||
if #right > 0 then result:join(right) end
|
||||
return result
|
||||
end
|
||||
|
||||
local function merge_sort(m)
|
||||
local len = #m
|
||||
if len <= 1 then return m end
|
||||
local middle = len // 2
|
||||
local left = m:slice(1, middle)
|
||||
local right = m:slice(middle + 1)
|
||||
left = merge_sort(left)
|
||||
right = merge_sort(right)
|
||||
if left:back() <= right[1] then return left:join(right) end
|
||||
return merge(left, right)
|
||||
end
|
||||
|
||||
local array = { {4, 65, 2, -31, 0, 99, 2, 83, 782, 1}, {7, 5, 2, 6, 1, 4, 2, 6, 3} }
|
||||
for array as a do
|
||||
print($"Before: \{{a:concat(", ")}}")
|
||||
a = merge_sort(a)
|
||||
print($"After : \{{a:concat(", ")}}")
|
||||
print()
|
||||
end
|
||||
|
|
@ -0,0 +1,36 @@
|
|||
function MergeSort([object[]] $SortInput)
|
||||
{
|
||||
# The base case exits for minimal lists that are sorted by definition
|
||||
if ($SortInput.Length -le 1) {return $SortInput}
|
||||
|
||||
# Divide and conquer
|
||||
[int] $midPoint = $SortInput.Length/2
|
||||
# The @() operators ensure a single result remains typed as an array
|
||||
[object[]] $left = @(MergeSort @($SortInput[0..($midPoint-1)]))
|
||||
[object[]] $right = @(MergeSort @($SortInput[$midPoint..($SortInput.Length-1)]))
|
||||
|
||||
# Merge
|
||||
[object[]] $result = @()
|
||||
while (($left.Length -gt 0) -and ($right.Length -gt 0))
|
||||
{
|
||||
if ($left[0] -lt $right[0])
|
||||
{
|
||||
$result += $left[0]
|
||||
# Use an if/else rather than accessing the array range as $array[1..0]
|
||||
if ($left.Length -gt 1){$left = $left[1..$($left.Length-1)]}
|
||||
else {$left = @()}
|
||||
}
|
||||
else
|
||||
{
|
||||
$result += $right[0]
|
||||
# Without the if/else, $array[1..0] would return the whole array when $array.Length == 1
|
||||
if ($right.Length -gt 1){$right = $right[1..$($right.Length-1)]}
|
||||
else {$right = @()}
|
||||
}
|
||||
}
|
||||
|
||||
# If we get here, either $left or $right is an empty array (or both are empty!). Since the
|
||||
# rest of the unmerged array is already sorted, we can simply string together what we have.
|
||||
# This line outputs the concatenated result. An explicit 'return' statement is not needed.
|
||||
$result + $left + $right
|
||||
}
|
||||
|
|
@ -0,0 +1,245 @@
|
|||
# riscv assembly raspberry pico2 rp2350
|
||||
# program mergesort.s
|
||||
# connexion putty com3
|
||||
/*********************************************/
|
||||
/* CONSTANTES */
|
||||
/********************************************/
|
||||
/* for this file see risc-v task include a file */
|
||||
.include "../../constantesRiscv.inc"
|
||||
|
||||
/****************************************************/
|
||||
/* MACROS */
|
||||
/****************************************************/
|
||||
#.include "../ficmacrosriscv.inc" # for debugging only
|
||||
|
||||
|
||||
/*******************************************/
|
||||
/* INITIALED DATAS */
|
||||
/*******************************************/
|
||||
.data
|
||||
szMessStart: .asciz "Program riscv start.\r\n"
|
||||
szMessEnd: .asciz "\nProgram end OK.\r\n"
|
||||
szCariageReturn: .asciz "\r\n"
|
||||
|
||||
szMessSortOk: .asciz "Area sorted.\n"
|
||||
szMessSortNok: .asciz "Area not sorted !!!!!.\n"
|
||||
szLibSort: .asciz "\nAfter sort\n"
|
||||
szSpace: .asciz " "
|
||||
|
||||
.align 2
|
||||
tabNumber: .int 5,7,8,3,2,9,1,4,6
|
||||
#tabNumber: .int 9,8,7,6,5,4,3,2,1
|
||||
.equ NBTABNUMBER1, . - tabNumber
|
||||
.equ NBTABNUMBER, NBTABNUMBER1 / 4 # compute items number
|
||||
|
||||
/*******************************************/
|
||||
/* UNINITIALED DATA */
|
||||
/*******************************************/
|
||||
.bss
|
||||
.align 2
|
||||
sConvArea: .skip 24
|
||||
|
||||
/********************************...-..--*****/
|
||||
/* SECTION CODE */
|
||||
/**********************************************/
|
||||
.text
|
||||
.global main
|
||||
|
||||
main:
|
||||
call stdio_init_all # général init
|
||||
1: # start loop connexion
|
||||
li a0,0 # raz argument register
|
||||
call tud_cdc_n_connected # waiting for USB connection
|
||||
beqz a0,1b # return code = zero ?
|
||||
|
||||
la a0,szMessStart # message address
|
||||
call writeString # display message
|
||||
|
||||
la s0,tabNumber # number array address
|
||||
li s1,0
|
||||
li s2,NBTABNUMBER
|
||||
2: # item loop
|
||||
sh2add t3,s1,s0 # compute item address
|
||||
lw a0,(t3)
|
||||
call displayResultD
|
||||
add s1,s1,1
|
||||
blt s1,s2,2b
|
||||
la a0,szCariageReturn
|
||||
call writeString
|
||||
mv a0,s0 # number array address
|
||||
li a1,0 # first item
|
||||
addi a2,s2,-1 # last item
|
||||
call mergeSort # sort
|
||||
mv a0,s0 # number array address
|
||||
li a1,0 # first item
|
||||
addi a2,s2,-1 # last item
|
||||
call isSorted
|
||||
|
||||
la a0,szLibSort
|
||||
call writeString
|
||||
|
||||
li s1,0
|
||||
3: # item loop
|
||||
sh2add t3,s1,s0 # compute item address
|
||||
lw a0,(t3)
|
||||
call displayResultD
|
||||
add s1,s1,1
|
||||
blt s1,s2,3b
|
||||
la a0,szCariageReturn
|
||||
call writeString
|
||||
la a0,szMessEnd
|
||||
call writeString
|
||||
call getchar
|
||||
100: # final loop
|
||||
j 100b
|
||||
/**********************************************/
|
||||
/* display Result décimal */
|
||||
/**********************************************/
|
||||
/* a0 value */
|
||||
.equ LGZONECONV, 20
|
||||
displayResultD:
|
||||
addi sp, sp, -4 # reserve stack
|
||||
sw ra, 0(sp)
|
||||
la a1,sConvArea # conversion result address
|
||||
call conversion10 # binary conversion
|
||||
la a0,sConvArea # message address
|
||||
call writeString # display message
|
||||
la a0,szSpace
|
||||
call writeString
|
||||
100:
|
||||
lw ra, 0(sp)
|
||||
addi sp, sp, 4
|
||||
ret
|
||||
|
||||
/**********************************************/
|
||||
/* sort control */
|
||||
/**********************************************/
|
||||
/* a0 area address */
|
||||
/* a1 first element */
|
||||
/* a2 last element */
|
||||
.equ LGZONECONV, 20
|
||||
isSorted:
|
||||
addi sp, sp, -4 # reserve stack
|
||||
sw ra, 0(sp)
|
||||
mv t0,a1
|
||||
sh2add t1,t0,a0
|
||||
lw t2,(t1) # load first element
|
||||
1:
|
||||
addi t0,t0,1
|
||||
ble t0,a2,2f # end indice ?
|
||||
la a0,szMessSortOk
|
||||
call writeString
|
||||
li a0,1 # yes -> area is sorted
|
||||
j 100f
|
||||
2:
|
||||
sh2add t1,t0,a0
|
||||
lw t3,(t1) # load next element
|
||||
bge t3,t2,3f # >= ?
|
||||
la a0,szMessSortNok
|
||||
call writeString
|
||||
li a0,0 # no -> area is not sorted
|
||||
j 100f
|
||||
3:
|
||||
mv t2,t3
|
||||
j 1b
|
||||
100:
|
||||
lw ra, 0(sp)
|
||||
addi sp, sp, 4
|
||||
ret
|
||||
|
||||
/******************************************************************/
|
||||
/* merge sort */
|
||||
/******************************************************************/
|
||||
/* a0 contains array address */
|
||||
/* a1 contains the first element */
|
||||
/* a2 contains the last element */
|
||||
mergeSort:
|
||||
addi sp, sp, -20 # reserve stack
|
||||
sw ra, 0(sp) # save registers
|
||||
sw s0, 4(sp)
|
||||
sw s1, 8(sp)
|
||||
sw s2, 12(sp)
|
||||
sw s3, 16(sp)
|
||||
li t0,1
|
||||
blt a2,t0,100f # end < 1 -> end
|
||||
ble a2,a1,100f # first > last ? -> end
|
||||
srli t0,a2,1 # number of element of each subset
|
||||
bge t0,a1,1f
|
||||
mv t0,a1
|
||||
1:
|
||||
mv s0,a0 # save address area
|
||||
mv s1,a1 # save first indice
|
||||
mv s2,a2 # save last indice
|
||||
mv s3,t0 # save last indice
|
||||
mv a2,t0 # sort set 1
|
||||
call mergeSort #
|
||||
addi a1,s3,1 # index set 2
|
||||
mv a2,s2 # index end
|
||||
mv a0,s0 # array address
|
||||
call mergeSort # sort lower part
|
||||
mv a1,s1 # fist index set 1
|
||||
mv a2,s3 # last index set 1
|
||||
addi a2,a2,1 # start index set 2
|
||||
mv a3,s2 # last index set 2
|
||||
mv a0,s0 # array address
|
||||
call merge # merge two sets
|
||||
|
||||
100:
|
||||
lw ra, 0(sp)
|
||||
lw s0, 4(sp)
|
||||
lw s1, 8(sp)
|
||||
lw s2, 12(sp)
|
||||
lw s3, 16(sp)
|
||||
addi sp, sp, 20
|
||||
ret
|
||||
/******************************************************************/
|
||||
/* merge */
|
||||
/******************************************************************/
|
||||
/* a0 contains the address of table */
|
||||
/* a1 contains first start index */
|
||||
/* a2 contains second start index */
|
||||
/* a3 contains the last index */
|
||||
merge: # INFO: merge
|
||||
addi sp, sp, -4 # reserve stack
|
||||
sw ra, 0(sp) # save registers
|
||||
mv t0,a2 # init with second index
|
||||
1:
|
||||
sh2add t2,a1,a0
|
||||
lw t3,(t2) # load value first index
|
||||
sh2add t4,t0,a0
|
||||
lw t5,(t4) # load value second index
|
||||
ble t3,t5,4f # <= -> location first section OK
|
||||
|
||||
sw t5,(t2) # store value second section in first section
|
||||
addi t1,t0,1
|
||||
ble t1,a3,2f # end section 2 ?
|
||||
sw t3,(t4) # store element section 1 in section 2
|
||||
j 4f
|
||||
2: # loop insert element part 1 into part 2
|
||||
addi t6,t1,-1
|
||||
sh2add t4,t1,a0
|
||||
lw t5,(t4) # load value second set
|
||||
bge t3,t5,3f # compare value set 1 value set 2
|
||||
sh2add t4,t6,a0 # <
|
||||
sw t3,(t4) # store value 1
|
||||
j 4f # and loop
|
||||
3: # value 1 > value 2
|
||||
sh2add t4,t6,a0
|
||||
sw t5,(t4) # store value 2
|
||||
addi t1,t1,1 # increment indice
|
||||
ble t1,a3,2b # end set 2 ? no - > loop
|
||||
addi t1,t1,-1 #
|
||||
sh2add t4,t1,a0
|
||||
sw t3,(t4) # store value set 1 in last post
|
||||
4:
|
||||
addi a1,a1,1
|
||||
ble a1,a2,1b # end first section ?
|
||||
100:
|
||||
lw ra, 0(sp)
|
||||
addi sp, sp, 4
|
||||
ret
|
||||
/************************************/
|
||||
/* file include Fonctions */
|
||||
/***********************************/
|
||||
/* for this file see risc-v task include a file */
|
||||
.include "../../includeFunctions.s"
|
||||
|
|
@ -0,0 +1,43 @@
|
|||
msort: function [a compare] [msort-do merge] [
|
||||
if (length? a) < 2 [return a]
|
||||
; define a recursive Msort-do function
|
||||
msort-do: function [a b l] [mid] [
|
||||
either l < 4 [
|
||||
if l = 3 [msort-do next b next a 2]
|
||||
merge a b 1 next b l - 1
|
||||
] [
|
||||
mid: make integer! l / 2
|
||||
msort-do b a mid
|
||||
msort-do skip b mid skip a mid l - mid
|
||||
merge a b mid skip b mid l - mid
|
||||
]
|
||||
]
|
||||
; function Merge is the key part of the algorithm
|
||||
merge: func [a b lb c lc] [
|
||||
until [
|
||||
either (compare first b first c) [
|
||||
change/only a first b
|
||||
b: next b
|
||||
a: next a
|
||||
zero? lb: lb - 1
|
||||
] [
|
||||
change/only a first c
|
||||
c: next c
|
||||
a: next a
|
||||
zero? lc: lc - 1
|
||||
]
|
||||
]
|
||||
loop lb [
|
||||
change/only a first b
|
||||
b: next b
|
||||
a: next a
|
||||
]
|
||||
loop lc [
|
||||
change/only a first c
|
||||
c: next c
|
||||
a: next a
|
||||
]
|
||||
]
|
||||
msort-do a copy a length? a
|
||||
a
|
||||
]
|
||||
Loading…
Add table
Add a link
Reference in a new issue