Assembly language > Assembly discussions

Converting a string to uppercase

(1/1)

Vortex:
Hello,

Here is a quick example :


--- Code: ---.386
.model flat,stdcall
option casemap:none

ExitProcess PROTO :DWORD
printf PROTO C :DWORD,:VARARG

includelib  \PellesC\lib\Win\kernel32.lib
includelib  msvcrt.lib

.data

string db 'This is a test.',0

.code

UpperCase : ; PROC s:DWORD
       
    mov     edx,DWORD PTR [esp+4]
       
_loop:
       
    movzx   eax,BYTE PTR [edx]
    test    eax,eax
    jz      _exit

    cmp     eax,96
    setg    al
    Shr     eax,1
    sbb     eax,eax
       
    movzx   ecx,BYTE PTR [edx] 
    cmp     ecx,123
    setl    cl
    Shr     ecx,1
    sbb     ecx,ecx

    And     eax,ecx
    Shl     eax,5
       
    movzx   ecx,BYTE PTR [edx]
    Add     eax,ecx
    mov     BYTE PTR [edx],al

    inc     edx
    jmp     _loop
       
 _exit:
       
    mov     eax,DWORD PTR [esp+4]
    ret     4
       
start:

    push    OFFSET string
    call    UpperCase
    invoke  printf,ADDR string
    invoke  ExitProcess,0

END start

--- End code ---

Vortex:
Another version of the function employing a macro to create a lookup table :


--- Code: ---include UpperCase.inc

ASCII_a EQU 97
ASCII_z EQU 122

.data

UCaseTable :

i = 0

WHILE i LT 256

    IF (i GE ASCII_a ) and (i LE ASCII_z )
   
        db i AND NOT 32
     
    ELSE
   
        db i

    ENDIF
   
    i = i + 1

ENDM

s db 'This IS a test function.',0

.code

UpperCase PROC string:QWORD

    lea     r8,[rip+UCaseTable]
    mov     rax,rcx
    dec     rcx

_loop:

    inc     rcx
    movzx   edx,BYTE PTR [rcx]

    mov     r9b,[r8+rdx]
    mov     [rcx],r9b
    test    edx,edx
    jnz     _loop
    ret

UpperCase ENDP
.
.
.

--- End code ---

John Z:
I like it!  Often a lookup table is a faster approach with a small expense of memory, especially if the index
is the value being changed.  I used a table approach for Base64 conversions with significant speed improvement.


John Z

Navigation

[0] Message Index

Go to full version