Advertisement

c++ / asm problems

Started by September 13, 2001 09:14 PM
3 comments, last by akudoi 23 years, 5 months ago
im trying to implement a crc check for my custom file format for a game.. to make sure nobody hacks them. i cant seem to find any tutorials or code. well i did find this. the exe that came with it works perfectly.. but i cant compile it with msvc++6
  
/* CRC.CPP - Finds the CRC of a file

		by Colin Bennett

(for Borland Turbo C++)

Freely use, distribute, and modify.

How to contact me:
	The Code Room BBS (408.266.3818)
	The Net (cbennett@svpal.org)
	Snail Mail (719 Bicknell Road, Los Gatos, CA 95030-1645)

Set tabs to 3 spaces for proper indentation.

*/


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


unsigned CRC(char *fname)
	{
	unsigned a = 0;
	int c;
	unsigned i;
	FILE *fp;

	fp = fopen(fname, "r");
	if (!fp)
		{
		printf("File not found\n");
		exit(1);
		}

	while (1)
		{
		c = fgetc(fp);
		if (c == EOF)
			break;

		a = a ^ (c << 8);

		for (i = 0; i++ < 8;)
			{
			if (a & 0x8000)
				a = (a << 1) ^ 0x1021;
			else
				a <<= 1;
			}
		}

	fclose(fp);

	asm {
		mov ax,a
		xchg ah,al
		}

	return (_AX);
	}


int main(int argc, char *argv[])
	{
	unsigned crc;

	if (argc != 2)
		{
		printf("Usage: CRC <filename>\n");
		exit(0);
		}


	crc = CRC(argv[1]);

	printf("The CRC is %04X\n", crc);

	return 0;
	}
  
this does exactly what i want / need. if you can help me it would be great! thx. later -------------------------------------------------- [im not gonna start a big discussion on this subject [partly cause im sickend by it], but my condolences to all thoese who were affected by the terrible acts on tuesday.]
Inline ASM is different for each compiler. For msvc++, it''s like this:

  _asm  //NOTE THE UNDERSCORE{     //ASM goes here}  
Advertisement
Also, MSVC doesn''t use the Turbo C++ extensions like ''_AX''.

I think this bit of code will do what you want:
  unsigned retVal = 0;__asm{	mov ax, word ptr[a]	xchg ah, al	mov word ptr[retVal], ax;};return retVal;  


~~~~~~~~~~
Martee
ReactOS - an Open-source operating system compatible with Windows NT apps and drivers
YES!!!!!!!!!!!!!!!!!!!!

im clueless when it comes to asm.

thank you so much Martee! it works perfectly!

later

--------------
keep on coding
Glad to help

~~~~~~~~~~
Martee
ReactOS - an Open-source operating system compatible with Windows NT apps and drivers

This topic is closed to new replies.

Advertisement