
General format of writing the program.
- under .model define the model you will be using such as small ,medium or large
- under.data initialize the variables and constants
- under.code write the code for particular task
- To exit the file we use ,mov ah,4ch ,int 21h
- end our code with , end .
A.Code to accept a two digit number from keyboard
- mov ax,@data and move ds,ax is done to point data segment register to data segment
- mov ah,01h and int 21 h is done to take input of first digit(4 bits)
- cmp value of al,3Ah to see see if the value is a character or a number
- if it’s a number then carry flag becomes =1
- JC down 1 means , if carry is generated then jump to instruction down 1
- if carry flag =0, then subtract 07h as difference between ASCII value of A and 1 is 7.
- convert ASCII value of Operand , by using AND gate.
- then we ROR , to switch the value from unit’s place to ten’s place
- then store that value in a temporary variable , assume bl
- for second input we repeat the steps done from2 -7
- now al has unit’s place and bl has ten’s place we add the two to get the number
.model small
;tells the memory and code model
.data
n1 db 05h
;is used to start the initialized and constant values
.code
mov ax,@data
mov ds,ax
;This part is used to point data segment register to data segment in the memory
mov ah,01h
int 21h
;This part is used to input a number ,an int 21 h is an interrupt it contains functionality
CMP al,3Ah
;This part is used to compare value with 3A , as it is used to differentiate between character and a number.
JC Down1
;Jump if carry , if it is a number then CF=1, otherwise 0
SUB al,07h
;subtract from characters ,to make it in range to input
Down1: AND al,0Fh
;We and the ASCII value and convert it into hexadecimal value
ROR al,04h
;To Switch the unit digit and tens place, by rotating left 4 bits
mov bl,al
;This is used to store al value in bl ,temporary
mov ah,01h
int 21hCMP al,3Ah
JC Down2
SUB al,07h
Down2: AND al,0Fh
;al here has the the unit place of 4 bits and bl has tens place of 4 bits
ADD bl,al
;hence now in bl we got the two digit number
mov ah,4ch
int 21h
;DOS interrupt int 21/4Ch is EXIT - TERMINATE WITH RETURN CODE, the content of al is used as the return code and the process is terminate
end
;Assembler needs END directive to end the file. END can be written without Main. just end the file with END.

B.Code to Display 8 bit number
- The Accepting two digit number can be copied from 1st code ,
- BL contains the two digit number , and is moved to CL
- now to print 8 bit double digit number , we first have to print ten’s place and then , bl ,0f0h to get tens place of the 8 bit number
- then ror are it , to put it in units place
- cmp bl,0Ah to check if its alphabet or a number
- if its a alphabet then Carry flag=0 , then add 07 h ,as difference between ASCII value of A and 1 is 7.
- if carry flag =1, it means it was a number .
- Now add 30 to make it ASCII value of the number
- Move bl into dl for display
- then display the tenth’s value
- now repeat the process for 2 ND number display ,except extracting the ten’s place extract unit’s place by, AND BL,0Fh
MODEL SMALL
.DATA
.CODE
MOV AX,@DATA
MOV DS,AX
CALL ACCEPT ;CALL TO PROCEDURE-ACCEPT
CALL DISP ;CALL TO PROCEDURE- DISP
MOV AH,4CH
INT 21h
ACCEPT PROC NEAR ;TO ACCEPT A 8 BIT NUMBER
MOV AH,01h ;to accept keyword input
INT 21h
CMP AL,3Ah ;check if input is numeric or an alphabet
JC DOWN1
SUB AL,07h ;if alphabet is entered
DOWN1: AND AL,0Fh;if number is entered
ROR AL,04h ;putting to tens place
MOV BL,AL
MOV AH,01h ;to accept keyword input
INT 21h
CMP AL,3Ah ;check if input is numeric or an alphabet
JC DOWN2
SUB AL,07h ;if alphabet is entered
DOWN2: AND AL,0Fh;if number is entered
ADD BL,AL ;now BL has the final input
RET
ENDP ;end of procedure
DISP PROC NEAR ;to display 8 bit number
MOV BL,CL
AND BL,0F0h ;to get tens place of the 8 bit number
ROR BL,4h ;to put it in units place
CMP BL,0Ah ;to check if its an alphabet or a number
JC DOWN3
ADD BL,07h ;if alphabet
DOWN3: ADD BL,30h;if number
MOV DL,BL
MOV AH,02h ;printing DL register
INT 21h
MOV BL,CL
AND BL,0Fh ;to get units place of the 8 bit number
CMP BL,0Ah ;to check if its an alphabet or a number
JC DOWN4
ADD BL,07h ;if alphabet
DOWN4: ADD BL,30h;if number
MOV DL,BL
MOV AH,02h ;printing DL register
INT 21h
RET
ENDP
END

C.Menu driven program :
- We have a macro for printing the message’s
- Lea is used to load the first index of a string or an array
- mov ah,09h is used to print the string
- First we have to select the choice we want and store it in c
- CMP C,31h ,ASCII value of 1 = 31 .Hence he wanted to use addition procedure
- We call Accept procedure for accepting each digit , hence we call it twice
- ADD CL , N1 ; add the 2 numbers and thus being saved to CL register and then we display it.
- We repeat it for other methods as well
MESS MACRO MSG ;Macro to print a message on console ,Mess name and msg is para meter
MOV AH,09h
LEA DX,MSG
;Load effective address
INT 21h
;Prints a string stored in the address in the DX Register.
ENDM
;end the macro
.MODEL SMALL
.DATA
N1 DB 00h;to accept number
N2 DB 00h
C DB 00h
MSG1 DB 0Ah,0dh, "MENU:$";to print menu to the console
$ to NULL
MSG2 DB 0Ah,0dh, "1)ADDITION :$"
;0Ah,0dh is to display data on the next line
MSG3 DB 0Ah,0dh, "2)SUBTRACTION :$"
MSG4 DB 0Ah,0dh, "3)MULTIPLICATION :$"
MSG6 DB 0Ah,0dh, "ENTER 1ST NUMBER=:$"
MSG7 DB 0Ah,0dh, "ENTER 2ND NUMBER=:$"
MSG8 DB 0Ah,0dh, "ANSWER=:$"
.CODE
MOV AX,@DATA
MOV DS,AX
MESS MSG1;printing menu
MESS MSG2
MESS MSG3
MESS MSG4
MOV AH,01h
INT 21h ;accepting choice
MOV C,AL ;saving the choice to a variable
CMP C,31h ;Ascii value of 1 = 31
JNE STOP1 ;Jump if stop is choice is not addition
MESS MSG6
CALL ACCEPT ;to accept a number
MOV N1,CL
MESS MSG7
CALL ACCEPT ;to accept a number
ADD CL , N1 ; add the 2 numbers and thus being saved to CL register
MOV CL,N1
MESS MSG8
CALL DISP ;displaying the answer
JMP STOP; to jump to end of code
STOP1:CMP C,32h; to compare to ‘2’
JNE STOP2
MESS MSG6
CALL ACCEPT ;to accept a number
MOV N1,CL
MESS MSG7
CALL ACCEPT ;to accept a number
SUB N1,CL ;to subtract and save answer to CL
MOV CL,N1
MESS MSG8
CALL DISP ;to display answer
JMP STOP
STOP2:CMP C,33h ;to compare to ‘3’
JNE STOP3
MESS MSG6
CALL ACCEPT ;to accept a number
MOV N1,CL
MESS MSG7
CALL ACCEPT ;to accept a number
MOV N2,CL
MOV AL,N1
MOV BL,N2
MUL BL; multiplying AL and BL and saving the answer in Al register
MOV CL,AL
MESS MSG8
CALL DISP ;to display answer
JMP STOP
STOP3: JMP STOP
STOP:
MOV AH,4CH
INT 21h
ACCEPT PROC NEAR;to accept 8 bit numbers
MOV AH,01h
INT 21h;to accept keyword input
CMP AL,3Ah;check if input is numeric or an alphabet
JC DOWN1
SUB AL,07h;if alphabet is entered
DOWN1;AND AL,0Fh;if number is entered
ROR AL,04h;putting to tens place
MOV BL,AL
MOV AH,01h
INT 21h;to accept keyword input
CMP AL,3Ah;check if input is numeric or an alphabet
JC DOWN2
SUB AL,07h;if alphabet is entered
DOWN2;AND AL,0Fh;if number is entered
ADD BL,AL;now BL has the final input
RET
ENDP;end of procedure
DISP PROC NEAR;to display 8 bit numbers
MOV BL,CL
AND BL,0F0h;to get tens place of the 8 bit number
ROR BL,4h;to put it in units place
CMP BL,0Ah;to check if its an alphabet or a number
JC DOWN3
ADD BL,07h;if alphabet
DOWN3:ADD BL,30h;if number
MOV DL,BL
MOV AH,02h;printing DL register
INT 21h
MOV BL,CL
AND BL,0Fh;to get units place of the 8 bit number
CMP BL,0Ah;to check if its an alphabet or a number
JC DOWN4
ADD BL,07h;if alphabet
DOWN4:ADD BL,30h;if number
MOV DL,BL
MOV AH,02h;printing DL register
INT 21h
RET
ENDP
END
D.Assembly Language Program to perform 32/16 bit Division.
for 16 bit/8 bit ,
- Dividend -AX
- Quotient-al
- remainder-ah
for 32bit/16 bit
- Divident -DX AX
- Quotient-AX
- remainder-DX
- We first accept 4*8 bits of the Divident
- then we accept 2*8 bits of divisor
- DX has the higher 16 bits and AX has the lower 16 bits(divident)
- BX has the 16 bits (divisor)
- DIV BX (DX AX/BX)
- MOV R1,DH and MOV R2,DL , putting remainder in R1(high) and R2(low)
- Display R1 and R2
- MOV CX,AX , putting quotient in CX
- Display CX
No flag is altered by the DIV instruction.
MESS MACRO MSG;Macro to print a message on console
MOV AH,09h
LEA DX,MSG
INT 21h
ENDM
.MODEL SMALL
.DATA
MSG1 DB 0Ah,0dh, "ENTER 32 BIT NUMBER:$";messages to be shown to prompt user or inform user
MSG2 DB 0Ah,0dh, "ENTER 16 BIT NUMBER:$"
MSG3 DB 0Ah,0dh, "QUOTIENT=$"
MSG4 DB 0Ah,0dh, "REMAINDER=$"
N11 DB 00h; to store first 8 bits of the 32-bit number (DH)
N12 DB 00h; to store second 8 bits of the 32-bit number (DL)
N21 DB 00h; to store third 8 bits of the 32-bit number (AH)
N22 DB 00h; to store the last 8 bits of the 32-bit number (AL)
D1 DB 00h; to store first 8 bits of the 16-bit number
d2 db 00h; to store second 8 bits of the 16-bit number
R1 DB 00h; to store first 8 bits of the 16-bit number(remainder)
R2 DB 00h; to store second 8 bits of the 16-bit number(remainder)
.CODE
MOV AX,@DATA
MOV DS,AX
MESS MSG1
CALL ACCEPT;accept first 8 bits of the 32 bit number
MOV N11,BL
CALL ACCEPT; accept second 8 bits of the 32 bit number
MOV N12,BL
CALL ACCEPT; accept third 8 bits of the 32 bit number
MOV N21,BL
CALL ACCEPT; accept last 8 bits of the 32 bit number
MOV N22,BL
MESS MSG2
CALL ACCEPT; accept first 8 bits of the 16 bit number
MOV D1,BL
CALL ACCEPT; accept last 8 bits of the 16 bit number
MOV D2,BL; note that 32 bit number is stored like
;DX has the higher 16 bits and AX has the lower 16 bits
MOV DH,N11
MOV DL,N12
MOV AH,N21
MOV AL,N22
MOV BH,D1; moving the 2-8 bit numbers to BX register
MOV BL,D2
DIV BX; DX AX/BX
MOV R1,DH; putting remainder in R1(high) and R2(low)
MOV R2,DL
MOV CX,AX; putting quotient in CX
MESS MSG3
CALL DISP; displaying quotient
MOV CH,R1
MOV CL,R2
MESS MSG4
CALL DISP; displaying remainder
MOV AH,4CH
INT 21h
ACCEPT PROC NEAR; to accept 8 bit numbers
MOV AH,01h
INT 21h; to accept keyword input
CMP AL,3Ah; check if input is numeric or an alphabet
JC DOWN1
SUB AL,07h; if alphabet is entered
DOWN1: AND AL,0Fh; if number is entered
ROR AL,04h; putting to tens place
MOV BL,AL
MOV AH,01h
INT 21h; to accept keyword input
CMP AL,3Ah; check if input is numeric or an alphabet
JC DOWN2
SUB AL,07h; if alphabet is entered
DOWN2: AND AL,0Fh; if number is entered
ADD BL,AL; now BL has the final input
RET
ENDP; end of procedure
DISP PROC NEAR; to display 16-bit numbers
MOV BH,CH
AND BH,0F0h; to get tens place of the 8 bit number
ROR BH,4h; to put it in units place
CMP BH,0Ah; to check if its an alphabet or a number
JC DOWN3
ADD BH,07h; if alphabet
DOWN3: ADD BH,30h; if number
MOV DL,BH
MOV AH,02h; printing DL register
INT 21h
MOV BH,CH
AND BH,0Fh; to get units place of the 8 bit number
CMP BH,0Ah; to check if its an alphabet or a number
JC DOWN4
ADD BH,07h; if alphabet
DOWN4: ADD BH,30h; if number
MOV DL,BH
MOV AH,02h; printing DL register
INT 21h
MOV BH,CH
AND BH,0F0h; to get tens place of the 8-bit number
ROR BH,4h; to put it in units place
CMP BH,0Ah; to check if its an alphabet or a number
JC DOWN5
ADD BH,07h; if alphabet
DOWN5: ADD BH,30h; if number
MOV DL,BH
MOV AH,02h; printing DL register
INT 21h
MOV BH,CH
AND BH,0Fh; to get units place of the 8 bit number
CMP BH,0Ah; to check if its an alphabet or a number
JC DOWN6
ADD BH,07h; if alphabet
DOWN6: ADD BH,30h; if number
MOV DL,BH
MOV AH,02h; printing DL register
INT 21h
RET
ENDP
END
E.String handling


- first index tells the actual max length initialized
- second index tells the actual length of the word
- we point si to starting of the string Str1
- we then accept string from console ,
- then we request the second value from string to check the length,
- then we add 30 to increment the value and make it hexadecimal
MESS MACRO MSG; to let us to print a string
MOV AH,09h
LEA DX,MSG
INT 21h
ENDM
.MODEL SMALL
.STACK 1000H
.DATA
STR1 DB 30,?,30 dup('$');blank string
;arr db max_length, actual length, max length dup(initialization)
;first char in string is max length , second is actual length , further on is the ASCII values of characters
MSG1 DB 0Ah,0Dh,"ENTER STRING $";prompt message
MSG2 DB 0Ah,0Dh,"LENGTH IS : $"
.CODE
MOV AX,@DATA;start of code
MOV DS,AX
MESS MSG1
LEA SI, STR1; si will point to the start index of s
MOV AH,01Ah;to accept string from console
MOV DX,SI
INT 21h
MOV AL,[SI+1];moving the value in the second index (real length)
ADD AL,30h;to convert number to hexadecimal
MESS MSG2
MOV AH,02h;to print length
MOV DL,AL
INT 21h
MOV AH,4CH
INT 21h
END
One thought on “Assembly Code”