SunQuest
           C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
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  
Old May 6th, 2008, 11:58 PM
dirtysouth121 dirtysouth121 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2008
Posts: 1 dirtysouth121 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 15 m 34 sec
Reputation Power: 0
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);
}

}
Comments on this post
sizablegrin disagrees: Please read the "New Users - HOW TO POST A QUESTION - READ THIS FIRST" thread.

Reply With Quote
  #2  
Old May 7th, 2008, 12:10 AM
SimonB2 SimonB2 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2008
Posts: 7 SimonB2 Negative: is most likely a SPAMMER and a traitor to the cause. 
Time spent in forums: 3 h 47 m 58 sec
Reputation Power: 0
Red face

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.
Comments on this post
sizablegrin disagrees!

Reply With Quote
  #3  
Old May 7th, 2008, 06:26 AM
L7Sqr L7Sqr is online now
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jan 2004
Location: Constant Limbo
Posts: 591 L7Sqr User rank is Second Lieutenant (5000 - 10000 Reputation Level)L7Sqr User rank is Second Lieutenant (5000 - 10000 Reputation Level)L7Sqr User rank is Second Lieutenant (5000 - 10000 Reputation Level)L7Sqr User rank is Second Lieutenant (5000 - 10000 Reputation Level)L7Sqr User rank is Second Lieutenant (5000 - 10000 Reputation Level)L7Sqr User rank is Second Lieutenant (5000 - 10000 Reputation Level)L7Sqr User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 13 h 59 m 28 sec
Reputation Power: 100
Send a message via AIM to L7Sqr
Quote:
Originally Posted by dirtysouth121
Does anybody know how to convert this C++ code to assembly?

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.

Reply With Quote
  #4  
Old May 7th, 2008, 07:16 AM
spoon! spoon! is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2006
Posts: 247 spoon! User rank is Second Lieutenant (5000 - 10000 Reputation Level)spoon! User rank is Second Lieutenant (5000 - 10000 Reputation Level)spoon! User rank is Second Lieutenant (5000 - 10000 Reputation Level)spoon! User rank is Second Lieutenant (5000 - 10000 Reputation Level)spoon! User rank is Second Lieutenant (5000 - 10000 Reputation Level)spoon! User rank is Second Lieutenant (5000 - 10000 Reputation Level)spoon! User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 3 Days 8 h 15 m 23 sec
Reputation Power: 77
g++ -S yourfile.cpp

Reply With Quote
  #5  
Old May 9th, 2008, 08:35 AM
SimonB2 SimonB2 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2008
Posts: 7 SimonB2 Negative: is most likely a SPAMMER and a traitor to the cause. 
Time spent in forums: 3 h 47 m 58 sec
Reputation Power: 0
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)

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Converting


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway