NO

Author Topic: Converting a string to uppercase  (Read 1819 times)

Offline Vortex

  • Member
  • *
  • Posts: 902
    • http://www.vortex.masmcode.com
Converting a string to uppercase
« on: July 03, 2023, 10:32:11 PM »
Hello,

Here is a quick example :

Code: [Select]
.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
Code it... That's all...

Offline Vortex

  • Member
  • *
  • Posts: 902
    • http://www.vortex.masmcode.com
Re: Converting a string to uppercase
« Reply #1 on: October 22, 2023, 01:19:36 PM »
Another version of the function employing a macro to create a lookup table :

Code: [Select]
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
.
.
.
Code it... That's all...

Offline John Z

  • Member
  • *
  • Posts: 920
Re: Converting a string to uppercase
« Reply #2 on: October 23, 2023, 11:15:41 AM »
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