0% found this document useful (0 votes)
9 views27 pages

DOS Assembly Language Examples

The document contains several assembly language programs demonstrating various functionalities, including arithmetic operations, input/output handling, file operations, and a simple password check. Each program is structured with comments explaining the purpose of each section and the operations performed. The examples illustrate basic programming concepts in assembly language, such as data manipulation, loops, and system calls.

Uploaded by

nadarharshaw
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views27 pages

DOS Assembly Language Examples

The document contains several assembly language programs demonstrating various functionalities, including arithmetic operations, input/output handling, file operations, and a simple password check. Each program is structured with comments explaining the purpose of each section and the operations performed. The examples illustrate basic programming concepts in assembly language, such as data manipulation, loops, and system calls.

Uploaded by

nadarharshaw
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Exp02

ORG 100h

MOV AL, 05H ; Move the first number (5) into the 8-bit AL register
(low part of AX)
MOV BL, 03H ; Move the second number (3) into the 8-bit BL register
(low part of BX)
ADD AL, BL ; Add BL to AL. The result (08H) is stored in AL.

RET ; Return to OS

END
Exp 03:
org 100h

mov al, 0Ah


mov bl, al
mov cx, 8

print:
mov dl, '0'
test bl, 10000000b
jz zero
mov dl, '1'
zero:
mov ah,2
int 21h
shl bl, 1
loop print

mov dl, 'b'


int 21h

mov ah, 0
int 16h

ret
Exp 04
.model small
.data
msg1 db "Enter first number (0-9): $"
msg2 db 0dh, 0ah, "Enter second number (0-9): $" ; 0dh, 0ah for new
line
msg3 db 0dh, 0ah, "The sum is: $"
.code
main proc
; Initialize Data Segment (DS)
mov ax, @data
mov ds, ax

; Display the first prompt message


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

; Get the first number from the keyboard


mov ah, 01h
int 21h
; Convert the ASCII character to a decimal number (e.g., '1' becomes
1)
sub al, '0'
mov bl, al ; Store the first number in the BL register

; Display the second prompt message


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

; Get the second number from the keyboard


mov ah, 01h
int 21h
; Convert ASCII to decimal
sub al, '0'

; Add the second number to the first one (result is in AL)


add al, bl

; ASCII Adjust for Addition to handle two-digit results


; This divides AL by 10. Quotient goes to AH, remainder to AL.
aam

; Move the two digits of the result into BX for easy access
mov bx, ax
; Convert the two digits back to ASCII for printing
add bh, '0' ; Convert tens digit
add bl, '0' ; Convert units digit

; Display the result message


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

; Print the tens digit of the sum


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

; Print the units digit of the sum


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

; Terminate the program and return to DOS


mov ah, 4ch
int 21h
main endp
end main
Exp 05
.MODEL SMALL
.STACK 100H

.DATA
BLOCK_A DW 10H, 20H, 30H, 40H
BLOCK_B DW 50H, 60H, 70H, 80H

.CODE
MAIN PROC
; Initialize DS and ES to the data segment
MOV AX, @DATA
MOV DS, AX
MOV ES, AX

; Initialize pointers and loop counter


MOV SI, OFFSET BLOCK_A
MOV DI, OFFSET BLOCK_B
MOV CX, 4

EXCHANGE_LOOP:
; Swap words using AX as temporary storage
MOV AX, [SI] ; Load word from BLOCK_A
XCHG AX, ES:[DI] ; Swap AX with word from BLOCK_B (AX now holds old
BLOCK_B word)
MOV [SI], AX ; Store old BLOCK_B word back into BLOCK_A

; Move pointers to the next word (2 bytes)


ADD SI, 2
ADD DI, 2

LOOP EXCHANGE_LOOP

; Terminate the program


MOV AH, 4CH
INT 21H

MAIN ENDP
END MAIN
Exp 06
; Filename: PASSWORD_LOOP.ASM; Filename: PASSWORD_LOOP.ASM
.MODEL SMALL
.CODE
ORG 100H

START:
; --- 1. SETUP ---
MOV AH, 09H ; Display Prompt
LEA DX, PROMPT
INT 21H

; Setup for Loop (Explicit Address Storage/Comparison)


LEA SI, PASSWORD ; SI now points to the stored password string
('P','A','S','S')
MOV CX, 4 ; CX = Number of characters to check (length of
'PASS')

LOOP_START:
; --- 2. GET INPUT (BIOS INT 16h / AH=00h) ---
MOV AH, 00H ; Read character, NO ECHO
INT 16H ; AL = User's keypress
;Read Character with Echo
;MOV AH,01H
;INT 21H

; --- 3. COMPARE KEYPRESS (AL) TO STORED ADDRESS ([SI]) ---


CMP AL, [SI] ; Compare user input (AL) with the byte at address SI
JNE FAIL_LOOP ; Mismatch? Fail immediately.

; --- 4. ADVANCE AND LOOP ---


INC SI ; Move SI to the next character in the stored
password
LOOP LOOP_START ; Decrement CX and jump to LOOP_START if CX is not
zero

; --- 5. SUCCESS ---


MOV AH, 09H
LEA DX, SUCCESS
INT 21H
JMP END_PROG

FAIL_LOOP:
; --- 6. FAILURE ---
MOV AH, 09H
LEA DX, FAILURE
INT 21H
END_PROG:
; --- 7. TERMINATE PROGRAM (DOS INT 21h / AH=4Ch) ---
MOV AH, 4CH
INT 21H

; --- DATA SECTION ---


CR EQU 0DH
LF EQU 0AH

PROMPT DB 'Enter 4-character Password: $'


PASSWORD DB 'PASS' ; <<< The stored password string
SUCCESS DB CR, LF, 'Access Granted!', CR, LF, '$'
FAILURE DB CR, LF, 'Access Denied!', CR, LF, '$'

END START
Exp07
.MODEL SMALL
.STACK 100h

.DATA
; Filename (must be null-terminated for handle functions)
fileName DB '[Link]', 0

; Data to write
writeData DB 'Hello World!!', 13, 10 ; 13, 10 is CR/LF
writeLen EQU $ - writeData

; Buffer to read data into


readBuffer DB 100 DUP(?)

; Variable to store the file handle


fileHandle DW ?

; User messages
msgCreating DB '1. Creating file...$'
msgWriting DB 13, 10, '2. Writing to file...$'
msgClosing1 DB 13, 10, '3. Closing file...$'
msgOpening DB 13, 10, '4. Re-opening file...$'
msgReading DB 13, 10, '5. Reading from file...$'
msgClosing2 DB 13, 10, '6. Closing file...$'
msgDisplay DB 13, 10, '7. Content read from file:', 13, 10, '$'
msgDisplayEnd DB 13, 10, '$'
msgDeleting DB 13, 10, '8. Deleting file...$'
msgSuccess DB ' Success!', 13, 10, '$'
msgError DB ' Error!', 13, 10, '$'

.CODE
main PROC
; Set up DS to point to our data segment
MOV AX, @DATA
MOV DS, AX

; --- 1. Create file ---


LEA DX, msgCreating
MOV AH, 09h ; DOS print string function
INT 21h

MOV AH, 3Ch ; DOS function: Create file


MOV CX, 0 ; Attribute: Normal
LEA DX, fileName
INT 21h
JC handleError ; Jump if Carry Flag is set (error)
MOV [fileHandle], AX ; Store the returned handle
LEA DX, msgSuccess
MOV AH, 09h
INT 21h

; --- 2. Write to file ---


LEA DX, msgWriting
MOV AH, 09h
INT 21h

MOV AH, 40h ; DOS function: Write to file


MOV BX, [fileHandle]
MOV CX, writeLen
LEA DX, writeData
INT 21h
JC handleError

LEA DX, msgSuccess


MOV AH, 09h
INT 21h

; --- 3. Close file ---


LEA DX, msgClosing1
MOV AH, 09h
INT 21h

MOV AH, 3Eh ; DOS function: Close file


MOV BX, [fileHandle]
INT 21h
JC handleError

LEA DX, msgSuccess


MOV AH, 09h
INT 21h

; --- 4. Open file for reading ---


LEA DX, msgOpening
MOV AH, 09h
INT 21h

MOV AH, 3Dh ; DOS function: Open file


MOV AL, 0 ; Access mode: Read-only
LEA DX, fileName
INT 21h
JC handleError
MOV [fileHandle], AX ; Store new handle
LEA DX, msgSuccess
MOV AH, 09h
INT 21h

; --- 5. Read from file ---


LEA DX, msgReading
MOV AH, 09h
INT 21h

MOV AH, 3Fh ; DOS function: Read from file


MOV BX, [fileHandle]
MOV CX, 100 ; Max bytes to read
LEA DX, readBuffer
INT 21h
JC handleError

; Null-terminate the buffer for printing


MOV BX, AX ; AX contains bytes read
MOV BYTE PTR readBuffer[BX], '$' ; Use '$' for INT 21h/09h

LEA DX, msgSuccess


MOV AH, 09h
INT 21h

; --- 6. Close file again ---


LEA DX, msgClosing2
MOV AH, 09h
INT 21h

MOV AH, 3Eh


MOV BX, [fileHandle]
INT 21h
JC handleError

LEA DX, msgSuccess


MOV AH, 09h
INT 21h

; --- 7. Display content ---


LEA DX, msgDisplay
MOV AH, 09h
INT 21h

LEA DX, readBuffer


MOV AH, 09h
INT 21h
LEA DX, msgDisplayEnd
MOV AH, 09h
INT 21h

; --- 8. Delete file ---


LEA DX, msgDeleting
MOV AH, 09h
INT 21h

MOV AH, 41h ; DOS function: Delete file


LEA DX, fileName
INT 21h
JC handleError

LEA DX, msgSuccess


MOV AH, 09h
INT 21h

JMP exitProgram

handleError:
LEA DX, msgError
MOV AH, 09h
INT 21h
; Here you could print the error code from AX if desired

exitProgram:
; Terminate program
MOV AH, 4Ch
INT 21h

main ENDP
END main
Exp08
DATA SEGMENT
STR1 DB 0AH,0DH,"[Link] CURSOR"
DB 0AH,0DH,"[Link] BLOCK CURSOR"
DB 0AH,0DH,"[Link] BLOCK CURSOR"
DB 0AH,0DH,"[Link] CURSOR"
DB 0AH,0DH,"OTEHR KEY: EXIT"
DB 0AH,0DH,"=$"
DATA ENDS

CODE SEGMENT
ASSUME CS:CODE,DS:DATA
START:MOV AX,DATA
MOV DS,AX

LEA DX,STR1
MOV AH,09H
INT 21H

BACK:MOV AH,07H
INT 21H

MOV AH,03H
INT 10H
MOV AH,02H
INT 10H

CMP AL,'1'
JE UNDERLINE
CMP AL,'2'
JE HALFBLOCK
CMP AL,'3'
JE FULLBLOCK
CMP AL,'4'
JE HIDE
JMP EXIT

UNDERLINE:MOV CX,0607H
JMP NEXT

HALFBLOCK:MOV CX,0407H
JMP NEXT
FULLBLOCK:MOV CX,0007H
JMP NEXT
HIDE:MOV CX,2607H
JMP NEXT
NEXT:MOV AH,01H
INT 10H
JMP BACK

EXIT:MOV AH,4CH
INT 21H
CODE ENDS
END START
Exp09
DATA SEGMENT
Q DB 8H
M DB 5H
RESULT DW ?
DB 100 DUP(00)
DATA ENDS

CODE SEGMENT
ASSUME CS: CODE, DS: DATA

START: MOV AX, DATA


MOV DS, AX

MOV AX, 0000H


MOV BX, 0000H
MOV AL, Q
MOV BL, M
MOV CL, 8
CLC

BACK: MOV DX, 00H


MOV DX, AX
RCL DX, 01
AND DX, 03H

CMP DX, 01H


JE Q01
CMP DX, 02H
JE Q10

NEXT: SAR AX, 01


LOOP BACK
JMP EXIT

Q10: SUB AH, BL


JMP NEXT

Q01: ADD AH, BL


JMP NEXT

EXIT: MOV RESULT, AX


INT 3H
CODE ENDS
END START
Exp10
ORG 100h

JMP START

; --- DATA DEFINITIONS ---


; Each word in CACHE_LINES represents a line.
; High Byte = Valid Bit (0=Invalid, 1=Valid)
; Low Byte = Tag
CACHE_LINES DW 4 DUP(0)

; A small, predefined sequence of memory block numbers to access.


; This sequence is designed to show different cache behaviors.
ACCESS_SEQUENCE DB 0, 1, 4, 0, 1
SEQUENCE_LEN EQU $-ACCESS_SEQUENCE

HITS DW 0
MISSES DW 0

; --- STRINGS FOR OUTPUT ---


HIT_MSG DB 'Cache Hits: $'
MISS_MSG DB 'Cache Misses: $'
NEWLINE DB 0Dh, 0Ah, '$' ; Carriage Return + Line Feed

START:
; --- Main Simulation Loop ---
MOV CX, SEQUENCE_LEN ; Load the number of accesses into counter
LEA SI, ACCESS_SEQUENCE ; Point SI to the start of the sequence

SIM_LOOP:
MOV AL, [SI] ; Get the current block number to access

CALL DIRECT_MAP_CHECK ; Check cache for this block number

INC SI ; Move to the next block number in the


sequence
LOOP SIM_LOOP ; Repeat for all accesses

; --- Display Results ---


; Print "Hits" count
LEA DX, HIT_MSG
MOV AH, 09h
INT 21h ; Display the hits message
MOV AX, [HITS]
CALL PRINT_NUM ; Call procedure to print the number

; Print a new line


LEA DX, NEWLINE
MOV AH, 09h
INT 21h

; Print "Misses" count


LEA DX, MISS_MSG
MOV AH, 09h
INT 21h ; Display the misses message
MOV AX, [MISSES]
CALL PRINT_NUM ; Call procedure to print the number

; --- Program Termination ---


MOV AH, 4Ch
INT 21h

;
==========================================================================
===
; PROCEDURE: DIRECT_MAP_CHECK
; Description: Simulates a cache lookup for a direct-mapped cache.
; Input: AL = Main Memory Block Number
; Output: Increments global HITS or MISSES counter.
; Registers modified: AX, BX, CX, DX, DI
;
==========================================================================
===
DIRECT_MAP_CHECK PROC
PUSH AX ; Preserve registers
PUSH BX
PUSH DI

; --- Calculate Index and Tag ---


; Index = AL MOD 4 (Remainder)
; Tag = AL / 4 (Quotient)
XOR AH, AH ; Clear AH for 8-bit division
MOV BL, 4 ; Divisor is the number of cache lines
DIV BL ; AX / BL => AL=Quotient(Tag),
AH=Remainder(Index)

MOV BH, AH ; Store Index in BH


MOV BL, AL ; Store Tag in BL

; --- Locate Cache Line ---


MOV AX, BX ; Copy Index (in AH/BH) to AX for calculation
MOV AL, AH ; Move Index to AL
XOR AH, AH ; Clear AH
MOV DI, 2 ; Each cache entry is 2 bytes (a word)
MUL DI ; AX = Index * 2
LEA DI, CACHE_LINES ; Get base address of cache
ADD DI, AX ; DI now points to the correct cache line

; --- Check for Hit or Miss ---


MOV AX, [DI] ; Load the Valid/Tag word from the cache line
; AH = Valid Bit, AL = Stored Tag

CMP AH, 1 ; Is the valid bit set?


JNE IS_MISS ; If not, it's a compulsory miss.

CMP AL, BL ; Valid bit is set, now compare tags


JNE IS_MISS ; If tags don't match, it's a conflict miss.

IS_HIT:
INC [HITS]
JMP END_CHECK

IS_MISS:
INC [MISSES]
; Update the cache line with the new tag and set valid bit
MOV AH, 1 ; Set Valid bit to 1
MOV AL, BL ; Load the new Tag
MOV [DI], AX ; Store the new Valid/Tag word in the cache
line

END_CHECK:
POP DI ; Restore registers
POP BX
POP AX
RET
DIRECT_MAP_CHECK ENDP

;
==========================================================================
===
; PROCEDURE: PRINT_NUM
; Description: Prints a 16-bit unsigned number in decimal.
; Input: AX = The number to print.
; Output: Prints number to the screen.
; Registers modified: AX, BX, CX, DX
;
==========================================================================
===
PRINT_NUM PROC
PUSH AX
PUSH BX
PUSH CX
PUSH DX

MOV CX, 0 ; Initialize digit counter


MOV BX, 10 ; Divisor for decimal conversion

CONVERT_LOOP:
XOR DX, DX ; Clear DX for 16-bit division
DIV BX ; AX = AX / 10, DX = Remainder
PUSH DX ; Push the digit (remainder) onto the stack
INC CX ; Increment digit count
CMP AX, 0 ; Is the quotient zero?
JNE CONVERT_LOOP ; If not, continue converting

PRINT_LOOP:
POP DX ; Pop a digit
ADD DL, '0' ; Convert digit to ASCII character
MOV AH, 02h ; DOS function to print a character
INT 21h
LOOP PRINT_LOOP ; Repeat for all digits

POP DX
POP CX
POP BX
POP AX
RET
PRINT_NUM ENDP
Exp11
DATA SEGMENT
MSG DB 0DH,0AH,"-- -- -- --OF DF IF TF SF ZF -- AF -- PF -- CF$"
NEWL DB 0DH,0AH,"$"
FLAG DW ?
DATA ENDS

CODE SEGMENT
ASSUME CS:CODE,DS:DATA

START:MOV AX,DATA
MOV DS,AX

MOV DX,OFFSET MSG


MOV AH,09H
INT 21H
LEA DX,NEWL
MOV AH,09H
INT 21H
CLI
STC
STD
PUSHF
POP BX
MOV FLAG,BX
MOV CX,16
MOV BX,8000H

REPEAT:MOV AX,FLAG
AND AX,BX
JZ DIS_0
MOV DL,'1'
JMP NEXT

DIS_0:MOV DL,'0'
NEXT:MOV AH,02H
INT 21H
MOV DL,' '
MOV AH,02H
INT 21H
INT 21H
ROR BX,1
LOOP REPEAT

MOV AH,4CH
INT 21H
CODE ENDS
END START

You might also like