flat assembler
Message board for the users of flat assembler.

flat assembler > High Level Languages > Programs without runtime in VS2005

Goto page 1, 2, 3  Next
Author
Thread Post new topic Reply to topic
vivik



Joined: 29 Oct 2016
Posts: 476
Look. This program compiles, and weights only 2kb.

Code:
#define UNICODE
#define _UNICODE

#include <windows.h>
#include <tchar.h>

#pragma comment(lib,"User32.lib")


//void mainCRTStartup(void)
void WinMainCRTStartup(void)
{
        SetThreadAffinityMask(GetCurrentThread(),1);

        return;
}    


And this one doesn't work!

Code:
#define UNICODE
#define _UNICODE

#include <windows.h>
#include <tchar.h>

#pragma comment(lib,"User32.lib")


//void mainCRTStartup(void)
void WinMainCRTStartup(void)
{
    SetThreadAffinityMask(GetCurrentThread(),1);

    LARGE_INTEGER t, f;

    QueryPerformanceCounter(&t);
    QueryPerformanceFrequency(&f);
    double res = (double)t.QuadPart/(double)f.QuadPart;

    return;
}    


The problem is, .c and .cpp are compiled differently, according to this.

As .c, it doesn't allow for variables to appear mid function. As .cpp, the WinMainCRTStartup hack no longer works, it only prints "LIBCMT.lib(wincrt0.obj) : error LNK2019: unresolved external symbol _WinMain@16 referenced in function ___tmainCRTStartup"
Post 09 Feb 2018, 18:30
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 16048
Location: 112 Ocean Avenue, Amityville
Moved to HLL forum.
Post 09 Feb 2018, 18:33
View user's profile Send private message Visit poster's website Reply with quote
vivik



Joined: 29 Oct 2016
Posts: 476
I guess I can bear with writing variables at the beginning of a function, until i write a proper freaking compiler singlehandedly. Yes, in another 5 years, if I'll be alive by then.
Post 09 Feb 2018, 18:33
View user's profile Send private message Reply with quote
rugxulo



Joined: 09 Aug 2005
Posts: 2309
Location: Usono (aka, USA)
Is it really that burdensome to declare variables at the start of a function? AFAIK, no, but I still like classic C89 (and Pascal, et al).

Anyways, C99 is what you want. Apparently MS never considered it a priority until recently (formerly focusing mostly on C++). A quick search shows that MSVC 2013 added support for "C99 variable declarations". So if you're stuck to VS2005 (for whatever reason), you'll have to do without. However, there are several other freeware Windows C compilers out there (I can think of seven, not counting GCC or Clang), so it shouldn't be that big a deal-breaker.
Post 09 Feb 2018, 21:15
View user's profile Send private message Visit poster's website Reply with quote
vivik



Joined: 29 Oct 2016
Posts: 476
Which compiler has good ide and debugger support? Which compiler has edit and continue?
Post 10 Feb 2018, 07:34
View user's profile Send private message Reply with quote
vivik



Joined: 29 Oct 2016
Posts: 476
Ok, my plan is:

Use .c files for all minibenchmarks, or cases when I want to see assembly.

Use .cpp files for everything else.

Write a compiler that doesn't suck in the meantime.
Post 10 Feb 2018, 10:00
View user's profile Send private message Reply with quote
vivik



Joined: 29 Oct 2016
Posts: 476
Look at this code

Code:
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <tchar.h>

#pragma comment(lib,"user32.lib")


//void mainCRTStartup(void)
//void WinMainCRTStartup(void)
//wWinMainCRTStartup
void WinMainCRTStartup(void)
{
        LARGE_INTEGER t, f;
        double res;

        SetThreadAffinityMask(GetCurrentThread(),1);

    QueryPerformanceCounter(&t);
    QueryPerformanceFrequency(&f);
    //res = (double)t.QuadPart/(double)f.QuadPart;

        return;
}    


When I uncomment the last line, it throws "error LNK2019: unresolved external symbol _main referenced in function ___tmainCRTStartup"
Post 10 Feb 2018, 10:16
View user's profile Send private message Reply with quote
vivik



Joined: 29 Oct 2016
Posts: 476
Post 10 Feb 2018, 10:22
View user's profile Send private message Reply with quote
vivik



Joined: 29 Oct 2016
Posts: 476
Hm.

Tried void mainCRTStartup(void)

Resulted in POLINK: error: Unresolved external symbol '_WinMain@16'.

Tried setting breakpoint in the linker options

Resulted in POLINK: error: Unresolved external symbol '_my_main'.
Post 10 Feb 2018, 12:26
View user's profile Send private message Reply with quote
vivik



Joined: 29 Oct 2016
Posts: 476
Let's go back to gcc now.
Post 10 Feb 2018, 12:35
View user's profile Send private message Reply with quote
vivik



Joined: 29 Oct 2016
Posts: 476
Somebody please register there https://forum.pellesc.de/index.php , ask them what's wrong.
Post 10 Feb 2018, 12:55
View user's profile Send private message Reply with quote
vivik



Joined: 29 Oct 2016
Posts: 476
msys2 32bit version still installed 64bit console as well. Don't understand why, nothing 64bit will work here.

I guess, I'll just delete it, it's just a shortcut.

(trying to remember how to remove runtime, or at least where I placed these files) (actually, just relaxing somewhat)
Post 11 Feb 2018, 12:00
View user's profile Send private message Reply with quote
vivik



Joined: 29 Oct 2016
Posts: 476
Here is that watermark I complained about "GCC: (GNU) 6.4.0". I don't remember if it's save to remove the entire .rdata section. The .eh_fram is also trash, probably. It's probably "exceptions handling", even though gcc doesn't support seh. I don't get how it works.

Code:
$ objdump -s main.o

main.o:     формат файла pei-i386

Содержимое раздела .text:
 401000 5589e583 ec48a13c 504000ff d0c74424  U....H.<P@....D$
 401010 04010000 00890424 a1485040 00ffd083  .......$.HP@....
 401020 ec088d45 e8890424 a1405040 00ffd083  ...E...$.@P@....
 401030 ec048d45 e0890424 a1445040 00ffd083  ...E...$.DP@....
 401040 ec048b45 e88b55ec 8945c889 55ccdf6d  ...E..U..E..U..m
 401050 c8dd5dd0 dd45d08b 45e08b55 e48945c8  ..]..E..E..U..E.
 401060 8955ccdf 6dc8dd5d d0dd45d0 def9dd5d  .U..m..]..E....]
 401070 f090c9c3 ffffffff 00000000 ffffffff  ................
 401080 00000000                             ....
Содержимое раздела .rdata:
 402000 4743433a 2028474e 55292036 2e342e30  GCC: (GNU) 6.4.0
 402010 00000000                             ....
Содержимое раздела .buildid:
 403000 00000000 00000000 00000000 02000000  ................
 403010 19000000 1c300000 1c080000 52534453  .....0......RSDS
 403020 29869824 1759d4ae 56a00760 f56530e9  )..$.Y..V..`.e0.
 403030 01000000 00                          .....
Содержимое раздела .eh_fram:
 404000 14000000 00000000 017a5200 017c0801  .........zR..|..
 404010 1b0c0404 88010000 1c000000 1c000000  ................
 404020 e0cfffff 74000000 00410e08 8502420d  ....t....A....B.
 404030 050270c5 0c040400                    ..p.....
Содержимое раздела .idata:
 405000 28500000 00000000 00000000 c4500000  (P...........P..
 405010 3c500000 00000000 00000000 00000000  <P..............
 405020 00000000 00000000 50500000 64500000  ........PP..dP..
 405030 7e500000 9a500000 00000000 50500000  ~P...P......PP..
 405040 64500000 7e500000 9a500000 00000000  dP..~P...P......
 405050 cb014765 74437572 72656e74 54687265  ..GetCurrentThre
 405060 61640000 b5035175 65727950 6572666f  ad....QueryPerfo
 405070 726d616e 6365436f 756e7465 7200b603  rmanceCounter...
 405080 51756572 79506572 666f726d 616e6365  QueryPerformance
 405090 46726571 75656e63 79007804 53657454  Frequency.x.SetT
 4050a0 68726561 64416666 696e6974 794d6173  hreadAffinityMas
 4050b0 6b000000 00500000 00500000 00500000  k....P...P...P..
 4050c0 00500000 4b45524e 454c3332 2e646c6c  .P..KERNEL32.dll
 4050d0 00000000                             ....    


Compiled with: gcc main.c -nostdlib -ffreestanding -mwindows -lkernel32 -o main.o
Post 11 Feb 2018, 12:41
View user's profile Send private message Reply with quote
vivik



Joined: 29 Oct 2016
Posts: 476
This works:

Code:
#define WIN32_LEAN_AND_MEAN
#include <windows.h>

void my_main(void)
{
    MessageBoxA(0, "hi", "hi", 0);

    return;
}    


gcc main.c -nostdlib -ffreestanding -mwindows -o -lkernel32 -luser32 -o main.exe

And this doesn't, throws Segmentation fault. Simply doing the same thing, but in two steps, with object files.

gcc main.c -nostdlib -ffreestanding -mwindows -lkernel32 -luser32 -o main.o

gcc main.o -nostdlib -ffreestanding -mwindows -lkernel32 -luser32 -o main.exe

Tried using this link, nothing http://nullprogram.com/blog/2016/01/31/
Post 11 Feb 2018, 17:35
View user's profile Send private message Reply with quote
vivik



Joined: 29 Oct 2016
Posts: 476
Before I forgot, it's possible to delete sections with this command:

objcopy main.o -R .rdata -R .buildid -R .eh_fram

Need to make main.o work first...
Post 11 Feb 2018, 17:37
View user's profile Send private message Reply with quote
vivik



Joined: 29 Oct 2016
Posts: 476
oh, right

gcc -nostdlib -ffreestanding -mwindows -Os -fno-stack-check -fno-stack-protector -mno-stack-arg-probe -luser32 -c main.c

gcc main.o -nostdlib -ffreestanding -mwindows -fno-stack-check -fno-stack-protector -mno-stack-arg-probe -o main.exe -luser32
Post 11 Feb 2018, 17:40
View user's profile Send private message Reply with quote
vivik



Joined: 29 Oct 2016
Posts: 476
Still hasn't solved this btw. I guess I'll develop with visual studio, and do the final compile with gcc. And will just switch back and forth from time to time.

So many compiler flags... Sections that do who knows what...
Post 13 Mar 2018, 06:11
View user's profile Send private message Reply with quote
donn



Joined: 05 Mar 2010
Posts: 114
Which part doesn't work still? You can't upgrade to a newer VS (no Win10 maybe)? You said you didn't want the runtime I think, I was able to get this to run (your commented line un-commented in VS 2017):

Code:
#define WIN32_LEAN_AND_MEAN 
#include <windows.h> 
#include <stdio.h>


int main(void)
{
        LARGE_INTEGER t, f;
        double res;

        SetThreadAffinityMask(GetCurrentThread(), 1);

        QueryPerformanceCounter(&t);
        QueryPerformanceFrequency(&f);
        res = (double)t.QuadPart/(double)f.QuadPart; 
        printf("Completed test run..");

        return;
}    


I don't think I'll revert VS 2017 to an earlier version, but this guy has a ton of 2005 example links.

If you're looking for something stripped down where you can use WinAPI functions (like QueryPerformanceCounter), you can also link with the .libs as obj files (from asm or with MS's cl compiler and Microsoft's Linker. I posted my build scripts here before, though I use VS2017 libs now and don't compile .c/.cpp anymore.
Post 14 Mar 2018, 03:16
View user's profile Send private message Reply with quote
vivik



Joined: 29 Oct 2016
Posts: 476
@donn

VS2005 doesn't allow .c programs to have variable declarations mid function, it's only for .cpp programs. I will not move declarations up just because this compiler doesn't understand it, it will make my program unreadable.

And you can't make .cpp programs runtimeless with VS2005, at least I couldn't do that.

And even VS2010 lags here (it's bearable though, but I don't want to install it again). And gcc + codelocks + gdb is terrible for actually developing anything. I guess I'll develop in visual studio, and will do the final compile in gcc. Doubt they are super compatible though, but I should start working on it now.

>you can also link with the .libs as obj files (from asm or with MS's cl compiler and Microsoft's Linker.

Hm, right, I should study command line options of those tools... cl.exe and link.exe ...
Post 14 Mar 2018, 09:20
View user's profile Send private message Reply with quote
vivik



Joined: 29 Oct 2016
Posts: 476
If my only problem is "I don't want to keep variable declarations at the beginning of a function", I might just write a program that will move them up there. Something that parses "cpp" code, and generates c code.

Might as well generate c from something else, while I'm at it. Always wanted to get rid of those semicolons.
Post 23 Mar 2018, 00:14
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  
Goto page 1, 2, 3  Next

< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2018, Tomasz Grysztar.

Powered by rwasa.