September Morn Update
This commit is contained in:
parent
4e2d22a71d
commit
aac6731f2c
6856 changed files with 141342 additions and 21127 deletions
|
|
@ -22,4 +22,5 @@ If the number 10 is never generated as the first number in a loop, loop forever.
|
|||
* [[Loops/N plus one half]]
|
||||
* [[Loops/Nested]]
|
||||
* [[Loops/While]]
|
||||
* [[Loops/with multiple ranges]]
|
||||
<br><br>
|
||||
|
|
|
|||
181
Task/Loops-Break/ARM-Assembly/loops-break.arm
Normal file
181
Task/Loops-Break/ARM-Assembly/loops-break.arm
Normal file
|
|
@ -0,0 +1,181 @@
|
|||
/* ARM assembly Raspberry PI */
|
||||
/* program loopbreak.s */
|
||||
|
||||
/* Constantes */
|
||||
.equ STDOUT, 1 @ Linux output console
|
||||
.equ EXIT, 1 @ Linux syscall
|
||||
.equ WRITE, 4 @ Linux syscall
|
||||
|
||||
/*********************************/
|
||||
/* Initialized data */
|
||||
/*********************************/
|
||||
.data
|
||||
szMessEndLoop: .asciz "loop break with value : \n"
|
||||
szMessResult: .ascii "Resultat = " @ message result
|
||||
sMessValeur: .fill 12, 1, ' '
|
||||
.asciz "\n"
|
||||
.align 4
|
||||
iGraine: .int 123456
|
||||
/*********************************/
|
||||
/* UnInitialized data */
|
||||
/*********************************/
|
||||
.bss
|
||||
/*********************************/
|
||||
/* code section */
|
||||
/*********************************/
|
||||
.text
|
||||
.global main
|
||||
main: @ entry of program
|
||||
push {fp,lr} @ saves 2 registers
|
||||
1: @ begin loop
|
||||
mov r4,#20
|
||||
2:
|
||||
mov r0,#19
|
||||
bl genereraleas @ generate number
|
||||
cmp r0,#10 @ compar value
|
||||
beq 3f @ break if equal
|
||||
ldr r1,iAdrsMessValeur @ display value
|
||||
bl conversion10 @ call function with 2 parameter (r0,r1)
|
||||
ldr r0,iAdrszMessResult
|
||||
bl affichageMess @ display message
|
||||
subs r4,#1 @ decrement counter
|
||||
bgt 2b @ loop if greather
|
||||
b 1b @ begin loop one
|
||||
|
||||
3:
|
||||
mov r2,r0 @ save value
|
||||
ldr r0,iAdrszMessEndLoop
|
||||
bl affichageMess @ display message
|
||||
mov r0,r2
|
||||
ldr r1,iAdrsMessValeur
|
||||
bl conversion10 @ call function with 2 parameter (r0,r1)
|
||||
ldr r0,iAdrszMessResult
|
||||
bl affichageMess @ display message
|
||||
|
||||
100: @ standard end of the program
|
||||
mov r0, #0 @ return code
|
||||
pop {fp,lr} @restaur 2 registers
|
||||
mov r7, #EXIT @ request to exit program
|
||||
svc #0 @ perform the system call
|
||||
|
||||
iAdrsMessValeur: .int sMessValeur
|
||||
iAdrszMessResult: .int szMessResult
|
||||
iAdrszMessEndLoop: .int szMessEndLoop
|
||||
/******************************************************************/
|
||||
/* 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 */
|
||||
/******************************************************************/
|
||||
/* r0 contains value and r1 address area */
|
||||
conversion10:
|
||||
push {r1-r4,lr} @ save registers
|
||||
mov r3,r1
|
||||
mov r2,#10
|
||||
|
||||
1: @ start loop
|
||||
bl divisionpar10 @ r0 <- dividende. quotient ->r0 reste -> r1
|
||||
add r1,#48 @ digit
|
||||
strb r1,[r3,r2] @ store digit on area
|
||||
sub r2,#1 @ previous position
|
||||
cmp r0,#0 @ stop if quotient = 0 */
|
||||
bne 1b @ else loop
|
||||
@ and move spaces in first on area
|
||||
mov r1,#' ' @ space
|
||||
2:
|
||||
strb r1,[r3,r2] @ store space in area
|
||||
subs r2,#1 @ @ previous position
|
||||
bge 2b @ loop if r2 >= zéro
|
||||
|
||||
100:
|
||||
pop {r1-r4,lr} @ restaur registres
|
||||
bx lr @return
|
||||
/***************************************************/
|
||||
/* division par 10 signé */
|
||||
/* Thanks to http://thinkingeek.com/arm-assembler-raspberry-pi/*
|
||||
/* and http://www.hackersdelight.org/ */
|
||||
/***************************************************/
|
||||
/* r0 dividende */
|
||||
/* r0 quotient */
|
||||
/* r1 remainder */
|
||||
divisionpar10:
|
||||
/* r0 contains the argument to be divided by 10 */
|
||||
push {r2-r4} /* save registers */
|
||||
mov r4,r0
|
||||
mov r3,#0x6667 @ r3 <- magic_number lower
|
||||
movt r3,#0x6666 @ r3 <- magic_number upper
|
||||
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 */
|
||||
|
||||
/***************************************************/
|
||||
/* Generation random number */
|
||||
/***************************************************/
|
||||
/* r0 contains limit */
|
||||
genereraleas:
|
||||
push {r1-r4,lr} @ save registers
|
||||
ldr r4,iAdriGraine
|
||||
ldr r2,[r4]
|
||||
ldr r3,iNbDep1
|
||||
mul r2,r3,r2
|
||||
ldr r3,iNbDep1
|
||||
add r2,r2,r3
|
||||
str r2,[r4] @ maj de la graine pour l appel suivant
|
||||
|
||||
mov r1,r0 @ divisor
|
||||
mov r0,r2 @ dividende
|
||||
bl division
|
||||
mov r0,r3 @ résult = remainder
|
||||
|
||||
100: @ end function
|
||||
pop {r1-r4,lr} @ restaur registers
|
||||
bx lr @ return
|
||||
/********************************************************************/
|
||||
iAdriGraine: .int iGraine
|
||||
iNbDep1: .int 0x343FD
|
||||
iNbDep2: .int 0x269EC3
|
||||
/***************************************************/
|
||||
/* integer division unsigned */
|
||||
/***************************************************/
|
||||
division:
|
||||
/* r0 contains dividend */
|
||||
/* r1 contains divisor */
|
||||
/* r2 returns quotient */
|
||||
/* r3 returns remainder */
|
||||
push {r4, lr}
|
||||
mov r2, #0 @ init quotient
|
||||
mov r3, #0 @ init remainder
|
||||
mov r4, #32 @ init counter bits
|
||||
b 2f
|
||||
1: @ loop
|
||||
movs r0, r0, LSL #1 @ r0 <- r0 << 1 updating cpsr (sets C if 31st bit of r0 was 1)
|
||||
adc r3, r3, r3 @ r3 <- r3 + r3 + C. This is equivalent to r3 <- (r3 << 1) + C
|
||||
cmp r3, r1 @ compute r3 - r1 and update cpsr
|
||||
subhs r3, r3, r1 @ if r3 >= r1 (C=1) then r3 <- r3 - r1
|
||||
adc r2, r2, r2 @ r2 <- r2 + r2 + C. This is equivalent to r2 <- (r2 << 1) + C
|
||||
2:
|
||||
subs r4, r4, #1 @ r4 <- r4 - 1
|
||||
bpl 1b @ if r4 >= 0 (N=0) then loop
|
||||
pop {r4, lr}
|
||||
bx lr
|
||||
|
|
@ -1,7 +1,7 @@
|
|||
do
|
||||
a = int(rnd * 20)
|
||||
print a
|
||||
if a = 10 then exit loop 'EXIT FOR works the same inside FOR loops
|
||||
b = int(rnd * 20)
|
||||
print b
|
||||
loop
|
||||
100 RANDOMIZE
|
||||
110 DO
|
||||
120 LET A=RND(20)+1
|
||||
130 PRINT A,
|
||||
140 IF A=10 THEN EXIT DO
|
||||
150 PRINT RND(20)+1
|
||||
160 LOOP
|
||||
|
|
|
|||
|
|
@ -1,5 +1,7 @@
|
|||
10 FOR l = 1 TO 20
|
||||
20 IF l = 10 THEN LET l = 20: GO TO 40: REM terminate the loop
|
||||
30 PRINT l
|
||||
40 NEXT l
|
||||
50 STOP
|
||||
do
|
||||
a = int(rnd * 20)
|
||||
print a
|
||||
if a = 10 then exit loop 'EXIT FOR works the same inside FOR loops
|
||||
b = int(rnd * 20)
|
||||
print b
|
||||
loop
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
10 LET a = INT (RND * 20)
|
||||
20 PRINT a
|
||||
30 IF a = 10 THEN STOP
|
||||
40 PRINT INT (RND * 20)
|
||||
50 GO TO 10
|
||||
10 FOR l = 1 TO 20
|
||||
20 IF l = 10 THEN LET l = 20: GO TO 40: REM terminate the loop
|
||||
30 PRINT l
|
||||
40 NEXT l
|
||||
50 STOP
|
||||
|
|
|
|||
5
Task/Loops-Break/BASIC/loops-break-6.basic
Normal file
5
Task/Loops-Break/BASIC/loops-break-6.basic
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
10 LET a = INT (RND * 20)
|
||||
20 PRINT a
|
||||
30 IF a = 10 THEN STOP
|
||||
40 PRINT INT (RND * 20)
|
||||
50 GO TO 10
|
||||
11
Task/Loops-Break/Befunge/loops-break.bf
Normal file
11
Task/Loops-Break/Befunge/loops-break.bf
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
>60v *2\<
|
||||
>?>\1-:|
|
||||
1+ $
|
||||
>^ 7
|
||||
v.:%++67<
|
||||
>55+-#v_@
|
||||
>60v *2\<
|
||||
>?>\1-:|
|
||||
1+ $
|
||||
>^ 7
|
||||
^ .%++67<
|
||||
22
Task/Loops-Break/Neko/loops-break.neko
Normal file
22
Task/Loops-Break/Neko/loops-break.neko
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
/**
|
||||
Loops/Break in Neko
|
||||
Tectonics:
|
||||
nekoc loops-break.neko
|
||||
neko loops-break
|
||||
*/
|
||||
|
||||
var random_new = $loader.loadprim("std@random_new", 0);
|
||||
var random_int = $loader.loadprim("std@random_int", 2);
|
||||
|
||||
var random = random_new();
|
||||
|
||||
while true {
|
||||
var r = random_int(random, 20);
|
||||
$print(r, " ");
|
||||
|
||||
if r == 10 break;
|
||||
|
||||
r = random_int(random, 20);
|
||||
$print(r, " ");
|
||||
}
|
||||
$print("\n");
|
||||
8
Task/Loops-Break/Ol/loops-break.ol
Normal file
8
Task/Loops-Break/Ol/loops-break.ol
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
(import (otus random!))
|
||||
|
||||
(call/cc (lambda (break)
|
||||
(let loop ()
|
||||
(if (= (rand! 20) 10)
|
||||
(break #t))
|
||||
(print (rand! 20))
|
||||
(loop))))
|
||||
|
|
@ -1,14 +1,16 @@
|
|||
// cargo-deps: rand
|
||||
|
||||
extern crate rand;
|
||||
use rand::distributions::{Range, IndependentSample};
|
||||
use rand::{thread_rng, Rng};
|
||||
|
||||
fn main() {
|
||||
let mut rng = thread_rng();
|
||||
loop {
|
||||
let num = Range::new(0, 19 + 1).ind_sample(&mut rand::thread_rng());
|
||||
let num = rng.gen_range(0, 20);
|
||||
if num == 10 {
|
||||
println!("{}", num);
|
||||
break;
|
||||
}
|
||||
println!("{}", rng.gen_range(0, 20));
|
||||
}
|
||||
}
|
||||
|
|
|
|||
25
Task/Loops-Break/Spin/loops-break.spin
Normal file
25
Task/Loops-Break/Spin/loops-break.spin
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
con
|
||||
_clkmode = xtal1 + pll16x
|
||||
_clkfreq = 80_000_000
|
||||
|
||||
obj
|
||||
ser : "FullDuplexSerial.spin"
|
||||
|
||||
pub main | r, s
|
||||
ser.start(31, 30, 0, 115200)
|
||||
|
||||
s := 1337 ' PRNG seed
|
||||
|
||||
repeat
|
||||
r := ||?s // 20
|
||||
ser.dec(r)
|
||||
ser.tx(32)
|
||||
if r == 10
|
||||
quit
|
||||
r := ||?s // 20
|
||||
ser.dec(r)
|
||||
ser.tx(32)
|
||||
|
||||
waitcnt(_clkfreq + cnt)
|
||||
ser.stop
|
||||
cogstop(0)
|
||||
10
Task/Loops-Break/VBA/loops-break.vba
Normal file
10
Task/Loops-Break/VBA/loops-break.vba
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
Public Sub LoopsBreak()
|
||||
Dim value As Integer
|
||||
Randomize
|
||||
Do While True
|
||||
value = Int(20 * Rnd)
|
||||
Debug.Print value
|
||||
If value = 10 Then Exit Do
|
||||
Debug.Print Int(20 * Rnd)
|
||||
Loop
|
||||
End Sub
|
||||
16
Task/Loops-Break/Visual-Basic-.NET/loops-break.visual
Normal file
16
Task/Loops-Break/Visual-Basic-.NET/loops-break.visual
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
Module Program
|
||||
Sub Main()
|
||||
' Initialize with seed 0 to get deterministic output (may vary across .NET versions, though).
|
||||
Dim rand As New Random(0)
|
||||
|
||||
Do
|
||||
Dim first = rand.Next(20) ' Upper bound is exclusive.
|
||||
Console.Write(first & " ")
|
||||
|
||||
If first = 10 Then Exit Do
|
||||
|
||||
Dim second = rand.Next(20)
|
||||
Console.Write(second & " ")
|
||||
Loop
|
||||
End Sub
|
||||
End Module
|
||||
Loading…
Add table
Add a link
Reference in a new issue