Click here to Skip to main content
15,867,308 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I want to create .com file using C++. I already know the machine code in HEX. For example to print 'A', the machine code is:

B2 41 B4 02 CD 21 CD 20

Then, when i compile, it is automatically created new .com file which will print 'A'. I know this will sounds like compiler.
Are the machine code write to binary files? or how?

Any response will be appreciated.
Thanks,

-Rudy
Posted
Comments
Philippe Mori 12-Jun-15 12:40pm    
Uses a binary editor... If you really want to create that file. Otherwise, it does not make much sense to write machine code like that. At least, you should write assembler code if you want to be really low level.
rudy-peto 13-Jun-15 18:43pm    
Got it... Thanks

1 solution

You can write to any file using binary: just write the bytes from a buffer using a file stream.

The extension is part of the filename, is all - it isn't specific to any "set" of applications.

But...you may want to be careful here: in production this is likely to be classed as "probable virus activity" by antivirus products - so you will need a damn good reason to do this! :laugh:
 
Share this answer
 
Comments
rudy-peto 11-Jun-15 11:29am    
Thanks, but i don't get in a part of 'write the bytes from a buffer'.

FILE *fileBin;
char NameBin[100];

strcpy(NameBin,"D:\\hello.com");
fileBin=fopen(NameBin,"wb");
if(fileBin){
fprintf(fileBin,"B241B402CD21");
}
fclose(fileBin);

like that?
OriginalGriff 11-Jun-15 11:39am    
No, that's writing strings of characters.

unsigned char data[] = { 0xB2, 0x41, 0xB4, 0x02, 0xCD, 0x21};
FILE* file = fopen( "D:\\Temp\Hello.com", "wb" );
fwrite(data, 1, 6, file );

rudy-peto 11-Jun-15 11:46am    
Wow.. Finally i have it! Thank you so much, but still have a problem. Why when i execute the file from cmd, it stops working. The dialog shows "NTVDM.EXE has stopped working".
rudy-peto 11-Jun-15 11:50am    
Upsss.. my mistake, i forgot to add 0xCD, 0x20 to return to Windows.. Thanks...! Really help!

--Rudy
OriginalGriff 11-Jun-15 12:15pm    
You're welcome!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900