NO

Author Topic: Universal C Runtime example  (Read 5019 times)

Online Vortex

  • Member
  • *
  • Posts: 870
    • http://www.vortex.masmcode.com
Universal C Runtime example
« on: December 09, 2023, 09:33:24 PM »
Poasm example continuing the thread Import libraries for the Universal C Runtime :

https://forum.pellesc.de/index.php?topic=11052.0

Code: [Select]
.386
.model flat,stdcall
option casemap:none

_stdout equ 1

ExitProcess             PROTO :DWORD
printf                  PROTO C :DWORD,:VARARG
__acrt_iob_func         PROTO C :DWORD

; int __cdecl __stdio_common_vfprintf(unsigned __int64 _Options,FILE* _Stream,
;                                     char const* _Format,int _Locale,va_list);

__stdio_common_vfprintf PROTO C :DWORD,:DWORD,:DWORD,:DWORD,:DWORD,:DWORD
_strupr                 PROTO C :DWORD

includelib  \PellesC\lib\Win\kernel32.lib
includelib  ucrt.lib

.data

frmt    db '%s %s %s %u.',0
a       db 'This',0
b       db 'is',0
c       db 'printf test',0

.code

start:

    invoke  _strupr,ADDR c

    invoke  printf,ADDR frmt,ADDR a,ADDR b,eax,1

    invoke  ExitProcess,0


printf PROC C _format:DWORD,args:VARARG

;   #define _stdout (__acrt_iob_func(1))

    invoke  __acrt_iob_func,_stdout   

    lea     ecx,args
    invoke  __stdio_common_vfprintf,0,0,\ ; unsigned __int64 _Options
            eax,_format,0,ecx
    ret

printf ENDP

END start

You can download the attchment ucrt-implib.zip from the other thread to get the import libraries for the Universal C Runtime.
« Last Edit: December 09, 2023, 09:35:12 PM by Vortex »
Code it... That's all...

Online Vortex

  • Member
  • *
  • Posts: 870
    • http://www.vortex.masmcode.com
Re: Universal C Runtime example
« Reply #1 on: December 24, 2023, 09:58:26 AM »
Here is the 64-bit version :

Code: [Select]
_stdout EQU 1

ExitProcess             PROTO :QWORD
printf                  PROTO :QWORD,:VARARG
__acrt_iob_func         PROTO :QWORD

; int __cdecl __stdio_common_vfprintf(unsigned __int64 _Options,FILE* _Stream,
;                                     char const* _Format,int _Locale,va_list);

__stdio_common_vfprintf PROTO :QWORD,:QWORD,:QWORD,:QWORD,:QWORD
_strupr                 PROTO :QWORD


includelib  \PellesC\lib\Win64\kernel32.lib
includelib  ucrt.lib

.data

frmt    db '%s %s %s %u.',0
s1      db 'This',0
s2      db 'is',0
s3      db 'printf test',0

.code

start PROC PARMAREA=5*QWORD

    invoke  _strupr,ADDR s3

    invoke  printf,ADDR frmt,ADDR s1,ADDR s2,\
            ADDR s3,1

    invoke  ExitProcess,0

start ENDP

OPTION PROLOGUE:NONE
OPTION EPILOGUE:NONE

printf PROC _format:QWORD,args:VARARG

    mov     QWORD PTR [rsp+8],rcx
    mov     QWORD PTR [rsp+16],rdx
    mov     QWORD PTR [rsp+24],r8
    mov     QWORD PTR [rsp+32],r9

    sub     rsp,8+40+8

;   #define _stdout (__acrt_iob_func(1))

    invoke  __acrt_iob_func,_stdout   

    xor     rcx,rcx
    mov     rdx,rax
    mov     r8,QWORD PTR [rsp+64]
    xor     r9,r9
    lea     rax,QWORD PTR [rsp+72]
    mov     QWORD PTR [rsp+32],rax
    call    __stdio_common_vfprintf

    add     rsp,8+40+8
    retn

printf ENDP

OPTION PROLOGUE:PrologueDef
OPTION EPILOGUE:EpilogueDef

END start
Code it... That's all...

Online Vortex

  • Member
  • *
  • Posts: 870
    • http://www.vortex.masmcode.com
Re: Universal C Runtime example
« Reply #2 on: October 05, 2024, 09:21:11 PM »
Retrieval of the handle of the stdout stream moved out from printf :

Code: [Select]
start PROC PARMAREA=5*QWORD

;   #define _stdout (__acrt_iob_func(1))

    invoke  __acrt_iob_func,_stdout
    mov     stdout+rip,rax

Code it... That's all...

Offline jack

  • Member
  • *
  • Posts: 66
Re: Universal C Runtime example
« Reply #3 on: October 13, 2024, 01:58:29 PM »
thanks Vortex  :)