From d8893823b6d0134c18bb6bf58b75e03d6a7f72b0 Mon Sep 17 00:00:00 2001 From: julianb0 Date: Sun, 16 Jun 2019 22:18:09 +0200 Subject: [PATCH] rand --- ka/main.k | 2 +- vm/in/INSTRS | 17 +++++++++++++++++ vm/in/instrs.c | 41 ++++++++++++++++++++++++++++++++++------- 3 files changed, 52 insertions(+), 8 deletions(-) diff --git a/ka/main.k b/ka/main.k index 5400d9a..5607fc5 100644 --- a/ka/main.k +++ b/ka/main.k @@ -5,7 +5,7 @@ ; Main function ; main: - call itoa_test + call str_test ret showoff: diff --git a/vm/in/INSTRS b/vm/in/INSTRS index abc15ba..248362f 100644 --- a/vm/in/INSTRS +++ b/vm/in/INSTRS @@ -605,6 +605,23 @@ nop # cpuid +# +# Pseudo-random number generation (RAND32/RAND64) +# +# RAND32 generates a 32-bit pseudo-random number using +# a nonlinear additive feedback pseudo-random number generator +# of period 16 * (2^31 - 1). +# +# RAND64 generates two such numbers, and store them in the destination +# operand's higher and lower double words respectively +# +# The running program does not control the seeding and the processor +# may generate numbers from the same generator for other purposes +# than this instruction +# +rand32 rm +rand64 rm + # # Get code/data offset (GCO/GCD) # diff --git a/vm/in/instrs.c b/vm/in/instrs.c index 0a3dd08..9dc330d 100644 --- a/vm/in/instrs.c +++ b/vm/in/instrs.c @@ -2,10 +2,13 @@ // See the LICENSE file in the project root for more information. #include +#include #define _NEED_ARCH_I #include +//----------------------------------------------------------------------------// + IMPL_START_0(nop) { } @@ -13,6 +16,37 @@ IMPL_END; //----------------------------------------------------------------------------// +IMPL_START_0(cpuid) +{ +} +IMPL_END; + +//----------------------------------------------------------------------------// + +IMPL_START_1(rand32) +{ + v1 = (int)random(); +} +IMPL_OUT; + +IMPL_START_1(rand64) +{ + v1 = (random() << 32) | (int)random(); +} +IMPL_OUT; + +//----------------------------------------------------------------------------// + +IMPL_START_1(time) +{ + struct timeval time; + gettimeofday(&time, NULL); + v1 = (time.tv_sec * 1000) + (time.tv_usec / 1000); +} +IMPL_OUT; + +//----------------------------------------------------------------------------// + IMPL_START_1(prn) { if (p1->mlen > 1) { @@ -39,10 +73,3 @@ IMPL_START_0(cla) IMPL_END; //----------------------------------------------------------------------------// - -IMPL_START_0(cpuid) -{ -} -IMPL_END; - -//----------------------------------------------------------------------------//