急求汇编高手,查找字符串中给定字符首次出现的位置,汇编语言!!!

2024-12-02 18:48:53
推荐回答(2个)
回答(1):


        DATA  SEGMENT
        STR1  DB        'shfskldjfiuiruiyweurwgteuiwiX',0
        ; 以0结尾的字符串,即类似c语言的字符串        
       CHAR1  DB        '8'
        DATA  ENDS

        CODE  SEGMENT
              ASSUME    CS:CODE
      START:
              MOV       AX,SEG DATA
              MOV       DS,AX
              MOV       ES,AX
              LEA       DI,STR1
              MOV       AL,BYTE PTR DS:CHAR1
              CALL      FIND
              CALL      DISPAXS
              MOV       AH,4CH
              INT       21H
  ; ========================================
        FIND  PROC      NEAR
              ; al 为要查找的字符 
              ; di 为目的串的偏移地址                     
              MOV       DX,AX
              MOV       SI,DI
              MOV       AL,0
              MOV       CX,0
        @F1:
              CMP       AL,ES:[DI]
              JE        @F2
              INC       CX
              INC       DI
              JMP       @F1
        @F2:
              MOV       AX,DX
              MOV       DI,SI
              MOV       BX,CX
        @F3:
              CMP       AL,ES:[DI]
              JE        @F4
              INC       DI
              LOOP      @F3
        @F4:
              CMP       CX,0
              JE        @F5
              SUB       BX,CX
              MOV       AX,BX
              RET
        @F5:
              MOV       AX,-1
              RET
              ;  当返回0时,表示第1个元素匹配 
              ;  当返回n-1时,表示第n个元素匹配 
              ;  当返回 -1 时,表示未找到   
        FIND  ENDP
 ;====================================
 ; 将要显示的有符号数置于 ax 中
     DISPAXS  PROC      NEAR
              PUSH      AX
              PUSH      BX
              PUSH      CX
              PUSH      DX
              PUSH      SI
              PUSH      DI
              PUSH      BP
              PUSH      DS
              PUSH      ES
              PUSHF
              PUSH      CS
              POP       DS
              PUSH      CS
              POP       ES
              MOV       CX,6
              LEA       DI,DISPAXSS
   @DISPAXS:
              MOV       BYTE PTR [DI],32
              INC       DI
              LOOP      @DISPAXS
              PUSH      AX
              MOV       DL,32
              MOV       AH,2
              INT       21H
              POP       AX
              MOV       BYTE PTR NZS,0
              MOV       BYTE PTR SIGNS,0
              CMP       AX,0
              JGE       @DISPAXS0
              MOV       BYTE PTR SIGNS,1
              NEG       AX
  @DISPAXS0:
              PUSH      AX
              LEA       SI,DIVARRS
              LEA       DI,DISPAXSS
              INC       DI
              MOV       CX,5
  @DISPAXS1:
              POP       AX
              MOV       DX,0
              MOV       BX,[SI]
              DIV       BX
              PUSH      DX
              CMP       AL,0
              JNE       @DISPAXS2
              CMP       BYTE PTR NZS,1
              JE        @DISPAXS2
              CMP       CX,1
              JE        @DISPAXS2
              MOV       DL,20H
              JMP       @DISPAXS3
  @DISPAXS2:
              ADD       AL,30H
              MOV       DL,AL
              MOV       BYTE PTR NZS,1
  @DISPAXS3:
              MOV       BYTE PTR[DI],DL
              INC       SI
              INC       SI
              INC       DI
              LOOP      @DISPAXS1
              POP       DX

              CMP       BYTE PTR SIGNS,1
              JNE       @DISPAXS6
              LEA       SI,DISPAXSS
              ADD       SI,5
  @DISPAXS4:
              CMP       BYTE PTR [SI],32
              JE        @DISPAXS5
              DEC       SI
              JMP       @DISPAXS4
  @DISPAXS5:
              MOV       BYTE PTR [SI],'-'
  @DISPAXS6:
              LEA       DX,DISPAXSS
              MOV       AH,9
              INT       21H
              POPF
              POP       ES
              POP       DS
              POP       BP
              POP       DI
              POP       SI
              POP       DX
              POP       CX
              POP       BX
              POP       AX
              RET
     DIVARRS  DW        10000,1000,100,10,1
         NZS  DB        0
       SIGNS  DB        0
    DISPAXSS  DB        32,32,32,32,32,32,'$'
     DISPAXS  ENDP
; =============================================     
        CODE  ENDS
              END       START

回答(2):

;用MASM 5.0 编译通过。
;急求汇编高手,查找字符串中给定字符首次出现的位置,汇编语言!!!
;要求:字符串给定,从键盘输入单个字符,enter键结束,显示位置

data segment
addr db 0dh,0ah, 'A foreign language is a weapon in the struggle of life!',0dh,0ah,0DH,0AH
DB 'Input a char: $'

L dw $-ADDR-2
NO_SUCH DB 0DH,0AH,'No such char!$'
n db 1
data ends

code segment
main proc far
assume cs:code,ds:data

start:
MOV AX,DATA
MOV DS,AX
L0:
mov al,1
mov n,al
MOV DX,OFFSET ADDR
MOV AH,9
INT 21H
mov ah,1
int 21h
cmp al,0dh
je exit

MOV CX,L
MOV bX,OFFSET ADDR
add bx,2
NEXT:
CMP AL,[BX]
JE DISP
INC BX
INC N
LOOP NEXT
MOV DX,OFFSET NO_SUCH
MOV AH,9
INT 21H
JMP EXIT
DISP:
mov al,n
XOR AH,AH
MOV BL,10
DIV BL
ADD AX,3030H
PUSH AX
MOV AH,2
MOV DL,' '
INT 21H
MOV AH,2
POP DX
INT 21H
MOV DL,DH
INT 21H
JMP L0

EXIT:
MOV AX,4C00H
INT 21H

main endp
code ends
end start