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

jcfuller

  • Member
  • **
  • Posts: 191
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: 2377
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: 2583
  • 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: 2377
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

xanatose

  • Member
  • ***
  • Posts: 398
Re: if you are forced to use only C/C++ on a project...
« Reply #34 on: March 10, 2020, 07:10:25 AM »
Usual rules of thumb.
- Measure and don't assume.
- Use the best algorithm for the given problem. And you learn this by trying many algorithms. 
- Compile without any optimization to debug it troughly. It will be very slow. But it will give your debugger for the debugger to catch the errors you made while drunk at 2am in the morning. :)
- Compile it in release with whatever compiler you have and debug it with whatever debugger you have.
Now you have a product. But if you want to go one extra step.
Then, and only then, you look for a better optimizing compiler. In this case, I would go for Intel C++. It takes ages to do an optimized compile. But it does provide good results for intel CPU (Not sure if the best results for AMD).

Normally it will be a compromise between getting the best running time and getting the best development time.

daydreamer

  • Member
  • *****
  • Posts: 1387
  • building nextdoor
Re: if you are forced to use only C/C++ on a project...
« Reply #35 on: March 10, 2020, 10:12:44 PM »
Usual rules of thumb.
- Measure and don't assume.
- Use the best algorithm for the given problem. And you learn this by trying many algorithms. 
- Compile without any optimization to debug it troughly. It will be very slow. But it will give your debugger for the debugger to catch the errors you made while drunk at 2am in the morning. :)
- Compile it in release with whatever compiler you have and debug it with whatever debugger you have.
Now you have a product. But if you want to go one extra step.
Then, and only then, you look for a better optimizing compiler. In this case, I would go for Intel C++. It takes ages to do an optimized compile. But it does provide good results for intel CPU (Not sure if the best results for AMD).

Normally it will be a compromise between getting the best running time and getting the best development time.
good advices
Quote from Flashdance
Nick  :  When you give up your dream, you die
*wears a flameproof asbestos suit*
Gone serverside programming p:  :D
I love assembly,because its legal to write
princess:lea eax,luke
:)