News:

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

Main Menu

BCX+MSVC10+High level asm

Started by Emil_halim, July 20, 2012, 05:52:29 PM

Previous topic - Next topic

Emil_halim

 implemented .for - .endfor for using in HighLevelAsm.

Example
======


/********************************

  High level asm Example No 17
 
        By Emil Halim
 
          5-3-2013
*********************************/


$CPP
$NOMAIN
$NOWIN

#include "Windows.h"


FUNCTION main()

     !char* MyStr = "welcome....\n";
     raw ndx
     $AsmX
          ' using variable as a loop counter                   
          .for ndx = 5 to 10 
             cinvoke printf,MyStr
          .endfor
           
          ' using ecx rigester as a loop counter
          ' note well Printf will change ecx , so save it   
          .for ecx = 4 to 10 step 2
             push ecx                   
             cinvoke printf,MyStr     
             pop  ecx                 
          .endfor   
         
          ' using space memory in the stack as a loop counter
          ' do nt forget to blanced the stack when loop finished
          esp -= 4                 // allocate local variable
          .for [esp] = 5 to 10     // loop fro 5 to 10
             cinvoke printf,MyStr 
          .endfor                  //end the loop here
          esp += 4                 // de aloacate the local variable 
     $AsmX   
   
     Pause
END FUNCTION


Emil_halim

implemented masm label "@@:" with High Level Asm

here is example that has 2 procedures , both of them will convert string to value.

in the first one Ebx must hold string address , Ecx must hold end address of string

in the second one you call it by normal way.

the 2 procedures i got them from MASM forum and modified it.


/********************************

  High level asm Example No 18
 
        By Emil Halim
 
          6-3-2013
*********************************/


$CPP
$NOMAIN
$NOWIN

#include "Windows.h"

FastProc atod_proc
$AsmX
     xor edx, edx           
   @@:
     movzx eax, byte ptr [ebx]   
     lea edx, [edx+4*edx]       
     lea edx, [2*edx+eax-48]     
     inc ebx
     cmp ecx, ebx       
     jne @B
     xchg eax, edx
     ret
$AsmX   
End FastProc

FastProc atod_proc_2
  $AsmX
         edx = [esp+4]              ; get first para in edx
         xor eax, eax               ; clear eax

   @@:   movzx ecx, byte ptr [edx]  ; move 1 byte into ecx
         lea eax, [eax+4*eax]       ; eax -> 5 * eax
         lea eax, [2*eax+ecx-30h]   ; eax -> 2 * eax + (ecx-30h)
         edx++                      ; inc edx
         cmp byte ptr [edx], 0      ; if end of string then return
         jne @b
         ret
  $AsmX   
End FastProc

FUNCTION main()
   
     !const char MyStr[] = "2000";
     raw ln ,value
     !ln = strlen( MyStr );
     
     ^ ebx = &MyStr        ; get str address into ebx         
     ^ ecx = ebx           ; copy ebx to ecx
     ^ ecx += ln           ; get length of str into ecx
     ^ call atod_proc      ; call the function
     ^ value =  eax        ; store result in value variable
     print  value     
     
     print  atod_proc_2(MyStr$)   
     Pause
END FUNCTION     

Emil_halim

also implemented Uses keyword that will save some registers in front of procedure and will be restored automatically when ret from procedure

Example
======


/********************************

  High level asm Example No 19
 
        By Emil Halim
 
          7-3-2013
*********************************/


$CPP
$NOMAIN
$NOWIN

#include "Windows.h"

FastProc Test
$AsmX
     'we want to save 2 registers esi and edi
     uses esi edi
     esi = [esp+4+8]  ;get first arg into esi
     edi = [esp+8+8]  ;get second arg into edi
     eax = esi        ; copy esi to eax
     eax += edi       ; add eax and edi
     ' here HiegLivelAsm will restore the saved registers 
     ret
$AsmX   
End FastProc

FUNCTION main()
       
     print  Test(10,20)   
     Pause
END FUNCTION


Emil_halim


if someone interesting,

here is the source code of bcx 6.9.9.1 & HighLevelAsm source code.

www.spoilerspace.com/bcxdx/cHLasm/BCX6_9_9_1&HighLevelAsm_1.zip

you must have MSVC10 to create BC translator with HighLevelAsm.

Feel free to improve and enhanced it.

thanks.

dedndave

if it helps, you can temporarily disable the assembler prologue and epilogue
        OPTION  PROLOGUE:None
        OPTION  EPILOGUE:None

OpnFile PROC    lpFileName:LPSTR,dwOpenFlags:DWORD

;code
        ret     8   ;you must pop the parameters off the stack

OpnFile ENDP

        OPTION  PROLOGUE:PrologueDef
        OPTION  EPILOGUE:EpilogueDef

Emil_halim

thanks you.

actually  , we can make a function without frame stack by many way in bcx & HighLevelAsm & VC10

Example
======

; FastProc Example
FastProc  GetVal
      ^ Eax = 100
      ^ ret
End FastProc


this will translated to that

__declspec(naked) int GetVal(...)
{
  __asm{ 
           Mov Eax , 100
           ret
   }
}


second method is like that

!#define FastProc __declspec(naked)
!FastProc int add1(int a, int b)
!{
     ^ eax  = a
     ^ eax += b
     ^ ret
!}


and yet another method that specific for VC10

!#pragma optimize( "y", on )
function add(a as int,b as int)
   ^ eax  = a
   ^ eax += b
   ^ ret
end function
!#pragma optimize( "y", off )