News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests
NB: Posting URL's See here: Posted URL Change

Main Menu

masm with spinx c--

Started by Emil_halim, October 15, 2013, 11:35:23 PM

Previous topic - Next topic

Emil_halim

Hi all

sorry if this is a wrong place to post this topic.

because i liked sphinx c-- so much , so i have decided to extend it's features.

i have no source code of c-- , then i made a sphinx c-- translator then let c-- do the rest of work.

first i am working in masm translator to c-- , so you can insert an masm code directly with c-- code with out
or with a little  changes.

here is a working example , it has 3 masm porcs, taken from masm examples and  masm library.



/***************************************
*           Sphinx C--                 * 
*                                      *
*      demo  By Emil Halim             *
*                                      *
***************************************/


#pragma option w32c       //create Windows console EXE.
#pragma option OS         //speed optimization


#includepath "D:\Ext_c--\winlib" 

#include <windows.h> 
#include <MSVCRT.H-->
 
#pragma option ia


//#pragma option LST
#define NULL 0

dword app_path( dword buffer)
{
^^
   ; ---------------------------------------------------------
   ; call this procedure with the address of a 260 byte buffer
   ; return the path with a trailing "\" at address "buffer"
   ; ---------------------------------------------------------

   invoke GetModuleFileName,0,buffer,260

    mov ecx, buffer
    add ecx, eax            ; add length
    add ecx, 1

  @@:                       ; scan backwards to find last "\"
    sub ecx, 1
    cmp BYTE PTR [ecx], "\"
    je @F
    cmp ecx, buffer
    jge @B

  @@:
    mov BYTE PTR [ecx+1], 0 ; truncate string after last
^^   
}

dword app_name(dword buffer)
{
^^
  ; ---------------------------------------------------------
  ; call this procedure with the address of a 260 byte buffer
  ; returns the file name at address "buffer"
  ; ---------------------------------------------------------
    push ebx

    invoke GetModuleFileName,NULL,buffer,260

    mov ecx, buffer
    add ecx, eax                    ; add length
    add ecx, 1

  @@:                               ; scan backwords to find last "\"
    sub ecx, 1
    cmp BYTE PTR [ecx], "\"
    je @F
    cmp ecx, buffer
    jge @B

  @@:
    add ecx, 1
    mov eax, buffer
    or ebx, -1

  @@:                               ; overwrite buffer with file name
    add ebx, 1
    movzx edx, BYTE PTR [ecx+ebx]
    mov [eax+ebx], dl
    test dl, dl
    jnz @B

    pop ebx
  ^^ 
}

#pragma option LST

^^
; #########################################################################

GetCL proc ArgNum:DWORD, ItemBuffer:DWORD

  ; -------------------------------------------------
  ; arguments returned in "ItemBuffer"
  ;
  ; arg 0 = program name
  ; arg 1 = 1st arg
  ; arg 2 = 2nd arg etc....
  ; -------------------------------------------------
  ; Return values in eax
  ;
  ; 1 = successful operation
  ; 2 = no argument exists at specified arg number
  ; 3 = non matching quotation marks
  ; 4 = empty quotation marks
  ; -------------------------------------------------

    LOCAL lpCmdLine      :DWORD
    LOCAL cmdBuffer[192] :BYTE
    LOCAL tmpBuffer[192] :BYTE

    push esi
    push edi

    invoke GetCommandLine
    mov lpCmdLine, eax        ; address command line

  ; -------------------------------------------------
  ; count quotation marks to see if pairs are matched
  ; -------------------------------------------------
    xor ecx, ecx            ; zero ecx & use as counter
    mov esi, lpCmdLine
   
    @@:
      lodsb
      cmp al, 0
      je @F
      cmp al, 34            ; [ " ] character
      jne @B
      inc ecx               ; increment counter
      jmp @B
    @@:

    push ecx                ; save count

    shr ecx, 1              ; integer divide ecx by 2
    shl ecx, 1              ; multiply ecx by 2 to get dividend

    pop eax                 ; put count in eax
    cmp eax, ecx            ; check if they are the same
    je @F
      pop edi
      pop esi
      mov eax, 3            ; return 3 in eax = non matching quotation marks
      ret
    @@:

  ; ------------------------
  ; replace tabs with spaces
  ; ------------------------
    mov esi, lpCmdLine
    lea edi, cmdBuffer

    @@:
      lodsb
      cmp al, 0
      je rtOut
      cmp al, 9     ; tab
      jne rtIn
      mov al, 32
    rtIn:
      stosb
      jmp @B
    rtOut:
      stosb         ; write last byte

  ; -----------------------------------------------------------
  ; substitute spaces in quoted text with replacement character
  ; -----------------------------------------------------------
    lea eax, cmdBuffer
    mov esi, eax
    mov edi, eax

    subSt:
      lodsb
      cmp al, 0
      jne @F
      jmp subOut
    @@:
      cmp al, 34
      jne subNxt
      stosb
      jmp subSl     ; goto subloop
    subNxt:
      stosb
      jmp subSt

    subSl:
      lodsb
      cmp al, 32    ; space
      jne @F
        mov al, 254 ; substitute character
      @@:
      cmp al, 34
      jne @F
        stosb
        jmp subSt
      @@:
      stosb
      jmp subSl

    subOut:
      stosb         ; write last byte

  ; ----------------------------------------------------
  ; the following code determines the correct arg number
  ; and writes the arg into the destination buffer
  ; ----------------------------------------------------
    lea eax, cmdBuffer
    mov esi, eax
    lea edi, tmpBuffer

    mov ecx, 0          ; use ecx as counter

  ; ---------------------------
  ; strip leading spaces if any
  ; ---------------------------
    @@:
      lodsb
      cmp al, 32
      je @B

    l2St:
      cmp ecx, ArgNum     ; the number of the required cmdline arg
      je clSubLp2
      lodsb
      cmp al, 0
      je cl2Out
      cmp al, 32
      jne cl2Ovr           ; if not space

    @@:
      lodsb
      cmp al, 32          ; catch consecutive spaces
      je @B

      inc ecx             ; increment arg count
      cmp al, 0
      je cl2Out

    cl2Ovr:
      jmp l2St

    clSubLp2:
      stosb
    @@:
      lodsb
      cmp al, 32
      je cl2Out
      cmp al, 0
      je cl2Out
      stosb
      jmp @B

    cl2Out:
      mov al, 0
      stosb

  ; ------------------------------
  ; exit if arg number not reached
  ; ------------------------------
    .if ecx < ArgNum
      mov edi, ItemBuffer
      mov al, 0
      stosb
      mov eax, 2  ; return value of 2 means arg did not exist
      pop edi
      pop esi
      ret
    .endif

  ; -------------------------------------------------------------
  ; remove quotation marks and replace the substitution character
  ; -------------------------------------------------------------
    lea eax, tmpBuffer
    mov esi, eax
    mov edi, ItemBuffer

    rqStart:
      lodsb
      cmp al, 0
      je rqOut
      cmp al, 34    ; dont write [ " ] mark
      je rqStart
      cmp al, 254
      jne @F
      mov al, 32    ; substitute space
    @@:
      stosb
      jmp rqStart

  rqOut:
      stosb         ; write zero terminator

  ; ------------------
  ; handle empty quote
  ; ------------------
    mov esi, ItemBuffer
    lodsb
    cmp al, 0
    jne @F
    pop edi
    pop esi
    mov eax, 4  ; return value for empty quote
    ret
  @@:

    mov eax, 1  ; return value success

    pop edi
    pop esi

    ret

GetCL endp

; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
^^

main()
{
     char  buf[260];
     int k;
   
     app_path(  #buf ); 
     printf("%s\n",#buf);   
     
     app_name(  #buf ); 
     printf("%s\n",#buf);   

     MessageBox(0,"","",0); 
}



Not well , the term  ^^ means that path all the following lines to the translator untill it found an other ^^

also you can path only a line by putting ^ in start of the line.

sooner  i will post a working package of extended sphinx c--     

Gunther

Emil,

Quote from: Emil_halim on October 15, 2013, 11:35:23 PM
Hi all

sorry if this is a wrong place to post this topic.

because i liked sphinx c-- so much , so i have decided to extend it's features.

Why not? I don't know sphinx C. What is your goal?

Gunther
You have to know the facts before you can distort them.

Vortex

Sphinx C-- is a language designed to add C features to the assembly languages. You can consider it like the mixture of both of two. The compiler creates small executables.

Emil_halim


Gunther ,

Quote from: Gunther on October 16, 2013, 04:13:20 AM
  I don't know sphinx C.

as Mr Vortex has mentioned above , sphinx c-- is an intermediate position between Assembler and C.

here is a small example i have posted years ago

/***************************************
*             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.

#includepath "D:\Ext_c--\winlib"
#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); 
}



Quote from: Gunther on October 16, 2013, 04:13:20 AM
   What is your goal?

My goal is to revaival sphinx c-- , extend it's features , also to allow other to easy using it so that they do not have to reinvent the wheel  so if you have some masm code just enclosed it with ^^  and insert it with c--
code.

also i will extend it to accept some basic code too.

Vortex,

thank you for your clarification , found some old code in c-- that you giving a help to make c-- work with Masm.

Vortex

Hi Emil,

You are welcome. By the way, the Sphinx C-- site seems to be not available :

http://c--sphinx.narod.ru/indexe.htm


Vortex

Hi Emil,

Thanks. It works  :t

Emil_halim


nice , as i mentioned , i will post a full working folder that contains every thing to let you go.

note well there are many compiler there , i am using a ' C-- compiler (v0.239 b26 ) beta version.' one.   

avcaballero

Hello, I own a copy of original c-- as a gem  :biggrin:, also almost everything from Spnix c-- :t. Though I've never used it, I think it is very interesting. Glad to see that somebody wants to relaunch it  :t

I have had a look to your link, it seems that there isn't anything new yet, isn't it?

I have two suggestions:
* In my opinion, this site is a bit slow, why don't you move to any other faster site? http://dhost.info/ for example seems to be very interesting and free (I have not checked it)
* There are many resources scattered over there, why don't pack all of them?, especially considering that they haven't got a very large size.

Regards

Emil_halim

Hi avcaballero,

Quote from: avcaballero on October 16, 2013, 08:58:42 PM
I think it is very interesting. Glad to see that somebody wants to relaunch it  :t

very nice , so we can see more sphinx c-- van here .

Quote from: avcaballero on October 16, 2013, 08:58:42 PM
I have had a look to your link, it seems that there isn't anything new yet, isn't it?

I am afraid , the site i posted above is not mine , i have just found it by google.

Quote from: avcaballero on October 16, 2013, 08:58:42 PM
Hello, I own a copy of original c-- as a gem  :biggrin:, also almost everything from Spnix c-- :t.

note well there are some old one will not work with the new winlib , so i collected small of them ,and step by step adding more examples. as mentioned i will post a zip file that having a working stuff.   


Emil_halim

Hi all,

here is an other test that accepts c proc with VARARG.

/********************************************************************
*                       Sphinx C--                                  * 
*                                                                   *
*                 VarArg test from masm                             *
*                                                                   *
* topic : http://www.masmforum.com/board/index.php?topic=18885.0    *         
*                                                                   *
*********************************************************************/


#pragma option w32c       //create Windows console EXE.
#pragma option OS         //speed optimization


#includepath "D:\Ext_c--\winlib" 

#include <windows.h> 
#include <MSVCRT.H-->
 
#pragma option ia


#pragma option LST
#define NULL 0


^^
; #########################################################################
;
;  this Proc taken from old Masm see the link in the title
;  modifed by Emil to wrok with sphinx c--
;  c-- will not put PROLOGUE/EPILOGUE for (...) so that
;  i used esp to get return address 
; #########################################################################
testproc proc C args:VARARG
    xor eax, eax                ; assume zero args
    mov edx, [esp]              ; get return address
    cmp WORD PTR[edx], 0C483h   ; opcode for add esp,n
    je  @F
    ret
  @@:
    movzx eax, BYTE PTR[edx+2]  ; get value added to esp
    shr eax, 2                  ; divide by 4 for arg count
    ret     
testproc  endp
; #########################################################################
^^

void main()
{
         
     testproc();
     add esp,0;
     printf("%d\n",EAX);
     
     testproc(1);
     add esp,4;
     printf("%d\n",EAX);
     
     testproc(1,2);
     add esp,8;
     printf("%d\n",EAX);
     
     testproc(1,2,3);
     add esp,12;
     printf("%d\n",EAX);
     
     
     testproc(1,2,3,4);
     add esp,16;
     printf("%d\n",EAX);
     
     testproc(1,2,3,4,5);
     add esp,20;
     printf("%d\n",EAX);
     
     testproc(1,2,3,4,5,6);
     add esp,24;
     printf("%d\n\n",EAX);
       
     MessageBox(0,"","",0);
}



Emil_halim

#11
Hi all,

Here is an alpha version of Ext_c-- , just unzip it and put it in D: driver , the go ahead. 

if you put it in an other one then remember to adjust the path of c-- in cmpl bat file , also adjust winlib path in each example.


here is a masm strlen function and c-- strlen also

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

#pragma option w32c        //create Windows GUI EXE.
#pragma option OS         //speed optimization
#jumptomain NONE   


#includepath "c:\Ext_c--\winlib" 

#include <windows.h> 
#include <MSVCRT.H-->

#pragma option ia

#pragma option LST

char* mystr = "string4";


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;
}

? align 4
^^
strlen3 proc item:DWORD

    push    ebx

    mov     eax, item               ; get pointer to string
    lea     edx, [eax+3]            ; pointer+3 used in the end

@@:
    mov     ebx, [eax]              ; read first 4 bytes
    add     eax, 4                  ; increment pointer
    lea     ecx, [ebx-01010101h]    ; subtract 1 from each byte
    not     ebx                     ; invert all bytes
    and     ecx, ebx                ; and these two
    and     ecx, 80808080h
    jz      @B                      ; no zero bytes, continue loop

    test    ecx, 00008080h          ; test first two bytes
    jnz     @F

    shr     ecx, 16                 ; not in the first 2 bytes
    add     eax, 2

@@:
    shl     cl, 1                   ; use carry flag to avoid branch
    sbb     eax, edx                ; compute length

    pop     ebx

strlen3 endp
^^


main()

{
   
     printf("%d\n",strlen ("sphinx c-- & masm are good"));   
     printf("%d\n",strlen1("sphinx c-- & masm are good"));   
     printf("%d\n",strlen2("sphinx c-- & masm are good"));   
     printf("%d\n",strlen3("sphinx c-- & masm are good"));   
     
     MessageBox(0,"","",0);
     
}



enjoy coding with c-- & Masm.
 

habran

your folder is infected :icon13:
Cod-Father

Emil_halim

Oh... sorry.

thank you habran for this notification.

what anti virus should i use , any help pleas?

Vortex

Hi Emil,

Seriously, as a coder you should be aware of the damages caused by malwares. Please remove that wikiupload link.

Jotti's malware scan report :

http://virusscan.jotti.org/en/scanresult/8c0eded3abf8c1ccf9a0bbb62c37501b87052504