Процедур обработки строк Insert, Delete, Pos - Assembler

Узнай цену своей работы

Формулировка задачи:

Реализовать на языке ассемблера набор процедур обработки строк из системы Turbo Pascal, спасибо 1. Delete(S, p, i). Удалить из строки S i символов начиная с позиции p; 2. Insert(S1, S2, p). Вставить подстроку S1 в строку S2 начиная с позиции p; 3. Pos(S1, S2). Найти номер позиции, с которой начинается подстрока S1 в строке S2;

Решение задачи: «Процедур обработки строк Insert, Delete, Pos»

textual
Листинг программы
; *******************************************************
; *                                                     *
; *     Turbo Pascal Runtime Library Version 7.0        *
; *     String Pos Function                             *
; *                                                     *
; *     Copyright (C) 1990-1993 Norbert Juffa           *
; *                                                     *
; *******************************************************
 
             TITLE   STPOS
 
 
CODE         SEGMENT BYTE PUBLIC
 
             ASSUME  CS:CODE
 
; Publics
 
             PUBLIC  SPos
 
;-------------------------------------------------------------------------------
; Pos standard function: Pos (VAR Obj, Target: STRING): BYTE
;
; Pos searches the string Target for the first occurence of a string Obj. If Obj
; is found in Target, the index at which the first character of Obj appears in
; Target is returned. If Obj is not found in Target, zero is returned. The
; routine searches the first character of Obj in the first Len(Target)-Len(Obj)
; charcters of Target. Whenever there is a match, the following characters of
; Target are compared to the characters in Obj. If all characters match, Obj has
; been succesfully found in Target.
;
; On entry:  [SP+4] Address of Target
;            [SP+8] Address of Obj
;
; On exit:   AX     index at which Obj first found in Target (0 if not found)
;-------------------------------------------------------------------------------
 
SPos         PROC    FAR
             CLD                       ; auto-increment for string instructions
             PUSH    DS                ; save TURBO-Pascal data segment
             PUSH    BP                ; save TURBO-Pascal's frame pointer
             MOV     BP, SP            ; make new frame pointer
             LES     DI, [BP+8]        ; address of target string
             LDS     SI, [BP+12]       ; address of object string
             MOV     AX, [SI]          ; AL=length of object, AH=1. char of obj.
             INC     SI                ; adjust
             INC     SI                ;  string pointer
             XOR     CH, CH            ; init result
             DEC     AX                ; length - 1 (changes AH only when AL=0)
             MOV     CL, ES:[DI]       ; length of target string
             SUB     CL, AL            ; targetlength <= objectlength - 1 ?
             JBE     $not_found        ; yes, object not found
             MOV     BH, CL            ; save length of target string
             INC     DI                ; first char of target string
             XCHG    AL, AH            ; AL=1. char of object, AH=length of obj.
             MOV     BP, SI            ; save offset into object
$search:     REPNZ   SCASB             ; scan target string for 1. char of obj.
             JNZ     $not_found        ; exit, if not found
             MOV     BL, CL            ; save remaining length of target string
             MOV     CL, AH            ; number of char to be compared
             MOV     DX, DI            ; save offset into target string
             REPZ    CMPSB             ; compare remaining char of target & obj.
             MOV     SI, BP            ; restore offset into object string
             MOV     DI, DX            ; restore offset into target string
             MOV     CL, BL            ; restore remaining length of target str
             JNZ     $search           ; cont. search, if not all chars matched
             SUB     BH, BL            ; Pos = targetlength - remain. targetlen.
             MOV     CH, BH            ; Pos
$not_found:  POP     BP                ; restore TURBO-Pascal's frame pointer
             POP     DS                ; restore TURBO-Pascal data segment
             MOV     AL, CH            ; Pos
             XOR     AH, AH            ; clear msb
             RET     8                 ; return and pop parameters
SPos         ENDP
 
             ALIGN   4
 
CODE         ENDS
 
             END

Объяснение кода листинга программы

  1. Объявление титула программы, указание версии и авторских прав.
  2. Объявление сегмента кода.
  3. Объявление и описание функции SPos, которая ищет первое вхождение строки Obj в строке Target.
  4. Инициализация переменных и регистров для начала поиска.
  5. Процесс сканирования строк, поиск первого символа строки Obj в строке Target.
  6. Сравнение оставшихся символов строк Target и Obj.
  7. Возврат результата поиска (индекс, где найдена строка Obj в строке Target, или 0, если не найдена).

Оцени полезность:

14   голосов , оценка 4.286 из 5
Похожие ответы