HI all;
This demo will show you how to code in c-- close to c style.
it collects some famous c-like functions , you can make a comparison between actual c functions and those.
code of the demo
===============
/*************************************
* Sphinx C-- *
* *
* c style functions *
* *
* By Emil Halim *
* *
*************************************/
#pragma option w32c //create Windows console EXE.
#pragma option OS //speed optimization
#jumptomain NONE //just jump to main function
#includepath "D:\Ext_c--\winlib"
#include <windows.h>
#include <MSVCRT.H-->
#pragma option ia //allow inserte asm instructions
#pragma option LST
// declaring some data as masm to test
src1: db "1", 0
src2: db "12", 0
src3: db "123", 0
src4: db "1234", 0
src5: db "12345", 0
src6: db "123456", 0
src7: db "1234567", 0
src8: db "12345678", 0
src9: db "123456789", 0
src10: db "1234567890", 0
src11: db "12345678901", 0
src12: db "123456789012", 0
src13: db "1234567890123", 0
src14: db "12345678901234", 0
src15: db "123456789012345", 0
src16: db "1234567890123456", 0
src17: db "12345678901234567", 0
src18: db "123456789012345678", 0
src19: db "1234567890123456789", 0
src20: db "12345678901234567890", 0
/************************************************
*
* some c style functions
*
*************************************************/
int strLen(char* pStr)
{
char* pStrt;
pStrt = pStr;
while(byte *pStr !=0 ) pStr++;
return pStr - pStrt;
}
int strCmp(char* s, char* t)
{
for( ;byte *s == byte *t; s++, t++)
if (byte *s == '\0') return 0;
return *s - *t;
}
dword strCpy(char* dst, char* src)
{
char* rt = dst;
while (byte *src != 0)
{
*dst = *src;
dst++; src++;
}
return rt;
}
dword strCat( char* dst, char* src )
{
char* d = dst;
while (byte *d != 0) d++;
while (byte *src != '\0')
{
*d = *src;
d++; src++;
}
return dst;
}
dword strStr( 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;
}
dword memCpy(char* dst, char* src, dword count)
{
char* rt = dst;
while( count-- )
{
*dst = *src;
dst++; src++;
}
return rt;
}
dword memSet(char* src, dword chr, dword count)
{
char* rt = src;
while( count-- )
{
*src = chr;
src++;
}
return rt;
}
//
// testing stuff
//
void main()
{
char MyStr[1024];
// test StrLen function
printf("%d\n", strLen( src5 ) );
// test StrCmp function
printf("%d\n", strCmp( src10 , src5 ) );
// test MemCpy function
printf("%s\n", memCpy( #MyStr, src5, strLen(src5)) );
// test MemSet function
printf("%s\n", memSet( #MyStr, 65 , strLen(src5)) );
MessageBox(0,"","",0);
}
enjoy coding with c-- & Masm & Basic.