flat assembler
Message board for the users of flat assembler.

Index > Main > upper() and lower()

Author
Thread Post new topic Reply to topic
Hayden



Joined: 06 Oct 2005
Posts: 132
Hayden
this may sound stupid but I was wondering if anyone had some optimized code for upper/lower string conversions... this is the algo i usualy use... pesuedo code...
Code:
TO_LOWER = 'a' - 'A'

; convert string to lower case
    = x + (x >= 'A' and x <= 'Z') * TO_LOWER

; convert string to upper case
    = x - (x >= 'a' and x <= 'z') * TO_LOWER
    


I want to check for 1 of 3 possible ascii key presses, I'm useing int 16h and need to be capslock aware... I hav'nt seen upper/lower functions in asm before so i'm looking for a few pointers.

_________________
New User.. Hayden McKay.
Post 16 Jun 2007, 16:49
View user's profile Send private message Reply with quote
LocoDelAssembly
Your code has a bug


Joined: 06 May 2005
Posts: 4633
Location: Argentina
LocoDelAssembly
Here my branchless version (which doesn't means that it's the most optimized in existence):
Code:
upper:; AL = char to convert
  mov  dl, al

  sub  al, 'a'
  cmp  al, 'z'-'a'+1
  salc

  and  al, 'A'-'a'
  add  al, dl

  ret


lower:; AL = char to convert
  mov  dl, al

  sub  al, 'A'
  cmp  al, 'Z'-'A'+1
  salc

  and  al, 'a'-'A'
  add  al, dl

  ret    


[edit]
Code:
upperLetter: ; AL = letter to convert
  and al, not $20
  
  ret

lowerLetter: ; AL = letter to convert
  or  al, $20

  ret    


Added Goplat's suggestion. Check below for remarks.[/edit]


Last edited by LocoDelAssembly on 16 Jun 2007, 22:49; edited 1 time in total
Post 16 Jun 2007, 18:46
View user's profile Send private message Reply with quote
Goplat



Joined: 15 Sep 2006
Posts: 181
Goplat
If all you're doing is checking for specific letters, rather than actually converting to lower/uppercase, you don't need to do real case mapping, a simple bit flip will do the trick. Lowercase letters have the same codes as uppercase with bit 5 set, so for example
Code:
        or      al,20h
      cmp     al,'q'
    je      q_pressed
    

will jump on either 'Q' or 'q'. Of course, punctation and such will get changed - eg, '@' becomes '`' - but none of them become letters, so the check is still safe.
Post 16 Jun 2007, 21:51
View user's profile Send private message Reply with quote
LocoDelAssembly
Your code has a bug


Joined: 06 May 2005
Posts: 4633
Location: Argentina
LocoDelAssembly
I can't understand why I thought that trick was unsafe (without previous checkings) even on these cases :S

Well, maybe because I saw it about 6 years ago when I started with programming and in all these years I forgot to think again about it Embarassed

Thanks for pointing out this Goplat!!
Post 16 Jun 2007, 22:56
View user's profile Send private message Reply with quote
Hayden



Joined: 06 Oct 2005
Posts: 132
Hayden
thanks guys

_________________
New User.. Hayden McKay.
Post 19 Jun 2007, 14:51
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2020, Tomasz Grysztar. Also on YouTube, Twitter.

Website powered by rwasa.