Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
toUpper and toLower functions
Hey all. I'm not anything like a good Z80 programmer, but I'm learning snippets. I wanted a fast way to flip between lower and upper case characters. This can of course be done by +/- 32 to the ascii code. This is of course, the same as setting or resetting bit 5 of the character, which assembly is better at (you could also perhaps use the bAND function listed in How-To). Anyway, I found these two to work. I wasn't sure if resetting the carry flag was necessary or not in all cases, so cleared it to be on the safe side.

It is also with wry amusement that I note that Boriel has LCase and UCase functions in his library as well that I hadn't noticed. #include <lcase.bas> (and/or ucase) would let you use them. They lower/upper a whole string. His functions also use OR and AND instead of set and res. Irritatingly, though I was sure that bit set and bit reset instructions ought to be faster...they aren't. According to the documentation I have, OR and AND take 7 T states when run on the accumulator. SET and RES take 8. Even though that's pretty baffling, since OR can be used as a set of many bits, and AND can be used as a reset of many bits in one go - the documentation says they are faster. Boriel's code almost always is going to be better than mine - unless you happen to have a single character as an ascii code instead of a string. I'll leave these here then as potentially useful for dealing with things like Y/N? responses for a single letter.

REM Change a single character code to lower case
REM Anything except an UPPERCASE letter is returned unchanged.
FUNCTION FASTCALL toLower (letter as uByte) as uByte
    ;AND A ; clear carry flag (unnecessary)
    CP 65
    RET C
    CP 90
    RET NC
    ; set 5,a (slow)
        OR 32 ; faster
    end asm

REM Change a single character code to Upper Case
REM Anything except a lowercase letter is returned unchanged.
FUNCTION FASTCALL toUpper (letter as uByte) as uByte
       ; AND A ; Clear carry flag (unnecessary)
    CP 97
    RET C
    CP 122
    RET NC
        ; res 5,a (slow)
        AND 224 ; faster
    end asm    

Forum Jump:

Users browsing this thread: 1 Guest(s)