Skip to content

Instantly share code, notes, and snippets.

@hjpotter92
Last active December 22, 2015 16:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hjpotter92/6497303 to your computer and use it in GitHub Desktop.
Save hjpotter92/6497303 to your computer and use it in GitHub Desktop.
2013-09-09: Read two strings and check whether one of them is a subtring of other or not
DISP_STR PROC
SAVE_REG <AX, CX, DX, SI>
MOV CX, BX
CLD
MOV AH, 2
DISP_LOOP:
LODSB
MOV DL, AL
INT 21H
LOOP DISP_LOOP
LOAD_REG <SI, DX, CX, AX>
RET
DISP_STR ENDP
TITLE Assignment: Read two strings and check whether one is a substring of other
.MODEL SMALL
SAVE_REG MACRO REGS
IRP D, <REGS>
PUSH D
ENDM
ENDM
LOAD_REG MACRO REGS
IRP D, <REGS>
POP D
ENDM
ENDM
END_DOS MACRO
MOV AH, 76
INT 21H
ENDM
NEWLINE MACRO
SAVE_REG <AX, DX>
MOV AH, 2
MOV DL, 0AH
INT 21H
MOV DL, 0DH
INT 21H
LOAD_REG <DX, AX>
ENDM
.STACK 100H
.DATA
STR1 DB 200 DUP(65)
STR2 DB 200 DUP(0)
PROMPT DB "Enter string "
NUM DB "1: $"
L1 DW 0
UNEQL DB "No substring found$"
EQUAL DB "Substring was found$"
.CODE
MAIN PROC
MOV AX, @DATA
MOV DS, AX
MOV ES, AX
MOV AH, 9
LEA DX, PROMPT
INT 21H
LEA DI, STR1
CALL READ_STR
MOV L1, BX
NEWLINE
MOV NUM, '2'
INT 21H
LEA DI, STR2
CALL READ_STR
LEA SI, STR1
LEA DI, STR2 ; DI stores the longer string
CMP L1, BX
JE EQL
CMP L1, BX
JG INV
MOV CX, BX
SUB CX, L1
JMP COMPARE
INV:
MOV CX, L1
SUB CX, BX
MOV L1, BX
LEA SI, STR2
LEA DI, STR1 ; DI stores longer string
NEWLINE
CALL DISP_STR
JMP COMPARE
EQL:
MOV CX, 1
COMPARE:
XOR DX, DX
TOP:
SAVE_REG <DI, SI>
ADD DI, DX
PUSH CX
MOV CX, L1
REPE CMPSB
JE FOUND
INC DX
POP CX
LOAD_REG <SI, DI>
LOOP TOP
NOT_FOUND:
NEWLINE
LEA DX, UNEQL
MOV AH, 9
INT 21H
END_DOS
FOUND:
POP CX
LOAD_REG <SI, DI>
NEWLINE
LEA DX, EQUAL
MOV AH, 9
INT 21H
END_DOS
MAIN ENDP
INCLUDE READSTR.ASM
INCLUDE DISPSTR.ASM
END MAIN
READ_STR PROC
SAVE_REG <AX, DI>
XOR BX, BX
CLD
MOV AH, 1
INT 21H
WHILE_LOOP:
CMP AL, 0DH
JE END_WHILE
CMP AL, 08H
JNE STORAGE
DEC BX
DEC DI
JMP NEXT
STORAGE:
STOSB
INC BX
NEXT:
INT 21H
JMP WHILE_LOOP
END_WHILE:
LOAD_REG <DI, AX>
RET
READ_STR ENDP
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment