SparrowOSTS_121127.ISO

This commit is contained in:
Terry A. Davis authored and minexew committed 2018-09-08 21:45:28 +02:00
1 parent 0e9364ff4c
commit e57428affb
1218 files changed
+247142 -165507

No files matched your search

+83
View File
@@ -0,0 +1,83 @@
/*
This is a JIT file, not static.
Asm labels can only be defined once
in a task. Hit F5 to spawn a new task
and run it.
*/
asm {
//Opcodes are slightly different to make writing my x86_64 assembler easier.
//See $LK,"::/SparrowOS/Compiler/OpCodes.TXT.Z","FI:::/SparrowOS/Compiler/OpCodes.TXT.Z"$.
USE64 //Defaults to 64, but this is a good habit.
IMPORT Beep;
_BEEPS::
//You can clobber RAX,RBX,RCX,RDX,R8,R9 in a pure asm subroutine.
ENTER 0
MOV RCX,U64 SF_ARG1[RBP] //$LK,"SF_ARG1","FF:::/SparrowOS/OSMain/Adam1a.HPP.Z,SF_ARG1"$
@@05: PUSH RCX
//U0 Beep(I64 freq=500,Bool busy=FALSE)
PUSH FALSE //Do not busy (spin) wait
PUSH 500 //Hz
CALL Beep
POP RCX
LOOP @@05
LEAVE
//Return vals are in RAX if you have one.
RET1 8
}
//_extern binds a asm sym to a function.
//My convention is to put an underscore
//on C callable routines.
_extern _BEEPS U0 Beeps(I64 cnt);
I64 AsmAndC()
{
I64 noreg i; //Normally this would be stored in a reg
//Check with $LK,"Uf","MN:Uf"$("AsmAndC").
i=PmtI64("Num of beeps 1-5 (%d):",3,1,5);
Beeps(i);
asm {
//You can clobber RAX,RBX,RCX,RDX, but preserve the rest for code in a C+ routine.
USE64 //Defaults to 64, but this is a good habit.
IMPORT Snd;
MOV RCX,&i[RBP] //You can clobber RAX,RBX,RCX,RDX.
//You better preserve the rest.
@@05: PUSH RCX
//U0 Snd(F64 freq,I64 waveform=WF_SQUARE,F64 amp=1.0);
MOV RAX,1.0 //amplitude
PUSH RAX
PUSH WF_SQUARE
MOV RAX,RCX //freq=loop*100.0+100.0
IMUL2 RAX,100 //SparrowOS uses nonstandard opcodes
//to avoid multiple form of the same one.
//See $LK,"::/SparrowOS/Compiler/OpCodes.TXT.Z","FI:::/SparrowOS/Compiler/OpCodes.TXT.Z"$.
//$BK,1$Asm is kind-of a bonus -- there's no reason$BK,0$
//$BK,1$to use it, but I need it for the kernel.$BK,0$
ADD RAX,100
PUSH RAX
FILD U64 [RSP] //Convert from int to floating
FSTP U64 [RSP]
CALL Snd
MOV RCX,0x20000000 //Simple delay loop
@@10: LOOP @@10
POP RCX
LOOP @@05
}
Snd(0);
return i;
}
"Beeps:%d\n",AsmAndC;
+98
View File
@@ -0,0 +1,98 @@
/*
This is a JIT file, not static.
Asm labels can only be defined once
in a task. Hit F5 to spawn a new task
and run it.
*/
//This is to demo glbl var access.
//Glbs defined elsewhere can accessed too, like sys_jiffies.
I64 glbl_hz=400,glbl_hz_step=100,glbl_hz_base=100;
asm {
//Opcodes are slightly different to make writing my x86_64 assembler easier.
//See $LK,"::/SparrowOS/Compiler/OpCodes.TXT.Z","FI:::/SparrowOS/Compiler/OpCodes.TXT.Z"$.
USE64 //Defaults to 64, but this is a good habit.
//See $LK,"::/SparrowOS/OSMain/Str1a.CPP.Z","FL:::/SparrowOS/OSMain/Str1a.CPP.Z,1"$ and $LK,"::/SparrowOS/OSMain/Utils1a.CPP.Z","FI:::/SparrowOS/OSMain/Utils1a.CPP.Z"$.
//These are asm syms and must be imported to work properly.
//When in doubt, import syms.
IMPORT PUT_STR,PUT_CHAR,PUT_HEX_U64;
JIFFIES_MSG: DU8 "Jiffies:",0;
_BEEPS2::
//You can clobber RAX,RBX,RCX,RDX,R8,R9 in a pure asm subroutine.
ENTER 0
MOV RCX,U64 SF_ARG1[RBP] //$LK,"SF_ARG1","FF:::/SparrowOS/OSMain/Adam1a.HPP.Z,SF_ARG1"$
PUSH U64 [&sys_jiffies]
@@05: PUSH RCX
//U0 Beep(I64 freq=500,Bool busy=FALSE)
PUSH FALSE //Do not busy (spin) wait
PUSH U64 [&glbl_hz] //evaluated at run time
CALL &Beep
POP RCX
LOOP @@05
PUSH RSI //we must preserve this register.
MOV RSI,JIFFIES_MSG
CALL PUT_STR
POP RSI
POP RAX
SUB RAX,U64 [&sys_jiffies]
NEG RAX
CALL PUT_HEX_U64
MOV RAX,CH_NEW_LINE
CALL PUT_CHAR
LEAVE
RET1 8
}
//My convention is to put an underscore
//on C callable routines.
_extern _BEEPS2 U0 Beeps2(I64 cnt);
U0 AsmAndC2()
{
I64 reg R15 i;
i=PmtI64("$$FG,PURPLE$$\n\nNum of beeps 1-5 (%d):$$FG$$",3,1,5);
Beeps2(i);
asm {
LIST
//You can clobber RAX,RBX,RCX,RDX, but preserve the rest for code in a C+ routine.
USE64 //Defaults to 64, but this is a good habit.
MOV RCX,R15 //You can clobber RAX,RBX,RCX,RDX.
//You better preserve the rest.
@@05: PUSH RCX
//U0 Snd(F64 freq,I64 waveform=WF_SQUARE,F64 amp=1.0);
MOV RAX,1.0 //amplitude
PUSH RAX
PUSH WF_SQUARE
MOV RAX,RCX //freq=loop*100.0+100.0
IMUL2 RAX,glbl_hz_step //evaluated at compile ttime
ADD RAX,U64 [&glbl_hz_base] //evaluated at run time
PUSH RAX
FILD U64 [RSP] //Convert from int to floating
FSTP U64 [RSP]
CALL &Snd //We can skip IMPORT for this symbol if JIT.
MOV RCX,0x20000000 //Simple delay loop
@@10: LOOP @@10
POP RCX
LOOP @@05
}
Snd(0);
}
AsmAndC2;
+52
View File
@@ -0,0 +1,52 @@
/*
This is a JIT file, not static.
Asm labels can only be defined once
in a task. Hit F5 to spawn a new task
and run it.
These are many useful kernel
routines $LK,"::/SparrowOS/OSMain/Str1a.CPP.Z","FF:::/SparrowOS/OSMain/Str1a.CPP.Z,PUT_CHAR"$
intended to be called from
asm. Generally, they preserve
regs.
You can call any routine you
like, C or asm, if you import it.
Be aware that C routines do not
preserve RAX,RBX,RCX,RDX,R8,R9.
When calling from the shell or
from C, preserve all other regs.
*/
asm {
//Opcodes are slightly different to make writing my x86_64 assembler easier.
//See $LK,"::/SparrowOS/Compiler/OpCodes.TXT.Z","FI:::/SparrowOS/Compiler/OpCodes.TXT.Z"$.
USE64 //Defaults to 64, but this is a good habit.
IMPORT PUT_HEX_U8,PUT_CHAR;
MY_MSG:
//Define U8 does not put terminating zeros
//on strings.
DU8 "Hello World\n",0;
//My convention is to put an underscore
//on C callable routines.
_HELLO_WORLD::
MOV RCX,10
@@05: MOV RAX,RCX
CALL PUT_HEX_U8
MOV RAX,CH_SPACE
CALL PUT_CHAR
MOV RSI,MY_MSG
//Alternatively, if you use an '&', you don't need
//to import a SYS_SYM. This only works for JIT asm.
CALL &PUT_STR
LOOP @@05
RET
};
Call(_HELLO_WORLD);
+48
View File
@@ -0,0 +1,48 @@
#define RANGE_START 1
#define RANGE_END 100
#define FIZZ_SPAN 6
#define BUZZ_SPAN 7
asm {
MSG_BUZZ: DU8 "BUZZ\n\0";
MSG_FIZZ: DU8 "FIZZ\n\0";
MSG_BUZZ_FIZZ: DU8 "BUZZ_FIZZ\n\0";
MSG_NEW_LINE: DU8 "\n\0";
_BUZZ_FIZZ::
ENTER 0 //Always use Enter/Leave
PUSH ESI //RSI must be preserved. This instruction is 64-bit.
MOV RAX,RANGE_START
MOV DX,(FIZZ_SPAN-RANGE_START%FIZZ_SPAN+1)<<8+
(BUZZ_SPAN-RANGE_START%BUZZ_SPAN+1)
@@05: DEC DH
JNZ @@10
MOV DH,FIZZ_SPAN
MOV ESI,MSG_FIZZ
DEC DL
JNZ @@20
MOV DL,BUZZ_SPAN
MOV ESI,MSG_BUZZ_FIZZ
JMP @@20
@@10: DEC DL
JNZ @@15
MOV DL,BUZZ_SPAN
MOV ESI,MSG_BUZZ
JMP @@20
@@15: CALL &PUT_HEX_U64
MOV ESI,MSG_NEW_LINE
@@20: CALL &PUT_STR
INC RAX
CMP RAX,RANGE_END //Actually only a 32-bit instruction.
JBE @@05
POP ESI
LEAVE
RET
}
Call(_BUZZ_FIZZ);
+23
View File
@@ -0,0 +1,23 @@
asm {
DIV_U64_BY_U64::
ENTER 0
XOR RDX,RDX
MOV RCX,64
MOV RAX,SF_ARG1[RBP]
MOV RBX,SF_ARG2[RBP]
@@1: SHL1 RAX
RCL1 RDX
CMP RBX,RDX
JA @@2
INC RAX
SUB RDX,RBX
@@2: LOOP @@1
LEAVE
RET1 16
}
_extern DIV_U64_BY_U64 I64 DivU64ByU64(I64 dividend,I64 divisor);
"123456/10=%d\n",DivU64ByU64(123456,10);
+70
View File
@@ -0,0 +1,70 @@
/*
When I was a kid with a Commodore 64,
the 6502 chip had no multiply instruction
and this is how we had to do it, except,
I used more regs in this example.
*/
asm {
//Opcodes are slightly different to make writing my x86_64 assembler easier.
//See $LK,"::/SparrowOS/Compiler/OpCodes.TXT.Z","FI:::/SparrowOS/Compiler/OpCodes.TXT.Z"$.
USE64 //Defaults to 64, but this is a good habit.
//You can clobber RAX,RBX,RCX,RDX,R8,R9
MUL_BY_HAND_U8_U8_TO_U16: //This is only for fun.
//8bit * 8bit-->16bit
//AL*BL-->AX
MOV CL,8
SHL AX,8
@@05: SHL1 AX
JNC @@10
ADD AL,BL
@@10: DEC CL
JNZ @@05
RET
_MUL_BY_HAND_U8_U8_TO_U16:: //C callable
ENTER 0
MOV AL,U8 SF_ARG1[RBP] //$LK,"SF_ARG1","FF:::/SparrowOS/OSMain/Adam1a.HPP.Z,SF_ARG1"$
MOV BL,U8 SF_ARG2[RBP]
CALL MUL_BY_HAND_U8_U8_TO_U16
MOVZX RAX,AX
LEAVE
RET1 16
_MUL_U64_U64_TO_U128::
//64bit * 64bit-->128bit
ENTER 0
MOV RBX,U64 SF_ARG3[RBP]
MOV RAX,U64 SF_ARG1[RBP] //$LK,"SF_ARG1","FF:::/SparrowOS/OSMain/Adam1a.HPP.Z,SF_ARG1"$
MUL U64 SF_ARG2[RBP] //Result RDX:RAX 128bit
MOV U64 [RBX],RAX
MOV U64 8[RBX],RDX
LEAVE
RET1 24
};
//My convention is to put an underscore
//on C callable routines.
_extern _MUL_BY_HAND_U8_U8_TO_U16 U16 MulU8(U8 n1,U8 n2);
class U128
{
U64 lo,hi;
};
_extern _MUL_U64_U64_TO_U128 U0 MulU64(I64 n1,I64 n2,U128 *_prod);
U0 MulByHand()
{
U128 p;
"2*7 =0x%X\n",MulU8(2,7);
"100*10=0x%X\n",MulU8(100,10);
MulU64(0x0123456789ABCDEF,0x1000001,&p);
"0x0123466789ABCDEF*0x1000001=0x%016X%016X\n",p.hi,p.lo;
}
MulByHand;
+43
View File
@@ -0,0 +1,43 @@
asm {
PUT_DEC_U64::
//RAX is number to print in decimal.
//Preserves all regs.
ENTER 24 //24 char buffer on stack
PUSH_C_REGS //$LK,"Save regs","FF:::/SparrowOS/OSMain/Adam1a.HPP.Z,PUSH_C_REGS"$ which C code is free to clobber
PUSH RSI //This reg is not saved.
LEA RSI,-1[RBP] //Load addr of end of buffer.
//We picked RSI because of PUT_STR
//We'll move backward
MOV U8 [RSI],0 //Terminator
OR RAX,RAX //Special case -- zero
JNZ @@05
DEC RSI
MOV U8 [RSI],'0'
JMP @@15
@@05: MOV RBX,10 //Divides by 10
@@10: XOR RDX,RDX
DIV RBX //RAX=(RDX:RAX)/RBX with remainder in RDX
ADD RDX,'0'
DEC RSI
MOV U8 [RSI],DL
OR RAX,RAX
JNZ @@10
@@15: CALL &PUT_STR
POP RSI
POP_C_REGS
LEAVE
RET
_DO_IT:: //The convention is C-callable routines have underscores.
MOV RAX,122333221
CALL PUT_DEC_U64
PUSH 1
CALL &NewLine //Print one carriage return
RET
}
Call(_DO_IT);