Assembly language > Assembly discussions
RightTrim function
Vortex:
RightTrim function to remove the trailing TAB and SPACE characters from a string :
--- Code: ---.386
.model flat,stdcall
option casemap:none
StrLen PROTO :DWORD
.data
ltable db 0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
db 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
db 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
db 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
db 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
db 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
db 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
db 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
.code
OPTION PROLOGUE:NONE
OPTION EPILOGUE:NONE
RightTrim PROC _str:DWORD
mov eax,DWORD PTR [esp+4]
invoke StrLen,eax
add eax,DWORD PTR [esp+4]
mov ecx,OFFSET ltable
@@:
sub eax,1
movzx edx,BYTE PTR [eax]
cmp BYTE PTR [ecx+edx],1
je @b
@@:
add eax,1
mov BYTE PTR [eax],0
mov eax,DWORD PTR [esp+4]
retn 4
RightTrim ENDP
OPTION PROLOGUE:PROLOGUEDEF
OPTION EPILOGUE:EPILOGUEDEF
END
--- End code ---
Grincheux:
And the same for X64?
Vortex:
Hi Grincheux,
Here is the 64-bit version :
--- Code: ---.model flat,fastcall
option casemap:none
lstrlenA PROTO :QWORD
lstrlen EQU <lstrlenA>
.data
ltable db 0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
db 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
db 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
db 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
db 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
db 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
db 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
db 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
.code
RightTrim PROC _str:DWORD PARMAREA=1*QWORD
LOCAL _rcx:QWORD
mov _rcx,rcx
invoke lstrlen,rcx
add rax,_rcx
mov r8,1
mov rcx,OFFSET ltable
@@:
sub rax,r8
movzx rdx,BYTE PTR [rax]
cmp BYTE PTR [rcx+rdx],r8b
je @b
@@:
add rax,r8
mov BYTE PTR [rax],0
mov rax,_rcx
ret
RightTrim ENDP
--- End code ---
Testing the function :
--- Code: ---.model flat,fastcall
option casemap:none
includelib kernel32.lib
includelib user32.lib
includelib msvcrt.lib
ExitProcess PROTO :QWORD
RightTrim PROTO :QWORD
printf PROTO :QWORD,:VARARG
lstrlenA PROTO :QWORD
lstrlen EQU <lstrlenA>
.data
teststr db 'This is a test. ',0
msg db 'Length of the string = %u',0
.code
start:
sub rsp,8+4*8
invoke RightTrim,ADDR teststr
invoke lstrlen,rax
invoke printf,ADDR msg,rax
invoke ExitProcess,0
END start
--- End code ---
Grincheux:
Thank You Vortex. I take, it is better than mine. ;)
Grincheux:
Why do you return a pointer on lTable?
lTable could be got by using VirtualAlloc.
The resulting string cannot be longer than the one passed as an argument.
It can only be shorter or the same size.
So why not use lstrcpy?
The string argument is limited to 256 bytes!
If the string is longer than 256 the function must be called like this RightTrim(&_szString[lstrlen(_szString) - 256]) ;
Navigation
[0] Message Index
[#] Next page
Go to full version