用汇编语言写一个查找字符串中的指定字符的程序

lxc_kokom 2011-12-13 03:25:20
查找字符串中的指定字符



基本功能:1 做一个操作界面,提示操作:输入一串字符串、输入所查找的字符或字符串

等;2 显示出查找到的数目;3 用不同颜色或闪烁标示出所找到的字符或字符串;



很急哦,求各位大神的帮助。最好是有代码解释。谢谢啦

...全文
902 点赞 收藏 5
写回复
5 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
lxc_kokom 2011-12-13
这个解释怎么全英文噢。o(︶︿︶)o 唉,看不懂呀,能达到这个功能么。由于汇编是基本不懂,所以只求能实现这个功能的源码。帮忙写个呀。
回复
柯本 2011-12-13
有关界面的汇编代码,在masm32的ex中有一些的,而查找字串的,VC有asm源码的

page ,132
title strchr - search string for given character
;***
;strchr.asm - search a string for a given character
;
; Copyright (c) Microsoft Corporation. All rights reserved.
;
;Purpose:
; defines strchr() - search a string for a character
;
;*******************************************************************************

.xlist
include cruntime.inc
.list

page
;***
;char *strchr(string, chr) - search a string for a character
;
;Purpose:
; Searches a string for a given character, which may be the
; null character '\0'.
;
; Algorithm:
; char *
; strchr (string, chr)
; char *string, chr;
; {
; while (*string && *string != chr)
; string++;
; if (*string == chr)
; return(string);
; return((char *)0);
; }
;
;Entry:
; char *string - string to search in
; char chr - character to search for
;
;Exit:
; returns pointer to the first occurence of c in string
; returns NULL if chr does not occur in string
;
;Uses:
;
;Exceptions:
;
;*******************************************************************************

CODESEG

found_bx:
lea eax,[edx - 1]
pop ebx ; restore ebx
ret ; _cdecl return

align 16
public strchr, __from_strstr_to_strchr
strchr proc \
string:ptr byte, \
chr:byte

OPTION PROLOGUE:NONE, EPILOGUE:NONE

.FPO ( 0, 2, 0, 0, 0, 0 )

xor eax,eax
mov al,[esp + 8] ; al = chr (search char)

__from_strstr_to_strchr label proc

push ebx ; PRESERVE EBX
mov ebx,eax ; ebx = 0/0/0/chr
shl eax,8 ; eax = 0/0/chr/0
mov edx,[esp + 8] ; edx = buffer
test edx,3 ; test if string is aligned on 32 bits
jz short main_loop_start

str_misaligned: ; simple byte loop until string is aligned
mov cl,[edx]
add edx,1
cmp cl,bl
je short found_bx
test cl,cl
jz short retnull_bx
test edx,3 ; now aligned ?
jne short str_misaligned

main_loop_start: ; set all 4 bytes of ebx to [chr]
or ebx,eax ; ebx = 0/0/chr/chr
push edi ; PRESERVE EDI
mov eax,ebx ; eax = 0/0/chr/chr
shl ebx,10h ; ebx = chr/chr/0/0
push esi ; PRESERVE ESI
or ebx,eax ; ebx = all 4 bytes = [chr]

; in the main loop (below), we are looking for chr or for EOS (end of string)

main_loop:
mov ecx,[edx] ; read dword (4 bytes)
mov edi,7efefeffh ; work with edi & ecx for looking for chr

mov eax,ecx ; eax = dword
mov esi,edi ; work with esi & eax for looking for EOS

xor ecx,ebx ; eax = dword xor chr/chr/chr/chr
add esi,eax

add edi,ecx
xor ecx,-1

xor eax,-1
xor ecx,edi

xor eax,esi
add edx,4

and ecx,81010100h ; test for chr
jnz short chr_is_found ; chr probably has been found

; chr was not found, check for EOS

and eax,81010100h ; is any flag set ??
jz short main_loop ; EOS was not found, go get another dword

and eax,01010100h ; is it in high byte?
jnz short retnull ; no, definitely found EOS, return failure

and esi,80000000h ; check was high byte 0 or 80h
jnz short main_loop ; it just was 80h in high byte, go get
; another dword
retnull:
pop esi
pop edi
retnull_bx:
pop ebx
xor eax,eax
ret ; _cdecl return

chr_is_found:
mov eax,[edx - 4] ; let's look one more time on this dword
cmp al,bl ; is chr in byte 0?
je short byte_0
test al,al ; test if low byte is 0
je retnull
cmp ah,bl ; is it byte 1
je short byte_1
test ah,ah ; found EOS ?
je retnull
shr eax,10h ; is it byte 2
cmp al,bl
je short byte_2
test al,al ; if in al some bits were set, bl!=bh
je retnull
cmp ah,bl
je short byte_3
test ah,ah
jz retnull
jmp short main_loop ; neither chr nor EOS found, go get
; another dword
byte_3:
pop esi
pop edi
lea eax,[edx - 1]
pop ebx ; restore ebx
ret ; _cdecl return

byte_2:
lea eax,[edx - 2]
pop esi
pop edi
pop ebx
ret ; _cdecl return

byte_1:
lea eax,[edx - 3]
pop esi
pop edi
pop ebx
ret ; _cdecl return

byte_0:
lea eax,[edx - 4]
pop esi ; restore esi
pop edi ; restore edi
pop ebx ; restore ebx
ret ; _cdecl return

strchr endp
end


其实,最简的方案,就是用C写好程序,然后用
cl /FA x.c
它可以生成x.asm汇编源码的
回复
lxc_kokom 2011-12-13
呵呵,我本来也是弄C++的,但是现在需要用汇编写一个,所以就在这提了。
回复
赵4老师 2011-12-13
那为啥不发到汇编论坛里面捏?
回复
lxc_kokom 2011-12-13
是 用 汇编写哦。
回复
相关推荐
发帖
其它技术问题
创建于2007-09-28

3849

社区成员

C/C++ 其它技术问题
申请成为版主
帖子事件
创建了帖子
2011-12-13 03:25
社区公告
暂无公告