Skip to content

05

DOS

Display Number

mov dl, 34h
mov ah, 02h
int 21h

Display String

lea dx, msg
mov ah, 09h
int 21h

Reading

readkb proc near
    mov ah, 01h
    int 21h

    cmp al, 3Ah
    jc number

    sub al, 07h

    number:
        sub al, 30

    ret
readkb endp

Program 1

Write a program to transfer the given string from source to destination using string instruction and also display the destination string.

.MODEL SMALL
.STACK 20
.DATA
SRCSTR DB 'ELECTRONICS' ; Source String 'ELECTRONICS'
LEN DW $-SRCSTR ; String Length
MSG DB 'The Transferred String=' ; MSG is variable to store the transferred string
DSTSTR DB 40 DUP ('$') ; Destination string

.CODE ; code start
START:
MOV AX, @DATA ; Data segment start here
MOV DS, AX ; Move AX to DS
MOV ES, AX ; Move AX to ES

MOV CX, LEN ; Move length of string to CX register
LEA SI, SRCSTR ; Load Effective Address of source string to SI
LEA DI, DSTSTR ; Load Effective Address of Destination to DI
CLD ; for auto increment of SI and DI
REP MOVSB ; Repeat prefix to MOVSB, Move data as bytes
LEA DX, MSG

MOV AH, 09 ; Displays a message terminated by $
INT 21H ; DOS interrupt 21H

MOV AH, 4CH ; program termination
INT 21H ; DOS interrupt 21H

END START

Program 2

Write a program to read two digit decimal number using keyboard and search whether the number is present in an array or not. Display suitable message.

.MODEL SMALL
.STACK 20
.DATA
ARRAY DB 35H, 56H, 82H, 89H, 90H, 23H, 12H, 51H, 88H ; Input array
LEN DW $-ARRAY ; Length of the array value
MSG1 DB 0DH, 0AH, ' Enter two digit numbers: $' ; Message one for input
MSG2 DB 0DH, 0AH, ' The number is present $' ; Message to present output
MSG3 DB 0DH, 0AH, ' The number is not present $' ; Message to present output
.CODE
START:
MOV AX, @DATA ; Data segment start here
MOV DS, AX
MOV ES, AX ; MOVE AX, TO Extra Segment

MOV CX, LEN ; load the length of the array to CX
LEA DX, MSG1 ; Load effective address to DX
MOV AH, 09 ; To display MSG1
INT 21H ; DOS interrupt 21H

CALL READKB ; Call to READKB procedure
ROR AL, 4 ; shifting the digit to MSD position, by rotating Right by 4
; times of AL
MOV BL, AL ; Move AL value to BL

CALL READKB ; Call the READKB procedure
ADD AL, BL ; To make 2-digit number
LEA DX, MSG2 ; Load effective address to DX
LEA DI, ARRAY ; Load the effective address of array to DI
CLD ; Clear Direction Flag
REPNE SCASB ; Compares AL with memory pointed by DI
JE GO ; conditional jump when zero flat equal to 1
LEA DX, MSG3 ; Load Effective Address of MSG3 to DX

GO:
MOV AH, 09 ; To display the output
INT 21H ; DOS interrupt 21H
MOV AH, 4CH ; To terminate
INT 21H ; DOS interrupt 21H

READKB PROC NEAR
MOV AH,01 ; Accepting number from keyboard
INT 21H ; DOS interrupt 21H
CMP AL, 3AH ; compare AL with 3AH
JC SUB30 ; Jump if carry
SUB AL, 07H ; subtract 07H with AL

SUB30:
SUB AL, 30H ; ASCII to hex conversion
RET ; RET instruction used at the end of the procedures
READKB ENDP ; end to READKB procedure

END START ; end to start

Program 3

Write a program to read a string using DOS interrupts, reverse the entered string and display the same on the screen. Use MACRO for display.

.MODEL SMALL
.STACK 20

DISP MACRO MSG ; Macro Declaration, DISP is the name of the Macro.
MOV AH, 09H ; To display message
MOV DX, OFFSET MSG ; Load the MSG offset address to DX
INT 21H ; DOS interrupt 21H
ENDM ; End Macro

.DATA ; Data segment start here
MSG1 DB 0DH, 0AH, 'Input a string:$' ; 0DH,0AH are carriage Return & Line Feed

; $ to terminate
SRC DB 80 ; Maximum size of the string
DB ? ; Actual size of the string
DB 30 DUP (?) ; To store actual string
MSG2 DB 0DH, 0AH, 'The reversed string is:' ; MSG2 to store the reversed string is
REV DB 30 DUP (?) ; To store the reversed string

.CODE ; code start here
START:
MOV AX, @DATA ; Data segment start here
MOV DS, AX
MOV ES, AX ; moving AX to ES
DISP MSG1 ; MSG1 is a parameter to MSG of macro DISP, it displays
; input a string:
MOV DX, OFFSET SRC ; move Source offset address to DX
MOV AH, 0AH ; Function code to read a string
INT 21H ; DOS interrupt 21H
MOV SI, OFFSET SRC+2 ; increment the source offset by 2 and store the address
; to SI
MOV DI, OFFSET REV-1 ; decrement the reverse offset address by 1 and store the
; results in DI
MOV CL, SRC+1 ; Length of the string, here SRC is size of the string, it is
; incremented by 1
MOV CH, 00 ; make CH=0
ADD DI, CX ; Add cout value to DI
MOV BYTE PTR [DI+1], '$' ; End character for function 09H
CLD ; Clear Direction Flag

NEXT:
MOVSB ; move string of words depending on CLD, SI,DI
; automatically increase or decrease by 2

SUB DI, 0002 ; subtract 0002 with DI
LOOP NEXT

DISP MSG2 ; Display message 2
MOV AH, 4CH ; program termination
INT 21H ; DOS interrupt 21H

END START

Assignment

Write a program to compare two arrays. If they are same then display 'IDENTICAL', if not, display ‘NOT IDENTICAL’. Make use of the string instruction CMPSB.

Tried, but did not work correctly

.model small
.stack 20

.data
array1 db 'Electronic$'
array2 db 'Electronics$'

equalMsg do 'Identical$'
unequalMsg do 'Not Identical$'

.code
start:
mov ax, @data
mvo ds, ax
mov es, ax

lea si, array1
lea di, array2

cmpsb
jz equal
jnz unequal

equal:
mov ah, 09h
mov dx, offset equalMsg
int 21h
mov ah, 4Ch
int 21h

unequal:
mov ah, 09h
mov dx, offset unequalMsg
int 21h
mov ah, 4Ch
int 21h

end start
Last Updated: 2023-01-25 ; Contributors: AhmedThahir

Comments