News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests

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

jcfuller


Vortex

MinGW 64-bit V5.4.0 :

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.

aw27

Playing with stdio file control structures from msvcrt.dll


#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

Calling __p__iob to get STDIN :


.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

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

Quote from: xanatose 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.
good advices
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