September Morn Update
This commit is contained in:
parent
4e2d22a71d
commit
aac6731f2c
6856 changed files with 141342 additions and 21127 deletions
|
|
@ -0,0 +1,226 @@
|
|||
/* ARM assembly Raspberry PI */
|
||||
/* program selectionSort.s */
|
||||
|
||||
/************************************/
|
||||
/* Constantes */
|
||||
/************************************/
|
||||
.equ STDOUT, 1 @ Linux output console
|
||||
.equ EXIT, 1 @ Linux syscall
|
||||
.equ WRITE, 4 @ Linux syscall
|
||||
/*********************************/
|
||||
/* Initialized data */
|
||||
/*********************************/
|
||||
.data
|
||||
szMessSortOk: .asciz "Table sorted.\n"
|
||||
szMessSortNok: .asciz "Table not sorted !!!!!.\n"
|
||||
sMessResult: .ascii "Value : "
|
||||
sMessValeur: .fill 11, 1, ' ' @ size => 11
|
||||
szCarriageReturn: .asciz "\n"
|
||||
|
||||
.align 4
|
||||
iGraine: .int 123456
|
||||
.equ NBELEMENTS, 10
|
||||
#TableNumber: .int 1,3,6,2,5,9,10,8,4,7
|
||||
TableNumber: .int 10,9,8,7,6,5,4,3,2,1
|
||||
/*********************************/
|
||||
/* UnInitialized data */
|
||||
/*********************************/
|
||||
.bss
|
||||
/*********************************/
|
||||
/* code section */
|
||||
/*********************************/
|
||||
.text
|
||||
.global main
|
||||
main: @ entry of program
|
||||
|
||||
1:
|
||||
ldr r0,iAdrTableNumber @ address number table
|
||||
mov r1,#0
|
||||
mov r2,#NBELEMENTS @ number of élements
|
||||
bl selectionSort
|
||||
ldr r0,iAdrTableNumber @ address number table
|
||||
bl displayTable
|
||||
|
||||
ldr r0,iAdrTableNumber @ address number table
|
||||
mov r1,#NBELEMENTS @ number of élements
|
||||
bl isSorted @ control sort
|
||||
cmp r0,#1 @ sorted ?
|
||||
beq 2f
|
||||
ldr r0,iAdrszMessSortNok @ no !! error sort
|
||||
bl affichageMess
|
||||
b 100f
|
||||
2: @ yes
|
||||
ldr r0,iAdrszMessSortOk
|
||||
bl affichageMess
|
||||
100: @ standard end of the program
|
||||
mov r0, #0 @ return code
|
||||
mov r7, #EXIT @ request to exit program
|
||||
svc #0 @ perform the system call
|
||||
|
||||
iAdrsMessValeur: .int sMessValeur
|
||||
iAdrszCarriageReturn: .int szCarriageReturn
|
||||
iAdrsMessResult: .int sMessResult
|
||||
iAdrTableNumber: .int TableNumber
|
||||
iAdrszMessSortOk: .int szMessSortOk
|
||||
iAdrszMessSortNok: .int szMessSortNok
|
||||
/******************************************************************/
|
||||
/* control sorted table */
|
||||
/******************************************************************/
|
||||
/* r0 contains the address of table */
|
||||
/* r1 contains the number of elements > 0 */
|
||||
/* r0 return 0 if not sorted 1 if sorted */
|
||||
isSorted:
|
||||
push {r2-r4,lr} @ save registers
|
||||
mov r2,#0
|
||||
ldr r4,[r0,r2,lsl #2]
|
||||
1:
|
||||
add r2,#1
|
||||
cmp r2,r1
|
||||
movge r0,#1
|
||||
bge 100f
|
||||
ldr r3,[r0,r2, lsl #2]
|
||||
cmp r3,r4
|
||||
movlt r0,#0
|
||||
blt 100f
|
||||
mov r4,r3
|
||||
b 1b
|
||||
100:
|
||||
pop {r2-r4,lr}
|
||||
bx lr @ return
|
||||
/******************************************************************/
|
||||
/* selection sort */
|
||||
/******************************************************************/
|
||||
/* r0 contains the address of table */
|
||||
/* r1 contains the first element */
|
||||
/* r2 contains the number of element */
|
||||
selectionSort:
|
||||
push {r1-r7,lr} @ save registers
|
||||
mov r3,r1 @ start index i
|
||||
sub r7,r2,#1 @ compute n - 1
|
||||
1: @ start loop
|
||||
mov r4,r3
|
||||
add r5,r3,#1 @ init index 2
|
||||
2:
|
||||
ldr r1,[r0,r4,lsl #2] @ load value A[mini]
|
||||
ldr r6,[r0,r5,lsl #2] @ load value A[j]
|
||||
cmp r6,r1 @ compare value
|
||||
movlt r4,r5 @ j -> mini
|
||||
add r5,#1 @ increment index j
|
||||
cmp r5,r2 @ end ?
|
||||
blt 2b @ no -> loop
|
||||
cmp r4,r3 @ mini <> j ?
|
||||
beq 3f @ no
|
||||
ldr r1,[r0,r4,lsl #2] @ yes swap A[i] A[mini]
|
||||
ldr r6,[r0,r3,lsl #2]
|
||||
str r1,[r0,r3,lsl #2]
|
||||
str r6,[r0,r4,lsl #2]
|
||||
3:
|
||||
add r3,#1 @ increment i
|
||||
cmp r3,r7 @ end ?
|
||||
blt 1b @ no -> loop
|
||||
|
||||
100:
|
||||
pop {r1-r7,lr}
|
||||
bx lr @ return
|
||||
|
||||
/******************************************************************/
|
||||
/* Display table elements */
|
||||
/******************************************************************/
|
||||
/* r0 contains the address of table */
|
||||
displayTable:
|
||||
push {r0-r3,lr} @ save registers
|
||||
mov r2,r0 @ table address
|
||||
mov r3,#0
|
||||
1: @ loop display table
|
||||
ldr r0,[r2,r3,lsl #2]
|
||||
ldr r1,iAdrsMessValeur @ display value
|
||||
bl conversion10 @ call function
|
||||
ldr r0,iAdrsMessResult
|
||||
bl affichageMess @ display message
|
||||
add r3,#1
|
||||
cmp r3,#NBELEMENTS - 1
|
||||
ble 1b
|
||||
ldr r0,iAdrszCarriageReturn
|
||||
bl affichageMess
|
||||
100:
|
||||
pop {r0-r3,lr}
|
||||
bx lr
|
||||
/******************************************************************/
|
||||
/* display text with size calculation */
|
||||
/******************************************************************/
|
||||
/* r0 contains the address of the message */
|
||||
affichageMess:
|
||||
push {r0,r1,r2,r7,lr} @ save registres
|
||||
mov r2,#0 @ counter length
|
||||
1: @ loop length calculation
|
||||
ldrb r1,[r0,r2] @ read octet start position + index
|
||||
cmp r1,#0 @ if 0 its over
|
||||
addne r2,r2,#1 @ else add 1 in the length
|
||||
bne 1b @ and loop
|
||||
@ so here r2 contains the length of the message
|
||||
mov r1,r0 @ address message in r1
|
||||
mov r0,#STDOUT @ code to write to the standard output Linux
|
||||
mov r7, #WRITE @ code call system "write"
|
||||
svc #0 @ call systeme
|
||||
pop {r0,r1,r2,r7,lr} @ restaur des 2 registres */
|
||||
bx lr @ return
|
||||
/******************************************************************/
|
||||
/* Converting a register to a decimal unsigned */
|
||||
/******************************************************************/
|
||||
/* r0 contains value and r1 address area */
|
||||
/* r0 return size of result (no zero final in area) */
|
||||
/* area size => 11 bytes */
|
||||
.equ LGZONECAL, 10
|
||||
conversion10:
|
||||
push {r1-r4,lr} @ save registers
|
||||
mov r3,r1
|
||||
mov r2,#LGZONECAL
|
||||
|
||||
1: @ start loop
|
||||
bl divisionpar10U @ unsigned r0 <- dividende. quotient ->r0 reste -> r1
|
||||
add r1,#48 @ digit
|
||||
strb r1,[r3,r2] @ store digit on area
|
||||
cmp r0,#0 @ stop if quotient = 0
|
||||
subne r2,#1 @ else previous position
|
||||
bne 1b @ and loop
|
||||
@ and move digit from left of area
|
||||
mov r4,#0
|
||||
2:
|
||||
ldrb r1,[r3,r2]
|
||||
strb r1,[r3,r4]
|
||||
add r2,#1
|
||||
add r4,#1
|
||||
cmp r2,#LGZONECAL
|
||||
ble 2b
|
||||
@ and move spaces in end on area
|
||||
mov r0,r4 @ result length
|
||||
mov r1,#' ' @ space
|
||||
3:
|
||||
strb r1,[r3,r4] @ store space in area
|
||||
add r4,#1 @ next position
|
||||
cmp r4,#LGZONECAL
|
||||
ble 3b @ loop if r4 <= area size
|
||||
|
||||
100:
|
||||
pop {r1-r4,lr} @ restaur registres
|
||||
bx lr @return
|
||||
|
||||
/***************************************************/
|
||||
/* division par 10 unsigned */
|
||||
/***************************************************/
|
||||
/* r0 dividende */
|
||||
/* r0 quotient */
|
||||
/* r1 remainder */
|
||||
divisionpar10U:
|
||||
push {r2,r3,r4, lr}
|
||||
mov r4,r0 @ save value
|
||||
//mov r3,#0xCCCD @ r3 <- magic_number lower raspberry 3
|
||||
//movt r3,#0xCCCC @ r3 <- magic_number higter raspberry 3
|
||||
ldr r3,iMagicNumber @ r3 <- magic_number raspberry 1 2
|
||||
umull r1, r2, r3, r0 @ r1<- Lower32Bits(r1*r0) r2<- Upper32Bits(r1*r0)
|
||||
mov r0, r2, LSR #3 @ r2 <- r2 >> shift 3
|
||||
add r2,r0,r0, lsl #2 @ r2 <- r0 * 5
|
||||
sub r1,r4,r2, lsl #1 @ r1 <- r4 - (r2 * 2) = r4 - (r0 * 10)
|
||||
pop {r2,r3,r4,lr}
|
||||
bx lr @ leave function
|
||||
iMagicNumber: .int 0xCCCCCCCD
|
||||
|
|
@ -1,33 +1,33 @@
|
|||
import extensions.
|
||||
import system'routines.
|
||||
import extensions;
|
||||
import system'routines;
|
||||
|
||||
extension $op
|
||||
extension op
|
||||
{
|
||||
selectionSort
|
||||
[
|
||||
var copy := self clone.
|
||||
selectionSort()
|
||||
{
|
||||
var copy := self.clone();
|
||||
|
||||
0 till(copy length) do(:i)
|
||||
[
|
||||
int k := i.
|
||||
(i + 1) till(copy length) do(:j)
|
||||
[
|
||||
for(int i := 0, i < copy.Length, i += 1)
|
||||
{
|
||||
int k := i;
|
||||
for(int j := i + 1, j < copy.Length, j += 1)
|
||||
{
|
||||
if (copy[j] < copy[k])
|
||||
[
|
||||
k := j.
|
||||
]
|
||||
].
|
||||
copy exchange(i,k).
|
||||
].
|
||||
{
|
||||
k := j
|
||||
}
|
||||
};
|
||||
copy.exchange(i,k)
|
||||
};
|
||||
|
||||
^ copy
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
program =
|
||||
[
|
||||
var list := ("this", "is", "a", "test", "of", "generic", "selection", "sort").
|
||||
public program()
|
||||
{
|
||||
var list := new string[]{"this", "is", "a", "test", "of", "generic", "selection", "sort"};
|
||||
|
||||
console printLine("before:",list).
|
||||
console printLine("after:",list selectionSort).
|
||||
].
|
||||
console.printLine("before:",list.asEnumerable());
|
||||
console.printLine("after:",list.selectionSort().asEnumerable())
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,29 +1,25 @@
|
|||
/*REXX program sorts a stemmed array using the selection-sort algorithm. */
|
||||
/*REXX program sorts a stemmed array using the selection─sort algorithm. */
|
||||
@.=; @.1 = '---The seven hills of Rome:---'
|
||||
@.2 = '=============================='
|
||||
@.3 = 'Caelian'
|
||||
@.4 = 'Palatine'
|
||||
@.5 = 'Capitoline'
|
||||
@.6 = 'Virminal'
|
||||
@.7 = 'Esquiline'
|
||||
@.8 = 'Quirinal'
|
||||
@.9 = 'Aventine'
|
||||
do #=1 until @.#==''; end; #=#-1 /*find the number of items in the array*/
|
||||
/* [↑] adjust # ('cause of DO index)*/
|
||||
@.2 = '=============================='; @.6 = 'Virminal'
|
||||
@.3 = 'Caelian' ; @.7 = 'Esquiline'
|
||||
@.4 = 'Palatine' ; @.8 = 'Quirinal'
|
||||
@.5 = 'Capitoline' ; @.9 = 'Aventine'
|
||||
do #=1 until @.#==''; end /*find the number of items in the array*/
|
||||
# = # - 1 /*adjust # (because of DO index). */
|
||||
call show 'before sort' /*show the before array elements. */
|
||||
say copies('▒', 65) /*show a nice separator line (fence). */
|
||||
say copies('▒', 65) /*show a nice separator line (fence). */
|
||||
call selectionSort # /*invoke selection sort (and # items). */
|
||||
call show ' after sort' /*show the after array elements. */
|
||||
exit /*stick a fork in it, we're a;; done. */
|
||||
/*──────────────────────────────────────────────────────────────────────────────────────*/
|
||||
selectionSort: procedure expose @.; parse arg n
|
||||
do j=1 for n-1
|
||||
_=@.j; p=j; do k=j+1 to n
|
||||
if @.k<_ then do; _=@.k; p=k; end
|
||||
end /*k*/
|
||||
if p==j then iterate /*if the same, the order of items OK. */
|
||||
_=@.j; @.j=@.p; @.p=_ /*swap 2 items that're out-of-sequence.*/
|
||||
end /*j*/
|
||||
return
|
||||
selectionSort: procedure expose @.; parse arg n
|
||||
do j=1 for n-1; _= @.j; p= j
|
||||
do k=j+1 to n; if @.k>=_ then iterate
|
||||
_= @.k; p= k /*this item is out─of─order, swap later*/
|
||||
end /*k*/
|
||||
if p==j then iterate /*if the same, the order of items is OK*/
|
||||
_= @.j; @.j= @.p; @.p= _ /*swap 2 items that're out─of─sequence.*/
|
||||
end /*j*/
|
||||
return
|
||||
/*──────────────────────────────────────────────────────────────────────────────────────*/
|
||||
show: do i=1 for #; say ' element' right(i,length(#)) arg(1)":" @.i; end; return
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue