flat assembler
Message board for the users of flat assembler.
![]() Goto page 1, 2 Next |
Author |
|
me239 20 Jul 2011, 03:23
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--/ EXAMPLE PAGE: http://www.goosee.com/menuetos/ 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 $ ADD DL, 0x30 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: } |
|||
![]() |
|
typedef 20 Jul 2011, 06:04
so what language is this ?
PS: [url][/url][url][/url] |
|||
![]() |
|
f0dder 20 Jul 2011, 18:14
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. |
|||
![]() |
|
me239 21 Jul 2011, 02:56
f0dder wrote: I really don't see the point. |
|||
![]() |
|
vid 21 Jul 2011, 07:21
me239: 80386 code "unlike most compilers these days"?
What do you need 8086 and 80286 code for? |
|||
![]() |
|
garystampa 21 Jul 2011, 10:09
vid wrote: me239: 80386 code "unlike most compilers these days"? 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... |
|||
![]() |
|
revolution 21 Jul 2011, 10:23
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... ![]() |
|||
![]() |
|
f0dder 21 Jul 2011, 16:09
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. me239 wrote: Also another reason I started using it is because it can generate 8086, 80286, and 80386 code unlike most compilers these days. |
|||
![]() |
|
me239 22 Jul 2011, 03:03
f0dder wrote: I know of no compilers targetting those CPUs that produce decent code |
|||
![]() |
|
f0dder 22 Jul 2011, 08:36
me239 wrote:
![]() _________________ ![]() |
|||
![]() |
|
garystampa 22 Jul 2011, 11:29
f0dder wrote: I know of no compilers targetting those CPUs that produce decent code |
|||
![]() |
|
emil 23 Sep 2011, 14:32
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); } |
|||
![]() |
|
emil 23 Sep 2011, 15:22
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); } |
|||
![]() |
|
sleepsleep 23 Sep 2011, 15:27
cool...
|
|||
![]() |
|
emil 23 Sep 2011, 16:28
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); } |
|||
![]() |
|
emil 23 Sep 2011, 17:08
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); } |
|||
![]() |
|
emil 23 Sep 2011, 18:07
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); } |
|||
![]() |
|
f0dder 23 Sep 2011, 18:31
emil wrote: here is a simple demo that will show you the point of using Sphinx C-- Yeah, sounds like a great thing to do. _________________ ![]() |
|||
![]() |
|
emil 23 Sep 2011, 19:44
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. |
|||
![]() |
|
Goto page 1, 2 Next < Last Thread | Next Thread > |
Forum Rules:
|
Copyright © 1999-2025, Tomasz Grysztar. Also on GitHub, YouTube.
Website powered by rwasa.