News:

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

Main Menu

if you are forced to use only C/C++ on a project...

Started by daydreamer, August 22, 2019, 12:15:19 AM

Previous topic - Next topic

jj2007

Now I see the problem - using the C source from James' post at the oxygen forum:
#include <time.h>
#include <stdio.h>
#define SIZE 256

int main (void)
{
  char buffer[SIZE];
  time_t curtime;
  struct tm *loctime;

  /* Get the current time. */
  curtime = time (NULL);

  /* Convert it to local time representation. */
  loctime = localtime (&curtime);

  /* Print out the date and time in the standard format. */
  fputs (asctime (loctime), stdout);

  /* Print it out in a nice format. */
  strftime (buffer, SIZE, "Today is %A, %B %d.\n", loctime);
  fputs (buffer, stdout);
  strftime (buffer, SIZE, "The time is %I:%M %p.\n", loctime);
  fputs (buffer, stdout);
  strftime (buffer, SIZE, "ISO week day is %GW%V%u.\n", loctime);
  fputs (buffer, stdout);
  return 0;
}


Gcc "forgets" the ISO part, while Pelles C and Visual C print it correctly. Seems more a problem with strftime, though: It returns 0 (success?) for all calls but the last one leaves only a zero byte in the buffer.

TimoVJL

The OS msvcrt.dll thing is a good thing for clang, as it don't have own CRT.
So, a small examples can be done with msvcrt.lib from masm32 package, if we forget those pesky include files, other that Windows SDK.
If there is interest in this area, we can share our knowledge of making small programs.
A fare thing ?
May the source be with you

aw27

I think MSVCRT.DLL continues not recognizing the format specifiers %G and %V. However, the C runtimes after VS 2015 do recognize.

This is for Windows 10.


.model flat, stdcall

size_t typedef ptr
includelib \masm32\lib\msvcrt.lib
time proto C :ptr
strftime proto C :ptr, :size_t, :ptr, :vararg
localtime proto C :ptr
asctime proto C :ptr
puts proto C :ptr

.data
todayIs db "Today is %A, %B %d",0
timeIs db "The time is %I:%M %p.",0
isoMSG db "ISO week day is %GW%V%u.",0
buffer db 256 dup (0)

.code

main proc
LOCAL curtime : qword
LOCAL loctime : ptr

invoke time, 0
mov dword ptr curtime, eax
mov dword ptr curtime+4, edx
invoke localtime, addr curtime
mov loctime, eax
invoke asctime, eax
invoke puts, eax
invoke strftime, addr buffer, 256, offset todayIs, loctime
invoke puts, addr buffer
invoke strftime, addr buffer, 256, offset timeIs, loctime
invoke puts, addr buffer
invoke strftime, addr buffer, 256, offset isoMSG, loctime
invoke puts, addr buffer
ret
main endp

end



Output:
Fri Aug 23 15:34:45 2019

Today is Friday, August 23
The time is 03:34 PM.

(No sign of the ISO message.)

jcfuller

I did find a couple of discussions on MinGW gcc development accessing ucrtbase.dll but I got distracted elsewhere and did not follow up.
All the source for ucrtbase is in the ??\kits\10 folder (I forget the full path now) but all ucrtbase.dll functions I believe rely on exception handling??

James

TimoVJL

Quote from: AW on August 24, 2019, 12:41:07 AM
I think MSVCRT.DLL continues not recognizing the format specifiers %G and %V. However, the C runtimes after VS 2015 do recognize.
Sure it stays as a M$ Palmer era :rofl: , it don't change.
Quote from: TimoVJL on August 23, 2019, 10:21:46 PM
Of course it isn't, as M$ started to conform C99 somewhere 2012, it just took so long.
My mistake, i didn't specifically told that msvcrt.dll don't follow that.
May the source be with you

aw27

But this works, we need to fish ucrt.lib


.model flat, stdcall

size_t typedef ptr
includelib "C:\Program Files (x86)\Windows Kits\10\Lib\10.0.18362.0\ucrt\x86\ucrt.lib"
strftime proto C :ptr, :size_t, :ptr, :vararg
asctime proto C :ptr
puts proto C :ptr
includelib \masm32\lib\msvcrt.lib
localtime proto C :ptr
time proto C :ptr

.data
todayIs db "Today is %A, %B %d",0
timeIs db "The time is %I:%M %p.",0
isoMSG db "ISO week day is %GW%V%u.",0
buffer db 256 dup (0)

.code

main proc
LOCAL curtime : qword
LOCAL loctime : ptr

invoke time, 0
mov dword ptr curtime, eax
mov dword ptr curtime+4, edx
invoke localtime, addr curtime
mov loctime, eax
invoke asctime, eax
invoke puts, eax
invoke strftime, addr buffer, 256, offset todayIs, loctime
invoke puts, addr buffer
invoke strftime, addr buffer, 256, offset timeIs, loctime
invoke puts, addr buffer
invoke strftime, addr buffer, 256, offset isoMSG, loctime
invoke puts, addr buffer
ret
main endp

end


Output:
Fri Aug 23 16:14:41 2019

Today is Friday, August 23
The time is 04:14 PM.
ISO week day is 2019W345.

TimoVJL

Reply #9 http://masm32.com/board/index.php?topic=8045.msg88168#msg88168 ucrtbase.def
May the source be with you

aw27

Quote from: TimoVJL on August 24, 2019, 01:36:37 AM
Reply #9 http://masm32.com/board/index.php?topic=8045.msg88168#msg88168 ucrtbase.def

Yes, ucrtbase.dll is not enough.   :sad:

aw27

Now ucrtbase.dll is enough.  :thumbsup:


.model flat, stdcall

size_t typedef ptr
includelib "C:\Program Files (x86)\Windows Kits\10\Lib\10.0.18362.0\ucrt\x86\ucrt.lib"
;includelib ucrtbase.lib ; works also (supplied by TimoVJL)
strftime proto C :ptr, :size_t, :ptr, :vararg
asctime proto C :ptr
puts proto C :ptr
_localtime32 proto C :ptr
_time64 proto C :ptr

.data
todayIs db "Today is %A, %B %d",0
timeIs db "The time is %I:%M %p.",0
isoMSG db "ISO week day is %GW%V%u.",0
buffer db 256 dup (0)

.code

main proc
LOCAL curtime : qword
LOCAL loctime : ptr

invoke _time64, 0
mov dword ptr curtime, eax
mov dword ptr curtime+4, edx
invoke _localtime32, addr curtime
mov loctime, eax
invoke asctime, eax
invoke puts, eax
invoke strftime, addr buffer, 256, offset todayIs, loctime
invoke puts, addr buffer
invoke strftime, addr buffer, 256, offset timeIs, loctime
invoke puts, addr buffer
invoke strftime, addr buffer, 256, offset isoMSG, loctime
invoke puts, addr buffer
ret
main endp

end

TimoVJL

OK, i missed to show somethingtypedef void *FILE;
FILE *__acrt_iob_func(int);
...
FILE stdout = __acrt_iob_func(1);
then fputs() works
stdio.h is a real trouble maker, a compiler specific header, better to avoid it, mostly full of junk, as C standard group allows that.
May the source be with you

aw27

Yes, it works, (no need for headers  :badgrin:):


.model flat, stdcall

size_t typedef ptr
includelib "C:\Program Files (x86)\Windows Kits\10\Lib\10.0.18362.0\ucrt\x86\ucrt.lib"
;includelib ucrtbase.lib ; supplied by TimoVJL
strftime proto C :ptr, :size_t, :ptr, :vararg
asctime proto C :ptr
fputs proto C :ptr, :ptr
_localtime64 proto C :ptr
_time64 proto C :ptr
__acrt_iob_func proto C :BYTE

.data
todayIs db "Today is %A, %B %d",10,0
timeIs db "The time is %I:%M %p.",10,0
isoMSG db "ISO week day is %GW%V%u.",10,0
buffer db 256 dup (0)


localtime equ _localtime64
time equ _time64

.code

main proc
LOCAL curtime : qword
LOCAL loctime : ptr
LOCAL stdout :ptr

invoke time, 0
mov dword ptr curtime, eax
mov dword ptr curtime+4, edx
invoke __acrt_iob_func, 1
mov stdout, eax

invoke localtime, addr curtime
mov loctime, eax
invoke asctime, eax
invoke fputs, eax, stdout
invoke strftime, addr buffer, 256, offset todayIs, loctime
invoke fputs, addr buffer, stdout
invoke strftime, addr buffer, 256, offset timeIs, loctime
invoke fputs, addr buffer, stdout
invoke strftime, addr buffer, 256, offset isoMSG, loctime
invoke fputs, addr buffer, stdout
ret
main endp

end

aw27

Using fputs with msvcrt.dll


.model flat, stdcall

FILE struct
_ptr dd ?
_cnt dd ?
_base dd ?
_flag dd ?
_file dd ?
_charbuf dd ?
_bufsiz dd ?
_tmpfname dd ?
FILE ends

includelib \masm32\lib\msvcrt.lib
fputs proto C :ptr, :ptr
externdef C _imp___iob : ptr
stdin equ <_imp___iob>

.data

HelloMsg db "Testing fputs",10,0

.code

main proc
LOCAL stdout : ptr

mov ecx,  stdin
add ecx, sizeof FILE
mov stdout, ecx;

mov eax, offset HelloMsg
invoke fputs, eax, stdout
ret
main endp

end


Output:
Testing fputs

Vortex

James' code built with Cygwin :

$ gcc GetTimeTest.c  -o GetTimeTest.exe

$ ./GetTimeTest.exe
Sat Aug 24 12:06:00 2019
Today is Saturday, August 24.
The time is 12:06 PM.
ISO week day is 2019W346.

daydreamer

Quote from: AW on August 23, 2019, 08:46:51 PM
Quote from: daydreamer on August 23, 2019, 08:37:04 PM
but there is both cons and pros with DLL's,isnt one problem if you use big arrays,you get an additional memcopy between dll library and main program of those?

I don't think I understand what you mean, but the DLL once loaded is in the same memory space as the main program, no additional memcopy is needed.
judge for yourself very old unomptimized kinda raycast dll,made with vc++
my none asm creations
https://masm32.com/board/index.php?topic=6937.msg74303#msg74303
I am an Invoker
"An Invoker is a mage who specializes in the manipulation of raw and elemental energies."
Like SIMD coding

hutch--

Magnus,

This one is not a debate, a DLL is mapped into the memory space of the app that calls it, all you have to do to get data from a DLL is to get the pointer to it as it is already in memory. If someone has to copy memory from a DLL to a calling app, they have done it the wrong way. Just pass the pointer.