|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Get inside! Sample the range of functionality easily built with JMSL Library for Time Series Data Analysis, Heat Maps, Portfolio Optimization, Monte Carlo Simulation, Stock Price Charting and more. Download Now! |
|
#1
|
|||
|
|||
|
Converting
Does anybody know how to convert this C++ code to assembly?
include "translat.h" void TranslateBuffer( char * buf, unsigned count, unsigned char eChar ) { __asm { mov esi,buf ; set index register mov ecx,count /* set loop counter */ mov al,eChar L1: xor [esi],al inc esi Loop L1 } } #include <iostream> #include <fstream> #include "translat.h" using namespace std; void main( int argcount, char * args[] ) { // Read input and output files from the command line. if( argcount < 3 ) { cout << "Usage: encode infile outfile" << endl; return; } const int BUFSIZE = 2000; char buffer[BUFSIZE]; unsigned int count; // character count unsigned char encryptCode; cout << "Encryption code [0-255]? "; cin >> encryptCode; ifstream infile( args[1], ios::binary ); ofstream outfile( args[2], ios::binary ); cout << "Reading " << args[1] << " and creating " << args[2] << endl; while (!infile.eof() ) { infile.read(buffer, BUFSIZE ); count = infile.gcount(); __asm { lea esi,buffer mov ecx,count mov al, encryptCode L1: xor [esi],al inc esi Loop L1 } // asm outfile.write(buffer, count); } } |
|
#2
|
|||
|
|||
|
Yeah, sure thing. Do you want it as a standalone MASM32 source or as inline code?
Lemme know and I'll get back to you. Also, do you just want it converted, or do you wanna know how to do it? It'll take me about 10-15 mins after I know what you're after. [EDIT:] scratch the above line. I still can't find out how to access command line arguments under winblows. Once upon a time it was just a matter of looking in the PSP (program segment prefix), but alas I can't quite work it out at the moment. However, I can give you some advice for this type of task in the future - many IDE's will give you the option of viewing the assembly source code of a program written in C/C++, however I can't even remember which ones or how to do it, so I'll leave that option alone. Have a look for OllyDbg - Olly Debugger. You can step through your program asm line by asm line. You'll have the code in no time flat. Don't worry - it's smart enough to turn it into something readable by humans. I use it all the time to convert C stuff into asm. I'm just feeling a little too lazy to compile the code, trace, then assemble it right now. Sorry. S. |
|
#3
|
|||
|
|||
|
Quote:
compiler?
__________________
-- I'll provide you with reference points; if they dont work, refer to something else. If you process text, this might make your life a little easier. |
|
#4
|
|||
|
|||
|
g++ -S yourfile.cpp
|
|
#5
|
|||
|
|||
|
Against my better judgment, I'll post what I bashed out while waiting for a friend to arrive. Perhaps you'll get some mileage from it. - It's a 3072 byte executable inc padding & without compression.
Tested against a 610 MB iso file. Optimizations to be had include a) the inner loop - xor multiple bytes at once. b) size of read buffer - make it larger. I reamed it a little to 8kb, but it could still be much larger. h__p://www.savefile.com/files/1547685 (and yes, it did take a little longer than "10-15 mins" )[EDIT]: Decided to have another quick play. I increased the size of the read-buffer to 32kb and made the inner-loop xor 4 bytes at once. Result? The code runs (as was to be expected) much faster now. A 4.5 gb ISO was encrypted from one drive and saved on a different one in ~2m45s, which is a touch under 28 MB/s (about the speed of the drive the output was written to) Code:
encryptOuterLoop: invoke ReadFile, [inputFileHandle], ADDR readBuffer, bufLength, ADDR bytesActuallyRead, NULL cmp [bytesActuallyRead], 0 je readingDone mov esi, OFFSET readBuffer mov ecx, [bytesActuallyRead] shr ecx, 2 ; ecx = ecx / 4 inc ecx ; do 1 extra long in case [bytesActuallyRead]%4 != 0 ; this causes 1st 4 bytes of input filename to be ; overwritten on all runs except the last one. ; (unless filesize is a multiple of 32768), when it ; will also overwrite these 4 bytes mov al, encryptChar mov ah, al mov bx, ax shl eax, 16 mov ax, bx mov edx, 4 encryptInnerLoop: xor [esi], eax add esi, edx ; doing 4 bytes at a time, instead of 1 dec ecx jnz encryptInnerLoop invoke WriteFile, [outputFileHandle], ADDR readBuffer, [bytesActuallyRead], ADDR bytesActuallyWritten, NULL jmp encryptOuterLoop readingDone: I've bolded all the parts that need to be changed in order to process multiple bytes at once. The inputFileName buffer occupies the memory immediately following readBuffer. The read buffer is 32768 bytes long. But by dividing 32768 by 4 and then adding 1, we end up with (32768/4)+1 = 8193 dwords to be processed. 8193 * 4 = 37772, yet the buffer is only 32768 bytes long. This is why the first 4 bytes of the inputFileName are overwritten. The input filename is no longer needed after the file is opened, so it has no ill-effect in this case, but is worth pointing out. One last thing that comes to mind: filenames with spaces aren't supported unless you drag'n'drop files onto encrypt.exe, which requires that the output file - empty or not, already exists. - select input file, ctrl-select output file, drag'n'drop onto encrypt.exe) |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > C Programming > Converting |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|