Author Topic: if you are forced to use only C/C++ on a project...  (Read 738 times)

jj2007

  • Member
  • *****
  • Posts: 9683
  • Assembler is fun ;-)
    • MasmBasic
Re: if you are forced to use only C/C++ on a project...
« Reply #15 on: August 23, 2019, 11:22:39 PM »
Now I see the problem - using the C source from James' post at the oxygen forum:
Code: [Select]
#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

  • Member
  • ***
  • Posts: 428
Re: if you are forced to use only C/C++ on a project...
« Reply #16 on: August 24, 2019, 12:02:52 AM »
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

AW

  • Member
  • *****
  • Posts: 2310
  • Let's Make ASM Great Again!
Re: if you are forced to use only C/C++ on a project...
« Reply #17 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.

This is for Windows 10.

Code: [Select]
.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

  • Member
  • **
  • Posts: 182
Re: if you are forced to use only C/C++ on a project...
« Reply #18 on: August 24, 2019, 12:44:50 AM »
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

  • Member
  • ***
  • Posts: 428
Re: if you are forced to use only C/C++ on a project...
« Reply #19 on: August 24, 2019, 12:50:21 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.
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

AW

  • Member
  • *****
  • Posts: 2310
  • Let's Make ASM Great Again!
Re: if you are forced to use only C/C++ on a project...
« Reply #20 on: August 24, 2019, 01:17:40 AM »
But this works, we need to fish ucrt.lib

Code: [Select]
.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

  • Member
  • ***
  • Posts: 428
May the source be with you

AW

  • Member
  • *****
  • Posts: 2310
  • Let's Make ASM Great Again!

AW

  • Member
  • *****
  • Posts: 2310
  • Let's Make ASM Great Again!
Re: if you are forced to use only C/C++ on a project...
« Reply #23 on: August 24, 2019, 02:35:24 AM »
Now ucrtbase.dll is enough.  :thumbsup:

Code: [Select]
.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

  • Member
  • ***
  • Posts: 428
Re: if you are forced to use only C/C++ on a project...
« Reply #24 on: August 24, 2019, 03:55:24 AM »
OK, i missed to show something
Code: [Select]
typedef 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

AW

  • Member
  • *****
  • Posts: 2310
  • Let's Make ASM Great Again!
Re: if you are forced to use only C/C++ on a project...
« Reply #25 on: August 24, 2019, 05:28:08 AM »
Yes, it works, (no need for headers  :badgrin:):

Code: [Select]
.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

AW

  • Member
  • *****
  • Posts: 2310
  • Let's Make ASM Great Again!
Re: if you are forced to use only C/C++ on a project...
« Reply #26 on: August 24, 2019, 07:02:24 AM »
Using fputs with msvcrt.dll

Code: [Select]
.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

  • Member
  • *****
  • Posts: 1993
Re: if you are forced to use only C/C++ on a project...
« Reply #27 on: August 24, 2019, 07:07:41 PM »
James' code built with Cygwin :

Code: [Select]
$ 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

  • Member
  • ****
  • Posts: 904
  • watch Chebyshev on the backside of the Moon
Re: if you are forced to use only C/C++ on a project...
« Reply #28 on: August 24, 2019, 09:42:21 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++
Quote from Flashdance
Nick  :  When you give up your dream, you die
*wears a flameproof asbestos suit*
what cpu handle "press any key"? any cpu of course(from C#) :D

hutch--

  • Administrator
  • Member
  • ******
  • Posts: 6657
  • Mnemonic Driven API Grinder
    • The MASM32 SDK
Re: if you are forced to use only C/C++ on a project...
« Reply #29 on: August 24, 2019, 10:51:05 PM »
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.
hutch at movsd dot com
http://www.masm32.com    :biggrin:  :skrewy: