Developed by: Sujit Jhare Assembly Language Programming

11
Developed By: Sujit Jhare Assembly Language Programming in 8086 Arithmetic operation: Assembly Language program for swapping two numbers ; REGISTER : AX BX ; PORT : no uses CODE SEGMENT LDA AL, 2000H ; load address to AX MOV BX, AX ; initialize bx LDA AH, 2200H ; load address to AX STA AL, 2000H ; swap the value MOV AX, BX ; load value to AX STA AX, 2200H ; swap register value HLT ; halt for some time END CODE once (1's) and twos (2's) complement of a number 1's complement of the number LDA 2200H CMA STA 2300H HLT cma will give 1's complement of a number 2's complement of a number LDA 2200H CMA ADI 01H STA 2300H HLT Adding the 4 four most significant bit to a hexa no

description

Assembly Language Programming in 8086

Transcript of Developed by: Sujit Jhare Assembly Language Programming

Developed By: Sujit Jhare

Assembly Language Programming in 8086

Arithmetic operation:

Assembly Language program for swapping two numbers

; REGISTER : AX BX

; PORT : no uses

CODE SEGMENT

LDA AL, 2000H ; load address to AXMOV BX, AX ; initialize bxLDA AH, 2200H ; load address to AXSTA AL, 2000H ; swap the valueMOV AX, BX ; load value to AXSTA AX, 2200H ; swap register valueHLT ; halt for some time

END CODE

once (1's) and twos (2's) complement of a number

1's complement of the number

LDA 2200HCMASTA 2300HHLTcma will give 1's complement of a number

2's complement of a number

LDA 2200HCMAADI 01HSTA 2300HHLT

Adding the 4 four most significant bit to a hexa no

While loop program:

Object: program to read ASCII code after a strobe signal is sent from keyboard

; REGISTERS: uses CS, DX, AL

; PORTS : uses FFFAH – strobe signal input on LSB

; FFF8H – ASCII data input port

CODE SEGMENT

ASSUME CS: CODE

MOV DX, 0FFFAH ; point DX at Strobe port

LOOK_AGAIN : IN AL, DX ; read keyboard strobe

AND AL, 01 ; mask extra bits and set flages

JZ LOOK_AGAIN ; if strobe is low then keep looking

MOV DX, 0FFF8H ; else point DX at data port

IN AL , DX ; read ASCII code

CODE END

END

Do-While loop Program:

Object: program adds and inflation factor to a series of prices in memory, it copies the new prices over the old price.

;REGISTERS : uses DS, CS, AX, BX, CX

;PORTS : none uses

ARRAY SEGMENT

COST DB 20H 28H 15H 26H 19H 27H

PRICES DB 36H 55H 27H 42H 38H 41H

ARRAY END

CODE SEGMENT

ASSUME CS: CODE, DS: ARRAYS

START: MOV AX , ARRAYS ; initialize data segment

MOV DS, AX ; register

LEA BX , PRICES ; initialize pointer

MOV CX, 0008H ; initialize counter

DO_NEXT: MOV AL , [BX] ;copy a price to AL

ADD AL, 03H ; add inflation factor

DAA ; make sure result is BCD

MOV [BX], AL ; copy result back to memory

INC BX ; point to next price

DEC CX ; decrement counter

JNZ DO_NEXT ; if not last, to get next

CODE END

END START

program to find square root of 8-bit number

MOV SI,2000MOV DI,4000MOV CX,0001MOV BX,0000MOV AL,[SI] ; Load AL with the value given as at SIUP SUB AL,CLJL down ; jump to down labelINC BLADD CL,02 ; add 2 to contents of CL registerJMP UP ; jump to up labelDOWN MOV[DI],BLINT A5

Program to perform sum of elements in an arrayFollowing is the assembly language program to find the sum of elements in a given array. This code is for 16-bit addition with 64 stack size.

Code:

MOV AX,@DATA ; Load AX with dataMOV DS,AX ; Load DX with AXMOV ALPBCD ; Load AL with N1MOV BL,AL ; load BL with ALAND AL, OFH ; AND AL with OF HMOV UBCD2,AL; Load N3 with ALAND BL, OFOHMOV CX,0004 ; Load CX with 0004ROR BL,CL Rotate right BL,CL timesMOVE N2,BLINT 3

check wether given 16 bit number is palindrome or not

Palindrome program taking 16 bits.

taking C7H,EBH as given data i have done the program

which is equivalant to 1101011111101011

ORG 2000H

LXI H,2100H ;DATAS ARE STORED AT LOCATIONS 2100H,21001H

MOV A,M

MVI C,07H

LOOP: RRC

CALL SBR ;SBR IS SUBROUTINE

DCR C

JNZ LOOP

ANI 01H

ADD D

INX H

CMP M

LXI H,2400H ;1 IS STORED AT MEM-LOC 2400H IF PALIIN ELSE 0

JZ PALIN

MVI M,00H

JMP FIN

PALIN: MVI M,01H

FIN: HLT

SBR: MOV B,A

MOV A,D

JNC BYPAS ;IF CARRY IS NOT 1 IS NOT AADED

ADI 01H

BYPAS: RLC

MOV D,A

MOV A,B

RET

ORG 2100H

DB 00C7H,00EBH ;DATA BYTE

END

here EBH is reversed and compared with C7H, if they are equal then plain else not

palin

once (1's) and twos (2's) complement of a number

1's complement of the number

LDA 2200H

CMA

STA 2300H

HLT

cma will give 1's complement of a number

2's complement of a number

LDA 2200H

CMA

ADI 01H

STA 2300H

HLT

here first the once's complent of the number is determined and then one is added to it

Adding two hexa-decimal numbers with carry

LDA 2200H

MOV C,A

LXI H,2201H

SUB A

MOV B,A

Back: ADD M

JNC Skip

INR B

skip INX H

DCR C

JNZ BACK

STA 2300H

MOV A,B

STA 2301H

HLT

This program will add tho hexa deciaml numbers without carry

How to add two Hexa decimal numbers without carry

Adding hexa-decimal numbers without carry

LDA 2200

MOV C,A

SUB A

LXI H,2201H

Back: ADDM

INT H

DCR C

JNZ Back

STA 2300H

HLT

lnitialize counter c

then make sum=0

initialize the pointer

sum = sum+data

decrement counter c

if counter is zero then repeat else

store the result

halt the program.

Program to perfrom sum of elements in an array

Program to perform sum of elements in an array

Following is the assembly language program to find the sum of elements in a given

array. This code is for 16-bit addition with 64 stack size.

Code:

MOV AX,@DATA ; Load AX with data

MOV DS,AX ; Load DX with AX

MOV ALPBCD ; Load AL with N1

MOV BL,AL ; load BL with AL

AND AL, OFH ; AND AL with OF H

MOV UBCD2,AL; Load N3 with AL

AND BL, OFOH

MOV CX,0004 ; Load CX with 0004

ROR BL,CL Rotate right BL,CL times

MOVE N2,BL

INT 3

Assembly program to convert BCD to ASCII

Assembly program to convert BCD to ASCII

Initially the input is moved in SI register , then its divided by 10 and then 30 is added

to the number for both AH, AL i.e. the 8 bit higher and lower registers. Then the

output is moved to the destination index register.

Following is the program code in assembly language to convert a BCD number to ASCII.

MOV SI,2000

MOV DI,4000

MOV AX,[SI]

MOV BL,10

DIV BL

MOV DL,30

ADD AL,DL

ADD AH,DL

MOV [DI],AX

INT A5

program to find factorial of given numbers

following is the assembly language program to find factorial of given numbers

MOV SI , 2000

MOV DI,2002

MOV CX,[SI]

MOV AX,CX ; Move contents of CX to contents of AX register

DEC CX ; Decrement CX

UP: MUL CX

DEC CX ; Decrement CX

JNZ; UP ; Jump if not zero

MOV [DI], AX ; Load the values of AX into location given by DI

INT A5; Halt the program

assembly program to find out the largest number from an unordered array

The program to find out the largest number from an unordered array of sixteen 8 bit

numbers stored sequentially in the memory locations starting at offset 0500H in the

segment 2000H

Logic: The 1st number of the array is taken in a register, say AL. The 2nd number of

array is then compared with the 1st one. If the 1st one is greater than 2nd one, it is

left unchanged. However, if the 2nd one is greater than 1st, 2nd number replaces the

1st one in the AL register. The procedure is repeated for every number in array and

thus requires 15 iterations.

MOV CX, 0FH; Initialize counter for no. of iterations

MOV AX, 2000H; Initialize data segment

MOV DS, AX;

MOV SI, 0500H; Initialize source pointer

MOV AL, [SI]; Take 1st number in AL

BACK: INC SI;

CMP AL,[SI];

INC SI;

CMP AL,[SI];

JNC NEXT;

MOV AL,[SI];

NEXT: LOOP BACK; Repeat the procedure 15 times

HLT

stack program for push and pop

following is the assembly language program for push and pop operations in a stack.

code segment

main:

mov sp,1000h initialize SP to point to stack

mov ax,1234h

mov bx,5678h

mov cx,9abcdh

push ax

push cx

pop ax

pop ax

pop bx

pop cx

mov bx,0200h

mov w[bxx],1234h; address 0200 holds 1234

push [0200h]

push[bx]

mov bx,0210h

pop [bx]

pop[0212h]

imp main ; demo again

code ends

assembly language program to reverse a given string

Following is the assembly language program to reverse a given string.

MOV AX @ DATA ; AX IS INITIALIZED WITH DATA

MOV DS AX ; AX IS MOVED INTO DS

MOV CX 0005H ; CX IS INITIALIZED TO 5

LEA SI A1 ; SI IS HAVING LEAD E.A OF A1

LEA DI A2; DI IS HAVING LEAD E.A OF A2

ADD SI 0004

AGAIN: MOV AL[SI]

MOV [DI]AL ; AL IS MOVED INTO DI

DEC SI

INC DI

LOOP AGAIN

INT 3 ; INTERRUPT

END