strlwr 和 strupr 模拟

.DATA
strlwr_table BYTE
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20
BYTE
21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40
BYTE
41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64
BYTE
97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122 ; To lower case.
BYTE 91, 92, 93, 94, 95, 96
BYTE
97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122 ; Original lower case.
BYTE 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143
BYTE
144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164
BYTE
165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185
BYTE
186, 187, 188, 189, 190, 191, 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 206
BYTE
207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223, 224, 225, 226, 227
BYTE
228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247, 248
BYTE
249, 250, 251, 252, 253, 254, 255

strupr_table BYTE
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20
BYTE
21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40
BYTE
41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64
BYTE
65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90 ; Original upper case.
BYTE 91, 92, 93, 94, 95, 96
BYTE
65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90 ; To upper case.
BYTE 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143
BYTE
144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164
BYTE
165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185
BYTE
186, 187, 188, 189, 190, 191, 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 206
BYTE
207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223, 224, 225, 226, 227
BYTE
228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247, 248
BYTE
249, 250, 251, 252, 253, 254, 255

.CODE

; Prototype
;
eax strlwr(char *str) ;
;
Param
;
str: The address of null terminated string.
;
Return
;
eax: The address of the null terminated string.
;
Function own stack and response for adjust ESP after called.
strlwr PROC NEAR32
push ebp
mov ebp, esp
pushf
push ebx
push esi
push edi

; Get address of traslation table.
lea ebx, strlwr_table
; Save address of the string to be translated.
mov eax, [ebp + 8]
push eax
; Initailization
mov esi, eax
mov edi, eax
cld

while_strlwr:
cmp BYTE PTR [esi], 0
je exit_strlwr
lodsb
xlat
stosb
loop while_strlwr

exit_strlwr:
pop eax
pop edi
pop esi
pop ebx
popf
pop ebp
ret 4
strlwr ENDP

; Prototype
;
eax strupr(char *str) ;
;
Param
;
str: The address of null terminated string.
;
Return
;
eax: The address of the null terminated string.
;
Function own stack and response for adjust ESP after called.
strupr PROC NEAR32
push ebp
mov ebp, esp
pushf
push ebx
push esi
push edi

; Get address of traslation table.
lea ebx, strupr_table
; Save address of the string to be translated.
mov eax, [ebp + 8]
push eax
; Initailization
mov esi, eax
mov edi, eax
cld

while_strupr:
cmp BYTE PTR [esi], 0
je exit_strupr
lodsb
xlat
stosb
loop while_strupr

exit_strupr:
pop eax
pop edi
pop esi
pop ebx
popf
pop ebp
ret 4
strupr ENDP


  

More practical version:

; Prototype
;
eax strlwr(char *str) ;
;
Param
;
str: The address of null terminated string.
;
Return
;
eax: The address of the null terminated string.
;
Function own stack and response for adjust ESP after called.
strlwr PROC NEAR32
push ebp
mov ebp, esp
pushf
push esi
push edi

; Initialize
mov esi, [ebp + 8]
mov edi, esi
mov eax, 0
cld

while_strlwr:
lodsb ; memory => al
cmp al, 0
je exit_strlwr
or al, 00100000b ; Upper to lower
stosb ; al => memory
jmp while_strlwr

exit_strlwr:
mov eax, [ebp + 8] ; Rtn: address of original string
pop edi
pop esi
popf
pop ebp
ret 4
strlwr ENDP

; Prototype
;
eax strupr(char *str) ;
;
Param
;
str: The address of null terminated string.
;
Return
;
eax: The address of the null terminated string.
;
Function own stack and response for adjust ESP after called.
strupr PROC NEAR32
push ebp
mov ebp, esp
pushf
push esi
push edi

; Initialize
mov esi, [ebp + 8]
mov edi, esi
mov eax, 0
cld

while_strupr:
lodsb ; memory => al
and al, 11011111b ; Lower to upper
stosb ; al => memory
jnz while_strupr

exit_strupr:
mov eax, [ebp + 8] ; Rtn: address of original string
pop edi
pop esi
popf
pop ebp
ret 4
strupr ENDP

  

posted @ 2011-08-29 21:36  walfud  阅读(819)  评论(0编辑  收藏  举报