Valentin Emil Cudelcu
Valentin Emil Cudelcu

Reputation: 679

x86 Assembly: Change an UpperCase letter to a LowerCase letter

I've got this code which should change an uppercase letter to a lowercase letter:

void tolower(char *text)
{
    _asm
    {
        mov esi, text;
        mov ecx, 0;
        mov bl, 32;
      opakuj:
        cmp [esi + ecx], 0;
        je konec;
        cmp [esi + ecx], 97;
        jbe dolower;
        add ecx, 1;
        jmp opakuj;

      dolower:
        mov [esi + ecx], bl;
        add ecx, 1;
        jmp opakuj;
      konec:
    }
}

mov [esi + ecx], bl doesn't work. I get an

access violation writing error

What am I doing wrong?

P.S.: I can't use another array or pointer or something like that. I have to rewrite that char.

Upvotes: 1

Views: 3293

Answers (2)

zx485
zx485

Reputation: 29022

One solution would be isolating the lowercase chars and clearing or setting the bit 0x20 with an AND(uppercase) or OR(lowercase), respectively, like described in this SO answer: "How to access a char array and change lower case letters to upper case, and vice versa".

void tolower(char *text)
{
  _asm
  {
    mov esi, text;
    mov ecx, -1;
  opakuj:
    inc ecx;
    cmp [esi + ecx], 0;       // end of string
    je konec;
    cmp [esi + ecx], 65;      // lower bound for uppercase chars
    jb opakuj;
    cmp [esi + ecx], 90;      // upper bound for uppercase chars
    ja opakuj;
    ; and [esi + ecx], 223;   // 11011111 binary - sets uppercase
    or [esi + ecx], 32;       // 00100000 binary - sets lowercase
    jmp opakuj
  konec:
  }
}

Upvotes: 2

WhatsUp
WhatsUp

Reputation: 1626

Changing mov [esi + ecx], bl to add [esi + ecx], bl corrects the error.

But your program is still not robust. (E.g. what if the input is a number character?)

Upvotes: 0

Related Questions