News:

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

Main Menu

scanf macro and printf macro

Started by stevenxie, November 14, 2020, 11:53:04 PM

Previous topic - Next topic

Vortex

Hi Jochen,

\masm32\bin\dumpbin.exe /EXPORTS \Windows\System32\msvcrt.dll > exports.txt

The list will include two Masm reserved keywords, div and FABS.

FABS     ABSolute value of ST(0)

jj2007

I see - thanks, Erol. It works fine with my Masm64 implementation, but I can imagine that other SDKs have a problem.
  if @64
movlps xmm0, MyReal8
jinvoke fabs, xmm0
movlps MyDest, xmm0
  else
jinvoke fabs, MyReal8
fstp MyDest
  endif
  Print Str$("The absolute value is %f\n", MyDest)
  jinvoke div, 12345, 246
  mov rcx, rdx
  Print Str$("div 12345/246 = %i with remainder %i\n", rax, rcx)


Output:
The absolute value is 123.456000
div 12345/246 = 50 with remainder 45

Vortex

Hi Jochen,

The solution in the Masm package is to rename those functions :

32-bit :

c_msvcrt typedef PROTO C :VARARG

externdef _imp__fabs:PTR c_msvcrt
crt_fabs equ <_imp__fabs>

externdef _imp__div:PTR c_msvcrt
crt_div equ <_imp__div>


64-bit :

externdef __imp_fabs:PPROC
vc_fabs equ <__imp_fabs>

externdef __imp_div:PPROC
vc_div equ <__imp_div>

hutch--

Yes, open "msvcrt.inc" and you will find them. Prepending the "vc" makes it possible to,

(a) avoid namespace clashes
(b) create different VCRT includes and libraries with no clashes.

MASM is not subject to the restriction of VC, a mistake that many have made and it is not crippled like UASM with a quirky requirement for old format prototypes that no-one is willing to provide.

With a very extensive set of includes and libraries, 64 bit MASM has no need of this junk.

TouEnMasm

Quote
With a very extensive set of includes and libraries, 64 bit MASM has no need of this junk.
Ah Venise !!!!!!!!!
But more serious,made a masm package without a modified msvcrt is the perfect soluce.
for 64 ,
The need is to use the ucrt and msvcrt original lib and normal prototypes.
I doubt that the ucrt.lib will be happy with the modified mscvrt of the masm32 package.

Fa is a musical note to play with CL

Vortex

Hi TouEnMasm,

The modification is only the naming of the function prototypes prepended with the vc_ prefix. The import library remains intact - no any modification :

C:\masm32\lib64>\PellesC\bin\podump.exe /EXPORTS msvcrt.lib > exports.txt
C:\masm32\lib64>type exports.txt | findstr "vc_"

hutch--

The problem is that it is not a perfect solution, if you have a look at the list of VC runtime functions, some of the names are MASM reserve words which makes the direct approach useless. 64 bit MASM is very intolerant to errors. You use the standard C runtime libraries with Microsoft C. Differing from your assumptions, MASM does not need to try and emulate C, it does its own MASM notation well.

There is another problem, the MSVCRT include and library is only for MSVCRT, not any of the later versions so if you get some of the C runtime going, you cannot use a later version due to namespace collisions.

Here is how to do "printf" in both ASCII and UNICODE. The original was written by Michael Webster some years ago, it was later modified to include a UNICODE version using the same notation.

    printf MACRO format:REQ, args:VARARG
      IFNDEF __UNICODE__
        IFNB <args>
          fn crt_printf, cfm$(format), args
        ELSE
          fn crt_printf, cfm$(format)
        ENDIF
        EXITM <>
      ELSE
        IFNB <args>
          fn crt_wprintf, cfm$(format), args
        ELSE
          fn crt_wprintf, cfm$(format)
        ENDIF
        EXITM <>
      ENDIF
    ENDM


hutch--

A macro is the solution if you want to try and ape C code. Here is a simple text example.

; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

    include \masm32\include64\masm64rt.inc

    printf MACRO format:REQ, args:VARARG
        IFNB <args>
          fn vc_printf, cfm$(format), args
        ELSE
          fn vc_printf, cfm$(format)
        ENDIF
        EXITM <>
    ENDM

    .code

; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

entry_point proc

    USING r12

    SaveRegs

    printf("\n\n\tprintf demo\n\t\t\qNext Line\q\n\t\t\t\qAnother Line\q\n\n\n");

    waitkey
    RestoreRegs
    .exit

entry_point endp

; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

    end

TouEnMasm


The simple way in five minutes
Quote
   include sdk64.inc
   include stdio.sdk
   include conio.sdk
   includelib ucrt.lib
   includelib msvcrt.lib
   includelib H:\sdkrc100\crt_lib\X64\release\crt10_64.lib ;inline fonctions
   
.data
   a dq ?
   b dq ?
   s dq ?
.code

; ******************************************************

main proc
       
    invoke printf,TXT(" please input a and b ")
    invoke scanf,TXT("%ld%ld"),addr a, addr b
    mov rax,a
    add rax,b
    mov s,rax
    invoke printf,TXT(13,10," a+b=%ld",13,10),s
    invoke _getch

   ret
main endp
end

Fa is a musical note to play with CL

Vortex

Hi ToutEnMasm,

Nice work using your SDK. The executable depends on some exta DLLs.

\PellesC\bin\podump.exe /IMPORTS Downloads\demo.exe | findstr ".dll"

        api-ms-win-crt-conio-l1-1-0.dll
        VCRUNTIME140.dll
        KERNEL32.dll
        api-ms-win-crt-runtime-l1-1-0.dll
        api-ms-win-crt-math-l1-1-0.dll
        api-ms-win-crt-stdio-l1-1-0.dll
        api-ms-win-crt-locale-l1-1-0.dll
        api-ms-win-crt-heap-l1-1-0.dll

TouEnMasm


It is write under Windows 10 with all the official libraries of the windows SDK and of the visual studio C++.
If you write it in C , you wull have exactly the same dll needed.
Fa is a musical note to play with CL

hutch--

DLLs yes, libraries NO. You have not addressed CRT clashes with MASM reserve words yet. If you want to use C libraries, write in C.

TouEnMasm


Quote
DLLs yes, libraries NO. You have not addressed CRT clashes with MASM reserve words yet. If you want to use C libraries, write in C.
Have a look to the editmasm.ini who show all the path of the libraries: Libraries (all are of the last VS version and last SDK version)
libraries yes
Protototypes are the masm prototypes and all includes are write in masm syntax.
It is pure masm syntax who don't need complex macros to avoid conflicts between libraries.
Without conflict between libraries code can be far more simple,there is only one macro in use TXT.
Fa is a musical note to play with CL

Vortex

Hi ToutEnMasm,

Your example can be assembled to import functions only from kernel32.dll and msvcrt.dll :

include     \masm32\include64\masm64rt.inc

.data

a   dq ?
b   dq ?
s   dq ?

.code

start PROC

    invoke  vc_printf,"Please input a and b"
    invoke  vc_scanf,"%ld%ld",ADDR a, ADDR b
   
    mov     rax,a
    add     rax,b
    mov     s,rax
   
    invoke  vc_printf,\
            chr$(13,10," a+b=%ld",13,10),s

    invoke vc__getch

    invoke  ExitProcess,0

start ENDP

END


The final executable is just only 1.5 KB

hutch--

Yves,

You are still missing something, ML64 does not need the old prototypes. If you look in the import libraries there is no linker information. Below is all you need in ML64, your includes may be useful in UASM but ML64 does not use the same method as the old ML for prototypes.

externdef __imp_AddAtomA:PPROC
AddAtomA equ <__imp_AddAtomA>
  IFNDEF __UNICODE__
    AddAtom equ <__imp_AddAtomA>
  ENDIF

externdef __imp_AddAtomW:PPROC
AddAtomW equ <__imp_AddAtomW>
  IFDEF __UNICODE__
    AddAtom equ <__imp_AddAtomW>
  ENDIF

externdef __imp_AddConsoleAliasA:PPROC
AddConsoleAliasA equ <__imp_AddConsoleAliasA>
  IFNDEF __UNICODE__
    AddConsoleAlias equ <__imp_AddConsoleAliasA>
  ENDIF