x86综合实验-字符串编辑(跳转表实现)

简介

使用跳转表实现字符串增添、删除、大小写转换、查找、替换、比较、排序、输出等功能。

程序运行结果
输入初始字符串

字符串增添

字符串删除

大写转换

查找字符串

替换字符串

字符串比较

字符串内排序

字符串内排序
代码
ASSUME  CS:CODE1,DS:DATA1,SS:STACK1

STACK1  SEGMENT STACK
STACK_AREA      DW      200H DUP(?)
STACK_BTM       EQU     $-STACK_AREA
STACK1          ENDS

DATA1   SEGMENT
EXPLAIN     DB  13,10,"please select function:",13,10
            DB  "1. append a string to original string",13,10
            DB  "2.delete string from original stiring",13,10
            DB  "3.upper",13,10,"4.lower",13,10,"5.find",13,10
            DB  "6.replace",13,10,"7.compare",13,10,"8.sort",13,10,"9.copy",13,10,"0.output",13,10,"other. quit",13,10,0,'$'

STR_ADD_EXPLAIN DB  "please input string you want to append to orignal string ",13,10,0,'$'
STR_DEL_EXPLAIN DB  "please input string you want to delete from origin strnig ",13,10,0,'$'

STR_IN_EXPLAIN  DB  "please input original string",13,10,0,'$'

STR_FIND_EXPLAIN DB "please input string you want to find in original string",13,10,0

STR_FOUND_EXPLAIN DB "position of string you want to find:",0

STR_NOT_FOUND_EXPLAIN DB "the string you want to find not in original string",13,10,0

STR_SRC_EXPLAIN DB  "please input string you want to replace for",13,10,0
STR_DST_EXPLAIN DB  "please input string you want to replace to",13,10,0

STR_CMP_EXPLAIN DB  "pleas input string you want to compare with original string",13,10,0
STR_CMP_SAME_EXPLAIN    DB  "same",13,10,0
STR_CMP_LESS_EXPLAIN    DB  "original string is less",13,10,0
STR_CMP_GREATER_EXPLAIN    DB  "original string is greater",13,10,0

STR_ENTER   DB  13,10,0

NUMBER  DD  1235H
ASCII   DB  20H DUP(0)
FUNC_TABLE  DW    ;跳转表
 OUTPUT_ORI,ADD_STR,DEL_STR,UPPER,LOWER,FIND,REPLACE,COMPARE,SORT,COPY

STR_IN  DB  100H DUP(0) ;"HELLO_WORLD" ,0
STR_OUT DB  100H DUP(0) ;
STR_SRC DB  100H DUP(0) ;"L",0
STR_DST DB  100H DUP(0) ;"LL" ,0

DATA1   ENDS

CODE1 SEGMENT

MAIN    PROC    FAR

        MOV     AX,STACK1
        MOV     SS,AX
        MOV     SP,STACK_BTM
        MOV     AX,DATA1
        MOV     DS,AX
        MOV     ES,AX

        MOV     AX,OFFSET STR_IN_EXPLAIN    
        PUSH    AX
        CALL    OUTPUT_STR

        MOV     AX,OFFSET STR_IN
        PUSH    AX
        CALL    INPUT_STR


    MAIN_LOOP:

        MOV     DX,OFFSET EXPLAIN
        MOV     AH,09H
        INT     21H

        MOV     AH,01H
        INT     21H
        CMP     AL,'0'
        JB      EXIT
        CMP     AL,'9'
        JA      EXIT

        SUB     AL,'0'
        MOV     AH,0
        MOV     BX,AX
        SHL     BX,1


        MOV     SI,OFFSET FUNC_TABLE 
        JMP     [SI+BX]

    CONTINUE:
        JMP     MAIN_LOOP

        MOV     AX,OFFSET STR_IN_EXPLAIN ;read str in
        PUSH    AX
        CALL    OUTPUT_STR

        MOV     AX,OFFSET STR_IN   
        PUSH    AX
        CALL    INPUT_STR

    EXIT:
        MOV     AX,4C00H
        INT     21H


    OUTPUT_ORI:
        MOV     AX,OFFSET STR_IN
        PUSH    AX
        CALL    OUTPUT_STR
        
        JMP     CONTINUE


    ADD_STR:


        MOV     AX,OFFSET STR_ADD_EXPLAIN
        PUSH    AX
        CALL    OUTPUT_STR

        MOV     AX,OFFSET STR_SRC
        PUSH    AX
        CALL    INPUT_STR

        MOV     AX,OFFSET STR_IN
        PUSH    AX
        CALL    STR_LEN
        MOV     BX,AX

        MOV     AX,OFFSET STR_IN
        ADD     AX,BX
        ;DEC     AX
        PUSH    AX
        MOV     AX,OFFSET STR_SRC
        PUSH    AX
        CALL    SAFE_STRCPY

        JMP     CONTINUE

    DEL_STR:

        MOV     AX,OFFSET STR_DEL_EXPLAIN
        PUSH    AX
        CALL    OUTPUT_STR

        MOV     AX,OFFSET STR_SRC
        PUSH    AX
        CALL    INPUT_STR

        MOV     BX,OFFSET STR_DST
        MOV     BYTE PTR [BX],0

        JMP    DEL_CONTINUE;跳转到字符串替换部分  


    FIND: 

        MOV     AX,OFFSET STR_FIND_EXPLAIN
        PUSH    AX
        CALL    OUTPUT_STR

        MOV     AX,OFFSET STR_SRC
        PUSH    AX
        CALL    INPUT_STR


        MOV     AX,OFFSET STR_IN
        PUSH    AX
        CALL    STR_LEN
        
        MOV     CX,AX
        MOV     BX,0

    FIND_LOOP:
        
        MOV     AX,OFFSET STR_SRC
        PUSH    AX
        CALL    STR_LEN

        PUSH    CX
        MOV     CX,AX
        ;MOV     ES,DS
        MOV     SI,OFFSET STR_SRC
        MOV     DI,OFFSET STR_IN
        ADD     DI,BX

        REPZ    CMPSB

        POP     CX
        JZ      FIND_FOUND
        
        INC     BX
        LOOP    FIND_LOOP

    FIND_NOT_FOUND:

        MOV     AX,OFFSET   STR_NOT_FOUND_EXPLAIN
        PUSH    AX
        CALL    OUTPUT_STR


        JMP     CONTINUE

    FIND_FOUND:
        MOV     AX,OFFSET   STR_FOUND_EXPLAIN
        PUSH    AX
        CALL    OUTPUT_STR
        
        ;MOV     X,OFFSET STR_IN
        ;SUB     DI,BX
        
        PUSH    BX
        CALL    OUTPUT16

        JMP     CONTINUE


    UPPER:
        
        MOV     SI,OFFSET STR_IN
    UPPER_LOOP:
        MOV     DL,[SI]
        CMP     DL,0
        JZ      END_UPPER_LOOP

        CMP     DL,'a'
        JB      SKIP_UPPER
        CMP     DL,'z'
        JA      SKIP_UPPER
        AND      DL,223
        MOV     [SI],DL

    SKIP_UPPER:
        INC     SI
        JMP     UPPER_LOOP

    
    END_UPPER_LOOP:
        JMP     CONTINUE

        

    LOWER:

        MOV     SI,OFFSET STR_IN
    LOWER_LOOP:
        MOV     DL,[SI]
        CMP     DL,0
        JZ      END_LOWER_LOOP

        CMP     DL,'A'
        JB      SKIP_LOWER
        CMP     DL,'Z'
        JA      SKIP_LOWER
        OR      DL,32
        MOV     [SI],DL

    SKIP_LOWER:
        INC     SI
        JMP     LOWER_LOOP


    END_LOWER_LOOP:
        JMP     CONTINUE

    REPLACE:

        MOV     AX,OFFSET STR_SRC_EXPLAIN   ;read src str
        PUSH    AX
        CALL    OUTPUT_STR

        MOV     AX,OFFSET STR_SRC
        PUSH    AX
        CALL    INPUT_STR



        MOV     AX,OFFSET STR_DST_EXPLAIN
        PUSH    AX
        CALL    OUTPUT_STR

        MOV     AX,OFFSET STR_DST   ;read dst str
        PUSH    AX
        CALL    INPUT_STR

    DEL_CONTINUE: 

        MOV     AX,OFFSET STR_IN
        PUSH    AX
        MOV     AX,OFFSET STR_OUT
        PUSH    AX
        MOV     AX,OFFSET STR_SRC
        PUSH    AX
        MOV     AX,OFFSET STR_DST
        PUSH    AX
        CALL    STRRPL

        MOV     AX,OFFSET STR_IN
        PUSH    AX
        MOV     AX,OFFSET STR_OUT
        PUSH    AX
        CALL    SAFE_STRCPY 

        JMP     CONTINUE

    COMPARE:

        MOV     AX,OFFSET STR_CMP_EXPLAIN
        PUSH    AX
        CALL    OUTPUT_STR

        MOV     AX,OFFSET STR_SRC
        PUSH    AX
        CALL    INPUT_STR
        
        MOV     AX,OFFSET STR_SRC
        PUSH    AX
        CALL    STR_LEN

        MOV     CX,AX
        ;MOV     ES,DS
        MOV     SI,OFFSET STR_IN
        MOV     DI,OFFSET STR_SRC
        ;ADD     DI,BX

        REPZ    CMPSB
       
        JE      CMP_SAME 
        JL      CMP_LESS
        JG      CMP_GREATER 

    CMP_SAME:
        MOV     AX,OFFSET STR_CMP_SAME_EXPLAIN
        PUSH    AX
        CALL    OUTPUT_STR
        JMP     CONTINUE

    CMP_LESS:
        MOV     AX,OFFSET STR_CMP_LESS_EXPLAIN
        PUSH    AX
        CALL    OUTPUT_STR
        JMP     CONTINUE

    CMP_GREATER:
        MOV     AX,OFFSET STR_CMP_GREATER_EXPLAIN
        PUSH    AX
        CALL    OUTPUT_STR
        JMP     CONTINUE

    SORT:

        
        MOV     AX, OFFSET STR_IN
        PUSH    AX
        CALL    STR_LEN

        MOV     BX,OFFSET STR_IN
        MOV     CX,AX
        DEC     CX
        
        JCXZ    SORT_SKIP
    SORT_LOOP1:

        MOV     SI,AX
        DEC     SI

    SORT_LOOP2:

        MOV     DL,[BX+SI-1]
        MOV     DH,[BX+SI]
        CMP     DL,DH

        JBE     SORT_SKIP_SWAP 

        XCHG    DL,DH

    SORT_SKIP_SWAP:
        MOV     [BX+SI-1],DL
        MOV     [BX+SI],DH
        
        DEC     SI
        CMP     SI,0
        JNZ      SORT_LOOP2

        LOOP    SORT_LOOP1
        
    SORT_SKIP:
        JMP     CONTINUE

    COPY:

        MOV     AX,OFFSET STR_OUT
        PUSH    AX
        MOV     AX,OFFSET STR_IN
        PUSH    AX
        CALL    SAFE_STRCPY

        JMP     CONTINUE



INPUT_STR   PROC    NEAR    ; [SP+2]ADD of DST_STR \n end
    
    PUSH SI
    PUSH AX
    PUSH BX
    PUSH CX
    PUSH DX

    MOV     SI,SP
    MOV     SI,SS:[SI+12]
    MOV     BX,0    

INPUT_STR_LOOP:
    MOV     AH,01H
    INT     21H
    CMP     AL,13   ;\n
    JZ      END_INPUT_STR_LOOP    
    MOV     [SI+BX],AL 
    INC     BX
    JMP     INPUT_STR_LOOP

END_INPUT_STR_LOOP:
    MOV     BYTE PTR [SI+BX],0


    POP DX
    POP CX
    POP BX
    POP AX
    POP SI

    RET 2

    
INPUT_STR   ENDP

OUTPUT_STR  PROC    NEAR ;OUTPUT a string end with '\0' [SP+2] str
    PUSH    SI
    PUSH    AX
    PUSH    DX

    MOV     SI,SP
    MOV     SI,SS:[SI+8]

    MOV     AH,2H
OUTPUT_STR_LOOP:
    MOV     DL,[SI]
    CMP     DL,0
    JZ      END_OUTPUT_STR_LOOP
    INT     21H
    INC     SI
    JMP     OUTPUT_STR_LOOP

END_OUTPUT_STR_LOOP:    

    POP     DX
    POP     AX
    POP     SI
    RET     2
OUTPUT_STR  ENDP

SAFE_STRCPY PROC    NEAR    ; [SP+2]SRC_STR [SP+4]DST_STR

    PUSH    ES
    PUSH    SI 
    PUSH    DI
    PUSH    AX
    PUSH    BX
    PUSH    CX
    PUSH    DX

    MOV     BX,SP
    MOV     SI,SS:[BX+16]
    MOV     DI,SS:[BX+18]


    MOV     BX,-1
LOOP_GET_LEN:
    INC     BX
    MOV     AX,[SI+BX]
    CMP     AX,0
    JNE     LOOP_GET_LEN

    MOV     CX,BX
    INC     CX
    
    MOV     AX,DS
    MOV     ES,AX

    CMP     SI,DI
    JA      LOOP_CPY 

    STD
    ADD     SI,BX
    ADD     DI,BX

LOOP_CPY:
    MOVSB     
    LOOP LOOP_CPY
    


    POP     DX 
    POP     CX 
    POP     BX 
    POP     AX 
    POP     DI
    POP     SI
    POP     ES

    RET 4

SAFE_STRCPY ENDP

STR_LEN     PROC    NEAR ;[SP+2] addr

    PUSH    BP
    MOV     BP,SP
    PUSH    DI
    PUSH    ES

    PUSH    BX
    PUSH    CX
    
    CLD

    MOV     AL,0
    MOV     CX, 7FFFH
    MOV     DI,[BP+4]
    MOV     BX,DI
    ;MOV     ES,DS
    
    REPNZ   SCASB

    SUB     DI,BX
    DEC     DI

    MOV     AX,DI

    POP     CX
    POP     BX
    POP     ES
    POP     DI
    POP     BP
    
    RET     2


STR_LEN    ENDP 

STRRPL  PROC    NEAR    ;[SP+2]STR_DST [SP+4]STR_SRC [SP+6]STR_OUT [SP+8]STR_IN 

    
    PUSH    BP
    PUSH    SI 
    PUSH    DI
    PUSH    AX
    PUSH    BX
    PUSH    CX
    PUSH    DX
    
        
        MOV     BP,SP
        ADD     BP,14


        MOV     SI,    [BP+4] ;STR_SRC
        MOV     BX,-1
LOOP_GET_STR_SRC_LEN:   ;BP-2
        INC     BX
        MOV     AL,[BX+SI]
        CMP     AL,0
        JNZ      LOOP_GET_STR_SRC_LEN
        PUSH    BX 


        MOV     DI,     [BP+2];STR_DST
        MOV     BX,-1
LOOP_GET_STR_DST_LEN:   ;BP-4
        INC     BX
        MOV     AL,[BX+DI]
        CMP     AL,0
        JNZ      LOOP_GET_STR_DST_LEN
        PUSH    BX 

        
        MOV     SI, [BP+8]  ;STR_IN
        MOV     DI, [BP+6]  ;STR_OUT

LOOP_CREATE_NEW_STR:


        MOV     BX,0        
        MOV     CX,0
        PUSH    DI
        MOV     DI,[BP+4]   ;STR_SRC

LOOP_CMP_STR:   ;   CX:0 is same
        MOV     AL,[SI+BX]  
        MOV     AH,[DI+BX]

        CMP     AH,0 
        JZ      LOOP_CMP_STR_END

        XOR     AL,AH
        OR      CL,AL
        

        INC     BX
        JMP     LOOP_CMP_STR 

LOOP_CMP_STR_END:

        POP     DI      ;STR_OUT

        CMP     CX,0
        JNZ     SKIP_RPL

        PUSH    DI
        MOV     AX,[BP+2];STR_DST
        PUSH    AX

        CALL    SAFE_STRCPY


        MOV     AX,[BP-16]
        ADD     SI,AX
        MOV     AX,[BP-18]
        ADD     DI,AX

        JMP LOOP_CREATE_NEW_STR

SKIP_RPL:
        MOV AL,[SI]
        MOV [DI],AL
        INC SI
        INC DI

        CMP AL,0
        JNZ LOOP_CREATE_NEW_STR

LOOP_CREATE_NEW_STR_END:
    
    POP     BX
    POP     BX

    POP     DX 
    POP     CX 
    POP     BX 
    POP     AX 
    POP     DI
    POP     SI
    POP     BP 

        RET 8


STRRPL  ENDP
OUTPUT16  PROC    NEAR    ;OUTPUT ONE NUMBER(16) [SP+2] num
        
        PUSH AX 
        PUSH BX
        PUSH CX
        PUSH DX
        PUSH SI

        MOV     BX,SP 
        MOV     BX,SS:[BX+12]

        MOV     CX,4

OUTPUT_LOOP:
             
        MOV     DL,BH
        
        
        SHR     DL,1
        SHR     DL,1
        SHR     DL,1
        SHR     DL,1

        CMP     DL,10

        JB      OUTPUT_NUM

        ADD     DL,41H
        SUB     DL,10
        JMP     OUTPUT_FIN

OUTPUT_NUM:
        ADD     DL,30H


OUTPUT_FIN:
        MOV     AH,02H
        INT     21H

        SHL     BX,1
        SHL     BX,1
        SHL     BX,1
        SHL     BX,1
        

        LOOP    OUTPUT_LOOP

        POP SI
        POP DX 
        POP CX 
        POP BX 
        POP AX

        RET 2

OUTPUT16  ENDP


CODE1   ENDS

END     MAIN

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 216,324评论 6 498
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,356评论 3 392
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 162,328评论 0 353
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,147评论 1 292
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,160评论 6 388
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,115评论 1 296
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,025评论 3 417
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,867评论 0 274
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,307评论 1 310
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,528评论 2 332
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,688评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,409评论 5 343
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,001评论 3 325
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,657评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,811评论 1 268
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,685评论 2 368
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,573评论 2 353

推荐阅读更多精彩内容