概述
;
;Env:Masm 6.15 WinXP+SP2
;
;string libary
;
;to be contiune...
include irvine32.inc
.data
str1 byte "ABCD1234", 0
msg1 byte "Source String is :", 0
msg2 byte "Target String is :", 0
msg3 byte "After string concat :", 0
str2 byte 30 dup (0), 0
err byte 0dh, 0ah, "Error! source length bigger than target buffer length!", 0dh, 0ah, 0
.code
;----------------------------------
;function:caclute a string length
;reqiures:address of the string
;----------------------------------
Str_len PROC USES ecx esi,
string:PTR BYTE
mov esi, string ;address of the string
xor ecx, ecx ;ecx = 0
LP:
mov al, [esi] ;get a character
test al, al ;al == 0?
jz Exit_ ;yes, exit
inc ecx ;ecx++
inc esi ;point to next character
jmp LP
Exit_:
mov eax, ecx ;return eax
ret
Str_len ENDP
;----------------------------------
;function:copy a string to another
;reqiures:source string address,
; target string address
;----------------------------------
Str_cpy PROC USES eax ecx edx esi edi,
source:PTR BYTE,
target:PTR BYTE,
maxsize:DWORD
mov esi, source ;source address
mov edi, target ;target address
invoke Str_len, esi ;source string length
mov ecx, eax
mov eax, maxsize
cmp ecx, eax ;source length > target length?
ja Error_ ;yes, error
rep movsb ;copy
mov eax, ecx
jmp Exit_
Error_:
mov edx, offset err
call writestring
Exit_:
ret
Str_cpy ENDP
;-----------------------------------
;
;function:compare two string, set ZF flag if equal.
;reqiures:both address of the strings
;-----------------------------------
Str_cmp PROC USES eax esi edi,
source:PTR BYTE,
target:PTR BYTE
mov esi, source
mov edi, target
LP:
mov al, [esi]
test al, al ;end of string1?
jnz Next_ ;no
mov ah, [edi]
test ah, ah ;end of string2?
jne Next_ ;no
jmp Exit_ ;yes, return
Next_:
inc esi
inc edi
cmp al, ah ;cmpare characters
je LP
Exit_:ret
Str_cmp ENDP
;-----------------------------------
;
;function:Search an array of signed integers for a single value.
;return: if a match is found, eax = the array position of the matching element, otherwise, eax = -1;
;-----------------------------------
Bin_Search PROC USES ebx edx esi edi,
pArray:PTR DWORD, ;pointer to array
Count:DWORD, ;array size
searchVal:DWORD ;search value
LOCAL first:DWORD, ;first position
last:DWORD, ;last position
mid:DWORD ;mid point
mov first, 0 ;first = 0
mov eax, Count ;last = (count-1)
dec eax
mov last, eax
mov edi, searchVal ;EDI = searchVal
mov ebx, pArray ;EBX pointer to the array
L1:;while first <= last
mov eax, first
cmp eax, last
jg L5 ;exit search
;mid = (last+first) / 2
mov eax, last
add eax, first
shr eax, 1
mov mid, eax
;edx = values[mid]
mov esi, mid
shl esi, 2 ;scale mid value by 4
mov edx, [ebx+esi] ;edx = values[mid]
;if (edx < searchval(EDI))
;first = mid+1;
cmp edx, edi
jge L2
mov eax, mid ;first = mid+1
inc eax
mov first, eax
jmp L4
;else if (EDX > searchVal(EDI))
;last = mid-1;
L2:cmp edx, edi
jle L3
mov eax, mid ;last = mid-1
dec eax
mov last, eax
jmp L4
;else return mid
L3:mov eax, mid ;value found
jmp L9 ;return (mid)
L4:jmp L1 ;continue the loop
L5:mov eax, -1 ;search faild
L9:ret
Bin_Search ENDP
;------------------------------------
;
;function:Binary search
;------------------------------------
;function:concat a string to another
;reqires:source string address,
; target string address.
;------------------------------------
Str_concat PROC USES eax ebx ecx esi edi,
source:PTR BYTE,
target:PTR BYTE,
maxsize:DWORD
mov esi, source ;source string address
mov edi, target ;target string address
invoke Str_len, esi ;source string length
mov ecx, eax
invoke Str_len, edi ;target string length
mov ebx, eax ;protect target string length
add eax, ecx ;test after copy, the buffer whether enough.
cmp eax, maxsize
jb Next_ ;eax<ebx? yes, string can copy
mov ecx, maxsize
sub ecx, ebx ;no, string only be copy a part. maxsize-targetstringsize
Next_:
add edi, ebx ;begin address copy to
rep movsb
ret
Str_concat ENDP
;------------------------------------
;function:find the source string from target string
;reqiures:source string address
; target string address
;------------------------------------
Str_find PROC USES ebx ecx edx esi di,
source:PTR BYTE,
target:PTR BYTE
xor ebx, ebx
invoke Str_len, target ;target string length
mov edx, eax ;protect length to edx
invoke Str_len, source ;set source string length into eax
LP:
cld ;forward
mov esi, source
mov edi, target
add edi, ebx
mov ecx, eax ;set loop ecx
repe cmpsb ;cmp
jz Equal_
inc ebx ;next target character
cmp byte ptr [edi], 0
jz NotEqual_ ;in target string character whether zero.
jmp LP
Equal_:
mov eax, ebx
jmp Exit_
NotEqual_:
mov eax, -1
Exit_:ret
Str_find endp
;-----------------------------------
;function:delete a string from another string
;reqiures:source string, target string
;-----------------------------------
Str_del PROC USES eax ebx ecx esi edi,
source:PTR BYTE,
target:PTR BYTE
mov edi, target
invoke Str_len, target
mov edx, eax ;target string length
invoke Str_len, source
mov ebx, eax ;source string length
invoke Str_find, source, target
cmp eax, -1 ;find?
jz Exit_ ;no, exit
add edi, eax ;begin address to overwrite
mov esi, edi
add esi, ebx ;begin to copy address
sub edx, eax
sub edx, ebx
mov ecx, edx ;loop counter
cld
rep movsb
mov byte ptr [edi], 0
Exit_:ret
Str_del ENDP
;====================================
main PROC
mov edx, offset msg1
call writestring
mov edx, offset str1
call writestring
call crlf
call crlf
invoke Str_cpy, ADDR str1, ADDR str2, (LENGTHOF str2)-1
mov edx, offset msg2
call writestring
mov edx, offset str2
call writestring
invoke Str_concat, ADDR str1, ADDR str2, (LENGTHOF str2)-1
call crlf
call crlf
mov edx, offset msg3
call writestring
mov edx, offset str2
call writestring
invoke Str_del, ADDR str1+8, ADDR str2
call crlf
call crlf
mov edx, offset str2
call writestring
invoke exitprocess, 0
main endp
end main
最后
以上就是冷艳毛巾为你收集整理的汇编源代码---字符串处理(更新中...)的全部内容,希望文章能够帮你解决汇编源代码---字符串处理(更新中...)所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复