Microprocessor File

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 50

IT 306

Microprocessor File

Made by:
Name: Pulkit Aggarwal
Roll No: 755/IT/12
NSIT,Delhi

1)Study of 8086 microprocessor kit and implement following basic programs.


a) Find sum, average & multiplication of 2 decimal numbers.
Sum
.model small
.data
msg1 db 10,13,"Enter first no.$"
msg2 db 10,13,"Enter second no.$"
msg3 db 10,13,"Addition is:$"
.code
mov ax,@data
mov ds,ax
lea dx,msg1
mov ah,09H
int 21H
mov ah,01H
int 21H
sub al,30H
cmp al,09H
JBE L1
sub al,07H
L1: mov cl,04H
ROL al,cl
mov bl,al
mov ah,01H
int 21H
sub al,30H
cmp al,09H
JBE L2
sub al,07H
L2:ADD bl,al

lea dx,msg2
mov ah,09H
int 21H
mov ah,01H
int 21H
sub al,30H
cmp al,09H
JBE L3
sub al,07H
L3: mov cl,04H
ROL al,cl
mov bh,al
mov ah,01H
int 21H
sub al,30H
cmp al,09H
JBE L4
sub al,07H
L4:ADD bh,al
ADD bl,bh
lea dx,msg3
mov ah,09H
int 21H
mov ch,02H
mov cl,04H
L5:ROL bl,cl
mov bh,bl
AND bl,0FH
cmp bl,09H
JBE L6
ADD bl,07H

L6:ADD bl,30H
mov dl,bl
mov ah,02H
int 21H
mov bl,bh
dec ch
JNZ L5
mov ah,4ch
int 21H
end

Average
.model small
.stack 100
.data
No1

DB 63H

; First number storage

No2

DB 2EH

; Second number storage

Avg

DB ?

; Average of two numbers

.code
START:

MOV AX,@data

; [ Initialises

MOV DS,AX

; data segment ]

MOV AL,NO1

; Get first number in AL

ADD AL,NO2

; Add second to it

ADC AH,00H

; Put carry in AH

SAR AX,1
MOV Avg,AL

; Divide sum by 2
; Copy result to memory

END START

Multiplication
.model small
.data
a db 09H
b db 02H
.code
mov

ax, @data

mov

ds, ax

mov

ah, 0

mov

al, a

; Load number1 in al

mov

bl, b

; Load number2 in bl

mul

bl

; multiply numbers and result in ax

mov

ch, 04h

mov

cl, 04h

mov

bx, ax

; Result in reg bx

bx, cl

; roll bl so that msb comes to lsb

mov

dl, bl

; load dl with data to be displayed

and

dl, 0fH

cmp

dl, 09

jbe

l4

add

dl, 07

l2: rol

l4: add

; Initialize data section

; Count of digits to be displayed


; Count to roll by 4 bits

; get only lsb


; check if digit is 0-9 or letter A-F

; if letter add 37H else only add 30H

dl, 30H

mov

ah, 02

int

21H

dec

ch

jnz

l2

; Function 2 under INT 21H (Display character)

; Decrement Count

mov

ah, 4cH

int

21H

; Terminate Program

end

b) Bubble sort numbers in increasing order.


.model small
.data
M1

DB 10, 13, 'Enter the total number of elememts in the array : $'

M2

DB 10, 13, 'The sorted array is : ',0dh,0ah,'$'

M3

DB 10, 13, '$'

ARRAY DB 100 DUP(0)


.code
START: MOV AX, @data
MOV DS, AX
MOV AH, 09H

; Display message M1

MOV DX, OFFSET M1


INT 21H
MOV AH, 01H

; input the number of elements in the array

INT 21H
SUB AL,30H
MOV BH,AL
MOV BL,AL
XOR DI,DI
MOV CL,4
BACK:
MOV AH, 09H

; Carrige return and next line

MOV DX, OFFSET M3


INT 21H

MOV AH,01
INT 21H
SUB AL,30H
SAL AL,CL
MOV CH,AL
MOV AH,01
INT 21H
SUB AL,30H
OR CH,AL
MOV [ARRAY+DI],CH
INC DI
DEC BL
JNZ BACK
MOV CH,BH
AGAIN1: MOV DI,0
AGAIN: MOV DL,[ARRAY+DI]
CMP DL,[ARRAY+DI+1]
JBE SKIP
MOV AH,[ARRAY+DI+1]
MOV [ARRAY+DI],AH
MOV [ARRAY+DI+1],DL
SKIP: INC DI
MOV AH,0
MOV AL,BH
DEC AL
CMP AX,DI
JNZ AGAIN
DEC CH
JNZ AGAIN1
MOV AH, 09H

; Display message M1

MOV DX, OFFSET M2

INT 21H
MOV BL,BH
XOR DI,DI
BACK1:
MOV AH, 09H

; Carrige return and next line

MOV DX, OFFSET M3


INT 21H
MOV DL,[ARRAY+DI]
AND DL,0F0H
SAR DL,CL
ADD DL,30H
MOV AH,02
INT 21H
MOV DL,[ARRAY+DI]
AND DL,0FH
ADD DL,30H
MOV AH,02
INT 21H
INC DI
DEC BL
JNZ BACK1
TER: MOV AH, 4CH
INT 21H
END START

c) Transfer of bytes from ds to es.


.MODEL SMALL
.STACK 64
.DATA
DSMSG DB 13,10,'DS: $'
ARRONE DB 'ABCDE$'
ARRTWO DB 0,0,0,0,0
MSG DB 13,10,'10 bytes transferred to ES: $'
.CODE
MAIN PROC
MOV AX,@DATA
MOV DS,AX
MOV AX,@DATA
MOV ES,AX
LEA SI,ARRONE
LEA DX,DSMSG
MOV AH,09H
INT 21H
SKP1:

CALL SHOWARR
LEA SI,ARRONE
LEA DI,ARRTWO
MOV CL,10

MVLP:

MOV AL,[SI]
MOV ES:[DI],AL
INC SI
INC DI
CMP CL,00H
JE SKP3
DEC CL
LOOP MVLP

SKP3:

LEA DX,MSG
MOV AH,09H
INT 21H
CALL SHOWARR2
MOV AH,4CH
INT 21H
MAIN ENDP
SHOWARR PROC
LEA SI,ARRONE
MOV CL,10

DSPLP:

MOV DL,[SI]
MOV AH,02H
INT 21H
INC SI
CMP CL,00H
JE SKP2
DEC CL
LOOP DSPLP

SKP2:

RET
SHOWARR ENDP
SHOWARR2 PROC
LEA DI,ARRTWO
MOV CL,10

DSPLP2:

MOV DL,ES:[DI]
MOV AH,02H
INT 21H
INC DI
CMP CL,00H
JE SKP4
DEC CL
LOOP DSPLP2

SKP4:

RET
SHOWARR2 ENDP

END

2) Code conversion
a) BCD to Binary(HEX)
.MODEL SMALL
.STACK 64H
.DATA
TESTARR DB 'abc$'
CHARARR DB ?
.CODE
MAIN PROC
MOV AX,@DATA
MOV DS,AX
LEA SI,CHARARR
INP: MOV AH,01H
INT 21H
CMP AL,0DH
JE SKP1

AND AX,00FFH
SUB AL,30H
MOV [SI],AX
ADD SI,1
LOOP INP
SKP1: MOV [SI],'$'
LEA SI,CHARARR
MOV DL,0AH
MOV AH,02H
INT 21H
OPT: MOV DH,[SI]
CMP DH,'$'
JE SKP2
;MOV AH,02H
;INT 21H
CALL BCD2BIN
INC SI
LOOP OPT
SKP2:
MOV AH,4CH
INT 21H
MAIN ENDP
BCD2BIN PROC
PUSH AX
PUSH CX
PUSH BX
PUSH DX
PUSH SI
MOV CL,08H
LB :
SAL DH,1

JC PRTONE
CMP CL,04H
JG SKP3
MOV DL,48
MOV AH,02H
INT 21H
JMP SKP3
PRTONE: CMP CL,04H
JG SKP3
MOV DL,49
MOV AH,02H
INT 21H
SKP3: DEC CL
CMP CL,00H
JE SKP4
JMP 040H
SKP4: MOV DL,' '
MOV AH,02H
INT 21H
POP SI
POP DX
POP BX
POP CX
POP AX
RET
BCD2BIN ENDP

b) Binary (HEX) to BCD


.MODEL SMALL

.STACK 64H
.DATA
MSG1 DB 13,10,'Enter binary code in BCD format: $'
MSG2 DB 13,10,'Resultant decimal: $'
BINARRAY DB ?
.CODE
MAIN PROC
MOV AX,@DATA
MOV DS,AX
LEA DX,MSG1
MOV AH,09H
INT 21H
AND DX,00FFH
LEA SI,BINARRAY
IN1: MOV AH,01H
INT 21H
CMP AL,' '
JNE RST
MOV CL,00H
ADD DL,48
MOV [SI],DL
INC SI
AND DX,0000H
RST: CMP AL,0DH
JE IN1_BRK
SAL DX,1
AND AX,00FFH
SUB AL,30H
ADD DX,AX

LOOP IN1
IN1_BRK: ADD DL,48
MOV [SI],DL
INC SI
MOV DL,'$'
MOV [SI],DL
MOV DL,10
MOV AH,02H
INT 21H
LEA DX,MSG2
MOV AH,09H
INT 21H
LEA DX,BINARRAY
MOV AH,09H
INT 21H
MOV AH,4CH
INT 21H
MAIN ENDP

c) Binary (HEX) to ASCII


.MODEL SMALL
.STACK 64H
.DATA
MSG1 DB 13,10,'Enter binary code in ASCII format: $'
MSG2 DB 13,10,'Resultant ASCII string: $'
BINARRAY DB ?
.CODE

MAIN PROC
MOV AX,@DATA
MOV DS,AX
LEA DX,MSG1
MOV AH,09H
INT 21H
AND DX,00FFH
LEA SI,BINARRAY
IN1: MOV AH,01H
INT 21H
CMP AL,' '
JNE RST
MOV CL,00H
MOV [SI],DL
INC SI
AND DX,0000H
RST: CMP AL,0DH
JE IN1_BRK
SAL DX,1
AND AX,00FFH
SUB AL,30H
ADD DX,AX
LOOP IN1
IN1_BRK: MOV [SI],DL
INC SI
MOV DL,'$'
MOV [SI],DL
MOV DL,10
MOV AH,02H
INT 21H
LEA DX,MSG2

MOV AH,09H
INT 21H
LEA DX,BINARRAY
MOV AH,09H
INT 21H
MOV AH,4CH
INT 21H
MAIN ENDP

d) ASCII to Binary (HEX)


.MODEL SMALL
.STACK 64H
.DATA
TESTARR DB 'abc$'
CHARARR DB ?
.CODE
MAIN PROC
MOV AX,@DATA
MOV DS,AX
LEA SI,CHARARR
INP: MOV AH,01H
INT 21H
CMP AL,0DH
JE SKP1
AND AX,00FFH
MOV [SI],AX
ADD SI,1
LOOP INP

SKP1: MOV [SI],'$'


LEA SI,CHARARR
MOV DL,0AH
MOV AH,02H
INT 21H
OPT: MOV DH,[SI]
CMP DH,'$'
JE SKP2
;MOV AH,02H
;INT 21H
CALL ASC2BIN

INC SI
LOOP OPT
SKP2:
MOV AH,4CH
INT 21H
MAIN ENDP
ASC2BIN PROC
PUSH AX
PUSH CX
PUSH BX
PUSH DX
PUSH SI
MOV CL,08H
LB :
SAL DH,1
JC PRTONE
MOV DL,48
MOV AH,02H
INT 21H

JMP SKP3
PRTONE: MOV DL,49
MOV AH,02H
INT 21H
SKP3: DEC CL
CMP CL,00H
JE SKP4
JMP 03EH
SKP4: MOV DL,' '
MOV AH,02H
INT 21H
POP SI
POP DX
POP BX
POP CX
POP AX
RET
ASC2BIN ENDP

3a)Program for Password matching,palindrome checking.


Palindrome Checking
DATA SEGMENT
MSG1 DB 10,13,'ENTER THE STRING: $'
MSG2 DB 10,13,'STRING IS PALINDROME$'
MSG3 DB 10,13,'STRING IS NOT PALINDROME$'
NEW DB 10,13,'$'
INST DB 20 DUP(0)
DATA ENDS

CODE SEGMENT
ASSUME CS:CODE,DS:DATA
START:
MOV AX,DATA
MOV DS,AX
LEA DX,MSG1
MOV AH,09H
INT 21H
MOV BX,00
UP:
MOV AH,01H
INT 21H
CMP AL,0DH
JE DOWN
MOV [INST+BX],AL
INC BX
LOOP UP
DOWN:
MOV DI,0
DEC BX
JNZ CHECK
CHECK:
MOV AL,[INST+BX]
CMP AL,[INST+DI]
JNE FAIL
INC DI
DEC BX
JNZ CHECK
LEA DX,NEW
MOV AH,09H
INT 21H

LEA DX,MSG2
MOV AH,09H
INT 21H
JMP FINISH
FAIL:
LEA DX,MSG3
MOV AH,09H
INT 21H
FINISH:
MOV AH,4CH
INT 21H
CODE ENDS
END START
END

Password Matching
.MODEL SMALL
.DATA
MSG1 DB 13,10,'Create password: $'
MSG2 DB 13,10,'Input password: $'
MSG3 DB 13,10,'Password matched.$'
MSG4 DB 13,10,'Password did not match.$'
PASSWORD DB 20 DUP(0)
INPUTSTR DB 20 DUP(0)
.CODE
MAIN PROC
MOV DX,@DATA
MOV DS,DX

MOV ES,DX
LEA DX,MSG1
MOV AH,09H
INT 21H
LEA SI,PASSWORD
CALL INSTRING
LEA DX,MSG2
MOV AH,09H
INT 21H
LEA SI,INPUTSTR
CALL INSTRING
MOV DL,10
MOV AH,02H
INT 21H
LEA SI,PASSWORD
LEA DI,INPUTSTR
CALL PASSCHECK
MOV AH,4CH
INT 21H
MAIN ENDP
INSTRING PROC
INPASS_LP1: MOV AH,01H
INT 21H
CMP AL,0DH
JE INPASS_SKP
AND AX,00FFH
MOV [SI],AX
ADD SI,1
LOOP INPASS_LP1
INPASS_SKP: MOV [SI],'$'
RET

INSTRING ENDP
PASSCHECK PROC
PASSCHECK_LP: MOV DX,[SI]
MOV CX,[DI]
CMP DX,CX
JNE FAIL
CMP DL,'$'
JE SUCCESS
ADD SI,1
ADD DI,1
LOOP PASSCHECK_LP
SUCCESS: LEA DX,MSG3
MOV AH,09H
INT 21H
RET
FAIL: LEA DX,MSG4
MOV AH,09H
INT 21H
RET
PASSCHECK ENDP

3b) Find the register bit width, byte order of your processor.
Byte order
DATA_HERE SEGMENT
MSG DB "SIZE IS:",13,10,'$'
DATA_HERE ENDS
CODE_HERE SEGMENT

ASSUME DS:DATA_HERE, CS:CODE_HERE,


START:
MOV AX, DATA_HERE
MOV DS,AX
XOR BX,BX
ADD BX, 1
MOV DX,BX
MOV CX,0
LOOPS:

ROL BX,1
CMP BX,DX
JZ LOOPO
INC CX
JMP LOOPS

LOOPO:

MOV DL,CL
ADD DL,30H
MOV AH,02H
INT 21H
MOV AX, 4C00H
INT 21H

CODE_HERE ENDS
END START
Register Width
DATA_HERE SEGMENT
MSG1 DB "LITTLE ENDIAN",13,10,'$'
MSG2 DB "BIG ENDIAN",13,10,'$'
TEMP DW 1 DUP(0)
DATA_HERE ENDS
CODE_HERE SEGMENT
ASSUME DS:DATA_HERE, CS:CODE_HERE,
START:
MOV AX, DATA_HERE

MOV DS,AX
LEA SI, TEMP
MOV [SI], 1234H
MOV CX,[SI]
CMP CH,CL
JL MSG
MOV DX, OFFSET MSG2
MOV AX,0
MOV AH, 09H
INT 21H
MSG:

MOV DX, OFFSET MSG1


MOV AX,0
MOV AH, 09H
INT 21H
MOV AH,08H
INT 21H
MOV AX, 4C00H
INT 21H

CODE_HERE ENDS
END START

c) Generation of time delay counters.


.MODEL SMALL
.DATA
MSGIN DB 'Enter delay duration (0-50): $'
MSG1 DB 'Hello_World!$'
DELAYTIME DW 0000H
.CODE

MOV DX,@DATA
MOV DS,DX
LEA DX,MSGIN
MOV AH,09H
INT 21H
IN1:
MOV AH,01H
INT 21H
CMP AL,0DH
JE NXT
SUB AL,30H
MOV DL,AL
MOV AX,BX
MOV CL,0AH
MUL CL
MOV BX,AX
AND DX,00FFH
ADD BX,DX
MOV DELAYTIME,BX
LOOP IN1
NXT: MOV CX,DELAYTIME
MOV DL,10
MOV AH,02H
INT 21H
LEA SI,MSG1
LP: PUSH DX
MOV DL,[SI]
CMP DL,'$'
JE NXT2
MOV AH,02H

INT 21H
ADD SI,1
POP DX
MOV DI,DELAYTIME
MOV AH, 0
INT 1Ah
MOV BX, DX
Delay:
MOV AH, 0
INT 1Ah
SUB DX, BX
CMP DI, DX
JA Delay
LOOP LP
NXT2: MOV AH,4CH
INT 21H
END

4) Implement prefix to postfix conversion of a mathematical expression.


.model small
.data
prefix db '-+abc-d$'
length dw ($-prefix)
postfix db 56 dup(0)
temp db 28 dup(0)
enter db 10,13,'$'
msg1 db 'The prefix string is: $'
msg2 db 'The corresponding postfix string is: $'

.code
mov ax, @data
mov ds, ax
lea si, prefix
lea di, postfix
mov es, di

;moving pointer of postfix to es

lea di, temp


mov cx, length
mov ah, 09h
lea dx, msg1
int 21h
lea dx, prefix
int 21h
label1:
cmp cx,0
jz label6
cmp [si],'*'
jz label2
cmp [si],'/'
jz label2
cmp [si],'+'
jz label2
cmp [si],'-'
jz label2
jmp label3
label2:
mov al, [si]
mov [di], al ;di points to temp
inc di
inc si
dec cx

jmp label1
label3:
push di
mov di, es

;di pointing to temp is stored


;di points to postfix

mov al,[si]
mov [di],al

;operand put in postfix

inc di
mov es, di
pop di

;storing back pointer of postfix and


;retrieving pointer of temp

dec di
label5: cmp [di],'\' ;di pointing to temp
jz label4
inc di
mov [di],'\'
inc di
inc si
dec cx
jmp label1
label4:
dec di

;di pointing to temp

cmp [di],'\'
jz label4
push si

;storing si which points to prefix

push di

;storing di which points to temp

mov si, di

;si now points to temp

mov di, es

;di now points to postfix

mov al, [si]


mov [di], al ;putting operator to postfix
inc di
mov es, di
pop di

;storing back pointer of postfix


;di now points to temp

mov di, 0
pop si

;clearing the last operator used from temp


;si now points to prefix

jmp label5
label6:
mov ah, 09h
lea dx, enter
int 21h
lea dx, msg2
int 21h
lea dx, postfix
int 21h
end

5) Data structures programs:


a) Implement linked list : creation ,insertion ,deletion.
.model small
.stack 128
.data
s1 db 13,10," 1. insert a node ",13,10,'$'
s2 db " 2. delete a node ",13,10,'$'
s3 db " 3. display the list ",13,10,'$'
s4 db " 4. exit ",13,10,'$'
s5 db "enter the choice : $"
s6 db 13,10,13,10," enter the data : $"
s7 db 13,10,13,10,'$'
s8 db "

$"

s9 db 13,10," insert first node or after any node (1/2) : $"


s10 db 13,10," after which node : $"

mem db 10 dup(0)
.code
main proc
mov ax,@data
mov ds,ax
mov si,0100h
mov bp,0104h
ok:
lea dx,s1
mov ah,09h
int 21h
lea dx,s2
mov ah,09h
int 21h
lea dx,s3
mov ah,09h
int 21h
lea dx,s4
mov ah,09h
int 21h
lea dx,s5
mov ah,09h
int 21h
mov ah,01h

;taking input

int 21h
cmp al,34h

;if exit

je exit
cmp al,31h

;insert

je insert
cmp al,33h
je display1

;display

jmp ok

;45

insert:

lea dx,s9
mov ah,09h
int 21h
mov ah,01h
int 21h
cmp al,31h
je first
jmp after
ok1: jmp ok
first:
lea dx,s6
mov ah,09h
int 21h
mov ah,01h
int 21h
mov byte ptr [si],al
inc si
mov word ptr [si],0000h
jmp ok
after:
lea dx,s6
mov ah,09h
int 21h
mov ah,01h
int 21h
mov cl,al
lea dx,s10
mov ah,09h

; cl contains the data

int 21h
mov ah,01h
int 21h

;al contains after which node

mov di,0100h
search :

cmp byte ptr [di],al


je nowinsert
cmp word ptr [di+1],0000h
je ok1
inc di
mov ax,word ptr [di]
mov di,ax
jmp search
nowinsert:

;90
mov bx,word ptr [di+1]
mov byte ptr [bp],cl

; storing the data

mov word ptr [bp+1],bx

;storind address

mov dx,bp
inc di
mov word ptr [di],dx
inc bp
inc bp
inc bp
inc bp
jmp ok1

exit : jmp exit1


ok2 :jmp ok1
display1 :
mov si,0100h

lea dx,s7
mov ah,09h
int 21h
now:
mov dl,byte ptr [si]
mov ah,02h
int 21h
lea dx,s8

;for space

mov ah,09h
int 21h
inc si
mov cx , word ptr [si]
cmp cx,0000h
je ok2
mov si,word ptr[si]
jmp now

exit1: mov ax,4c00h


int 21h
main endp
end main

;129

b) Implement binary search tree.


.MODEL SMALL
.STACK 100H
.DATA
PROMPT_1 DB 'State 1 : $'
PROMPT_2 DB 13,10,'State 2 : $'
ARRAY DB 5,3,9,0,2,6,1,7,8,4
.CODE
MAIN PROC
MOV AX, @DATA
MOV DS, AX
MOV BX, 10
LEA DX, PROMPT_1
MOV AH, 9
INT 21H
LEA SI, ARRAY
CALL PRINT_ARRAY
LEA SI, ARRAY

; initialize DS

; set BX=10
; load and display the string PROMPT_1

; set SI=offset address of ARRAY


; call the procedure PRINT_ARRAY
; set SI=offset address of the ARRAY

CALL BUBBLE_SORT

; call the procedure BUBBLE_SORT

LEA DX, PROMPT_2


MOV AH, 9
INT 21H

; load and display the string PROMPT_2

LEA SI, ARRAY


CALL PRINT_ARRAY
MOV AH, 4CH

; set SI=offset address of ARRAY


; call the procedure PRINT_ARRAY
; return control to DOS

INT 21H
MAIN ENDP
;**************************************************************************;
;**************************************************************************;
;------------------------- Procedure Definitions ------------------------;
;**************************************************************************;
;**************************************************************************;
;**************************************************************************;
;----------------------------- PRINT_ARRAY ------------------------------;
;**************************************************************************;
PRINT_ARRAY PROC
; this procedure will print the elements of a given array
; input : SI=offset address of the array
;
: BX=size of the array
; output : none
PUSH AX
PUSH CX
PUSH DX

; push AX onto the STACK


; push CX onto the STACK
; push DX onto the STACK

MOV CX, BX

; set CX=BX

@PRINT_ARRAY:
XOR AH, AH
MOV AL, [SI]

; loop label
; clear AH
; set AL=[SI]

CALL OUTDEC
MOV AH, 2
MOV DL, 20H
INT 21H

; call the procedure OUTDEC


; set output function
; set DL=20H
; print a character

INC SI
; set SI=SI+1
LOOP @PRINT_ARRAY
; jump to label @PRINT_ARRAY while CX!=0
POP DX
POP CX
POP AX

; pop a value from STACK into DX


; pop a value from STACK into CX
; pop a value from STACK into AX

RET
; return control to the calling procedure
PRINT_ARRAY ENDP
BUBBLE_SORT PROC
; this procedure will sort the array in ascending order
; input : SI=offset address of the array
;
: BX=array size
; output : none
PUSH AX
PUSH BX
PUSH CX
PUSH DX
PUSH DI

; push AX onto the STACK


; push BX onto the STACK
; push CX onto the STACK
; push DX onto the STACK
; push DI onto the STACK

MOV AX, SI
MOV CX, BX
DEC CX

; set AX=SI
; set CX=BX
; set CX=CX-1

@OUTER_LOOP:
MOV BX, CX
MOV SI, AX
MOV DI, AX

; loop label
; set BX=CX
; set SI=AX
; set DI=AX

INC DI
@INNER_LOOP:
MOV DL, [SI]

; set DI=DI+1
; loop label
; set DL=[SI]

CMP DL, [DI]


; compare DL with [DI]
JNG @SKIP_EXCHANGE
; jump to label @SKIP_EXCHANGE if DL<[DI]
XCHG DL, [DI]
MOV [SI], DL

; set DL=[DI], [DI]=DL


; set [SI]=DL

@SKIP_EXCHANGE:
; jump label
INC SI
; set SI=SI+1
INC DI
; set DI=DI+1
DEC BX
; set BX=BX-1
JNZ @INNER_LOOP
; jump to label @INNER_LOOP if BX!=0
LOOP @OUTER_LOOP
; jump to label @OUTER_LOOP while CX!=0
POP DI
POP DX
POP CX
POP BX
POP AX

; pop a value from STACK into DI


; pop a value from STACK into DX
; pop a value from STACK into CX
; pop a value from STACK into BX
; pop a value from STACK into AX

RET
; return control to the calling procedure
BUBBLE_SORT ENDP

OUTDEC PROC
; this procedure will display a decimal number
; input : AX
; output : none
PUSH BX
PUSH CX
PUSH DX
XOR CX, CX
MOV BX, 10
@OUTPUT:
XOR DX, DX
DIV BX
PUSH DX
INC CX
OR AX, AX
JNE @OUTPUT
MOV AH, 2

; push BX onto the STACK


; push CX onto the STACK
; push DX onto the STACK
; clear CX
; set BX=10
; loop label
; clear DX
; divide AX by BX
; push DX onto the STACK
; increment CX
; take OR of Ax with AX
; jump to label @OUTPUT if ZF=0
; set output function

@DISPLAY:
POP DX
OR DL, 30H
INT 21H
LOOP @DISPLAY

; loop label
; pop a value from STACK to DX
; convert decimal to ascii code
; print a character
; jump to label @DISPLAY if CX!=0

POP DX
POP CX
POP BX

; pop a value from STACK into DX


; pop a value from STACK into CX
; pop a value from STACK into BX

RET
OUTDEC ENDP
END MAIN

; return control to the calling procedure

c) Implement heap sort algorithm.


.model small
.data
M1 DB 10, 13, 'Enter the total number of elememts in the array : $'
M2 DB 10, 13, 'The sorted array is : ',0dh,0ah,'$'
M3 DB 10, 13, '$'
ARRAY DB 100 DUP(0)
.code
START: MOV AX, @data
MOV DS, AX
MOV AH, 09H
; Display message M1
MOV DX, OFFSET M1
INT 21H
MOV AH, 01H
; input the number of elements in the array
INT 21H
SUB AL,30H
MOV BH,AL
MOV BL,AL
XOR DI,DI
MOV CL,4
BACK:
MOV AH, 09H
; Carrige return and next line
MOV DX, OFFSET M3
INT 21H
MOV AH,01
INT 21H
SUB AL,30H
SAL AL,CL
MOV CH,AL
MOV AH,01
INT 21H
SUB AL,30H
OR CH,AL
MOV [ARRAY+DI],CH
INC DI
DEC BL
JNZ BACK

MOV CH,BH
AGAIN1: MOV DI,0
AGAIN: MOV DL,[ARRAY+DI]
CMP DL,[ARRAY+DI+1]
JBE SKIP
MOV AH,[ARRAY+DI+1]
MOV [ARRAY+DI],AH
MOV [ARRAY+DI+1],DL
SKIP: INC DI
MOV AH,0
MOV AL,BH
DEC AL
CMP AX,DI
JNZ AGAIN
DEC CH
JNZ AGAIN1
MOV AH, 09H
; Display message M1
MOV DX, OFFSET M2
INT 21H
MOV BL,BH
XOR DI,DI
BACK1:
MOV AH, 09H
; Carrige return and next line
MOV DX, OFFSET M3
INT 21H
MOV DL,[ARRAY+DI]
AND DL,0F0H
SAR DL,CL
ADD DL,30H
MOV AH,02
INT 21H
MOV DL,[ARRAY+DI]
AND DL,0FH
ADD DL,30H
MOV AH,02
INT 21H
INC DI
DEC BL
JNZ BACK1
TER: MOV AH, 4CH
INT 21H
END START

6) Implement Booths multiplication algorithm.


.model small
.data
multiplier db 00001100b ; x=-4:in 2's complement form
multiplicand db 00100000b ; y=2
compl db 11100000b
.code
mov ax, @data
mov ds, ax
mov bl, multiplier
mov bh, multiplicand
mov dh, compl
mov ax, 0
mov cx, 4
start:
dec cx
cmp cx, 0
jl next
push ax
mov ah, bl
and ah, 01h
cmp ah, 01h
jz label1
jmp label2
label1:
mov es,ax
pop ax
cmp ah, 01h
jz label3
mov ax, es

;complement of y

;sbb al, bh
add al, dh
sar al, 1
shr bl, 1
jmp start
label3:
mov ax, es
sar al, 1
jmp start
label2:
mov es, ax
pop ax
cmp ah, 01h
jz label4
mov ax, es
sar al, 1
shr bl, 1
jmp start
label4:
mov ax, es
add al, bh
sar al, 1
shr bl, 1
jmp start
next:
mov cx, 8
mov bl, al
print: mov ah, 2 ; print function.
mov dl, '0'
test bl, 10000000b ; test first bit.
jz zero

mov dl, '1'


zero: int 21h
shl bl, 1
loop print
end

7a) Implement restoring division algorithm.


.MODEL SMALL
.STACK 64H
.DATA
MSG1 DB 'Enter 8-bit dividend: $'
MSG2 DB 'Enter 8-bit divisor: $'
MSG3 DB 'Result: $'
DIVIDEND DW 0000H
DIVISOR DW 0000H
RESULT DW 0000H
.CODE
MOV AX,DATA
;USER INPUT for DIVIDEND=========

lea dx,msg1
mov ah,09h
int 21h
mov dx,0000H
mov cl,8
IN1: CMP CL,0
JE IN1_BRK
MOV AH,01H
INT 21H
SAL DX,1
AND AX,00FFH
SUB AL,30H
ADD DX,AX
LOOP IN1
IN1_BRK:
MOV AX,DX
;================================
AND AX,00FFH
MOV DIVIDEND,AX
;NEXT LINE AND CARRIAGE RETURN==
MOV DL,10
MOV AH,02H
INT 21H
MOV DL,13
MOV AH,02H
INT 21H
;================================
;USER INPUT for DIVISOR==========
lea dx,msg2
mov ah,09h
int 21h

mov dx,0000H
mov cl,8
IN2:CMP CL,0
JE IN2_BRK
MOV AH,01H
INT 21H
SAL DX,1
AND AX,00FFH
SUB AL,30H
ADD DX,AX
LOOP IN2
IN2_BRK:
MOV BX,DX
;================================
MOV DIVISOR,BX
;RESTORING DIVISION==============
MOV AX,DIVIDEND
MOV BX,DIVISOR
MOV CX,8
RD_LP: ROL AX,1
CMP AH,BL
JB NEGVAL
SUB AH,BL
ADD AX,1
NEGVAL: LOOP RD_LP
AND AX,00FFH
MOV RESULT,AX
;NEXT LINE AND CARRIAGE RETURN==
MOV DL,10

MOV AH,02H
INT 21H

;
;

MOV DL,13

MOV AH,02H
INT 21H

;
;

;================================
;DECIMAL OUTPUT CODE================
MOV BL,0AH
MOV AX,RESULT
MOV CX,0100H
PUSH CX
RES1: DIV BL
CMP AL,00H
JE NXT3
MOV CL,AL
MOV CH,00H
MOV RESULT,CX
MOV DL,AH
ADD DL,30H
AND DX,00FFH
PUSH DX
MOV AX,RESULT
LOOP RES1
NXT3: MOV DL,AH
CMP DL,00H
JNE NXT4
ADD DL,01H
NXT4: ADD DL,30H
PUSH DX
LEA DX,msg3
MOV AH,09H
INT 21H
MOV CL,08H

RES2: POP AX
CMP CL,00H
JE SKPND
MOV DL,AL
MOV AH,02H
INT 21H
DEC CL
LOOP RES2
;=================================
SKPND:
MOV AH,4CH
INT 21H
END

7b) Implement non-restoring division algorithm.


.MODEL SMALL
.STACK 64H
.DATA
MSG1 DB 'Enter 8-bit dividend: $'
MSG2 DB 'Enter 8-bit divisor: $'
MSG3 DB 'Result: $'
DIVIDEND DW 0000H
DIVISOR DW 0000H
RESULT DW 0000H
.CODE
MOV AX,DATA
MOV DS,AX

;USER INPUT for DIVIDEND=========


lea dx,msg1
mov ah,09h
int 21h
mov dx,0000H
mov cl,8
IN1: CMP CL,0
JE IN1_BRK
MOV AH,01H
INT 21H
SAL DX,1
AND AX,00FFH
SUB AL,30H
ADD DX,AX
LOOP IN1
IN1_BRK:
MOV AX,DX
;================================
AND AX,00FFH
MOV DIVIDEND,AX
;NEXT LINE AND CARRIAGE RETURN==
MOV DL,10
MOV AH,02H
INT 21H
MOV DL,13
MOV AH,02H
INT 21H
;================================
;USER INPUT for DIVISOR=========
lea dx,msg2
mov ah,09h

int 21h
mov dx,0000H
mov cl,8
IN2:CMP CL,0
JE IN2_BRK
MOV AH,01H
INT 21H
SAL DX,1
AND AX,00FFH
SUB AL,30H
ADD DX,AX
LOOP IN2
IN2_BRK:
MOV BX,DX
;================================
MOV DIVISOR,BX
;NON-RESTORING DIVISION==============
MOV AX,DIVIDEND
MOV BX,DIVISOR
MOV CX,8
ROL AX,1
SUB AH,BL
NRD_LP: CMP AH,80H
JB POSVAL
JMP NEGVAL
NEGVAL: CMP CX,00H
JE NRD_LP_BRK
ROL AX,1
ADD AH,BL
DEC CX
JMP NRD_LP

POSVAL: ADD AX,1


CMP CX,00H
JE NRD_LP_BRK
ROL AX,1
SUB AH,BL
DEC CX
JMP NRD_LP
NRD_LP_BRK: AND AX,00FFH
MOV RESULT,AX
;NEXT LINE AND CARRIAGE RETURN==
MOV DL,10

MOV AH,02H

INT 21H

;
;

MOV DL,13

MOV AH,02H
INT 21H

;
;

;================================
;DECIMAL OUTPUT CODE================
MOV BL,0AH
MOV AX,RESULT
MOV CX,0100H
PUSH CX
RES1: DIV BL
CMP AL,00H
JE NXT3
MOV CL,AL
MOV CH,00H
MOV RESULT,CX
MOV DL,AH
ADD DL,30H

AND DX,00FFH
PUSH DX
MOV AX,RESULT
LOOP RES1
NXT3: MOV DL,AH
CMP DL,00H
JNE NXT4
ADD DL,01H
NXT4: ADD DL,30H
PUSH DX
LEA DX,msg3
MOV AH,09H
INT 21H
MOV CL,08H
RES2: POP AX
CMP CL,00H
JE SKPND
MOV DL,AL
MOV AH,02H
INT 21H
DEC CL
LOOP RES2
;=================================
SKPND:
MOV AH,4CH
INT 21H
END

You might also like