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

powershadow

  • Regular Member
  • *
  • Posts: 20
Re: Asmc source and binaries
« Reply #15 on: January 16, 2017, 07:55:04 PM »
Confirmed.

Quotes in command line arguments are not expanded in the same way in the RTL now used, so additional expansion was needed for switch -D, -I and -Fi used with -I"quoted text".

Wow so fast, this project has better support then hjwasm! :t
Nidud, may I request a feature?
Can you add to the listing file (/Fl), dump for all used procedure protos.
Example:

Code: [Select]
...
.NOLIST
Include kernel32.inc
...
.code

TestProc1 proc Par1:HANDLE,Par2:DWORD,Par3:PVOID
xor eax,eax
ret

TestProc1 endp

start:

invoke CreateFile,SADD('file.txt'),GENERIC_READ,FILE_SHARE_READ,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL
invoke TestProc1,eax,11111111h,NULL
invoke ExitProcess,0
end start

Listing file:
...
Procedures protos:

                N a m e                 PROTO

TestProc1  . . . . . . . . . . .        PROTO Par1:HANDLE, Par2:DWORD, Par3:PVOID
CreateFileA  . . . . . . . . . .        PROTO lpFileName:PSTR ,dwDesiredAccess:DWORD ,dwShareMode:DWORD ,lpSecurityAttributes:PVOID ,dwCreationDisposition:DWORD ,dwFlagsAndAttributes:DWORD ,hTemplateFile:HANDLE
ExitProcess  . . . . . . . . . .        PROTO uExitCode:DWORD
...

Where TestProc1 generated from source, CreateFileA & ExitProcess - just copy used proto from inc file.

nidud

  • Member
  • *****
  • Posts: 1408
    • https://github.com/nidud/asmc
Re: Asmc source and binaries
« Reply #16 on: January 17, 2017, 03:12:40 AM »
Nidud, may I request a feature?
Can you add to the listing file (/Fl), dump for all used procedure protos.

Unused functions have a '*' added to the list if that helps:
Code: [Select]
.386
.model flat,stdcall
.code

foo proto
bar proto

foo()

END

Code: [Select]
Procedures, parameters and locals:

                N a m e                 Type     Value    Segment  Length

bar  . . . . . . . . . . . . . .        P Near32 00000000 No Seg   00000000 *External STDCALL
foo  . . . . . . . . . . . . . .        P Near32 00000000 No Seg   00000000 External  STDCALL

powershadow

  • Regular Member
  • *
  • Posts: 20
Re: Asmc source and binaries
« Reply #17 on: January 17, 2017, 03:21:56 AM »
Unused functions have a '*' added to the list if that helps:
Thanks for reply nidud, unfortunately it's not help :(. I need PROTOs

nidud

  • Member
  • *****
  • Posts: 1408
    • https://github.com/nidud/asmc
Re: Asmc source and binaries
« Reply #18 on: January 17, 2017, 03:48:16 AM »
Well, I can see the usefulness of this and the information to compile the list may also be available, so I may look into this someday.

nidud

  • Member
  • *****
  • Posts: 1408
    • https://github.com/nidud/asmc
Re: Asmc source and binaries
« Reply #19 on: March 02, 2017, 08:08:18 AM »
Some imort libraries are added to the source base. I was hoping to do this the simple way by target the .dll files directly, at least the 64-bit without any decoration.
Code: [Select]
32 libw /n /c /l /b /fac /ii version.lib %windir%\system32\version.dll
64 libw /n /c /l /b /fac /i6 version.lib %windir%\system32\version.dll

However, this didn't work very well for some (unknown) reason, so I ended up using .def files for all of them. The command that works looks like this:
Code: [Select]
++_BlockWOWIdle@4.'NTVDM.exe'

Given this works (I assume), the import record for the simple approach should in theory also work, at least for C and FASTCALL.

I have also done some testing with using windows.inc for differnt targets. The same include files should now work for both 32 and 64.bit, unicode/ascii, C/stdcall/fastcall, creating PE (no libs), using msvcrt (C/fastcall), or using the local library with startup modules.

Some changes have also been added to MAKE to simplify the build process. If %AsmcDir% is set MAKE will use the .\lib directory as a common include directory for .mif files.

Well, parsing these includes is tidious work so the current state looks like this:
Code: [Select]
WIN32_LEAN_AND_MEAN equ 1
include windows.inc

I currently only need kernel32 and user32 in the source base, but as the parsing improves more files will be added. Depending on how these functions are decleared, some will be defined with return type and line breaks, others with a single line:

Code: [Select]
if (_WIN32_WINNT GE 0x0500)
;BOOL
InitializeCriticalSectionAndSpinCount proto _CType \
     lpCriticalSection: LPCRITICAL_SECTION,
   dwSpinCount: DWORD
;DWORD
SetCriticalSectionSpinCount proto _CType \
     lpCriticalSection: LPCRITICAL_SECTION,
   dwSpinCount: DWORD
endif

Code: [Select]
ifndef _WDIRECT_DEFINED

;; also declared in direct.h

_wchdir proto :ptr wchar_t
_wgetcwd proto :ptr wchar_t, :SINT
_wgetdcwd proto :SINT, :ptr wchar_t, :SINT
_wmkdir proto :ptr wchar_t
_wrmdir proto :ptr wchar_t

_WDIRECT_DEFINED equ 1
endif

Vortex

  • Member
  • *****
  • Posts: 1723
Re: Asmc source and binaries
« Reply #20 on: March 03, 2017, 07:01:57 AM »
Hi nidud,

Regarding 64-bit coding, it's easy to create .def files and import libraries from DLLs :

Creating include files and import libraries for ml64

nidud

  • Member
  • *****
  • Posts: 1408
    • https://github.com/nidud/asmc
Re: Asmc source and binaries
« Reply #21 on: March 03, 2017, 10:25:40 AM »
Hey Vortex

The problem here is that LIBW is capable of creating a working import record from the command line but fail to do so from a .dll file. Asmc also use the same code for the /pe switch. This means that the problem is fixable within the source for LIBW, but I haven't looked into this yet.

The include files used in the Asmc package is the same for both 32 and 64-bit, so I need the arguments for all the functions as well. In addition to this I also need the type and size of each of them as oppose to just flip from DWORD to QWORD.

It would have been possible to just flip :DWORD to :PTR in the MASM32 package as well as appose to create a new set of files, but having the correct size and types makes a huge difference with regards to debugging.

What I'm doing now is moving (and adding) declarations to the correct MS header files to get them better organized and thereby easier yo use for those more familiar with these. So there is a C/ASM merger of sorts going on here.

As for the decorated symbols for 32-bit they available here:
https://github.com/open-watcom/open-watcom-v2/tree/master/bld/w32api/lib

It's possible parse these to create simple .inc files for both 32 and 64-bit.
.def 32:
AddConsoleAliasA@12
.def 64:
AddConsoleAliasA
.inc 32/64
AddConsoleAliasA proto :ptr, :ptr, :ptr

This is currently how the imports are created.
32-bit:
https://github.com/nidud/asmc/blob/master/lib/makefile
64-bit:
https://github.com/nidud/asmc/blob/master/lib/x64/makefile

Given no libraries exist at this point and the .def files needs parsing, the parser is made using the includes with the /pe switch.
32-bit:
https://github.com/nidud/asmc/blob/master/lib/def/lbc.asm
64-bit:
https://github.com/nidud/asmc/blob/master/lib/def/lbc64.asm

nidud

  • Member
  • *****
  • Posts: 1408
    • https://github.com/nidud/asmc
Re: Asmc source and binaries
« Reply #22 on: March 04, 2017, 06:35:13 AM »
A sample on how to use TCHAR.INC for multiple targets. Most of the samples used by MS apply this methood for unicode/ascii.
Code: [Select]
include stdio.inc
include stdlib.inc
include tchar.inc

.code

_tmain proc _CDecl argc:SINT, argv:PVOID

.for RSI = argv, edi = argc, ebx = 0: edi: edi--, ebx++, RSI += size_t

_tprintf("[%d]: %s\n", ebx, [RSI])
.endf
xor eax,eax
ret

_tmain endp

end _tstart

The command line for Asmc /pe:
asmc -pe -D__PE__ $*.asm
asmc -pe -ws -D_UNICODE -D__PE__ $*.asm
asmc -pe -D_WIN64 -D__PE__ $*.asm
asmc -pe -ws -D_UNICODE -D_WIN64 -D__PE__ $*.asm

The same code may also be built using the local library, but only the 32-bit ASCII version has the appropriate startup module to handle the the main arguments for now.

Thus it may not be recommendable to mix 32 and 64-bit code in this way the possibility to do just that is at least within reach. The CALL directive is somewhat limited for this approach so you have to use INVOKE on function calls to handle the local/import declarations (_imp_*) correctly.

The startup module is now defined in the tchar.inc file for the /pe switch, so the _tstart macro is either <> or <mainCRTStartup>. The Rxx registers are define in up-case chars so they become case sensitive in 32-bit.
« Last Edit: March 04, 2017, 08:43:32 AM by nidud »

Vortex

  • Member
  • *****
  • Posts: 1723
Re: Asmc source and binaries
« Reply #23 on: March 04, 2017, 06:39:21 AM »
Hi nidud,

I am fine with the DWORD to QWORD conversion but knowing the size and the type of the arguments is useful while debugging as you said.

The same include files supporting both 32-bit and 64-bit coding is a good idea. Creating the module definition files is not a problem, I do it with my lib2def tool. Pelle's library manager polib can be used for the same task.

Keep up the good work nidud :t

jj2007

  • Member
  • *****
  • Posts: 7728
  • Assembler is fun ;-)
    • MasmBasic
Re: Asmc source and binaries
« Reply #24 on: March 04, 2017, 07:06:06 AM »
The same include files supporting both 32-bit and 64-bit coding is a good idea.

I agree, see 64-bit assembly with RichMasm. It is pretty easy, actually, mainly because Microsoft needs it for the transition from 32-bit to 64-bit ("The header files for the Windows API enable you to create 32- and 64-bit applications."). My solution works fine, not a single extra library or header needed, but it is limited to the most important WinAPIs. Currently, I can't find enough enthusiasm to develop it further, simply because I don't see a convincing reason to go 64-bit 8)

nidud

  • Member
  • *****
  • Posts: 1408
    • https://github.com/nidud/asmc
Re: Asmc source and binaries
« Reply #25 on: March 11, 2017, 05:48:07 AM »
Code: [Select]
WIN32_LEAN_AND_MEAN equ 1
include windows.inc

The rest of the include files are now added but not tested: no assembly error.

nidud

  • Member
  • *****
  • Posts: 1408
    • https://github.com/nidud/asmc
Re: Asmc source and binaries
« Reply #26 on: April 04, 2017, 03:02:50 AM »
The Machine Architectures defined in windows.inc:
     _M_IX86      32-bit
     _M_AMD64   32/64-bit
     _M_IA64      64-bit

I added two directives: .amd64 and .win64. This to enable source level control as oppose to be depended on command line switches. I added this to simplify testing but I'm not sure how useful this will be elsewhere.

.win64 is the same as /win64 and .asm64 is equal to .x64.
I may add some options to the .win64 directive later.
.win64[: rsp | rbp | align | ...]

.win64 should be the first directive used since it needs to reset the assembler. The /win64 switch is similar to the /LARGEADDRESSAWARE switch, using "real" 64-bit.

Code: [Select]
.win64
.code

Code: [Select]
; Disassembly of file: win64.obj
; Mon Apr 03 18:46:46 2017
; Mode: 64 bits
; Syntax: MASM/ML64
; Instruction set: 8086, x64

Code: [Select]
.amd64
.model flat, fastcall
.code

Code: [Select]
; Disassembly of file: win64.obj
; Mon Apr 03 18:13:50 2017
; Mode: 32 bits
; Syntax: MASM/ML
; Instruction set: 80386

.386
.model flat

nidud

  • Member
  • *****
  • Posts: 1408
    • https://github.com/nidud/asmc
Re: Asmc source and binaries
« Reply #27 on: April 06, 2017, 11:57:33 PM »
Quote
I may add some options to the .win64 directive later.
.win64[: rsp | rbp | align | ...]

I added the following syntax:

.WIN64 [: RSP | RBP | ALIGN | NOALIGN | SAVE | NOSAVE | AUTO | NOAUTO | FRAME | NOFRAME]

This may also be used with .X64 and .AMD64

RBP:
   STACKBASE = RBP
   FRAME = AUTO
   W64F_AUTOSTACKSP = 1
   W64F_SAVEREGPARAMS = 1
RSP:
   STACKBASE = RSP
   W64F_AUTOSTACKSP = 1
ALIGN:
   IF WIN64 == 0
      W64F_AUTOSTACKSP = 1
   W64F_STACKALIGN16 = 1
NOALIGN:
   W64F_STACKALIGN16 = 0
SAVE:
   W64F_SAVEREGPARAMS = 1
NOSAVE:
   W64F_SAVEREGPARAMS = 0
AUTO:
   W64F_AUTOSTACKSP = 1
NOAUTO:
   W64F_AUTOSTACKSP = 0
FRAME:
   FRAME = AUTO
NOFRAME:
   FRAME = NOAUTO

Code: [Select]
.win64: rbp align
.code

foo proc uses rdi a1:byte, a2:word, a3:dword, a4:qword

  local l1: byte,
l2: xmmword,
l3: byte,
l4: ymmword,
l5: byte

lea rax,l1
lea rax,l2
lea rax,l3
lea rax,l4
lea rax,l5

mov al,a1
mov ax,a2
mov eax,a3
mov rax,a4

ret
foo endp

Code: [Select]
00000000  48894C2408        *   mov [rsp+8], rcx
00000005  4889542410        *   mov [rsp+16], rdx
0000000A  4C89442418        *   mov [rsp+24], r8
0000000F  4C894C2420        *   mov [rsp+32], r9
00000014  55                *   push rbp
00000015  488BEC            *   mov rbp, rsp
00000018  57                *   push rdi
00000019  4881EC88000000    *   sub rsp, 104 + @ReservedStack
00000020  488D45F7              lea rax,l1
00000024  488D45E0              lea rax,l2
00000028  488D45DF              lea rax,l3
0000002C  488D45A0              lea rax,l4
00000030  488D459F              lea rax,l5

00000034  8A4510                mov al,a1
00000037  668B4518              mov ax,a2
0000003B  8B4520                mov eax,a3
0000003E  488B4528              mov rax,a4

00000042                        ret
00000042  4881C488000000    *   add rsp, 104 + @ReservedStack
00000049  5F                *   pop rdi
0000004A  C9                *   leave
0000004B  C3                *   retn

   .win64: rsp

Code: [Select]
00000000  57                *   push rdi
00000001  4881EC80000000    *   sub rsp, 96 + @ReservedStack
00000008  488D44247F            lea rax,l1
0000000D  488D442468            lea rax,l2
00000012  488D442467            lea rax,l3
00000017  488D442430            lea rax,l4
0000001C  488D44242F            lea rax,l5

00000021  8A842490000000        mov al,a1
00000028  668B842498000000      mov ax,a2
00000030  8B8424A0000000        mov eax,a3
00000037  488B8424A8000000      mov rax,a4
0000003F                        ret
0000003F  4881C480000000    *   add rsp, 96 + @ReservedStack
00000046  5F                *   pop rdi
00000047  C3                *   retn

Vortex

  • Member
  • *****
  • Posts: 1723
Re: Asmc source and binaries
« Reply #28 on: April 07, 2017, 05:28:31 AM »
Hi nidud,

.win64:rbp align works fine, thanks :t

Code: [Select]
.win64:rbp align
option casemap:none

include    Window.inc

.data

ClassName  db "WndClass",0
AppName    db "Window",0

.code

start PROC

LOCAL hInstance:QWORD
   
    invoke  GetModuleHandle,0
    mov     hInstance,rax
   
    invoke  GetCommandLine
   
    invoke  WinMain,hInstance,0,rax,SW_SHOWDEFAULT
    invoke  ExitProcess,rax

start ENDP

WinMain PROC hInst:QWORD,hPrevInst:QWORD,CmdLine:QWORD,CmdShow:QWORD

    LOCAL   wc:WNDCLASSEX
    LOCAL   msg:MSG
    LOCAL   hwnd:QWORD

    mov     wc.cbSize,SIZEOF WNDCLASSEX
    mov     wc.style,CS_HREDRAW or CS_VREDRAW
    mov     rax,OFFSET WndProc
    mov     wc.lpfnWndProc,rax

    xor     rax,rax
    mov     wc.cbClsExtra,eax
    mov     wc.cbWndExtra,eax
    mov     wc.hInstance,rcx
    mov     wc.hbrBackground,COLOR_WINDOW+1
    mov     wc.lpszMenuName,rax

    lea     rax,ClassName
    mov     wc.lpszClassName,rax
    invoke  LoadIcon,0,IDI_APPLICATION
    mov     wc.hIcon,rax
    mov     wc.hIconSm,rax
    invoke  LoadCursor,0,IDC_ARROW
    mov     wc.hCursor,rax
    invoke  RegisterClassEx,ADDR wc

    mov     rax,CW_USEDEFAULT

    invoke  CreateWindowEx,0,ADDR ClassName,\
            ADDR AppName,\
            WS_OVERLAPPEDWINDOW,\
            rax,rax,rax,rax,0,0,\
            hInst,0

    mov     hwnd,rax

    invoke  ShowWindow,hwnd,SW_SHOWNORMAL
    invoke  UpdateWindow,hwnd

@@:
    invoke  GetMessage,ADDR msg,0,0,0
    test    rax,rax
    jz      @f
    invoke  TranslateMessage,ADDR msg
    invoke  DispatchMessage,ADDR msg
    jmp     @b
@@:
    mov     rax,msg.wParam
    ret

WinMain ENDP

WndProc PROC hWnd:QWORD,uMsg:QWORD,wParam:QWORD,lParam:QWORD

    cmp     edx,WM_DESTROY
    jne     @f

    invoke  PostQuitMessage,0
    jmp     _exit
@@:   
    invoke  DefWindowProc,hWnd,uMsg,wParam,lParam
    ret

_exit:

    xor     rax,rax
    ret

WndProc ENDP

END

nidud

  • Member
  • *****
  • Posts: 1408
    • https://github.com/nidud/asmc
Re: Asmc source and binaries
« Reply #29 on: April 07, 2017, 07:09:54 AM »
Thanks for the sample Vortex  :t

I will try to use windows.inc and see how that goes..