C++ with embedded ASM Question 
Author Message
 C++ with embedded ASM Question

This may be more for a good C++ news group, but haven't gotten much help on the
ones I have posted to... someone here wanna take a shot?

Below is a small encryption program I am playing with.  It work fine as is, but
I would like someone to explain why the fstream had to be opened this way (with
seperate in/out streams and not just one stream opened i/o)  
- also, I took an asm class several years ago, but it only went as far as 8088
asm registars/instructions.  Can someone clarify for me:
are the registers cx and ecx totally different spaces, or is ecx just an
extension of the smaller cx.  Are the _asm routines here "safe", or am I missing
something?

specific questions:
why could one stream not be opened as:
        ioFile.open(argc[i],ios::binary | ios::in | ios::out | ios::nocreate);  
why do both in this version have to be opened with
                        | ios::in | ios::out
                when each is only used one way?

/****
8/26/99
 simple encryption program - base routines for
  an MFC dialog-based prog. Command-line usage:  
  crypt <switch /d or /e> filename,<filename>, <filename>.....
****/
#include <fstream.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <conio.h>

char Encrypt(char);
char Decrypt(char);

void main(int argv, char* argc[])
{
        ifstream inFile;
        ofstream otFile;
        char ch, cMode, strTemp[25];

        strcpy(strTemp, argc[1]);
        cMode = toupper((strTemp[strlen(strTemp) -1]));

                for(int i = 2; i < argv; i++)
        {      
                inFile.open(argc[i],ios::binary | ios::in |
                                       ios::out | ios::nocreate);

                if(!inFile.fail())
                {
                        otFile.open(argc[i],ios::binary | ios::in |
                                            ios::out |ios::nocreate);
                        if(!otFile.fail())
                        {
                                if(cMode == 'E')
                                {
                                        while(inFile.get(ch))
                                                otFile.put(Encrypt(ch));
                                        printf("Finished encrypting file: %s\n", argc[i]);
                                }
                                else if(cMode == 'D')
                                {
                                        while(inFile.get(ch))
                                                otFile.put(Decrypt(ch));
                                        printf("Finished decrypting file: %s\n", argc[i]);
                                }
                                else
                                {
                                        printf("Unrecognized flag: %s\n", argc[1]);
                                        break;
                                }
                        }
                        else
                                printf("Unabe to open %s for output\n", argc[i]);
                }
                else
                        printf("Unabe to open %s for input\n", argc[i]);

                inFile.clear();
                inFile.close();
                otFile.clear();
                otFile.close();
        }
                printf("Done..\n\tPress any key to quit..");
                _getch();

Quote:
}

//////////////////////////////////////////////////////////////////
char Encrypt(char c)
{
        _asm
        {
                push  cx    ;
                mov   cl,  2;
                ror       c, cl;        
                not       c    ;
                pop   cx    ;
        }
        return c;
Quote:
}

////////////////////////////////////////////////////////////////////
char Decrypt(char c)
{
        _asm
        {
                push  cx    ;
                mov   cl,  2;
                not    c    ;
                rol    c, cl;
                pop   cx    ;
        }
        return c;


Wed, 13 Feb 2002 03:00:00 GMT  
 C++ with embedded ASM Question

Quote:

> This may be more for a good C++ news group, but haven't gotten much help on the
> ones I have posted to... someone here wanna take a shot?

> Below is a small encryption program I am playing with.  It work fine as is, but
> I would like someone to explain why the fstream had to be opened this way (with
> seperate in/out streams and not just one stream opened i/o)

  I have seen that question answered for similar situations
in comp.lang.c++;  You really should ask it there not here.
You will get more help there if you ask just one C++ question
in each post.  Asking a mix of unrelated C++ and ASM questions
in the same post isn't productive.

Quote:
> - also, I took an asm class several years ago, but it only went as far as 8088
> asm registars/instructions.  Can someone clarify for me:
> are the registers cx and ecx totally different spaces, or is ecx just an
> extension of the smaller cx.

  ecx is an extension of cx (cx is the bottom half of ecx).

Quote:
> Are the _asm routines here "safe", or am I missing
> something?

  They look safe, but I don't know the rules for asm()
in whatever compiler you might be using.

Quote:
> char Encrypt(char c)
> {
>         _asm
>         {
>                 push  cx    ;
>                 mov   cl,  2;
>                 ror       c, cl;
>                 not       c    ;
>                 pop   cx    ;
>         }
>         return c;
> }

  The usual reason to put ASM code in a C++ program is that
a good ASM programmer can do significantly better than the
C compiler would at code generation, and the situation
calls for optimum speed.

  I doubt the situation calls for optimum speed, and I
really doubt that the above code would achieve it.
Inline ASM for that task is more trouble than it is
worth.  Just write it in C.

char Encrypt(unsigned char c) {
  return (~ ((c<<6)+(c>>2)) ); }

For Decrypt just reverse the 6 and the 2.  BTW, the
"unsigned" is important.  If you don't have that there
you must mask the shifted values.

This C code is almost portable.  It assumes a char is
eight bits and it assumes the compiler can convert
char to unsigned char and back without any changes in
bitpattern.  Those are true in any compiler that might
accept your inline asm;  But might not be truely
portable.
--
http://www.erols.com/johnfine/
http://www.geocities.com/SiliconValley/Peaks/8600/



Wed, 13 Feb 2002 03:00:00 GMT  
 C++ with embedded ASM Question

John -
   thanks.
  obviously this program (segment) isn't of much importance other than as an
excercise for mysef.  I had never embeded assembler before, and I wasn't having
much luck doing it in C++.  
Now that I have isolated the problem to the streams, I'll try it again.

        kevin

Quote:

>  I have seen that question answered for similar situations
>in comp.lang.c++;  You really should ask it there not here.
>You will get more help there if you ask just one C++ question
>in each post.  Asking a mix of unrelated C++ and ASM questions
>in the same post isn't productive.

>  ecx is an extension of cx (cx is the bottom half of ecx).

>  They look safe, but I don't know the rules for asm()
>in whatever compiler you might be using.

>> char Encrypt(char c)
>> {
>>         _asm
>>         {
>>                 push  cx    ;
>>                 mov   cl,  2;
>>                 ror       c, cl;
>>                 not       c    ;
>>                 pop   cx    ;
>>         }
>>         return c;
>> }

>  The usual reason to put ASM code in a C++ program is that
>a good ASM programmer can do significantly better than the
>C compiler would at code generation, and the situation
>calls for optimum speed.

>  I doubt the situation calls for optimum speed, and I
>really doubt that the above code would achieve it.
>Inline ASM for that task is more trouble than it is
>worth.  Just write it in C.

>char Encrypt(unsigned char c) {
>  return (~ ((c<<6)+(c>>2)) ); }

>For Decrypt just reverse the 6 and the 2.  BTW, the
>"unsigned" is important.  If you don't have that there
>you must mask the shifted values.

>This C code is almost portable.  It assumes a char is
>eight bits and it assumes the compiler can convert
>char to unsigned char and back without any changes in
>bitpattern.  Those are true in any compiler that might
>accept your inline asm;  But might not be truely
>portable.
>--
>http://www.erols.com/johnfine/
>http://www.geocities.com/SiliconValley/Peaks/8600/



Thu, 14 Feb 2002 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. embedding asm in c++. OS = linux

2. Question about asm en c++

3. Very complex Python/C++ embedding/extension question(s)

4. Embedding Python in C++ application: Question #1

5. question about embedding Python using C++ and SWIG

6. 2 embedding in C++ questions

7. Special question to embedding Python into C++

8. Question About Embedding into C++

9. Newbie question: embedding python in a c++ main

10. question about character encodings with Tcl interpreter embedded in C++

11. question about character encodings with Tcl interpreter embedded in C++

12. Passing arguments from C++ to ASM with Borland C++

 

 
Powered by phpBB® Forum Software