Assembly language is a low-level programming language that allows developers to write software executed directly by a computer’s central processing unit (CPU). Unlike high-level languages, assembly provides precise control over hardware, making it particularly useful for microprocessor programming— such as the classic Intel 8086 microprocessor.
To write effective assembly programs for the 8086, it’s essential to understand the internal structure of the processor. This knowledge enables you to write code that efficiently uses the CPU’s registers, memory, and instructions—resulting in programs that are optimized for speed and performance.
Learning 8086 assembly programming with MASM is one of the best ways to build a solid understanding of microprocessors and low-level computing. Start small with simple programs like printing text or adding numbers, and gradually move on to more complex tasks such as loops, conditionals, and memory operations.
If you are just starting, MASM is an excellent way to get hands-on practice with real-world assembly coding and see How to run your First program using MASM assembler
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)
By studying and practicing with examples such as these, you’ll develop a strong grasp of how computers truly execute instructions—something that goes beyond high-level programming and become proficient in programming the 8086 microprocessor. Good luck!
good examples file handle should also be covered .. akjain 9323917311
ReplyDeleteI find the provided assembly language code examples to be very helpful for learning.
ReplyDelete