HeiGe__
HeiGe__
采纳率50%
2016-11-09 16:48 阅读 2.8k
已采纳

汇编scasb问题,字符串查找问题

题目要求:
1) 把从LIST 到LIST+316中的字符串传送到BLK到BLK+316 中去;
2) 在BLK到BLK+100中查找字符“cqu”字符串,并记下“CQU”字符串出现的起始位置到BX中。
说明,LIST 到LIST+100的内容(from:http://study.cqu.edu.cn/IndexOfEnglish/Index)为:
Chongqing University (CQU) is a key national university and a member of the “Excellence League”, located in Chongqing, Southwest China. It is also one of the “211 Project" and "985 Project” universities with full support in construction and development from the central government and Chongqing Municipal Government.

我是这样写的:

 data segment
     LIST   db   'Chongqing University (CQU) is a key national university and a member of the “Excellence League”,'
                db  'located in Chongqing, Southwest China. It is also one of the “211 Project" '
                 db   'and "985 Project” universities with full support in construction and develop'
                 db       ' ment from the central government and Chongqing Municipal Government.$'
     CQU   db   'CQU'
data ends
extra segment
     BLK    db    316 dup(?)
extra ends
stack segment
stack ends
code segment
     assume cs:code,ds:data,ss:stack
start:
     mov ax, data
     mov ds, ax
     mov ax, extra
     mov es, ax
     lea    si, LIST
     lea    di, BLK
     mov cx, 316
     cld
     rep   movsb
     mov al, 'C'
     lea   si, LIST-1
find:
     inc si
     repne   scasb
     jnz  check
check:
     mov cx, 3
     repz cmpsb
     jz     stop
     jnz   find
stop:
     mov bx, si
     mov ah, 4ch
     int 21h
code ends
end start

字符串的传递没有问题,但是查找字符串就出问题。
我的思路是,在find中,先将每一个字符与C判断,如果字符是C的话,就跳转到check中,在check中在与字符串CQU判断,如果不是CQU的话则返回到find中。现在的问题是scasb查找到C时,返回的结果不为0。
求大神解答,另外我的思路有问题吗?求更好的思路。

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

2条回答 默认 最新

  • 已采纳
    HeiGe__ HeiGe__ 2018-10-18 16:06

    以上解答错误,重新修正(鉴于三年了,年年都有人来问这个题,修正一下)
    正解

     data segment
         LIST   db   'Chongqing University (CQU) is a key national university and a member of the “Excellence League”,'
                    db  'located in Chongqing, Southwest China. It is also one of the “211 Project" '
                     db   'and "985 Project” universities with full support in construction and develop'
                     db       ' ment from the central government and Chongqing Municipal Government.$'
         CQU   db   'CQU'
    data ends
    extra segment
         BLK    db    316 dup(?)
    extra ends
    stack segment
    stack ends
    code segment
         assume cs:code,ds:data,ss:stack
    start:
         mov ax, data
         mov ds, ax
         mov ax, extra
         mov es, ax
         lea    si, LIST
         lea    di, BLK
         mov cx, 316
         cld
         rep   movsb
         lea    si, CQU
         lea    di, BLK
         mov  ax, si
         add   ax, 4
         mov  cx, 100
         mov  dx, ax
    cycle:
         cld
         repe cmpsb
         sub dx, si
         jz match
         lea si, CQU
         mov dx, ax
         loop cycle
    
    match:
         mov bx, di 
         sub  bx, 4h
         mov ah, 4ch
         int 21h
    code ends
    end start
    
    

    结果如下:
    字符串转移成功
    图片说明
    查找字符串成功,BX中存储内容为CQU的起始位置为16H=22D
    图片说明

    点赞 4 评论 复制链接分享
  • dabocaiqq dabocaiqq 2016-11-12 14:53

相关推荐