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

jcfuller

  • Member
  • **
  • Posts: 182
Re: if you are forced to use only C/C++ on a project...
« Reply #30 on: August 24, 2019, 11:56:25 PM »
working example for 64bit??

James

Vortex

  • Member
  • *****
  • Posts: 2030
Re: if you are forced to use only C/C++ on a project...
« Reply #31 on: August 25, 2019, 04:38:09 AM »
MinGW 64-bit V5.4.0 :

Code: [Select]
C:\mingw64>gcc GetTimeTest.c -o GetTimeTest.exe

C:\mingw64>GetTimeTest.exe
Sat Aug 24 21:35:29 2019
Today is Saturday, August 24.
The time is 09:35 PM.
ISO week day is W.

AW

  • Member
  • *****
  • Posts: 2435
  • Let's Make ASM Great Again!
Re: if you are forced to use only C/C++ on a project...
« Reply #32 on: August 25, 2019, 04:55:36 AM »
Playing with stdio file control structures from msvcrt.dll

Code: [Select]
#include <tchar.h>
#include <stdio.h>

#define SIZE 256

#undef stdin
#undef stdout
#undef stderr
#pragma comment(lib,"msvcrt.lib")
#ifdef _WIN64
extern char* __imp__iob;
#define stdin (FILE*)&__imp__iob[0]
#define stdout (FILE*)&__imp__iob[sizeof(FILE)]
#define stderr (FILE*)&__imp__iob[2*sizeof(FILE)]
#else
extern char* _imp___iob;
#define stdin (FILE*)&_imp___iob[0]
#define stdout (FILE*)&_imp___iob[sizeof(FILE)]
#define stderr (FILE*)&_imp___iob[2*sizeof(FILE)]
#endif


int mainCRTStartup(void)
{
char buffer[SIZE] = { 0 };
fputs("Was Stalin German, Italian, Russian or Chinese ?:\n", stdout);
while (1)
{
fgets(buffer, SIZE - 1, stdin);
buffer[strcspn(buffer, "\n")] = '\0';

if (_stricmp(buffer, "russian"))
fputs("Wrong, please retry\n", stderr);
else
{
fputs("Correct\n", stdout);
break;
}
}
getchar();
return 0;
}

Vortex

  • Member
  • *****
  • Posts: 2030
Re: if you are forced to use only C/C++ on a project...
« Reply #33 on: August 25, 2019, 05:28:16 AM »
Calling __p__iob to get STDIN :

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

include     \masm32\include\windows.inc
include     \masm32\include\kernel32.inc
include     \masm32\include\msvcrt.inc
includelib  \masm32\lib\kernel32.lib
includelib  \masm32\lib\msvcrt.lib

_iobuf STRUCT

    _ptr        DWORD ?
    _cnt        DWORD ?
    _base       DWORD ?
    _flag       DWORD ?
    _file       DWORD ?
    _charbuf    DWORD ?
    _bufsiz     DWORD ?
    _tmpfname   DWORD ?

_iobuf ENDS

FILE TYPEDEF _iobuf

.data

msg     db 'Message output to STDOUT',0

.data?

stdout  dd ?
stdin   dd ?
stderr  dd ?

.code

start:

    call    crt___p__iob
    mov     stdin,eax           ; #define stdin  (&__iob_func()[0])

    mov     ecx,SIZEOF(FILE)

    add     eax,ecx
    mov     stdout,eax          ; #define stdout (&__iob_func()[1])

    add     eax,ecx
    mov     stderr,eax          ; #define stderr (&__iob_func()[2])

    invoke  crt_fputs,ADDR msg,stdout
           
    invoke  ExitProcess,0

END start