Author Topic: Asmc source and binaries  (Read 31532 times)

nidud

  • Member
  • *****
  • Posts: 1800
    • https://github.com/nidud/asmc
Re: Asmc source and binaries
« Reply #195 on: September 06, 2019, 02:57:15 AM »
Added directive .ENUM

Added command line switch /Znk - Disable non Masm keywords.

Code: [Select]
x1 = 23.0
x2 = 0.271
x3 = 0.256
x4 = x2/x3
OXFACDc = x1 * x4
ConstantR8 OXFACD, OXFACDc
dont'work or it's not posible with /Zne option?

No, this is not possible with /Zne option. May I ask why you are using this option?

    x1 = 23.0
    x2 = 0.271
    x3 = 0.256
    x4 = x2 / x3
    x5 = x1 * x4

    x6 equ @CatStr(%x5)
    %echo x6
    end

24.34765625

HSE

  • Member
  • *****
  • Posts: 1148
  • <AMD>< 7-32>
Re: Asmc source and binaries
« Reply #196 on: September 06, 2019, 03:34:14 AM »
No, this is not possible with /Zne option.
Clear  :thumbsup:

May I ask why you are using this option?
 

Perfect now with /Znk  :thumbsup:

Thanks

nidud

  • Member
  • *****
  • Posts: 1800
    • https://github.com/nidud/asmc
Re: Asmc source and binaries
« Reply #197 on: September 06, 2019, 04:31:20 AM »
May I ask why you are using this option?
Perfect now with /Znk  :thumbsup:
:thumbsup:

Added a few changes to the .enum directive. This may be tweaked a bit more later but now this is possible (from parser.inc):

.enum special_token {
    T_NULL,
res macro tok, string, type, value, bytval, flags, cpu, sflags
    @CatStr(<T_>,@SubStr(<tok>,2)),
    endm
include special.inc
undef res
;; define tokens for SpecialTable (directives)
res macro token, string, value, bytval, flags, cpu, sflags
    @CatStr(<T_>,@SubStr(<token>,2)),
    endm
include directve.inc
undef res
    SPECIAL_LAST
}

nidud

  • Member
  • *****
  • Posts: 1800
    • https://github.com/nidud/asmc
Re: Asmc source and binaries
« Reply #198 on: September 13, 2019, 03:38:51 AM »
This is used in the parser, codegen, and expreval files now written in assembler.

 :thumbsup:

When macro.asm?  :biggrin: :biggrin:

I added macro.asm and expans.asm.

HSE

  • Member
  • *****
  • Posts: 1148
  • <AMD>< 7-32>
Re: Asmc source and binaries
« Reply #199 on: September 13, 2019, 07:37:09 AM »
Fantastic  :thumbsup:
Thanks.

nidud

  • Member
  • *****
  • Posts: 1800
    • https://github.com/nidud/asmc
Re: Asmc source and binaries
« Reply #200 on: September 17, 2019, 03:22:16 AM »
Added type to .enum

.enum name : byte {
    ok = 200,
    to_big = 300 ; error A2071: initializer too large for specified size
}

Memory operands now allows different size to be used. There is however no sign extension so all operations are (currently) unsigned.

foo proc b:byte, w:word, d:dword
    mov b,w
*    mov ax,w
*    mov b,al

    mov w,b
*    movzx eax,b
*    mov w,ax

    mov d,w
*    movzx eax,w
*    mov d,eax

foo endp

nidud

  • Member
  • *****
  • Posts: 1800
    • https://github.com/nidud/asmc
Re: Asmc source and binaries
« Reply #201 on: October 24, 2019, 11:12:19 PM »
Some new changes.

The enum directive do not need a line-break so now this works:

    .enum a { b, c, d }

The address operator [&] is extended to .return, .if, and memory operands.

    mov q,&p
    .if ecx == &p || q & &p
        nop
    .endif
    .return &struct.member

Code labels are somewhat portable. In 64-bit lea rax,address is used but ignored in 32-bit.

    xor eax,eax
    mov wc.cbSize,          WNDCLASSEX
    mov wc.style,           CS_HREDRAW or CS_VREDRAW or CS_BYTEALIGNCLIENT
    mov wc.cbClsExtra,      eax
    mov wc.cbWndExtra,      eax
    mov wc.hInstance,       rcx
    mov wc.hbrBackground,   COLOR_MENUTEXT
    mov wc.lpszMenuName,    rax
    mov wc.lpfnWndProc,     &WndProc
    mov wc.lpszClassName,   &@CStr("WndClass")
    mov wc.hIcon,           LoadIcon(0, 500)
    mov wc.hIconSm,         rax
    mov wc.hCursor,         LoadCursor(0, IDC_ARROW)

As for the compatibility issue with Masm a new command-line switch is added: /Znk - Disable non Masm keywords.

jj2007

  • Member
  • *****
  • Posts: 9801
  • Assembler is fun ;-)
    • MasmBasic
Re: Asmc source and binaries
« Reply #202 on: October 24, 2019, 11:33:10 PM »
/Znk - Disable non Masm keywords[/url].
What's the difference to /Zne?

nidud

  • Member
  • *****
  • Posts: 1800
    • https://github.com/nidud/asmc
Re: Asmc source and binaries
« Reply #203 on: October 25, 2019, 12:00:04 AM »
/Znk only targets the latest keywords changes.
/Zne targets all Asmc extensions including directives.

nidud

  • Member
  • *****
  • Posts: 1800
    • https://github.com/nidud/asmc
Re: Asmc source and binaries
« Reply #204 on: December 04, 2019, 04:43:24 AM »
Added @Cstr( n ) - get label of prev strings

@CStr( index ) returns current string label minus index.

    C equ @CStr

    lea rax, C("A") ; DS0900
    lea rbx, C("B") ; DS0901
    lea rcx, C("C") ; DS0902

    lea rcx, C(0)   ; DS0902
    lea rbx, C(1)   ; DS0901
    lea rax, C(2)   ; DS0900

Some limitations regarding expansion.

In this case lengthof() is expanded before the string is created so the result is from a previous string.

    _write(handle, "@echo off\n", lengthof( C(0) ) - 1 )

So the string needs to be created by lengthof().

    _write(handle, "@echo off\n", lengthof( C("@echo off\n") ) - 1 )

Duplicated strings:

    lea rsi, C( "X" )   ; DS0000
    lea rdi, C( "AB" )  ; DS0001
    lea rbx, C( "B" )   ; DS0001[1]

    lea rax, C( 0 )     ; DS0001
    lea rdx, C( 1 )     ; DS0000

Vortex

  • Member
  • *****
  • Posts: 2032
Re: Asmc source and binaries
« Reply #205 on: December 04, 2019, 06:18:38 AM »
Hi nidud,

Reading the latest release of asmc.chm :

Quote
@CStr( string )

Macro function that creates a string in the .DATA segment. The macro accepts C-escape characters in the string. Strings are added to a stack and reused if duplicated strings are found. The macro returns offset string.

Example

   mov   eax,@CStr( "\tCreate a \"C\" string: %s%d\n" )
   mov   ebx,@CStr( "string: %s%d\n" )
   mov   ecx,@CStr( "%s%d\n" )
   mov   edx,@CStr( "%d\n" )
   mov   edi,@CStr( "\n" )

Generated code

   .data
   DS0000 db 9,"Create a ",'"',"C",'"'," string: %s%d",10,0
   .code
   mov   eax,offset DS0000
   mov   ebx,offset DS0000[14]
   mov   ecx,offset DS0000[22]
   mov   edx,offset DS0000[24]
   mov   edi,offset DS0000[26]

And the new definition :

Quote
@CStr( index ) returns current string label minus index.

I guess the old functionality of the @CStr macro is valid as testing the code below outputs This is a test.

Code: [Select]
include Windows.inc
StdOut PROTO STDCALL :DWORD

.code

start:

    lea     eax,@CStr("This is a test.")
    invoke  StdOut,eax
    invoke  ExitProcess,0

OPTION PROLOGUE:NONE
OPTION EPILOGUE:NONE

StdOut PROC STDCALL string1:DWORD

    sub     esp,2*4
    invoke  GetStdHandle,STD_OUTPUT_HANDLE
    mov     DWORD PTR [esp+4],eax
    invoke  lstrlen,DWORD PTR [esp+12]
    mov     edx,esp
   
    invoke  WriteFile,DWORD PTR [esp+20],\
            DWORD PTR [esp+24],\
            eax,edx,0
           
    add     esp,2*4
    ret     4

StdOut ENDP

OPTION PROLOGUE:PROLOGUEDEF
OPTION EPILOGUE:EPILOGUEDEF

END start

The chm manual does not refer the latest feature of the @CStr macro.

nidud

  • Member
  • *****
  • Posts: 1800
    • https://github.com/nidud/asmc
Re: Asmc source and binaries
« Reply #206 on: December 04, 2019, 09:30:37 AM »
I updated the online version but not the .chm file.

Here's the new file entry:
Quote
@CStr( string | index )

Macro function that creates a string in the .DATA or .CONST segment.
The macro accepts C-escape characters in the string.
Strings are added to a stack and reused if duplicated strings are found.
The macro returns offset string.

Example

Code: [Select]
    T equ @CStr

    dq T("Create \"C\" string: %d\n")
    dq T("%d\n")
    dq T("\n")

      * .const
      * DS0000 sbyte "Create ",'"',"C",'"'," string: %d",10,0
      * .data
      * dq DS0000
      * dq DS0000[23]
      * dq DS0000[25]

@CStr( index ) returns current string label minus index.

Code: [Select]
    lea rax, T("A") ; DS0900
    lea rbx, T("B") ; DS0901
    lea rcx, T("C") ; DS0902

    lea rcx, T(0)   ; DS0902
    lea rbx, T(1)   ; DS0901
    lea rax, T(2)   ; DS0900

Added a new update tool to the asmc-2.30 release, this time from the root:  :biggrin:
Code: [Select]
;
; build: asmc -win64 -pe github.asm
;
include windows.inc
include wininet.inc
include stdio.inc
include tchar.inc

    .code

main proc argc:int_t, argv:array_t

  local wsaData             :WSADATA,
        hINet               :HINTERNET,
        hFile               :HANDLE,
        NumberOfBytesRead   :uint_t,
        size                :uint_t,
        fp                  :LPFILE,
        buffer[1024]        :char_t,
        url[128]            :char_t,
        file                :string_t,
        retval              :int_t

    .if ( ecx != 2 )

        printf("Download from github.com/nidud/asmc/\n"
               "Example: github bin/asmc.chm\n")

        .return(0)
    .endif

    mov file,[rdx+8]

    .ifd WSAStartup(2, &wsaData)

        printf("WSAStartup failed with error: %d\n", eax)

        .return(1)
    .endif

    .if !InternetOpen("InetURL/1.0", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0)

        perror("InternetOpen()")

        .return(1)
    .endif

    mov hINet,rax

    mov retval,1

    .if InternetOpenUrl(hINet,
            strcat(strcpy(&url, "https://github.com/nidud/asmc/raw/master/"), file),
            NULL, 0, 0, 0)

        mov hFile,rax

        .if strrchr(file, '/')

            inc rax
            mov file,rax
        .endif

        .if fopen(file, "wb")

            mov fp,rax
            mov size,0

            .while InternetReadFile(hFile, &buffer, 1024, &NumberOfBytesRead) && NumberOfBytesRead

                add size,NumberOfBytesRead

                fwrite(&buffer, NumberOfBytesRead, 1, fp)
            .endw

            fclose(fp)
            mov retval,0

        .else

            perror(file)
        .endif

        InternetCloseHandle(hFile)
    .endif

    InternetCloseHandle(hINet)
    WSACleanup()
    mov eax,retval
    ret

main endp

    end _tstart

nidud

  • Member
  • *****
  • Posts: 1800
    • https://github.com/nidud/asmc
Re: Asmc source and binaries
« Reply #207 on: December 04, 2019, 09:53:37 AM »
I guess the old functionality of the @CStr macro is valid as testing the code below outputs This is a test.

Yes. It's a text macro so the input is assumed to be a literal string including quotes. If the input is a const value and in range a label is returned from the string stack.

2B||!2B

  • Member
  • **
  • Posts: 72
Re: Asmc source and binaries
« Reply #208 on: December 05, 2019, 04:16:45 PM »
Hi nidud,

I cannot get linkw to get ASMCDIR Enviornment variable.
How do you install? i have already built the 64bit libs.
How to set lib path in linkw? LIBPATH does not work.

Code: [Select]
undefined system name: LIBPATH:
Code: [Select]
C:\masm32\asmc\bin\linkw.exe system gui_64 file Test.obj
Warning! W1115: system block gui_64: environment name ASMCDIR not found
Warning! W1008: cannot open libc.lib : No such file or directory
Warning! W1008: cannot open kernel32.lib : No such file or directory
Warning! W1008: cannot open user32.lib : No such file or directory
Warning! W1008: cannot open gdi32.lib : No such file or directory
Error! E2028: WinStart is an undefined reference
Error! E2028: printf is an undefined reference


Code: [Select]
start:
main proc
    ;
    ; Note alignment should be 2^N where N is any positive int.
    ;
    mov ebx,512
    ;
    ; Using _aligned_malloc
    ;
    mov rdi,_aligned_malloc(100, ebx)
    .if rdi
        mov ecx,ebx
        dec ecx
        .if !( rdi & rcx )
            printf("This pointer, %p, is aligned on %u\n", rdi, ebx)
        .else
            printf("This pointer, %p, is not aligned on %u\n", rdi, ebx)
        .endif
        xor eax,eax
    .else
        printf("Error allocation aligned memory.")
        mov eax,1
    .endif
    ret

main endp

    end start

nidud

  • Member
  • *****
  • Posts: 1800
    • https://github.com/nidud/asmc
Re: Asmc source and binaries
« Reply #209 on: December 06, 2019, 12:32:00 AM »
If you look at the _aligned_malloc test you'll see it uses SYSTEM which is a link macro defined in linkw.lnk.

makefile

_aligned_malloc.exe:
    asmc64 $*.asm
    linkw system con_64 file $*

con_64:

# Win64 console application
system begin con_64
    option  quiet
    libpath %ASMCDIR%\lib\amd64
    library libc,kernel32,user32
    symt    cstart
    format  windows pe ^
    runtime console=4.0
end

cstart

mainCRTStartup proc frame

   _initterm( &__xi_a, &__xi_z )
   exit( main( __argc, __argv, _environ ) )

mainCRTStartup endp

   end cstart


C:\masm32\asmc\bin\linkw.exe system gui_64 file Test.obj
Warning! W1115: system block gui_64: environment name ASMCDIR not found
Warning! W1008: cannot open libc.lib : No such file or directory
Warning! W1008: cannot open kernel32.lib : No such file or directory
Warning! W1008: cannot open user32.lib : No such file or directory
Warning! W1008: cannot open gdi32.lib : No such file or directory
Error! E2028: WinStart is an undefined reference
Error! E2028: printf is an undefined reference

The system gui_64 uses WinStart so con_64 should be used here and ASMCDIR needs to be set correctly. The shell (DZ) sets ASMCDIR and so do also MAKE but LINKW do not.

Something like this:

    set ASMCDIR=C:\masm32\asmc
    C:\masm32\asmc\bin\linkw.exe system con_64 file Test.obj


start:
main proc
...
main endp

    end start

You using a startup module here so start has to be removed.