flat assembler
Message board for the users of flat assembler.

 Index > High Level Languages > Sphinx C-- Goto page 1, 2  Next
Author
me239

Joined: 06 Jan 2011
Posts: 200
me239
Hey guys! I just recently came across an old program I downloaded a while back called Sphinx C--. C-- is basically C++, but you program the libraries yourself via assembly. Sphinx C-- can program Win32, DOS16, DOS32, FLAT, TEXE, EXE16, SYS, ROM, and a few more including for MenuetOS! Here is a sample program I wrote for my xOS, it's library, and a link to the website.
OFFICIAL PAGE: http://www.cs.utexas.edu/users/tbone/c--/
Code:
?resize FALSE
?include "XOS.H--"
int A, B;
byte Q;
main()
{
PRINTF("Press 'F' for Fahrenheit to Celsius or 'C' for Celsius to Fahrenheit? ");
GETKEY(Q);
Q = AL;
if (Q == 'C')
{
PRINTF("\nTemperature in Celsius? ");
getnum(A);
A = AX;
B = A;
B = B/5*4;
A = A + B+32;
PRINTF("Temperature in Fahrenheit = ");
PRINTNUM(A);
TERMINATE();
}
if (Q == 'F')
{
PRINTF("\nTemperature in Fahrenheit? ");
getnum(A);
A = AX;
A = A-32;
A = A/9*5;
PRINTF("Temperature in Celsius = ");
PRINTNUM(A);
TERMINATE();
}
else
{
TERMINATE();
}
}

And it's library xOS.H--
Code:
: void TERMINATE ()
{
\$INT 0x20
}

: void PRINTF ()
{
SI = AX;
AH = 0x03;
\$ INT 0x21
}

: void PRINTNUM ()
{
SI = AX;
CX = 0;
DOWN1:
BX = 0x0a;
DX = 0;
\$ DIV BX
\$ PUSH DX
\$ INC CX
\$ CMP AX, 0
\$ JNZ DOWN1
DOWN2:
\$ POP DX
AL = DL;
AH = 0x0e;
\$ INT 0x10
\$ DEC CX
\$ JNZ DOWN2
}

: void MSGBOX ()
{
SI = AX;
AH = 01;
\$ INT 0x21
}

: word getstring (word buffer; byte num)
{
CL = num;
DI = buffer;
AH = 0x0C;
\$ INT 0x21
}

: word getnum (int varnum)
{
AH = 0X0D;
\$ INT 0X21
}

: void GETKEY ()
{
\$ XOR AX, AX
\$ INT 0x16
\$ CMP AL, 0x61
\$ JB OK
\$ SUB AL, 0x20
OK:
}

[url][/url][url][/url]
20 Jul 2011, 03:23
typedef

Joined: 25 Jul 2010
Posts: 2908
Location: 0x77760000
typedef
so what language is this ?

PS: [url][/url][url][/url]
20 Jul 2011, 06:04
bitshifter

Joined: 04 Dec 2007
Posts: 786
Location: Massachusetts, USA
bitshifter
typedef wrote:
so what language is this ?

english

_________________
Coding a 3D game engine with fasm is like trying to eat an elephant,
you just have to keep focused and take it one 'byte' at a time.
20 Jul 2011, 07:09
f0dder

Joined: 19 Feb 2004
Posts: 3170
Location: Denmark
f0dder
I really don't see the point.

If you want really low-level programming, go for C and/or assembly - probably C with external assembly modules written using a proper assembler, instead of half-arsed inline assembly.

For high(er) level languages, I expect type safety and a decent optimizing compiler...

C-- looks like a bastard child that doesn't exactly encourage good programming practices.
20 Jul 2011, 18:14
me239

Joined: 06 Jan 2011
Posts: 200
me239
f0dder wrote:
I really don't see the point.

If you want really low-level programming, go for C and/or assembly - probably C with external assembly modules written using a proper assembler, instead of half-arsed inline assembly.

For high(er) level languages, I expect type safety and a decent optimizing compiler...

C-- looks like a bastard child that doesn't exactly encourage good programming practices.
It's almost the same as using a Macro in FASM and also I see it more fit for those who want to have a higher level for their own OS, but have little knowledge on creating C libraries. Also another reason I started using it is because it can generate 8086, 80286, and 80386 code unlike most compilers these days.
21 Jul 2011, 02:56
vid
Verbosity in development

Joined: 05 Sep 2003
Posts: 7105
Location: Slovakia
vid
me239: 80386 code "unlike most compilers these days"?

What do you need 8086 and 80286 code for?
21 Jul 2011, 07:21
garystampa

Joined: 25 May 2011
Posts: 52
Location: Central FLorida
garystampa
vid wrote:
me239: 80386 code "unlike most compilers these days"?

What do you need 8086 and 80286 code for?

There are some of us who use 8086 (80186) as microcontrollers. Some of us run DOS on embedded systems using 8086/80186 architectures. The world isn't just the PC...
21 Jul 2011, 10:09
revolution
When all else fails, read the source

Joined: 24 Aug 2004
Posts: 18070
revolution
garystampa wrote:
There are some of us who use 8086 (80186) as microcontrollers. Some of us run DOS on embedded systems using 8086/80186 architectures. The world isn't just the PC...
Yay. Someone else that works in the embedded world.
21 Jul 2011, 10:23
f0dder

Joined: 19 Feb 2004
Posts: 3170
Location: Denmark
f0dder
me239 wrote:
It's almost the same as using a Macro in FASM and also I see it more fit for those who want to have a higher level for their own OS, but have little knowledge on creating C libraries.
If you don't know how to manage libraries, and can't be arsed to learn how to do that, perhaps you shouldn't be dealing with osdev. If you want low-level, go with "real" assembly and C. If you want higher-level, add a proper HLL to the mix (C++ works very well, even for kernel code, as long as you know what you're doing and stick to a subset of features). I still really don't see the point of C--.

me239 wrote:
Also another reason I started using it is because it can generate 8086, 80286, and 80386 code unlike most compilers these days.
If you're stuck with such old CPUs, I see even less reason to use C--. I know of no compilers targetting those CPUs that produce decent code, so you're much better off staying mostly in asm-land. Even on 486 CPUs, we needed sizeable chunks of assembly code when using the Pascal, C and C++ compilers available back then... the smart people used external assembly modules instead of ending up with massive and unmaintainable inline-assembly hackjobs.
21 Jul 2011, 16:09
me239

Joined: 06 Jan 2011
Posts: 200
me239
f0dder wrote:
I know of no compilers targetting those CPUs that produce decent code
Borland C++
22 Jul 2011, 03:03
f0dder

Joined: 19 Feb 2004
Posts: 3170
Location: Denmark
f0dder
me239 wrote:
f0dder wrote:
I know of no compilers targetting those CPUs that produce decent code
Borland C++
Kidding, right?

_________________
- carpe noctem
22 Jul 2011, 08:36
garystampa

Joined: 25 May 2011
Posts: 52
Location: Central FLorida
garystampa
f0dder wrote:
I know of no compilers targetting those CPUs that produce decent code
Therefore they don't exist?
22 Jul 2011, 11:29
emil

Joined: 16 Dec 2003
Posts: 76
Location: egypt
emil
Hi all

I think that , Sphinx C-- is a very powerful and easy tool , that you can mix
low level coding "asm" with high level coding "c & c++"

here is a simple demo that will show you the point of using Sphinx C--
Code:

/***************************************
*             Sphinx C--               *
*                                      *
*     strlen demo  By Emil Halim       *
*           23 / 9 / 2011              *
***************************************/

#pragma option w32         //create Windows GUI EXE.
#pragma option OBJ         //create OBJ file
#pragma option OS          //speed optimization
#pragma option J0          //no startup code.

#include <Windows.h>

#pragma option ia          // allow inline asm
#pragma option LST

extern cdecl _printf();
#define printf  _printf

extern cdecl _strlen();
#define strlen _strlen

int strlen1(char* pStr)
{
EAX=0;
while(byte *pStr !=0 )
{
pStr++;
EAX++;
}
}

int fastcall strlen2(EAX)
{
EBX=EAX;
while(DSBYTE[EAX] !=0 )
{
EAX++;
}
EAX -= EBX;
}

int fastcall strlen3(EAX)      // pure ASM code
{
MOV EBX,EAX
@lop:
CMP DSBYTE[EAX],0
JE  fin
INC EAX
JMP lop
@fin:
SUB EAX,EBX
}

// *** SSE2 version  from MASM forum***
? aligncode 16
int  fastcall strlen4(EAX)
{
EBX = EAX ;                 // get the string pointer
LEA ECX, DSDWORD[EAX+16]        // save pointer to string, on par with eax after first loop
EAX &= 0xFFFFFFF0;          // align for use with SSE2
@shiftOK:
XORPS XMM0, XMM0                // zero xmm0 for finding zero bytes
@a1:
PCMPEQB XMM0, DSQWORD[EAX]      // ---- inner loop -----
PMOVMSKB EDX, XMM0              // set byte mask in edx
EAX += 16;                      // len counter (best position here)
TEST EDX,EDX
JE a1
if(ECX<=EAX) goto a2;
ECX -= EAX;                 // get difference, and cancel "misalign flag"
SHR EDX, CL                 // shift invalid
SHL EDX, CL                     // bits out
JE shiftOK
@a2:
BSF EDX, EDX                // bit scan for the index
SUB EAX, EBX                    // subtract original src pointer
LEA EAX, DSDWORD[EAX+EDX-16]    // add scan index
}
? aligncode 4

char* testStr = "SPHINX C-- is so easy (an intermediate position between Assembler and C)";

main()
{
_start:
printf("string length is %d\n",strlen( testStr ));
printf("string length is %d\n",strlen1( testStr ));
printf("string length is %d\n",strlen2( testStr ));
printf("string length is %d\n",strlen3( testStr ));
printf("string length is %d\n",strlen4( testStr ));
MessageBox(0,"","",0);
}

23 Sep 2011, 14:32
emil

Joined: 16 Dec 2003
Posts: 76
Location: egypt
emil
here is an other demo strcmp function

Code:

/***************************************
*             Sphinx C--               *
*                                      *
*     strcmp demo  By Emil Halim       *
*           23 / 9 / 2011              *
***************************************/

#pragma option w32         //create Windows GUI EXE.
#pragma option OBJ         //create OBJ file
#pragma option OS          //speed optimization
#pragma option J0          //no startup code.

#include <Windows.h>

#pragma option ia          // allow inline asm
#pragma option LST

extern cdecl _printf();
#define printf  _printf

/***************
s > t >>>  > 0
s = t >>>  = 0
s < t >>>  < 0
***************/
int strcmp1(char *s, char *t)           // pure C code
{
for( ;byte *s == byte *t; s++, t++)
if (byte *s == '\0') return 0;
return  DSBYTE[s] - DSBYTE[t];
}

int fastcall strcmp2(ESI,EDI)          // mixed asm & C
{
AL = DSBYTE[ESI];
while(AL==DSBYTE[EDI])
{
if(AL==0) return 0;
ESI++;  EDI++;
AL = DSBYTE[ESI];
}
return  DSBYTE[ESI] - DSBYTE[EDI];
}

char* testStr1 = "SPHINX C-- is so easy (an intermediate position between Assembler and C)";
char* testStr2 = "SPHINX C-- is so easy (an intermediate position between Assembler and C)";

main()
{
_start:
printf("string length is %d\n",strcmp1( testStr1 , testStr2 ));
printf("string length is %d\n",strcmp2( testStr1 , testStr2 ));
MessageBox(0,"","",0);
}

23 Sep 2011, 15:22
sleepsleep

Joined: 05 Oct 2006
Posts: 9550
Location: ˛　　　　　　　　　　　　　　　　　　　　　　　　　　　　　⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣Posts: 334455
sleepsleep
cool...
23 Sep 2011, 15:27
emil

Joined: 16 Dec 2003
Posts: 76
Location: egypt
emil
thanks sleepsleep.

here is another one strcpy in 3 versions

Code:

/***************************************
*             Sphinx C--               *
*                                      *
*     strcpy demo  By Emil Halim       *
*           23 / 9 / 2011              *
***************************************/

#pragma option w32         //create Windows GUI EXE.
#pragma option OBJ         //create OBJ file
#pragma option OS          //speed optimization
#pragma option J0          //no startup code.

#include <Windows.h>

#pragma option ia          // allow inline asm
#pragma option LST

extern cdecl _printf();
#define printf  _printf

// C version
dword strcpy1(char* dst, char* src)  // use dword instead of char* in return value for correct calling
{
char* rt = dst;
while (byte *src != 0)
{
*dst = *src;
dst++; src++;
}
return rt;
}

// asm version
dword fastcall strcpy2( EDI, ESI)
{
MOV EDX, EDI
MOV EDI, ESI
XOR ECX, ECX
XOR EAX, EAX
DEC ECX
REPNE SCASB
XOR ECX, 0FFFFFFFFH
MOV EDI, EDX
MOV EDX, ECX
MOV EAX, EDI
SHR ECX, 2
REP MOVSD
MOV ECX, EDX
AND ECX, 3
REP MOVSB
}

// asm & C
dword fastcall strcpy3(EDI, ESI)  // use dword instead of char* in return value for correct calling
{
EAX = EDI;
AL = DSBYTE[ESI];
while ( AL != 0)
{
DSBYTE[EDI] = AL;
ESI++; EDI++;
AL = DSBYTE[ESI];
}
return EAX;
}

char* testStr1 = "SPHINX C-- is so easy (an intermediate position between Assembler and C)";
char* testStr2 = "                                                                        ";

main()
{
_start:
strcpy1( testStr2 , testStr1 );
printf("string2 is %s\n", testStr2 );
strcpy2( testStr2 , testStr1 );
printf("string2 is %s\n", testStr2 );
strcpy3( testStr2 , testStr1 );
printf("string2 is %s\n", testStr2 );
MessageBox(0,"","",0);
}

23 Sep 2011, 16:28
emil

Joined: 16 Dec 2003
Posts: 76
Location: egypt
emil
one more demo , strcat

note how the demo uses DB to allocate space for the test

Code:

/***************************************
*             Sphinx C--               *
*                                      *
*     strcat demo  By Emil Halim       *
*           23 / 9 / 2011              *
***************************************/

#pragma option w32         //create Windows GUI EXE.
#pragma option OBJ         //create OBJ file
#pragma option OS          //speed optimization
#pragma option J0          //no startup code.

#include <Windows.h>

#pragma option ia          // allow inline asm
#pragma option LST

extern cdecl _printf();
#define printf  _printf

// C version
dword strcat1( char* dst, char* src )
{
char* d = dst;
while (byte *d != 0) d++;
while (byte *src != '\0')
{
*d = *src;
d++; src++;
}
return dst;
}

char* testStr1 = "SPHINX C-- is so easy ";
char* testStr2 = "(an intermediate position between Assembler and C)";
testStr3: DB 255 dup 0   // space for strcat test must be greater than the sum of testStr1 & testStr2

main()
{
_start:
strcat1( testStr3 , testStr1 );
strcat1( testStr3 , testStr2 );
printf("%s\n", testStr3 );
MessageBox(0,"","",0);
}

23 Sep 2011, 17:08
emil

Joined: 16 Dec 2003
Posts: 76
Location: egypt
emil
and strstr function

Code:

/***************************************
*             Sphinx C--               *
*                                      *
*     strstr demo  By Emil Halim       *
*           23 / 9 / 2011              *
***************************************/

#pragma option w32         //create Windows GUI EXE.
#pragma option OBJ         //create OBJ file
#pragma option OS          //speed optimization
#pragma option J0          //no startup code.

#include <Windows.h>

#pragma option ia          // allow inline asm
#pragma option LST

extern cdecl _printf();
#define printf  _printf

// C version
dword  strstr1( char * str1, char * str2 )
{
char* s1, *s2;
char* cp =  str1;
if ( !*str2 )
return str1;

while ( *cp != 0)
{
s1 = cp;
s2 = str2;
while ( *s1 ) && ( *s2  ) && (! *s1 - *s2)
{
s1++; s2++;
}
if (!*s2)
return cp;
cp++;
}
return 0;
}

char* testStr1 = "SPHINX C-- is so easy (an intermediate position between Assembler and C)";
char* testStr2 = "Assembler";

main()
{
_start:
printf("%s\n", strstr1( testStr1 , testStr2 ) );
MessageBox(0,"","",0);
}

23 Sep 2011, 18:07
f0dder

Joined: 19 Feb 2004
Posts: 3170
Location: Denmark
f0dder
emil wrote:
here is a simple demo that will show you the point of using Sphinx C--
...writing unportable & less readable source that generates slower code than any C++ compiler the last many years will produce from pure C++ code?

Yeah, sounds like a great thing to do.

_________________
- carpe noctem
23 Sep 2011, 18:31
emil

Joined: 16 Dec 2003
Posts: 76
Location: egypt
emil
the point here is not the fast code to be generated by Sphinx C--,

BUT ...........

which is more readable , using invoke macro or calling a function like a high level language do ?

which is more readable , declaring variables with (DB , DW , and DD) or with a high level language do ?

which is more readable , using arguments inside the procedure with EBP indexed or with the normal name

which is more readable , this expretion mov EAX,5 or EAX = 5; ?

.............
................ etc.

Sphinx C-- allows you to mix asm low level code with C high level code , so you can make a code that
is more faster easlly by using directlly SSE instructions.

any way this is my own opinion.
23 Sep 2011, 19:44
 Display posts from previous: All Posts1 Day7 Days2 Weeks1 Month3 Months6 Months1 Year Oldest FirstNewest First

 Jump to: Select a forum Official----------------AssemblyPeripheria General----------------MainTutorials and ExamplesDOSWindowsLinuxUnixMenuetOS Specific----------------MacroinstructionsOS ConstructionIDE DevelopmentProjects and IdeasNon-x86 architecturesHigh Level LanguagesProgramming Language DesignCompiler Internals Other----------------FeedbackHeapTest Area
Goto page 1, 2  Next

Forum Rules:
 You cannot post new topics in this forumYou cannot reply to topics in this forumYou cannot edit your posts in this forumYou cannot delete your posts in this forumYou cannot vote in polls in this forumYou cannot attach files in this forumYou can download files in this forum