C/MASM Relationship [they love each other] is better Static or Dynamic ?

Started by frktons, May 27, 2010, 09:45:35 AM

Previous topic - Next topic

frktons

Well, I used the suggested modifications to the previous code and:

#include <stdio.h>
#include <stdlib.h>

#pragma comment(lib, "\\masm32\\lib\\msvcrt.lib")

int main(void)
{
   char str1[5] = "ABCD";
   char str2[5] = "XXXX";
   char *p_str1, *p_str2;
   
   p_str1 = str1;
   p_str2 = str2;

   printf("original string = %s\n",str1);

__asm

{
  mov edx, p_str1         ; Pointer to first 4 bytes string - the one to reverse
  mov ebx, p_str2         ; Pointer to second 4 bytes string - the one that holds the result
  mov eax, [edx]
  bswap eax
  mov [ebx], eax
}

   printf("reversed string = %s\n",str2);
   getchar();

   return 0;
}

void __cdecl mainCRTStartup(void)
{
    exit(main());
}


compiles 3 Kb size EXE.  :U

The only thing I don't understand is:
Why the pgm stops with getchar() and I have to close the console windows
forcing it, and the pgm doesn't accept any char from the keyboard?

The same happens with #pragma comment(lib, "msvcrt.lib") that I copied
from vc 2010 folder. Something should be missing...
If I remove the getchar() function the pgm is ok.  ::) and it compiles 2 Kb EXE.
Mind is like a parachute. You know what to do in order to use it :-)

Arash


frktons

Quote from: Arash on May 27, 2010, 10:04:52 PM
see my previous post for solving getchar problem

Thanks Arash! now the pgm is 2 Kb and works fine  :U

This looks like a good start for making C/MASM programs small and fast  :P

Maybe you can help me solve this problem too:
http://www.masm32.com/board/index.php?topic=14042.0

Thanks

Frank
Mind is like a parachute. You know what to do in order to use it :-)

clive

Quote from: frktons
The same happens with #pragma comment(lib, "msvcrt.lib") that I copied
from vc 2010 folder. Something should be missing...

Libraries that link to DLL's contain no real code (an indirect jmp perhaps), so it doesn't really matter which library you use, the code loaded will be in \WINDOWS\SYSTEM32\MSVCRT.DLL (typically, or which every is better situated/pathed)
It could be a random act of randomness. Those happen a lot as well.

frktons

Quote from: clive on May 27, 2010, 11:05:48 PM
Quote from: frktons
The same happens with #pragma comment(lib, "msvcrt.lib") that I copied
from vc 2010 folder. Something should be missing...

Libraries that link to DLL's contain no real code (an indirect jmp perhaps), so it doesn't really matter which library you use, the code loaded will be in \WINDOWS\SYSTEM32\MSVCRT.DLL (typically, or which every is better situated/pathed)

The Pelle's C had this flaw about getchar()... well now it's ok.
Any idea why this project builds but won't run?
http://www.masm32.com/board/index.php?topic=14042.msg111608#msg111608
Mind is like a parachute. You know what to do in order to use it :-)

ecube

Let me give you an important tip, that I see plagued on the internet when it comes to visual studio 2003+ static lib linking. In those compilers microsoft introduced a new buffer overflow feature which is compiled in by default. As a result when you try and use the library in say masm you get errors talking about cookies and security and related. Now you can fix this either by linking to some random dll(forget the name) or by recompiling the library using the GS- flag in the linker settings. Theres also a checkbox in visual studio 2008 atleast that lets you uncheck buffer overflow protection.

clive

Oddly enough if I use MSVC 2.0, the EXE is also 3KB, and it also crashes on the getchar(), looks to be a problem in how it references __iob, and the fact that nothing is getting initialized.

I tried MSVC 1.0 (32-bit), but things got somewhat bigger. Historically as Microsoft moved from 2.x, 4.x, 5.x, 6.x the executables got bigger, and bigger, I remember building a couple of control panel apps with the older versions because the size practically halved.
It could be a random act of randomness. Those happen a lot as well.

hutch--

Something funny, I supply a couple of ancient SDK applications written in C under the licence that Microsoft have for redistributable code and by building them with the VCTOOLKIT and the libraries that were current when it was released, they are heaps smaller than the originals. I remember when MFC was first introduced that functionally identical apps from the SDK were over 3 times the size and were a lot slower than the C originals.

I have never had any beef with the VC compilers but the libraries were always a bit suspect so you tend to pick your way around them to get the results you want.
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

frktons

I'll try to stick with API and MASM, using a minimalistic approach to
standard C libraries.

By the way, if a pgm is 30 Kb bigger, it doesn't really matter, nowadays
we got Gbs of memory, don't we?
I am more concerned about speed, this could be a problem if I plan to
have millions of cycles to be done for some projects.

As far as C is just a tool to undestand how Windows APIs work, it is
worth to spend some time over it.  :P

Frank
Mind is like a parachute. You know what to do in order to use it :-)

GregL

frktons,

The reason getchar() isn't working correctly is because you are replacing the CRT startup code with nothing.


void __cdecl mainCRTStartup(void)
{
    exit(main());
}


Some of the CRT functions require the initialization that is done in the startup code to work properly.  I would recommend that you just do it the normal way.  Like you say, 30 Kb bigger, who cares?




Ghandi

Personally, i believe it's a matter of 'horses for courses'. I'm comfortable blending the two, whether it be an asm file in a VC project or a VC lib/obj in a MASM project, which i like to do sometimes. But that is me, there are others who would be more comfortable coding dlls to use with the other language or even keeping it to a single language. The answer is relative to each person who asks it, the only person who can truly answer it for each person is themselves.

HR,
Ghandi

GregL


frktons

Quote from: Greg Lyon on June 02, 2010, 09:23:33 PM
You are absolutely right Ghandi.


I agree as well, this is the approach I'm using at the moment.  :P
Mind is like a parachute. You know what to do in order to use it :-)