You are on page 1of 22

Programming Techniques

By:- Dr. Nikhil Kumar Marriwala


Problem-1
• Write a program to add a data byte located at the offset 0400h in
3000h segment to another data byte available at 0700h in the same
segment and then store the result at 0900h in the same segment?

• MOV AX, 3000h ; initializing data segment with the value


• MOV DS, AX ; 3000h
• MOV AX, [0400h] ; Get the first data byte from the offset 0400h
• ADD AX, [0700H] ; Add the first data byte to the second from offset
0700h
• MOV [0900H], AX ; Store the result of addition in accumulator register
• HLT ; Halt the operation

12/19/2023 Prepared By: Dr. Nikhil Marriwala 2


Problem-2
• Write a program to move the contents of the memory location
0400h to register BX and also to CX. Add immediate byte 07h to
the data residing in memory location, whose address is computed
using DS= 3000h and offset= 0700h. Store the result of the
addition in 0900h.

• MOV AX, 3000h ; Initialize the Ds with 3000h


• MOV DS, AX
• MOV BX, [0400H] ; Get the contents of 0400h in BX
• MOV CX, BX ; Copy the same contents in CX
• ADD [0700H], 07H ; Add byte 07h to the contents of 0700h
• MOV DX, [0700H] ; Store the result in DX
• MOV [0900H], DX ; Store the result in 0900h
• HLT ; Halt

12/19/2023 Prepared By: Dr. Nikhil Marriwala 3


Problem-3
• Problem 3. Write a program to add the contents of the memory location 3000: 0400h
to the contents of 4000: 0700h and store the result in 6000: 0900h?

• Solution. In this problem we will have to refer to two different memory locations in
different
• segments, hence we will have to initialize the data segment twice here with the given
values.

• MOV AX, 3000h ; Initialize the Ds with 3000h


• MOV DS, AX
• MOV AX, [0400H] ;Get the contents of 0400h in AX
• MOV CX, 4000H
• MOV DS, CX ; Initialize the Ds with 4000h
• MOV BX, [0700H] ; Get the contents of 0700h in AX
• ADD AX, BX ; Perform Addition
• MOV CX, 6000H ; Initialize the Ds with 6000h
• MOV DS, CX
• MOV [0900H], AX ; store the result in 0900h
• HLT ; Halt
12/19/2023 Prepared By: Dr. Nikhil Marriwala 4
TITLE: ADD TWO NUMBERS
OBJECTIVE: THIS PROGRAM TAKES ANY TWO NUMBERS AS INPUT FROM THE USER THROUGH
THE INPUT DEVICE(KEYBOARD) & PRINTS THEIR SUM ON THE STANDARD OUTPUT
DEVICE (SCREEN)
.MODEL SMALL
.STACK
.DATA
.CODE
Mov Ax, @Data ; Intializing of Data Segment
Mov Ds, AX
START:MOV AH,01 ;input first number
INT 21H
MOV BL,AL ;copying the contents of al i.e number inputted from keyboard to bl
AND BL,0FH ;converting ascii code of No 1 to hexa code
MOV AH,01 ;input second number
INT 21H
AND AL,0FH ;converting ascii code of No 2 to hexa
ADD AL,BL ;adding the two numbers
MOV DL,AL ;copying the sum to register dl
OR DL,30H ;converting hexa sum to its ascii equivalent

12/19/2023 Prepared By: Dr. Nikhil Marriwala 5


MOV AH,02 ;print the sum on the screen
INT 21H
MOV AH,4CH ;exit to dos prompt
INT 21H
END ;end of program

12/19/2023 Prepared By: Dr. Nikhil Marriwala 6


TITLE: ADD TWO NUMBERS
OBJECTIVE: THIS PROGRAM TAKES ANY TWO NUMBERS AS INPUT FROM THE USER THROUGH THE INPUT DEVICE(KEYBOARD)
ON TWO DIFFERENT LINES AND PRINTS THEIR SUM ON THE STANDARD OUTPUT DEVICE (SCREEN) ON A SEPARATE LINE
.MODEL SMALL
.STACK
.DATA
.CODE
START:MOV AH,01 ;input first number
INT 21H
MOV BL,AL ;copying the contents of al i.e number inputted from keyboard to bl
AND BL,0FH ;converting ascii code of No 1 to hexa code
MOV DH,0DH ;load dl register with carraige return code
MOV DL,0AH ;load dl register with line feed code
MOV AH,02 ;to position the cursor on the next line
INT 21H
MOV AH,01 ;input second number
INT 21H
AND AL,0FH ;converting ascii code of No 2 to hexa
ADD BL,AL ;add the 2 no.s & store the sum in bl register
MOV DL,0DH ;load dl register with carraige return code
MOV DL,0AH ;load dl register with line feed code

12/19/2023 Prepared By: Dr. Nikhil Marriwala 7


MOV AH,02 ;to position the cursor on the next line
INT 21H
MOV DL,BL ;copying the sum to register dl
OR DL,30H ;converting hexa sum to its ascii equivalent
MOV AH,02 ;print the sum on the monitor
INT 21H
MOV AH,4CH ;exit to dos prompt
INT 21H
END ;end of program

12/19/2023 Prepared By: Dr. Nikhil Marriwala 8


OBJECTIVE: THIS PROGRAM TAKES ANY TWO NUMBERS AS INPUT
FROM THE USER THROUGH THE INPUT DEVICE(KEYBOARD) AND
PRINTS THEIR SUM ON THE STANDARD OUTPUT DEVICE (SCREEN) BY
GIVING APPROPRIATE MESSAGES TO THE USER
.MODEL SMALL
.STACK
.DATA
MSG1 DB 'ENTER FIRST NO:$'
MSG2 DB 10,13,'ENTER SECOND NO:$'
MSG3 DB 10,13,'THE SUM IS:$'
.CODE
START:MOV AX,@DATA ;initializing the data segment
MOV DS,AX ;
LEA DX,MSG1 ;prompts the user to enter the first number
MOV AH,09 ;by displaying appropriate message
INT 21H
MOV AH,01 ;input first number
INT 21H
MOV BL,AL ;copy contents of al (i.e no.1) to bl register
AND BL,0FH ;convert ascii code of No. 1 to hexa code
LEA DX,MSG2 ;prompts the user to enter the second number
MOV AH,09 ;by displaying appropriate message
INT 21H

12/19/2023 Prepared By: Dr. Nikhil Marriwala 9


MOV AH,01 ;input second number
INT 21H
AND AL,0FH ;convert ascii code of No.2 to hexa
ADD BL,AL ;add 2 No.'s & store the sum in register bl
LEA DX,MSG3 ;prints a message telling the user that the sum is
being outputed on the screen
MOV AH,09
INT 21H
MOV DL,BL ;copying the sum to register dl
OR DL,30H ;convert the hexa sum to its ascii equivalent
MOV AH,02 ;prints the sum on the screen
INT 21H
MOV AH,4CH ;exit to dos prompt
INT 21H
END ;end of program

12/19/2023 Prepared By: Dr. Nikhil Marriwala 10


TITLE: BLINKING CHARACTER IN THE MIDDLE OF
THE SCREEN.
;OBJECTIVE: THIS PROGRAM PRINTS A BLINKING CHARACTER IN THE MIDDLE
OF THE SCREEN.
.MODEL SMALL
.STACK
.DATA
.CODE
START:MOV AH,0 ;set video mode
MOV AL,3 ;screen resolution 80*25
INT 10H
MOV AH,06 ;clearing window
MOV CX,081AH ;upper left corner
MOV DX,1034H ;lower right corner
MOV BH,43H ;cyan on red background
MOV AL,00H ;scroll all lines
INT 10H
MOV AH,02 ;set cursor position

12/19/2023 Prepared By: Dr. Nikhil Marriwala 11


MOV DX,0C27H ;center of screen
XOR BH,BH ;page 0
INT 10H
MOV AH,09 ;display character
XOR BH,BH ;page 0
MOV BL,0C3H ;blinking cyan character
MOV CX,1 ;character count
MOV AL,41H ;char 'A'
INT 10H
MOV AH,4CH ;exit to dos prompt
INT 21H
END ;end of program

12/19/2023 Prepared By: Dr. Nikhil Marriwala 12


TITLE: PRINT ALL THE ENGLISH ALPHABETS
.MODEL SMALL
.STACK
.DATA
.CODE
START:MOV CX,1AH ;load cx with count of 26(26 alphabets)
MOV DL,41H ;load dl with ascii value of 'A'
AGAIN:CALL DISP ;call procedure which displays the characters
INC DL ;increment dl so that it hold ascii value of next alphabet
LOOP AGAIN ;repeat this loop 26 times
JMP LAST ;jump to the end of program
DISP PROC
MOV AH,02 ;print the character/alphabet on the screen
INT 21H
RET
ENDP
LAST:MOV AH,4CH ;exit to dos prompt
INT 21H
END ;end of program

12/19/2023 Prepared By: Dr. Nikhil Marriwala 13


TITLE: FACTORIAL OF A NUMBER
.MODEL SMALL
.STACK
.DATA
MSG1 DB 'ENTER THE NUMBER:$'
MSG2 DB 10,13,'THE FACTORIAL IS:$'
.CODE
START:MOV AX,@DATA ;initializing the data segment
MOV DS,AX ;" " "
LEA DX,MSG1 ;prompts user to enter number by displaying
MOV AH,09 ;appropriate message
INT 21H ;
MOV AH,01 ;take input of the number
INT 21H ;
XOR AH,AH ;loading ah register with 00h
AND AL,000Fh ;convert ascii code of the No.to hexa code
MOV CX,AX ;loading the count with the number itself
MOV AX,01h ;loading ax register with 01h
AGAIN:MUL CX ;multiplying the cx with ax
LOOP AGAIN ;decrementing cx and repeating the loop

12/19/2023 Prepared By: Dr. Nikhil Marriwala 14


MOV CX,AX ;copy factorial result into cx register
LEA DX,MSG2 ;tell the user that the result will be displayed
MOV AH,09 ;on the screen by printing app.message
INT 21H
MOV DX,CX ;copying the factorial into dx register
OR DX,30H ;converting the hexa code to ascii equivalent
MOV AH,02 ;print the factorial result onto screen
INT 21H
MOV AH,4CH ;exit to dos prompt
INT 21H
END ;end of program

12/19/2023 Prepared By: Dr. Nikhil Marriwala 15


OBJECTIVE: THIS PROGRAM ACCEPTS A NUMBER FROM THE USER THROUGH THE
KEYBOARD CALCULATES ITS FACTORIAL AND PRINTS IT ON THE SCREEN
.MODEL SMALL
.STACK
.DATA
MSG1 DB 'ENTER THE NUMBER:$'
MSG2 DB 10,13,'THE FACTORIAL IS:$'
.CODE
START:MOV AX,@DATA ;initializing the data segment
MOV DS,AX ;" " "
LEA DX,MSG1 ;prompts user to enter number by displaying
MOV AH,09 ;appropriate message
INT 21H ;
MOV AH,01 ;take input of the number
INT 21H ;
XOR AH,AH ;loading ah register with 00h
AND AL,000Fh ;convert ascii code of the No.to hexa code
MOV CX,AX ;loading the count with the number itself
MOV AX,01h ;loading ax register with 01h
AGAIN:MUL CX ;multiplying the cx with ax
LOOP AGAIN ;decrementing cx and repeating the loop

12/19/2023 Prepared By: Dr. Nikhil Marriwala 16


MOV CX,AX ;copy factorial result into cx register
LEA DX,MSG2 ;tell the user that the result will be displayed
MOV AH,09 ;on the screen by printing app.message
INT 21H ;
MOV DX,CX ;copying the factorial into dx register
OR DX,30H ;converting the hexa code to ascii equivalent
MOV AH,02 ;print the factorial result onto screen
INT 21H ;
MOV AH,4CH ;exit to dos prompt
INT 21H
END ;end of program

12/19/2023 Prepared By: Dr. Nikhil Marriwala 17


Write an ALP in 8086 to find the average of two numbers
.mode small
.stack 100
.data
Label Mnemonics Comments
N0.1 DB 63H ; First number storage
No.2 DB 2EH ; Second number storage
Avg. DB? ; Average of two numbers.
.code
START :MOV AX, @data ; Initialises
MOV DX,AX ;data segment
MOV AL, No 1 ; Get first number in AL
ADD AL, No.2 ; Add second to it
ADC AH, 00H ; Put carry in AH
SAR AX, 1 ; Divide sum by 2
MOV AVG, AL ; Copy result to memory
END START

12/19/2023 Prepared By: Dr. Nikhil Marriwala 18


Write an ALP in 8086/8088 to find maximum number in
the array of 10 numbers.
.model small
.stack 100
.data
ARRAY DB 63H, 32H, 45H, 75H, 12H, 42H, 09H, 14H, 56H, 38H; Array of ; ten numbers
.code
START : MOV AX, @data ; [Initializes
MOV DS, AX ; data segment]
XOR DI, DI ; Initialize pointer
MOV CL, 10 ; Initialize counter
LEA BX, ARRAY ; Initialize base pointer for array
MOV AL, MAX ; Get maximum number
BACK: CMP AL, [BX+DI] ; Compare number with maximum
JNC SKIP
MOV DL, [BX+DI] ; [If number > MAX
MOV AL, DL ; MAX= number]
SKIP: INC DI ; Increment pointer
DEC CI ; Decrement counter
JNZ BACK ; IF count = 0 stop; otherwise go BACK
MOV MAX, AL ; Store maximum number
END START

12/19/2023 Prepared By: Dr. Nikhil Marriwala 19


• ;TITLE: INITIALIZING AN ARRAY
• ;OBJECTIVE: THIS PROGRAM INITIALIZES 100 POSITIONS IN AN ARRAY AND
• LOADS THEM WITH 0

• .MODEL SMALL
• .STACK
• .DATA
• .CODE
• START:MOV CX,64H ;load the count of 100
• XOR AL,AL ;load 00 in register al
• AGAIN:MOV [BX],AL ;load 00 in the memory location whose offset is provided
in bx
• INC BX ;increment bx to point to offset of next loc.
• LOOP AGAIN ;repeat the loop 100 times
• MOV AH,4CH ;exit to dos prompt
• INT 21H
• END ;end of program
12/19/2023 Prepared By: Dr. Nikhil Marriwala 20
Maximum number
TITLE 8086 ALP to find maximum in the arra
• .model small
• .stack 100
• .data
• ARRAY DB 63H, 32H, 45H, 75H, 12H, 42H, 09H, 14H, 56H, 38H; Array of ; ten numbers

• MAX DB 0 ; Maximum number


• .code
• START : MOV AX, @data ; Initializes
• MOV DS, AX ; data segment]
• XOR DI, DI ; Initialize pointer
• MOV CL, 10 ; Initialize counter
• LEA BX, ARRAY ; Initialize base pointer for array
• MOV AL, MAX ; Get maximum number
• BACK: CMP AL, [BX+DI] ; Compare number with maximum
• JNC SKIP
• MOV DL, [BX+DI] ; [If number > MAX
• MOV AL, DL ; MAX= number]
• SKIP: INC DI ; Increment pointer
• DEC CI ; Decrement counter
• JNZ BACK ;IF count = 0 stop ; otherwise go BACK
• MOV MAX, AL ; Store maximum number
• END START

12/19/2023 Prepared By: Dr. Nikhil Marriwala 21


Bubble Sorting
• .Model Small
• .data
• list dw 22h,18h,20h,21h,23h
• .code
• start:
• mov ax,@data
• mov ds,ax
• mov dx,05h
• again0:
• mov cx,dx
• mov si,offset list
• again1:
• mov ax,[si]
• cmp ax,[si+2]
• jg pr1
• xchg [si+2],eax
• xchg [si],eax
• pr1:
• add si,02
• loop again1
• mov bx,ax
• dec dx
• jnz again0
• mov ah,4ch
• int 21h
• end

12/19/2023 Prepared By: Dr. Nikhil Marriwala 22

You might also like