June 2018 Update
This commit is contained in:
parent
ba8067c3b7
commit
22f33d4004
5278 changed files with 84726 additions and 14379 deletions
171
Task/Call-a-function/ARM-Assembly/call-a-function.arm
Normal file
171
Task/Call-a-function/ARM-Assembly/call-a-function.arm
Normal file
|
|
@ -0,0 +1,171 @@
|
|||
/* ARM assembly Raspberry PI */
|
||||
/* program callfonct.s */
|
||||
|
||||
/* Constantes */
|
||||
.equ STDOUT, 1
|
||||
.equ WRITE, 4
|
||||
.equ EXIT, 1
|
||||
|
||||
/***********************/
|
||||
/* Initialized data */
|
||||
/***********************/
|
||||
.data
|
||||
szMessage: .asciz "Hello. \n" @ message
|
||||
szRetourLigne: .asciz "\n"
|
||||
szMessResult: .ascii "Resultat : " @ message result
|
||||
sMessValeur: .fill 12, 1, ' '
|
||||
.asciz "\n"
|
||||
/***********************/
|
||||
/* No Initialized data */
|
||||
/***********************/
|
||||
.bss
|
||||
iValeur: .skip 4 @ reserve 4 bytes in memory
|
||||
|
||||
.text
|
||||
.global main
|
||||
main:
|
||||
ldr r0,=szMessage @ adresse of message short program
|
||||
bl affichageMess @ call function with 1 parameter (r0)
|
||||
|
||||
@ call function with parameters in register
|
||||
mov r0,#5
|
||||
mov r1,#10
|
||||
bl fonction1 @ call function with 2 parameters (r0,r1)
|
||||
ldr r1,=sMessValeur @ result in r0
|
||||
bl conversion10S @ call function with 2 parameter (r0,r1)
|
||||
ldr r0,=szMessResult
|
||||
bl affichageMess @ call function with 1 parameter (r0)
|
||||
|
||||
@ call function with parameters on stack
|
||||
mov r0,#5
|
||||
mov r1,#10
|
||||
push {r0,r1}
|
||||
bl fonction2 @ call function with 2 parameters on the stack
|
||||
@ result in r0
|
||||
ldr r1,=sMessValeur
|
||||
bl conversion10S @ call function with 2 parameter (r0,r1)
|
||||
ldr r0,=szMessResult
|
||||
bl affichageMess @ call function with 1 parameter (r0)
|
||||
|
||||
|
||||
/* end of program */
|
||||
mov r0, #0 @ return code
|
||||
mov r7, #EXIT @ request to exit program
|
||||
swi 0 @ perform the system call
|
||||
|
||||
/******************************************************************/
|
||||
/* call function parameter in register */
|
||||
/******************************************************************/
|
||||
/* r0 value one */
|
||||
/* r1 value two */
|
||||
/* return in r0 */
|
||||
fonction1:
|
||||
push {fp,lr} /* save des 2 registres */
|
||||
push {r1,r2} /* save des autres registres */
|
||||
mov r2,#20
|
||||
mul r0,r2
|
||||
add r0,r0,r1
|
||||
pop {r1,r2} /* restaur des autres registres */
|
||||
pop {fp,lr} /* restaur des 2 registres */
|
||||
bx lr /* retour procedure */
|
||||
|
||||
/******************************************************************/
|
||||
/* call function parameter in the stack */
|
||||
/******************************************************************/
|
||||
/* return in r0 */
|
||||
fonction2:
|
||||
push {fp,lr} /* save des 2 registres */
|
||||
add fp,sp,#8 /* address parameters in the stack*/
|
||||
push {r1,r2} /* save des autres registres */
|
||||
ldr r0,[fp]
|
||||
ldr r1,[fp,#4]
|
||||
mov r2,#-20
|
||||
mul r0,r2
|
||||
add r0,r0,r1
|
||||
pop {r1,r2} /* restaur des autres registres */
|
||||
pop {fp,lr} /* restaur des 2 registres */
|
||||
add sp,#8 /* very important, for stack aligned */
|
||||
bx lr /* retour procedure */
|
||||
|
||||
/******************************************************************/
|
||||
/* affichage des messages avec calcul longueur */
|
||||
/******************************************************************/
|
||||
/* r0 contient l adresse du message */
|
||||
affichageMess:
|
||||
push {fp,lr} /* save des 2 registres */
|
||||
push {r0,r1,r2,r7} /* save des autres registres */
|
||||
mov r2,#0 /* compteur longueur */
|
||||
1: /*calcul de la longueur */
|
||||
ldrb r1,[r0,r2] /* recup octet position debut + indice */
|
||||
cmp r1,#0 /* si 0 c est fini */
|
||||
beq 1f
|
||||
add r2,r2,#1 /* sinon on ajoute 1 */
|
||||
b 1b
|
||||
1: /* donc ici r2 contient la longueur du message */
|
||||
mov r1,r0 /* adresse du message en r1 */
|
||||
mov r0,#STDOUT /* code pour écrire sur la sortie standard Linux */
|
||||
mov r7, #WRITE /* code de l appel systeme 'write' */
|
||||
swi #0 /* appel systeme */
|
||||
pop {r0,r1,r2,r7} /* restaur des autres registres */
|
||||
pop {fp,lr} /* restaur des 2 registres */
|
||||
bx lr /* retour procedure */
|
||||
/***************************************************/
|
||||
/* conversion registre en décimal signé */
|
||||
/***************************************************/
|
||||
/* r0 contient le registre */
|
||||
/* r1 contient l adresse de la zone de conversion */
|
||||
conversion10S:
|
||||
push {fp,lr} /* save des 2 registres frame et retour */
|
||||
push {r0-r5} /* save autres 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} /*restaur des autres registres */
|
||||
pop {fp,lr} /* restaur des 2 registres frame et retour */
|
||||
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 autres registres */
|
||||
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 */
|
||||
.align 4
|
||||
.Ls_magic_number_10: .word 0x66666667
|
||||
2
Task/Call-a-function/Elena/call-a-function-1.elena
Normal file
2
Task/Call-a-function/Elena/call-a-function-1.elena
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
var c0 := [ console writeLine("No argument provided") ].
|
||||
var c2 := (:a:b)<int,int>[ console printLine("Arguments ",a," and ",b," provided") ].
|
||||
1
Task/Call-a-function/Elena/call-a-function-2.elena
Normal file
1
Task/Call-a-function/Elena/call-a-function-2.elena
Normal file
|
|
@ -0,0 +1 @@
|
|||
c0().
|
||||
1
Task/Call-a-function/Elena/call-a-function-3.elena
Normal file
1
Task/Call-a-function/Elena/call-a-function-3.elena
Normal file
|
|
@ -0,0 +1 @@
|
|||
c2(2,4).
|
||||
3
Task/Call-a-function/Elena/call-a-function-4.elena
Normal file
3
Task/Call-a-function/Elena/call-a-function-4.elena
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
var exch := (:x)<ref<object>>[ x value := 2 ].
|
||||
var a := 1.
|
||||
exch(&a).
|
||||
1
Task/Call-a-function/Factor/call-a-function-1.factor
Normal file
1
Task/Call-a-function/Factor/call-a-function-1.factor
Normal file
|
|
@ -0,0 +1 @@
|
|||
foo
|
||||
1
Task/Call-a-function/Factor/call-a-function-2.factor
Normal file
1
Task/Call-a-function/Factor/call-a-function-2.factor
Normal file
|
|
@ -0,0 +1 @@
|
|||
foo
|
||||
2
Task/Call-a-function/Factor/call-a-function-3.factor
Normal file
2
Task/Call-a-function/Factor/call-a-function-3.factor
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
"a" "b" "c" 3 narray
|
||||
! { "a" "b" "c" }
|
||||
6
Task/Call-a-function/Factor/call-a-function-4.factor
Normal file
6
Task/Call-a-function/Factor/call-a-function-4.factor
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
<email>
|
||||
"jack@aol.com" >>from
|
||||
{ "jill@aol.com" } >>to
|
||||
"Hello there" >>subject
|
||||
body >>body
|
||||
send-email
|
||||
1
Task/Call-a-function/Factor/call-a-function-5.factor
Normal file
1
Task/Call-a-function/Factor/call-a-function-5.factor
Normal file
|
|
@ -0,0 +1 @@
|
|||
\ foo
|
||||
1
Task/Call-a-function/Factor/call-a-function-6.factor
Normal file
1
Task/Call-a-function/Factor/call-a-function-6.factor
Normal file
|
|
@ -0,0 +1 @@
|
|||
{ foo } [ foo ]
|
||||
1
Task/Call-a-function/Factor/call-a-function-7.factor
Normal file
1
Task/Call-a-function/Factor/call-a-function-7.factor
Normal file
|
|
@ -0,0 +1 @@
|
|||
foo
|
||||
1
Task/Call-a-function/Factor/call-a-function-8.factor
Normal file
1
Task/Call-a-function/Factor/call-a-function-8.factor
Normal file
|
|
@ -0,0 +1 @@
|
|||
\ foo primitive?
|
||||
2
Task/Call-a-function/Factor/call-a-function-9.factor
Normal file
2
Task/Call-a-function/Factor/call-a-function-9.factor
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
{ 1 2 3 } 2 [ - ] curry map .
|
||||
! { -1 0 1 }
|
||||
38
Task/Call-a-function/Fortress/call-a-function.fortress
Normal file
38
Task/Call-a-function/Fortress/call-a-function.fortress
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
component call_a_function
|
||||
export Executable
|
||||
(* Declaring test functions that allow the various ways to call functions in Fortress to be demonstrated. *)
|
||||
addition(i:ZZ32, j:ZZ32): ZZ32 = i+j
|
||||
addition(i:ZZ32): ZZ32 = i+1
|
||||
|
||||
(* Strings are concatenated by using a space as an infix operator. *)
|
||||
addition(i:String, j:String): String = i j
|
||||
|
||||
printAString(s:String): () = println(s)
|
||||
|
||||
(* Functions can be passed to other functions as arguments. When passing a function as an argument, the argument's type should be
|
||||
represented as follows: "typeOfArgument(s)->returnType," which, in this case, is "String->()." You could also technically use the
|
||||
"Any" type, but that isn't type-safe. *)
|
||||
printAString(s:String, f:String->()) = f(s)
|
||||
|
||||
(* Defined functions can then be called as follows. *)
|
||||
var x:ZZ32 = addition(1, 2)
|
||||
var str:String = addition("This is ", "another string.")
|
||||
|
||||
run() = do
|
||||
(* You can call built-in functions the same way that you call functions that you define. *)
|
||||
println("x at start: " x)
|
||||
|
||||
x := addition(x, 2)
|
||||
|
||||
println("x at middle: " x)
|
||||
|
||||
printAString("This " "is " "a " "string.")
|
||||
printAString(str)
|
||||
printAString("\nThis is a string that is being printed by a function of the same name \nthat takes a function as an argument.\n",
|
||||
printAString)
|
||||
|
||||
x := addition(4)
|
||||
|
||||
println("x at end: " x)
|
||||
end
|
||||
end
|
||||
90
Task/Call-a-function/Julia/call-a-function.julia
Normal file
90
Task/Call-a-function/Julia/call-a-function.julia
Normal file
|
|
@ -0,0 +1,90 @@
|
|||
# Calling a function that requires no arguments:
|
||||
f() = print("Hello world!")
|
||||
f()
|
||||
|
||||
|
||||
# Calling a function with a fixed number of arguments:
|
||||
function f(x, y, z)
|
||||
x*y - z^2
|
||||
end
|
||||
|
||||
f(3, 4, 2)
|
||||
|
||||
|
||||
# Calling a function with optional arguments:
|
||||
# Note Julia uses multiple dispatch based on argument number and type, so
|
||||
# f() is always different from f(x) unless default arguments are used, as in:
|
||||
|
||||
pimultiple(mult=1.0) = pi * mult # so pimultiple() defaults to pi * (1.0) or pi
|
||||
|
||||
|
||||
# Calling a function with a variable number of arguments:
|
||||
|
||||
f(a,b,x...) = reduce(+, 0, x) - a - b
|
||||
|
||||
|
||||
# here a and b are single arguments, but x is a tuple of x plus whatever follows x, so:
|
||||
a = b = c = d = e = 3
|
||||
f(a,b,c) # x within the function is (c) so == 0 + c - a - b
|
||||
f(a,b,c,d,e) # x is a tuple == (c,d,e) so == (0 + c + d + e) - a - b
|
||||
f(a,b) # x is () so == 0 - a - b
|
||||
|
||||
|
||||
# Calling a function with named arguments:
|
||||
# Functions with keyword arguments are defined using a semicolon in the function signature,
|
||||
# as in
|
||||
# function plot(x, y; style="solid", width=1, color="black")
|
||||
#
|
||||
# When the function is called, the semicolon is optional, so plot here can be
|
||||
# either called with plot(x, y, width=2) or less commonly as plot(x, y; width=2).
|
||||
|
||||
|
||||
# Using a function in statement context:
|
||||
# Any function can be used as a variable by its name.
|
||||
|
||||
circlearea(x) = x^2 * pi
|
||||
map(circlearea, [r1, r2, r3, r4])
|
||||
|
||||
|
||||
# Using a function in first-class context within an expression:
|
||||
cylindervolume = circlearea(r) * h
|
||||
|
||||
|
||||
# Obtaining the return value of a function:
|
||||
radius = 2.5
|
||||
area = circlearea(2.5)
|
||||
|
||||
|
||||
# Distinguishing built-in functions and user-defined functions:
|
||||
# Julia does not attempt to distinguish these in any special way,
|
||||
# but at the REPL command line there is ? help available for builtin
|
||||
# functions that would not generally be available for the user-defined ones.
|
||||
|
||||
|
||||
# Distinguishing subroutines and functions:
|
||||
# All subroutines are called functions in Julia, regardless of whether they return values.
|
||||
|
||||
|
||||
# Stating whether arguments are passed by value or by reference:
|
||||
# As in Python, all arguments are passed by pointer reference, but assignment to a passed argument
|
||||
# only changes the variable within the function. Assignment to the values referenced by the argument
|
||||
## DOES however change those values. For instance:
|
||||
|
||||
a = 3
|
||||
b = [3]
|
||||
c = [3]
|
||||
|
||||
function f(x, y)
|
||||
a = 0
|
||||
b[1] = 0
|
||||
c = [0]
|
||||
end # a and c are now unchanged but b = [0]
|
||||
|
||||
|
||||
# Is partial application possible and how:
|
||||
# In Julia, there are many different ways to compose functions. In particular,
|
||||
# Julia has an "arrow" operator -> that may be used to curry other functions.
|
||||
|
||||
f(a, b) = a^2 + a + b
|
||||
v = [4, 6, 8]
|
||||
map(x -> f(x, 10), v) # v = [30, 52, 82]
|
||||
Loading…
Add table
Add a link
Reference in a new issue