RosettaCodeData/Task/String-case/68000-Assembly/string-case.68000

61 lines
2.2 KiB
Plaintext

UpperCase:
;input: A0 = pointer to the string's base address.
;alters the string in-place.
MOVE.B (A0),D0 ;load a letter
BEQ .Terminated ;we've reached the null terminator.
CMP.B #'a',D0 ;compare to ascii code for a
BCS .overhead ;if less than a, keep looping.
CMP.B #'z',D0 ;compare to ascii code for z
BHI .overhead ;if greater than z, keep looping
AND.B #%1101111,D0 ;this "magic constant" turns lower case to upper case, since they're always 32 apart.
.overhead:
MOVE.B D0,(A0)+ ;store the letter back and increment the pointer.
;If this isn't an alphabetical character, D0 won't change and this store won't affect the string at all.
;If it was a letter, it will have been changed to upper case before storing back.
BRA UpperCase ;next letter
.Terminated:
RTS
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
LowerCase:
MOVE.B (A0),D0 ;load a letter
BEQ .Terminated ;we've reached the null terminator.
CMP.B #'A',D0 ;compare to ascii code for A
BCS .overhead ;if less than A, keep looping.
CMP.B #'Z',D0 ;compare to ascii code for Z
BHI .overhead ;if greater than Z, keep looping
OR.B #%00100000,D0 ;this "magic constant" turns upper case to lower case, since they're always 32 apart.
.overhead:
MOVE.B D0,(A0)+ ;store the result and get ready to read the next letter.
BRA LowerCase ;next letter
.Terminated:
RTS
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
ToggleCase:
MOVE.B (A0),D0 ;load a letter and inc the pointer to the next letter
BEQ .Terminated ;we've reached the null terminator.
MOVE.B D0,D1 ;copy the letter
AND.B #%11011111 ;convert the copy to upper case so we can check it.
CMP.B #'A',D1 ;compare to ascii code for A
BCS overhead ;if less than A, keep looping.
CMP.B #'Z',D1 ;compare to ascii code for Z
BHI overhead ;if greater than Z, keep looping
EOR.B #%00100000,D0 ;swaps the case of the letter
overhead:
MOVE.B D0,(A0)+ ;store the result
BRA ToggleCase ;next letter
.Terminated:
RTS