September Morn Update

This commit is contained in:
Ingy döt Net 2019-09-12 10:33:56 -07:00
parent 4e2d22a71d
commit aac6731f2c
6856 changed files with 141342 additions and 21127 deletions

View file

@ -0,0 +1,186 @@
/* ARM assembly Raspberry PI */
/* program areaString.s */
/* Constantes */
.equ STDOUT, 1 @ Linux output console
.equ EXIT, 1 @ Linux syscall
.equ WRITE, 4 @ Linux syscall
/* Initialized data */
.data
szMessStringsch: .ascii "The string is at item : "
sZoneconv: .fill 12,1,' '
szCarriageReturn: .asciz "\n"
szMessStringNfound: .asciz "The string is not found in this area.\n"
/* areas strings */
szString1: .asciz "Apples"
szString2: .asciz "Oranges"
szString3: .asciz "Pommes"
szString4: .asciz "Raisins"
szString5: .asciz "Abricots"
/* pointer items area 1*/
tablesPoi1:
pt1_1: .int szString1
pt1_2: .int szString2
pt1_3: .int szString3
pt1_4: .int szString4
ptVoid_1: .int 0
ptVoid_2: .int 0
ptVoid_3: .int 0
ptVoid_4: .int 0
ptVoid_5: .int 0
szStringSch: .asciz "Raisins"
szStringSch1: .asciz "Ananas"
/* UnInitialized data */
.bss
/* code section */
.text
.global main
main: /* entry of program */
push {fp,lr} /* saves 2 registers */
@@@@@@@@@@@@@@@@@@@@@@@@
@ add string 5 to area
@@@@@@@@@@@@@@@@@@@@@@@@
ldr r1,iAdrtablesPoi1 @ begin pointer area 1
mov r0,#0 @ counter
1: @ search first void pointer
ldr r2,[r1,r0,lsl #2] @ read string pointer address item r0 (4 bytes by pointer)
cmp r2,#0 @ is null ?
addne r0,#1 @ no increment counter
bne 1b @ and loop
@ store pointer string 5 in area at position r0
ldr r2,iAdrszString5 @ address string 5
str r2,[r1,r0,lsl #2] @ store address
@@@@@@@@@@@@@@@@@@@@@@@@
@ display string at item 3
@@@@@@@@@@@@@@@@@@@@@@@@
mov r2,#2 @ pointers begin in position 0
ldr r1,iAdrtablesPoi1 @ begin pointer area 1
ldr r0,[r1,r2,lsl #2]
bl affichageMess
ldr r0,iAdrszCarriageReturn
bl affichageMess
@@@@@@@@@@@@@@@@@@@@@@@@
@ search string in area
@@@@@@@@@@@@@@@@@@@@@@@@
ldr r1,iAdrszStringSch
//ldr r1,iAdrszStringSch1 @ uncomment for other search : not found !!
ldr r2,iAdrtablesPoi1 @ begin pointer area 1
mov r3,#0
2: @ search
ldr r0,[r2,r3,lsl #2] @ read string pointer address item r0 (4 bytes by pointer)
cmp r0,#0 @ is null ?
beq 3f @ end search
bl comparaison
cmp r0,#0 @ string = ?
addne r3,#1 @ no increment counter
bne 2b @ and loop
mov r0,r3 @ position item string
ldr r1,iAdrsZoneconv @ conversion decimal
bl conversion10S
ldr r0,iAdrszMessStringsch
bl affichageMess
b 100f
3: @ end search string not found
ldr r0,iAdrszMessStringNfound
bl affichageMess
100: /* standard end of the program */
mov r0, #0 @ return code
pop {fp,lr} @restaur 2 registers
mov r7, #EXIT @ request to exit program
swi 0 @ perform the system call
iAdrtablesPoi1: .int tablesPoi1
iAdrszMessStringsch: .int szMessStringsch
iAdrszString5: .int szString5
iAdrszStringSch: .int szStringSch
iAdrszStringSch1: .int szStringSch1
iAdrsZoneconv: .int sZoneconv
iAdrszMessStringNfound: .int szMessStringNfound
iAdrszCarriageReturn: .int szCarriageReturn
/******************************************************************/
/* display text with size calculation */
/******************************************************************/
/* r0 contains the address of the message */
affichageMess:
push {fp,lr} /* save registres */
push {r0,r1,r2,r7} /* save others registers */
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" */
swi #0 /* call systeme */
pop {r0,r1,r2,r7} /* restaur others registers */
pop {fp,lr} /* restaur des 2 registres */
bx lr /* return */
/***************************************************/
/* conversion register signed décimal */
/***************************************************/
/* r0 contient le registre */
/* r1 contient l adresse de la zone de conversion */
conversion10S:
push {r0-r5,lr} /* save des registres */
mov r2,r1 /* debut zone stockage */
mov r5,#'+' /* par defaut le signe est + */
cmp r0,#0 /* nombre négatif ? */
movlt r5,#'-' /* oui le signe est - */
mvnlt r0,r0 /* et inversion en valeur positive */
addlt r0,#1
mov r4,#10 /* longueur de la zone */
1: /* debut de boucle de conversion */
bl divisionpar10 /* division */
add r1,#48 /* ajout de 48 au reste pour conversion ascii */
strb r1,[r2,r4] /* stockage du byte en début de zone r5 + la position r4 */
sub r4,r4,#1 /* position précedente */
cmp r0,#0
bne 1b /* boucle si quotient different de zéro */
strb r5,[r2,r4] /* stockage du signe à la position courante */
subs r4,r4,#1 /* position précedente */
blt 100f /* si r4 < 0 fin */
/* sinon il faut completer le debut de la zone avec des blancs */
mov r3,#' ' /* caractere espace */
2:
strb r3,[r2,r4] /* stockage du byte */
subs r4,r4,#1 /* position précedente */
bge 2b /* boucle si r4 plus grand ou egal a zero */
100: /* fin standard de la fonction */
pop {r0-r5,lr} /*restaur desregistres */
bx lr
/***************************************************/
/* division par 10 signé */
/* Thanks to http://thinkingeek.com/arm-assembler-raspberry-pi/*
/* and http://www.hackersdelight.org/ */
/***************************************************/
/* r0 contient le dividende */
/* r0 retourne le quotient */
/* r1 retourne le reste */
divisionpar10:
/* r0 contains the argument to be divided by 10 */
push {r2-r4} /* save registers */
mov r4,r0
ldr r3, .Ls_magic_number_10 /* r1 <- magic_number */
smull r1, r2, r3, r0 /* r1 <- Lower32Bits(r1*r0). r2 <- Upper32Bits(r1*r0) */
mov r2, r2, ASR #2 /* r2 <- r2 >> 2 */
mov r1, r0, LSR #31 /* r1 <- r0 >> 31 */
add r0, r2, r1 /* r0 <- r2 + r1 */
add r2,r0,r0, lsl #2 /* r2 <- r0 * 5 */
sub r1,r4,r2, lsl #1 /* r1 <- r4 - (r2 * 2) = r4 - (r0 * 10) */
pop {r2-r4}
bx lr /* leave function */
bx lr /* leave function */
.Ls_magic_number_10: .word 0x66666667

View file

@ -1,5 +1,9 @@
10 DIM A%(11): REM ARRAY OF ELEVEN INTEGER ELEMENTS
20 LET A%(1) = -1
30 LET A%(11) = 1
40 PRINT A%(1), A%(11)
50 END
REDIM dynamicArray(10) AS INTEGER
dynamicArray(0) = -1
PRINT dynamicArray(0)
REDIM dynamicArray(20)
dynamicArray(20) = 1
PRINT dynamicArray(0), dynamicArray(20)

View file

@ -1,9 +1,4 @@
REDIM dynamicArray(10) AS INTEGER
dynamicArray(0) = -1
PRINT dynamicArray(0)
REDIM dynamicArray(20)
dynamicArray(20) = 1
PRINT dynamicArray(0), dynamicArray(20)
10 DIM A%(11): REM ARRAY OF TWELVE INTEGER ELEMENTS
20 LET A%(0) = -1
30 LET A%(11) = 1
40 PRINT A%(0), A%(11)

View file

@ -1 +1 @@
var aStaticArray := (1, 2, 3).
var staticArray := new int[]{1, 2, 3};

View file

@ -1,4 +1,4 @@
var anArray := system'Array new:3.
anArray[0] := 1.
anArray[1] := 2.
anArray[2] := 3.
var array := system'Array.allocate:3;
array[0] := 1;
array[1] := 2;
array[2] := 3;

View file

@ -1,4 +1,4 @@
V<int,3> aStackAllocatedArray.
aStackAllocatedArray[0] := 1.
aStackAllocatedArray[1] := 2.
aStackAllocatedArray[2] := 3.
int stackAllocatedArray[3];
stackAllocatedArray[0] := 1;
stackAllocatedArray[1] := 2;
stackAllocatedArray[2] := 3;

View file

@ -1,6 +1,6 @@
var aDynamicArray := ArrayList new.
aDynamicArray append:1.
aDynamicArray append:2.
aDynamicArray append:4.
var dynamicArray := new system'collections'ArrayList();
dynamicArray.append:1;
dynamicArray.append:2;
dynamicArray.append:4;
aDynamicArray[2] := 3.
dynamicArray[2] := 3;

View file

@ -1,3 +1,3 @@
system'console writeLine(anArray[0]).
system'console writeLine(aStackAllocatedArray[1]).
system'console writeLine(aDynamicArray[2]).
system'console.writeLine(array[0]);
system'console.writeLine(stackAllocatedArray[1]);
system'console.writeLine(dynamicArray[2]);

View file

@ -0,0 +1,56 @@
:- module array_example.
:- interface.
:- import_module io.
:- pred main(io::di, io::uo) is det.
:- implementation.
:- import_module array, int.
:- use_module exception.
:- type example_error ---> impossible.
main(!IO) :-
some [!A] ( % needed to introduce a state variable not present in the head
% Create an array(int) of length 10, with initial values of 0
array.init(10, 0, !:A),
% create an empty array (with no initial value)
% since the created array is never used, type inference can't tell what
% kind of array it is, and there's an unresolved polymorphism warning.
array.make_empty_array(_Empty),
% resize our first array, so that we can then set its 17th member
% new values are set to -1
array.resize(20, -1, !A),
!A ^ elem(17) := 5,
% Mercury data structures tend to have deterministic (exception thrown
% on error), semideterministic (logical failure on error), and unsafe
% (undefined behavior on error) access methods.
array.lookup(!.A, 5, _), % det
( if array.semidet_lookup(!.A, 100, _) then % semidet
exception.throw(impossible)
else
true
),
array.unsafe_lookup(!.A, 5, _), % could cause a segfault on a smaller array
% output: array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, -1, -1, -1, -1, -1, -1, 5, -1, -1])
io.print_line(!.A, !IO),
plusminus(2, 0, !A),
% output: array([2, -2, 2, -2, 2, -2, 2, -2, 2, -2, 1, -3, 1, -3, 1, -3, 1, 3, 1, -3])
io.print_line(!.A, !IO)
).
% Sample predicate operating on an array.
% Note the array_* modes instead of in/out.
:- pred plusminus(int, int, array(int), array(int)).
:- mode plusminus(in, in, array_di, array_uo) is det.
plusminus(N, I, !A) :-
( if array.semidet_lookup(!.A, I, X) then
!A ^ unsafe_elem(I) := X + N,
plusminus(-N, I+1, !A)
else
true
).

15
Task/Arrays/Ol/arrays.ol Normal file
View file

@ -0,0 +1,15 @@
; making an array
#(1 2 3 4 5)
; making an empty array
#()
#0
; making n-length array with undefined values (actually, #false)
(make-array 5)
; making n-length array with default value
(make-array 5 0)
; getting n-th element of array
(ref array 1)

View file

@ -9,6 +9,7 @@ get list (N);
begin;
declare B(N) float initial (9, 4, 7, 3, 8, 11, 0, 5, 15, 6);
B(3) = -11;
put (B(2));
end;
/* Example of a dynamic array. */
@ -17,3 +18,4 @@ end;
allocate C;
C = 0;
c(7) = 12;
put (C(9));

View file

@ -0,0 +1,9 @@
set "index" to 0
. "Assign random values to array"
: "loop"
set "array&index&" to random 0 to 99
inc "index" by 1
if "index" < 100 then "loop"
* "Value of index 50 is ('array('50')')."
end

View file

@ -0,0 +1,13 @@
set "xx" to 0
set "yy" to 0
. "Assign random values to array"
: "loopX"
set "array&xx&,&yy&" to random 0 to 99
inc "xx" by 1
if "xx" < 32 then "loopX"
set "xx" to 0
inc "yy" by 1
if "yy" < 32 then "loopX"
* "Value of 16,16 is ('array('16'),('16')')."
end

View file

@ -0,0 +1,29 @@
Sub matrix()
'create an array,
Dim a(3) As Integer
Dim i As Integer
'assign a value to it,
For i = 1 To 3
a(i) = i * i
Next i
'and retrieve an element
For i = 1 To 3
Debug.Print a(i)
Next i
'dynamic
Dim d() As Integer
ReDim d(3)
For i = 1 To 3
d(i) = i * i
Next i
'and retrieve an element
For i = 1 To 3
Debug.Print d(i)
Next i
'push a value to it - expand the array and preserve existing values
ReDim Preserve d(4)
d(4) = 16:
For i = 1 To 4
Debug.Print d(i)
Next i
End Sub

View file

@ -0,0 +1,57 @@
entity Array_Test is
end entity Array_Test;
architecture Example of Array_test is
-- Array type have to be defined first
type Integer_Array is array (Integer range <>) of Integer;
-- Array index range can be ascending...
signal A : Integer_Array (1 to 20);
-- or descending
signal B : Integer_Array (20 downto 1);
-- VHDL array index ranges may begin at any value, not just 0 or 1
signal C : Integer_Array (-37 to 20);
-- VHDL arrays may be indexed by enumerated types, which are
-- discrete non-numeric types
type Days is (Mon, Tue, Wed, Thu, Fri, Sat, Sun);
type Activities is (Work, Fish);
type Daily_Activities is array (Days) of Activities;
signal This_Week : Daily_Activities := (Mon to Fri => Work, Others => Fish);
type Finger is range 1 to 4; -- exclude thumb
type Fingers_Extended is array (Finger) of Boolean;
signal Extended : Fingers_Extended;
-- Array types may be unconstrained.
-- Objects of the type must be constrained
type Arr is array (Integer range <>) of Integer;
signal Uninitialized : Arr (1 to 10);
signal Initialized_1 : Arr (1 to 20) := (others => 1);
constant Initialized_2 : Arr := (1 to 30 => 2);
constant Const : Arr := (1 to 10 => 1, 11 to 20 => 2, 21 | 22 => 3);
signal Centered : Arr (-50 to 50) := (0 => 1, others => 0);
signal Result : Integer;
begin
A <= (others => 0); -- Assign whole array
B <= (1 => 1, 2 => 1,
3 => 2, others => 0); -- Assign whole array, different values
A (1) <= -1; -- Assign individual element
A (2 to 4) <= B (3 downto 1); -- Assign a slice
A (3 to 5) <= (2, 4, -1); -- Assign an aggregate
A (3 to 5) <= A (4 to 6); -- It is OK to overlap slices when assigned
-- VHDL arrays does not have 'first' and 'last' elements,
-- but have 'Left' and 'Right' instead
Extended (Extended'Left) <= False; -- Set leftmost element of array
Extended (Extended'Right) <= False; -- Set rightmost element of array
Result <= A (A'Low) + B (B'High);
end architecture Example;