开发者

C++ with inline assembly, whats wrong?

First please use easy words, as i'm not really good in english ;)

An开发者_开发知识库d now the problem:

I wanted to code a program which can encrypt a text of mine with an easy Caesar algorithm. That means that the alphabetic character becomes a later character in the alphabet, but my program does not begin at the beginning of the alphabet after Z. Now the code:

void Entschlüsseln(char Text[50], int Schlüssel)
{
    char Entschlüsselt[sizeof(Text)];
    for (int x = 0; x < sizeof(Text)-1; x++)
    {
        Entschlüsselt[x] = '\0';
    }
        char Zeichen;
    for (int i = 0; i < sizeof(Text)-1; i++)
    {
        if (Text[i] != '\0')
        {
            Zeichen = Text[i];
            for (int j = 0; j < Schlüssel; j++)
            {
                _asm
                {
                        mov al, Zeichen   
                        cmp al, 90
                        jb Großbuchstabe
                        mov al, Zeichen
                        sub al, 32
                        mov Zeichen, al
                    Großbuchstabe:
                        inc Zeichen
                        mov al, Zeichen
                        cmp al, 90
                        ja Anfang_Alphabet
                        jmp Ende
                    Anfang_Alphabet:
                        mov Zeichen, 65
                    Ende:
                }   
            }
            Entschlüsselt[i] = Zeichen;
        }
    }
    cout << endl << Entschlüsselt;
}

i hope it's ok and you can help me


This would be much easier to implement in pure C++, without assembly.

Zeichen = tolower(Text[i]);
Zeichen += Schlussel; // Note, the C++ standard does not guarantee that non-ASCII characters such as U-umlaut are allowed in identifiers
if (Zeichen > 'Z') Zeichen -= 26;
Entschlusselt[i] = Zeichen;


Probably you only want to change the character if it is really a letter, so that space and other characters stay. And you are using C++, therefore it is easier to work with a std::string instead of a char[].

#include <iostream>
#include <string>

using std::string;

void caesar_encode(std::string &text, int schluessel) {

  for (size_t i = 0; i < text.length(); i++) {
    char zeichen = text[i];

    int buchstabe = -1; // -1 == unbekannt, 0 = a, 1 = b, ... 25 = z
    if ('A' <= zeichen && zeichen <= 'Z') {
      buchstabe = zeichen - 'A';
    } else if ('a' <= zeichen && zeichen <= 'z') {
      buchstabe = zeichen - 'a';
    }

    char codiertes_zeichen = zeichen;
    if (buchstabe != -1) {
      int codierter_buchstabe = (buchstabe + schluessel) % 26;
      char alphabet_anfang = zeichen - buchstabe; // 'A' oder 'a'
      codiertes_zeichen = alphabet_anfang + codierter_buchstabe;
    }

    text[i] = codiertes_zeichen;
  }
}

int main() {
  string s("Hallo, Welt");
  caesar_encode(s, 13);
  std::cout << s << "\n";
  return 0;
}

Just try to understand this code, it should be possible.


I think the wrong code is this line:

cmp al, 90
jb Großbuchstabe

Since 90 (which in ASCII is a Z) is a Großbuchstabe, you should not subtract 32 from it. Make it:

cmp al, 90
jbe Großbuchstabe
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜