Top Banner
8086 programs
28

86 pr0grams

Jun 24, 2015

Download

Technology

This programs may be usefull to undergraduate B.Tech Students
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: 86 pr0grams

8086 programs

Page 2: 86 pr0grams

16 bit multiplication.model tiny

• .stack 32h• .code• Org 2000h• .startup• Mov ax,num1• Mov bx,num2• Mul bx• Mov product,ax• Mov product+2,dx• .exit• Org 2100• Num1 dw 1200h• Num2 dw 3458h• Product dw 2 dup(0)• end

.model tiny• .stack 32h• .code• Org 2000h• .startup• Mov ax, dividend• Mov dx, dividend+2• Div divisor• Mov quotient, ax• Mov remainder, dx• .exit• Org 2100• Dividend dd 65894000h• Divisor dw 4000h• Quotient dw ?• Remainder dw ?• end

Page 3: 86 pr0grams

Multi byte addition.model smallData segmentNum1 dd 38009800hNum2 dd 34003400hSum dd ?.stack 100hCode segment.startupLea si, num1, Lea di, num2Mov bx, sum, Mov cx,2clcL1: mov ax,[si]Adc ax,[di], mov [bx], axInc si, inc si, inc di, inc di, inc bx, inc bx• Loop l1, jc l2, .exit• L2: add [bx+1], 01• .exit• Code ends, • end

• .startup directive will insert while assembling , the following.

• Assume cs: code, ds: data• Mov ax,data• Movds,ax

Page 4: 86 pr0grams

Multiply decimal nos: 120 and -48• .model small • Data segment• Product dw ?• Data ends• .code• .startup• Mov al, 78h• Mov bl, 0doh• Imul• Mov product, ax• .exit• end

• 120 decimal = 78h• Find 2’s complement for –ve no.• 48d= 30h• 30h = 00110000• 11001111• 1• 11010000 = doh• Hence num1=78h• Num2= do h • Product expected = −5760d• −5760d = a8a0h

Page 5: 86 pr0grams

Signed divison• .model small• .data• Dividend db 0d5h• Divisor db 65• Quotient db ?• Remainder db ?• .stack 100h• .code• .startup• Mov al, dividend, cbw• Mov bl, divisor, idiv bl• Mov quotient, al• Mov remaider,ah• .exit• end

Page 6: 86 pr0grams

Ascii arithmatic• .model tiny• .stack 100h• .code• Mov al, ‘9’• Mov bl, ‘9’• Mov ax,0000h• Add al, bl• AAA• .exit• end

• ‘9’ = 0011 1001• ‘9’ = 0011 1001• 18 = 0111 0010• 0110 ; ac=1• 0000 1000 • AAA adjust upper nibble to zero and

as ac=1 adds 06 to lower nibble and automatically increment ah to 1

• Hence sum upper digit is in ah and lower digit is in al

Page 7: 86 pr0grams

Ascii division• .model tiny• .stack 100h• .code• Mov ax, 3634sub ax,

3030h• Mov bl, 38• Sub bl,30• AAD• Div bl• .exit• end

• Divide BCD no 64 by bcd no 8

• ‘64’ = 3634h• ‘8’ = 38• Result available in al &

ah.

Page 8: 86 pr0grams

Packed bcd to unpacked bcd• .model small• .data• BCD db 89h• Unp-BCD dw 0• .stack 100h• .code• .startup• Mov ah,00h• Mov al, BCD• Mov cl,4• Rol ax, cl• Ror al, cl • Mov unp-BCD, ax• .exit• end

Page 9: 86 pr0grams

Convert a doubleword packed bcd to unpacked bcd using near Procedure.

• .model small• .stack 100h• .data• BCD DD 65457686h• UNBCD dw 4 dup(0)• .code• .startup• Lea SI,BCD• Lea Bx, UNBCD• Mov cx,04• L1: call BCD-TO• Inc si, inc bx, loop L1• Mov cx,08, dec bx• L2: mov dl, [bx], Mov ah,02• Int 21h, dec bx ,loop L2• .exit

• BCD-TO PROC NEAR USES cx,ax• Mov al, [si]• Mov ah,al• And al, 0fh• And ah,0foh• Mov cl,04• Shr ah, cl• Mov[bx], ax• Ret

• BCD-TO ENDP

• end

Page 10: 86 pr0grams

Factotorial of a number between 1 to 8

• Model tiny• .stack 100h• . Code• .startup• N equ 8h• Mov dx,0000h• Mov cx, N-1• Mov ax,N• Mov bx, ax• L1: dec bx• Mul bx• loopL1• .exit• .end

Page 11: 86 pr0grams

Write a program to find the parity of 32-bit number

• .model small• .data• Num dd 23423565h• Evenp db ?• Oddp db ?• .stack 100h• .code • .startup• Lea si, num• Mov cx, length num• Mov bl, 00; odd counter• L1: mov al,[si]• Or al, al

• Jnp odd• Jmp L2• Odd: inc bl• L2: inc si• loopL1• Shr bl,1• Jz even• Mov oddp, 01• .exit• Even:mov evenp, 01• .exit• end

Page 12: 86 pr0grams

Convert binary/hex to decimal/ascii• .model small• .data• Num dw 0cef6h• Ascii db 5 dup(0)• .stack 100h• .code• .startup• Mov ax, num• Mov dx, 0000• Mov cx, 0ah• L1: div cx• Add dl, 30• Mov ascii, dl• Mov dx, 0000h• Cmp ax,0000h• Jnz L1.• .exit• End

• First divide by 10d will leave ones in dl(remainder)

• Next divide of quotient by 10 will leave tens in remainder etc.

Page 13: 86 pr0grams

Convert 4 digit ascii number to binary• .model small• .data • Ascii-no db ‘ 6754’• Multiplier dw 03e8h, 64h, ah,1h• Binary dw 0• Digit-total equ 04h• .stack 100h• .code• .Startup• Mov cx, digit-total• Lea si, ascii-no• Lea di, multiplier• L1: Mov al,[si]• And ax, 000fh• Mov bx,[di]• Mul bx• Add binary,ax• Inc si, inc di, inc di, dec cx• loop L1• .exit• end

Page 14: 86 pr0grams

Convert hex number to decimal number• .model small• .stack 100h• .data• Num dw 0d356h• .code • .startup• Mov bx,0000h; decimel

digit counter• Mov ax, num• Mov dx,0000h• Mov cx,0ah• L1: div cx• Push dx

• Mov dx,0000h• Inc bx• Cmp ax,0000h• Jnz L1• L2: pop dx• Add dl ,30h• Mov ah,02• Int 21h• Dec bx• Jnz L2• .exit• end

Page 15: 86 pr0grams

Moving a string block• .modal small• .stack 100• .data• Src db 100 dup(20)• db 100 dup(?)• Dst db 100 dup(0)• Length EQU N• .code• .startup• Mov ax,data• Mov es, ax• Mov cx, N• Lea si, src, lea di, dst• Cld, rep, movsb• .exit, end

• This program works well if destination block starts at an address lower than source bloch starting address. Or when there is no overlap.

• For the other case movement will start from the end of string.

Page 16: 86 pr0grams

Reverse a string• .modal small• .stack 100• .data• Src db ‘gtrajan’• Dst db 7 dup(0)• .code• .startup• Count equ N• Lea si, src• Lea di, dst• Add di, N-1• Mov cx, N

• L1: cld• Lodsb• Std• Stosb• Loop L1• Lea si, dst• Lea di, src• Mov cx,N• Cld • Rep• Movsb• .exit• end

Page 17: 86 pr0grams

Write a procedure to find the cube of cx register

• Cube proc near• Pushf• Push ax, push bx• Push dx, push si• Mov ax,cx• Mul cx• Mov bx,dx, mul cx• Mov cube,ax• Mov si,dx, mov ax,bx• Mul cx, add ax, si• Adc dx,0000h• Mov cube+2,ax• Mov cube+4, dx

• Pop si• Pop dx• Pop bx• Pop ax• Popf• Cube endp

Page 18: 86 pr0grams

Sorting in ascending order

• .modal small• .stack 100• .data• Array db 10h, 20h, 05h• .code • .startup• Mov bl, length array• Dec bl; iteration counter• L1: mov cl,bl; cl is compare counter• Lea si, array• Lea di, array+1• L2: Mov al, [si]• Cmp al,[di]• Jna L3• Xchg al,[di]• Xchg al, [si]

• L3: inc si, inc di• Dec cl• Jnz L2

Dec bl• Jnz L1• .exit• end

Page 19: 86 pr0grams

Sorting using string instructions

• .modal small• .stack 100• .data• Array db 10h, 20h, 05h• .code • .startup• Mov bl, length array• Dec bl; iteration counter• Cld• L1: mov cl, bl• Lea si, array• Lea di, array+1

• L2: lodsb• Dec si• Cmpsb• Jna L3• Mov dh,[si]• Xchg al,dh• Mov [si-1],al• Mov [si], dh• L3:dec cl, jnz L2• Dec bl, jnz L1• Exit• end

Page 20: 86 pr0grams

Searching a string byte in a string

• .modal small• .stack 100• .data• List db ‘govindaswamy’• Pr db ‘string byte present$’• Notpr db ‘string byte not

present$’• .code• .startup• Mov ax,data• Mov ds, ax• Lea di, list• Add di, length list

• Dec di• Std• Mov cx, lengh list• Mov al, ‘$’• Repne scasb• Jz msg• Lea dx, Notpr• Jmp disp• Msg: lea dx, pr• Disp: mov ah, 09• Int 21h• .exit• end

Page 21: 86 pr0grams

Checking password

• .model small• .stack 100h• .data• Password db ‘sonia’• Input –str db ‘sonim’• Good db ‘same$’• Bad db ‘not same$’• .code• .startup• Mov ax, data• Mov ds, ax• Lea si, password• Lea di, input-str• Mov cx, length password

• Cld• Repe cmpsb• Jnz msg• Lea dx, good• Jmp disp• Msg: lea dx, bad• Disp: mov ah, 09• Int 21h• .exit• end

Page 22: 86 pr0grams

Finding a number is prime or not• . Model small• . Stack 100h• .data• Prime db 00• Noprime db 00• Divisor db 02• Number equ X• .code• .startup• Start: mov ax, X• Mov cl, divisor• Div cl• Cmp ah,00• Jz L1• Cmp al, cl• Jna L2• Inc cl• Jmp start• L1: inc byte ptr noprime• .exit• L2: : inc byte ptr noprime• .exit, end

AlgorithmRepeat

divde the number X by 2If remainder = 0, ThenX is not a prime numberelse

if quotient is not greater than divisor, then X is prime number

Else quotient is not greater than divisor

Page 23: 86 pr0grams

Find the gcd or hcf two byte numbers• . Modal small• . Data• X1 db 23,42• Hcf db 00• .code • .startup• Lea si, X1• Start: mov ax, 0000h• Mov al, [si]• Mov bl, [si+1]• Cmp al,bl• Ja L1• Xchg al,bl• L1: div bl• Cmp ah,00• Jz hcf• Mov [si], bl• Mov [si+1], ah• Jmp start, hcf: mov hcf, bl• .exit, end

Algorithm

Compare number bytes and select X1 as large number and the smaller as X2.

Repeat divide X1 by X2. If remainder= 0, then X2 = hcf. Else replace X1 by X2 and X2 by remainder

Untill remainder = 0

Page 24: 86 pr0grams

Find LCM of two 16 bit numbers• .modal small• .stack 100h• .data• X1 dw 2000h• X2 dw 3450h• Lcm dw 2 dup(0)• .code• .startup• Mov bx , 0002• Mov ax, X1• Mov cx, X2• Cmp ax, cx, Ja ok , Xchg ax,cx• Ok: mov si, ax• L1: mov dx, 0000• Div cx, cmp dx, 0000• Jz Lcm , mov ax,si, mul bx• Jmp L1 • Lcm: mov Lcm, ax, mov Lcm+1, dx• .exit• end

Algorithm Compute the large number

and call it X1 and other as X2.

Repeat divide X1 by X2If remainder r=0, then X1=lcmElse replace X1 by 2X1Untill r=0

Page 25: 86 pr0grams

Find the largest number in a number string

• .modal small• .stack 100h• .data• Array db 10h,20h,30,h• Count equ ($-array)• Largest db ?• .code• .startup• Lea si,aray• Mov cx, count-1• Mov al,[si]• L1: cmp al, [si+1]• Jae next• Mov al,[si+1]• Next: inc si, loop L1• Mov largest, al, .exit , end

Page 26: 86 pr0grams

Compute total of odd and even numbers in a number array

• .modal small• .stack 100h• .data• Array db 10h,21h,32h• Count db ($-array)• Even db 00• Odd db 00• .code• .startup• Mov cx, count

• Lea si, array• L1: mov al, [si]• Shr al, 1• Jnc L2• Inc byteptr odd• Jmp L3• L2: inc byteptr even• L3: inc si• Loop L1• .exit , end

Page 27: 86 pr0grams

The following program 10 sets thevideo mode and clears the screen;makes a window of specified size and colour ; sets the cursor at a specified position within the window; displays 10 times the character

‘*’ at the cursor position

• .modal tiny• .code• .startup• Mov ah, 00;set video mode• Mov al,3, mode 3 normal text mode• Int 10h; clear screen• Mov ah,06; function no. for scrolling up• Mov al,10; no. of lines =10• Mov bh, 61h ; brown background,blue foreground• Mov ch,5; starting row co-ordinate=5• Mov cl, 20; starting column co-ordinate=20• Mov dh,14, mov dl, 60; ending row and column co-ordinate• Int 10h; create the scroll window

Page 28: 86 pr0grams

Continued from previous page• Mov ah,02;function number to set the cursor• Mov bh,0; page no=0• Mov dh,09, mov dl,39• Int 10h; cursor at (9,39)• Mov ah,9; function to display character• Mov al, ‘* ’ ; character to be displayed in al• Mov bh, 0; page no• Mov bl, 16h; blue with brown text• Mov cx,10; count =10• Int10h; display the character in al 10 times• .exit• end