Code-1: Print a message 'hello world'
.MODEL SMALL ; Use the small memory model (code and data in separate 64KB segments)
.STACK 100H ; Reserve 256 bytes (100H in hex) for the stack
.DATA ; Start of the DATA segment
MSG DB 'HELLO WORLD$' ; Define a string "HELLO WORLD" ending with '$' (DOS uses '$' as end marker)
.CODE ; Start of the CODE segment
MAIN PROC ; Begin the MAIN procedure (program starts here)
MOV AX, @DATA ; Load the address of DATA segment into register AX
MOV DS, AX ; Copy that address into DS register (so CPU knows where our data is stored)
LEA DX, MSG ; Load the address of the string MSG into register DX
MOV AH, 09H ; Set DOS function 09H (Display string until '$')
INT 21H ; Call DOS interrupt 21H → prints "HELLO WORLD" on screen
MOV AH, 4CH ; Set DOS function 4CH (Terminate program)
INT 21H ; Call DOS interrupt 21H → end the program cleanly
MAIN ENDP ; End of the MAIN procedure
END MAIN ; Tell assembler that program execution starts from MAIN(code-box)
Code-2: Print all the ASCII symbols from 0 to 256
.MODEL SMALL
.STACK 100H
.CODE
MAIN PROC
MOV AH,2
MOV CX,256
MOV DL,0
PRINT_LOOP:
INT 21H
INC DL
DEC CX
JNZ PRINT_LOOP
MOV AH,4CH
INT 21H
MAIN ENDP
END MAIN (code-box)
Code-3: Convert a lowercase letter into uppercase
.MODEL SMALL
.STACK 100H
.DATA
MSG1 DB 'ENTER THE LOWERCASE LATTER:$'
MSG2 DB 'CORRESPONDING UPPERCASE LATTER IS:'
CHAR DB ? , '$'
.CODE
MAIN PROC
MOV AX,@DATA
MOV DS,AX
LEA DX,MSG1
MOV AH,9
INT 21H
MOV AH,1
INT 21H
SUB AL,20H
MOV CHAR,AL
LEA DX,MSG2
MOV AH,9
INT 21H
MOV AH,4CH
INT 21H
MAIN ENDP
END MAIN (code-box)
Code-4: Convert an uppercase letter into lowercase
.MODEL SMALL
.STACK 100H
.DATA
MSG1 DB 'ENTER THE UPPERCASE LATTER:$'
MSG2 DB 'CORRESPONDING LOWERCASE LATTER IS:'
CHAR DB ? , '$'
.CODE
MAIN PROC
MOV AX,@DATA
MOV DS,AX
LEA DX,MSG1
MOV AH,9
INT 21H
MOV AH,1
INT 21H
ADD AL,20H
MOV CHAR,AL
LEA DX,MSG2
MOV AH,9
INT 21H
MOV AH,4CH
INT 21H
MAIN ENDP
END MAIN (code-box)
Code-5: Addition of two numbers
.MODEL SMALL
.STACK 100H
.DATA
MSG1 DB 10,13,'ENTER YOUR FIRST NUMBER:$'
MSG2 DB 10,13,'ENTER YOUR SECOND NUMBER:$'
MSG3 DB 10,13,'ADD=$'
N1 DB ?
N2 DB ?
RE DB ?
.CODE
MAIN PROC
MOV AX,@DATA
MOV DS,AX
LEA DX,MSG1
MOV AH,9
INT 21H
MOV AH,1
INT 21H
SUB AL,30H
MOV N1,AL
LEA DX,MSG2
MOV AH,9
INT 21H
MOV AH,1
INT 21H
SUB AL,30H
MOV N2,AL
MOV AL,N1
ADD AL,N2
MOV RE,AL
ADD AH,30H
ADD AL,30H
MOV BX,AX
LEA DX,MSG3
MOV AH,9
INT 21H
MOV AH,2
MOV DL,BH
INT 21H
MOV AH,2
MOV DL,BL
INT 21H
MOV AH,4CH
INT 21H
MAIN ENDP
END MAIN (code-box)
Code-6: Multiplication of two numbers
.MODEL SMALL
.STACK 100H
.DATA
NUM1 DB ?
NUM2 DB ?
RESULT DB ?
MSG1 DB 10,13,'ENTER FIRST NUMBER: $'
MSG2 DB 10,13,'ENTER SECOND NUMBER: $'
MSG3 DB 10,13,'AFTER MULTIPLYCATION RESULT IS: $'
.CODE
MAIN PROC
MOV AX,@DATA
MOV DS,AX
LEA DX,MSG1
MOV AH,9
INT 21H
MOV AH,1
INT 21H
SUB AL,30H
MOV NUM1,AL
LEA DX,MSG2
MOV AH,9
INT 21H
MOV AH,1
INT 21H
SUB AL,30H
MOV NUM2,AL
MUL NUM1
MOV RESULT,AL
AAM
ADD AH,30H
ADD AL,30H
MOV BX,AX
LEA DX,MSG3
MOV AH,9
INT 21H
MOV AH,2
MOV DL,BH
INT 21H
MOV AH,2
MOV DL,BL
INT 21H
MOV AH,4CH
INT 21H
MAIN ENDP
END MAIN (code-box)
Code-7: Taking input a string
.MODEL SMALL
.STACK 100H
.DATA
STRING DB ?
EXT DB '$'
MSG DB 10,13,'ENTER STRING $'
.CODE
MAIN PROC
MOV AX,@DATA
MOV DS,AX
LEA DX,MSG
MOV AH,9
INT 21H
LEA SI,STRING
INP:
MOV AH,1
INT 21H
MOV [SI],AL
INC SI
CMP AL,0DH
JNZ INP
MOV [SI],'$'
LEA DX,STRING
MOV AH,9
INT 21H
MOV AH,4CH
INT 21H
MAIN ENDP
END MAIN (code-box)
Code-8: Reverse a string
.MODEL SMALL
.STACK 100H
.CODE
MAIN PROC
MOV AH,2
MOV DL,'?'
INT 21H
XOR CX,CX
MOV AH,1
INT 21H
WHILE_:
CMP AL,0DH
JE END_WHILE
PUSH AX
INC CX
INT 21H
JMP WHILE_
END_WHILE:
MOV AH,2
MOV DL,0DH
INT 21H
MOV DL,0AH
INT 21H
JCXZ EXIT
TOP:
POP DX
INT 21H
LOOP TOP
EXIT:
MOV AH,4CH
INT 21H
MAIN ENDP
END MAIN(code-box)
Code-9: Determine prime number
.MODEL SMALL
.STACK 100H
.DATA
NUM DB ?
MSG1 DB 10,13,'ENTER NO: $'
MSG2 DB 10,13,'NOT PRIME: $'
MSG3 DB 10,13,'PRIME $'
.CODE
MAIN PROC
MOV AX,@DATA
MOV DS,AX
LEA DX,MSG1
MOV AH,9
INT 21H
MOV AH,1
INT 21H
SUB AL,30H
MOV NUM,AL
CMP AL,1
JLE LBL2
MOV AH,00
CMP AL,3
JLE LBL3
MOV AH,00
MOV CL,2
DIV CL
MOV CL,AL
LBL1:
MOV AH,00
MOV AL,NUM
DIV CL
CMP AH,00
JZ LBL2
DEC CL
CMP CL,1
JNE LBL1
JMP LBL3
LBL2:
MOV AH,9
LEA DX,MSG2
INT 21H
JMP EXIT
LBL3:
MOV AH,9
LEA DX,MSG3
INT 21H
EXIT:
MOV AH,4CH
INT 21H
MAIN ENDP
END MAIN (code-box)
Quick Reference: DOS INT 21H Functions Used in These Programs
| Function (AH) | What it does | Input / Output |
|---|---|---|
| 01H | Read one character from the keyboard (with echo) | Character returned in AL |
| 02H | Display one character | Character to print must be in DL |
| 09H | Display a string | DS:DX points to the string, which must end with $ |
| 4CH | Terminate the program and return to DOS | AL = return code (optional) |
Common Mistakes and How to Fix Them
1. Two-digit results in Code-5. If the sum is 10 or more (for example 7 + 5), the original code prints the wrong characters. UseAAM to split the result into tens and units digits before converting to ASCII:
MOV AL,N1
ADD AL,N2
MOV AH,0
AAM ; AH = tens digit, AL = units digit
ADD AH,30H ; convert tens to ASCII
ADD AL,30H ; convert units to ASCII
MOV BX,AX
; then print BH and BL with AH=2, INT 21H as before
2. "Operand must have size" error in Code-7. MASM cannot tell whether MOV [SI],'$' is a byte or a word. Write MOV BYTE PTR [SI],'$' instead.
3. String buffer too small in Code-7. STRING DB ? reserves only one byte, so a long input overwrites the data after it. Reserve a proper buffer, for example STRING DB 100 DUP('$').
4. Forgetting the $ terminator. DOS function 09H keeps printing until it finds a $ character, so every string printed this way must end with $.
5. Forgetting to load DS. Always start with MOV AX,@DATA and MOV DS,AX, otherwise your messages print as garbage.
Practice MCQs for Job and University Exams
1. Which DOS function displays a string ending with $?
(a) 01H
(b) 02H
(c) 09H
(d) 4CH
Answer: (c)
2. To print a single character with INT 21H function 02H, the character must be placed in:
(a) AL
(b) BL
(c) DL
(d) CL
Answer: (c)
3. After INT 21H function 01H, the character typed by the user is stored in:
(a) AL
(b) DL
(c) BL
(d) AH
Answer: (a)
4. Which instruction converts a binary value in AL into two unpacked BCD digits in AH and AL?
(a) AAA
(b) AAM
(c) DAA
(d) CBW
Answer: (b)
5. What is the difference between the ASCII codes of a lowercase letter and its uppercase letter?
(a) 10H
(b) 20H
(c) 30H
(d) 40H
Answer: (b)
6. Which instruction ends an 8086 program and returns control to DOS?
(a) MOV AH,4CH followed by INT 21H
(b) HLT
(c) RET
(d) IRET
Answer: (a)
Frequently Asked Questions (FAQs)
Q: Why do we subtract 30H from a digit typed on the keyboard?
A: The keyboard returns the ASCII code of the digit. The ASCII codes for 0 to 9 are 30H to 39H, so subtracting 30H gives the actual number value.
Q: Why do we add or subtract 20H to change letter case?
A: In ASCII, each lowercase letter is exactly 20H (32) higher than its uppercase letter. For example, A is 41H and a is 61H.
Q: Can I run these programs on Windows 10 or 11?
A: Yes. Use MASM inside the DOSBox emulator. Our step-by-step DOSBox tutorial (linked below) shows how.
good examples file handle should also be covered .. akjain 9323917311
ReplyDeleteI find the provided assembly language code examples to be very helpful for learning.
ReplyDeleteThank you, Damian! I'm glad the examples are helping with your learning. Feel free to ask if you get stuck on any program.
Delete