diff --git a/ka/crt/fmt/ltostr.k b/ka/crt/fmt/ltostr.k index e2b1bd6..e3961e3 100644 --- a/ka/crt/fmt/ltostr.k +++ b/ka/crt/fmt/ltostr.k @@ -38,7 +38,7 @@ ltostr: sgn rx8, ax1 ; extract ax1 sign cmp rx8, -1 ; negative? - neg.z ax1 + neg.z ax1, ax1 ; main loop .conv: diff --git a/ka/crt/fmt/strtol.k b/ka/crt/fmt/strtol.k index 85fa9c5..d1e06a3 100644 --- a/ka/crt/fmt/strtol.k +++ b/ka/crt/fmt/strtol.k @@ -147,7 +147,7 @@ strtoq: ret.z ; yes - neg rax + neg rax, rax ret .bad: diff --git a/vm/in/ARITH b/vm/in/ARITH index d8022a9..6064e65 100644 --- a/vm/in/ARITH +++ b/vm/in/ARITH @@ -36,14 +36,14 @@ sgnf rm rim # # Arithmetical NEG operation # -# $1 = NOT($1) + 1 +# $1 = NOT($2) + 1 # -# Sets CF if $1 == 0; clears it otherwise -# Sets OF if $1 == $LONG_MIN; clears it otherwise +# Sets CF if $2 == 0; clears it otherwise +# Sets OF if $2 == $LONG_MIN; clears it otherwise # Sets ZF and SF according to the result # -neg rm -negf rm +neg rm rim +negf rm rim # # Arithmetical INC operation @@ -81,6 +81,17 @@ decf rm add rm rim addf rm rim +# +# Atomic exchange and add (XADD) +# +# $tmp = $1 +# $1 = $1 + $2 +# $2 = $1 +# +# Preserves all flags +# +xadd rm rm + # # Arithmetical ADD operation, with carry # @@ -116,6 +127,18 @@ subf rm rim mul rm rim mulf rm rim +# +# Arithmetical unsigned 128-bit MUL operation +# +# $1 = HI($2 * $3) +# $2 = LO($2 * $3) +# +# Sets CF and OF if HI($2 * $3) > 0, clears them otherwise +# Preserves ZF and SF +# +mulx rm rm rim +mulfx rm rm rim + # # Arithmetical unsigned DIV operation # @@ -125,6 +148,16 @@ mulf rm rim # div rm rim +# +# Arithmetical unsigned combined DIV and MOD operations +# +# $1 = ($2 MOD $3) +# $2 = ($2 DIV $3) +# +# Preserves all flags +# + +divx rm rm rim # # Arithmetical unsigned modulo operation (REM) # @@ -134,25 +167,3 @@ div rm rim # rem rm rim -# -# Arithmetical unsigned 128-bit MUL operation -# -# RDX = HI(RAX * $1) -# RAX = LO(RAX * $1) -# -# Sets CF and OF if HI($1 * $2) > 0, clears them otherwise -# Preserves ZF and SF -# -mul2 rim -mul2f rim - -# -# Arithmetical unsigned combined DIV and MOD operations -# -# RDX = (RAX MOD $1) -# RAX = (RAX DIV $1) -# -# Preserves all flags -# -div2 rim - diff --git a/vm/in/BITMP b/vm/in/BITMP new file mode 100644 index 0000000..f22a0c7 --- /dev/null +++ b/vm/in/BITMP @@ -0,0 +1,8 @@ +# The OS/K Team licenses this file to you under the MIT license. +# See the LICENSE file in the project root for more information. + +#---------------------------------------------------------------------------# +# Bit manipulation instructions # +#---------------------------------------------------------------------------# + + diff --git a/vm/in/INSTRS b/vm/in/INSTRS index 584b155..87b5e7e 100644 --- a/vm/in/INSTRS +++ b/vm/in/INSTRS @@ -11,6 +11,7 @@ include "JUMPS" include "STACK" include "FLAGS" include "INOUT" +include "BITMP" include "DEBUG" include "STRING" include "TERNARY" diff --git a/vm/in/LOGIC b/vm/in/LOGIC index eb6b270..56fe8ab 100644 --- a/vm/in/LOGIC +++ b/vm/in/LOGIC @@ -18,11 +18,11 @@ test rim rim # # Bitwise NOT operation # -# $1 = NOT($1) +# $1 = NOT($2) # # Preserves all flags # -not rm +not rm rim # # Bitwise OR operation diff --git a/vm/in/TERNARY b/vm/in/TERNARY index 22c84a3..3fa48fa 100644 --- a/vm/in/TERNARY +++ b/vm/in/TERNARY @@ -5,11 +5,19 @@ # 3-operand instructions # #---------------------------------------------------------------------------# +# +# 3-operand rotation (KROTd) +# +# $3 -> $2 -> $1 -> $3 (KROTL) +# $1 -> $2 -> $3 -> $1 (KROTR) +# +krotr rm rm rm +krotl rm rm rm + # # 2-sources logical & arithmetical operations # -# Preserves CF and OF -# Sets ZF and SF according to the result +# Preserves all flags # kor rm rim rim kand rm rim rim @@ -22,6 +30,8 @@ kxnor rm rim rim kxorn rm rim rim kshl rm rim rim kshr rm rim rim +ksal rm rim rim +ksar rm rim rim kadd rm rim rim kadc rm rim rim kado rm rim rim diff --git a/vm/in/arith.c b/vm/in/arith.c index 460c69a..054ba30 100644 --- a/vm/in/arith.c +++ b/vm/in/arith.c @@ -3,7 +3,7 @@ #include -//-------------------------------------------------------------------------- +//----------------------------------------------------------------------------// IMPL_START_2(sgn) { @@ -18,28 +18,31 @@ IMPL_START_2(sgnf) } IMPL_OUT_ZSF; -IMPL_START_1(neg) +IMPL_START_2(neg) { - v1 = ~v1 + 1; + v1 = ~v2 + 1; } IMPL_OUT; -IMPL_START_1(negf) +IMPL_START_2(negf) { - if (v1 == 0) flg |= CF; + if (v2 == 0) { + flg |= CF; + flg &= ~OF; + } else { flg &= ~CF; - if (v1 == LONG_MIN) flg |= OF; + if (v2 == LONG_MIN) flg |= OF; else flg &= ~OF; } - v1 = ~v1 + 1; + v1 = ~v2 + 1; } IMPL_OUT_ZSF; -//-------------------------------------------------------------------------- +//----------------------------------------------------------------------------// IMPL_START_1(inc) { @@ -71,7 +74,7 @@ IMPL_START_1(decf) } IMPL_OUT_ZSF; -//-------------------------------------------------------------------------- +//----------------------------------------------------------------------------// IMPL_START_2(add) { @@ -79,6 +82,14 @@ IMPL_START_2(add) } IMPL_OUT; +IMPL_START_2(xadd) +{ + ulong tmp = v1; + v1 += v2; + v2 = tmp; +} +IMPL_OUT_2; + IMPL_START_2(addf) { if (v1 + v2 < v1) flg |= OF; @@ -115,7 +126,7 @@ IMPL_START_2(adcf) } IMPL_OUT_ZSF; -//-------------------------------------------------------------------------- +//----------------------------------------------------------------------------// IMPL_START_2(sub) { @@ -139,7 +150,7 @@ IMPL_START_2(cmp) } IMPL_END; -//-------------------------------------------------------------------------- +//----------------------------------------------------------------------------// // // www.codeproject.com/Tips/618570/UInt-Multiplication-Squaring @@ -190,17 +201,17 @@ IMPL_START_2(mulf) } IMPL_OUT; -IMPL_START_1(mul2) +IMPL_START_3(mulx) { - multiply(rax, v1, &rdx, &rax); + multiply(v2, v3, &v1, &v2); } -IMPL_END; +IMPL_OUT_2; -IMPL_START_1(mul2f) +IMPL_START_3(mulfx) { - multiply(rax, v1, &rdx, &rax); + multiply(v2, v3, &v1, &v2); - if (rdx > 0) { + if (v1 > 0) { flg |= CF; flg |= OF; } @@ -210,9 +221,9 @@ IMPL_START_1(mul2f) flg &= ~OF; } } -IMPL_END; +IMPL_OUT_2; -//-------------------------------------------------------------------------- +//----------------------------------------------------------------------------// IMPL_START_2(div) { @@ -226,10 +237,12 @@ IMPL_START_2(rem) } IMPL_OUT; -IMPL_START_1(div2) +IMPL_START_3(divx) { - rdx = rax % v1; - rax = rax / v1; + v1 = v2 % v3; + v2 = v2 / v3; } -IMPL_END; +IMPL_OUT_2; + +//----------------------------------------------------------------------------// diff --git a/vm/in/bitmp.c b/vm/in/bitmp.c new file mode 100644 index 0000000..e69de29 diff --git a/vm/in/logic.c b/vm/in/logic.c index 542db29..7f9a25a 100644 --- a/vm/in/logic.c +++ b/vm/in/logic.c @@ -3,13 +3,15 @@ #include -IMPL_START_1(not) +//----------------------------------------------------------------------------// + +IMPL_START_2(not) { - v1 = ~v1; + v1 = ~v2; } IMPL_OUT; -//-------------------------------------------------------------------------- +//----------------------------------------------------------------------------// IMPL_START_2(test) { @@ -33,7 +35,7 @@ IMPL_START_2(andf) } IMPL_OUT_ZSF; -//-------------------------------------------------------------------------- +//----------------------------------------------------------------------------// IMPL_START_2(or) { @@ -63,7 +65,7 @@ IMPL_START_2(xorf) } IMPL_OUT_ZSF; -//-------------------------------------------------------------------------- +//----------------------------------------------------------------------------// IMPL_START_2(shl) { @@ -135,5 +137,5 @@ IMPL_START_2(sarf) } IMPL_OUT_ZSF; -//-------------------------------------------------------------------------- +//----------------------------------------------------------------------------// diff --git a/vm/in/ternary.c b/vm/in/ternary.c index 302428c..678c4f0 100644 --- a/vm/in/ternary.c +++ b/vm/in/ternary.c @@ -5,23 +5,43 @@ //----------------------------------------------------------------------------// +IMPL_START_3(krotl) +{ + ulong tmp = v1; + v1 = v2; + v2 = v3; + v3 = tmp; +} +IMPL_OUT_3; + +IMPL_START_3(krotr) +{ + ulong tmp = v3; + v3 = v2; + v2 = v1; + v1 = tmp; +} +IMPL_OUT_3; + +//----------------------------------------------------------------------------// + #define K_IMPL_ALU(name, expr) \ IMPL_START_3(k##name) \ { \ v1 = (expr); \ } \ -IMPL_OUT_ZSF +IMPL_OUT K_IMPL_ALU(or, v2 | v3); K_IMPL_ALU(and, v2 & v3); K_IMPL_ALU(xor, v2 ^ v3); K_IMPL_ALU(nor, ~(v2 | v3)); -K_IMPL_ALU(orn, ~v2 | v3); +K_IMPL_ALU(orn, v2 | ~v3); K_IMPL_ALU(nand, ~(v2 & v3)); -K_IMPL_ALU(andn, ~v2 & v3); +K_IMPL_ALU(andn, v2 & ~v3); K_IMPL_ALU(xnor, ~(v2 ^ v3)); -K_IMPL_ALU(xorn, ~v2 ^ v3); +K_IMPL_ALU(xorn, v2 ^ ~v3); K_IMPL_ALU(shl, v2 << v3); K_IMPL_ALU(shr, v2 >> v3); @@ -38,3 +58,25 @@ K_IMPL_ALU(rem, v2 % v3); //----------------------------------------------------------------------------// +IMPL_START_3(ksal) +{ + long w2 = v2; + long w3 = v3; + + w2 <<= w3; + v1 = (ulong)w2; +} +IMPL_OUT; + +IMPL_START_3(ksar) +{ + long w2 = v2; + long w3 = v3; + + w2 >>= w3; + v1 = (ulong)w2; +} +IMPL_OUT; + +//----------------------------------------------------------------------------// +