Sie sind auf Seite 1von 18

INDEX

S.NO.

Name of the Program

Page No.

1.

Program to transfer a block of data

2-3

2.

Program to exchange a block of data

3-5

3.

Program to sort the given array in ascending order

5-6

4.

Program to find largest no. in given array

5.

Program for arithmetic instructions

8-9

6.

Program to compute square and cube of the given 10-11


number

7.

Program to compare two numbers

11-12

8.

Program to generate 8-bit hexadecimal up/down 12-13


counter

9.

Program to generate 8-bit BCD up/down counter

13-14

10.

Program for decimal to hexadecimal conversion

14-15

11.

Program for hexadecimal to decimal conversion

15

12.

Program for BCD to ASCII conversion

16

13.

Program for hexadecimal to ASCII conversion

16-17

14.

Program for ASCII to hexadecimal conversion

18

I.

DATA TRANSFER INSTRUCTION Block Move, Exchange, Sorting, Largest element in an


array
PROGRAM 1:
AIM: Write a assembly language program to transfer n=10 bytes of data from location
8035h to location 8041(without overlap).

LABEL

BACK:

HERE:

MNEMONICS
ORG 0000H
SJMP 30H
ORG 30H
MOV DPH,#80H
MOV R0,#35H
MOV R1,#41H
MOV R3,#0AH
MOV DPL,R0
MOVX A@DPTR
MOV DPL,R1
MOVX @DPTR,A
INC RO
INC R1
DJNZ R3,BACK
SJMP HERE
END

COMMENTS

//Source address
//destination address
//count

RESULT:
Before execution : 10 locations X:8035H are filled up with data

After execution : 10 locations X: 8041H are filled up with data from 8035H.

ASSEMBLY LANGUAGE PROGRAM TO EXCHANGE A BLOCK OF DATA

PROGRAM -2:
AIM: Write an assembly language program to exchange n=5 bytes of data at location
0027H and at location 0041H.

LABEL

BACK:

MNEMONICS
ORG 00H
SJMP 30H
ORG 30H
MOV R0,#27H
MOV R1,#41H
MOV R3,#05H
MOVX A@r0
MOV r2,a
MOVX a,@r1

COMMENTS

//Source address
//destination address
//count

HERE:

MOVX @r0,a
MOV a,r2
MOVX @r1,a
INC RO
INC R1
DJNZ R3,BACK
SJMP HERE
END

USING XCH COMMAND


LABEL

BACK:

HERE:

MNEMONICS
ORG 00H
SJMP 30H
ORG 30H
MOV R0,#27H
MOV R1,#41H
MOV R3,#05H
MOVX A@r0
MOV r2,a
MOVX a,@r1
XCH a,r2
MOV @r1,a
XCH a,r2
MOVX @r0,a
INC RO
INC R1
DJNZ R3,BACK
SJMP HERE
END

COMMENTS

//Source address
//destination address
//count

RESULT:Before execution : 5 locations X:0027H & X:0041H are filled up with data

After execution : the data at X: 8027H & X:8041H are exchanged.

ASSEMBLY LANGUAGE PROGRAM TO SORT NUMBERS


PROGRAM- 3
AIM: Write an assembly language program to sort an array of n=6 bytes of data in ascending
order stored from location 8035H.

LABEL

L1:

L2:

NOEXCHG:

MNEMONICS
ORG 0000H
SJMP 30H
ORG 30H
MOV R0,#05
MOV DPTR,#9000h
MOV A,R0
MOV R1,A
MOVX a, @DPTR
MOV B,A
INC DPTR
MOVX a, @DPTR
CLR C
MOV R2,A
SUBB A,B
JC NOEXCHG
MOV A,B
MOVX @DPTR,a
DEC DPL
MOV a,R2
MOVX @DPTR,a
INC DPTR
DJNZ R1,L2
DJNZ R0,L1
SJMP $
END

COMMENT

//count n-1-ARRAY SIZE-Pass counter


//array stored from address 9000h

//get number from array


//& Store in B
//next number in the array
//reset borrow flag
//store in R2
//2nd -1st no.- no compare inst. in 8051
//JNC-for ascending order
//Exchange the Numbers in the array
//DEC DPTR -instruction not present

//decrement compare counter


//decrement pass counter

RESULT:Before execution : Unsorted Array at 9000h


After execution : sorted Array (descending order ) at 9000H

PROGRAM-4:
AIM: Write an assembly language program to find the largest element in a given string of n=
6bytes at location 4000h .store the largest element at location 4062h.

LABEL

NEXTBYTE:

SKIP:

MNEMONICS
ORG 0000H
SJMP 30H
ORG 30H
MOV R3,#6
MOV DTR,#4000H
MOVX A,@DPTR
MOV r1,a
INC DPTR
MOVX A,@
CLR C
MOV R2,A
SUBB A,R1
JC SKIP
MOV A,r2
MOV R1,A
DJNZ R3,NEXTBYTE
MOV DPL,#62H
MOVA,R1
MOVX @DPTR,A
SJMP $
END

COMMENTS

//length of array
//starting address of array

//reset borrow flag


//next number in the array
//other Num-previous largest no
//JNC for smallest element
//Update larger number in r1

//location of the result -4062H


//largest number
//store at #4062H

RESULT:Before Execution:

After Execution : location 4062 has the largest element

(II)

ARITHMETIC INSTRUCTIONS :-

PROGRAM -5
AIM: Write an assembly language program the following: If x=0 perform w+v else if x=1
perform w-v; else if x=2 perform w*v, else if x=3 perform w/v, where w &v are eight bit
numbers.

LABEL

SKIP:
CKSUB:

SKIP:
CKMUL:

CKDIV:

OTHER:

MNEMONICS
ORG 0000H
SJMP 30H
ORG 30H
MOV R0,#40H
MOVX A,@R0
MOV R1,A
INC R0
MOVX A,@R0
MOV B,A
INC R0
MOVX A,@R0
CJNE R1,#00,CKSUB
ADD A,B
MOV B,#00
JNC SKIP
MOV B,#01H
SJMP LAST
CJNE R1,#01,CKMUL
CLR C
SUBB A,B
MOV B,#00
JNC SKIP1
MOV B,#0FFH
SJMP LAST
CJNE R1,#02,CKDIV
MUL AB
SJMP LAST
CJNE R1,#03,OTHER
DIV AB
SJMP LAST
MOV A,#00
MOV,B#00
8

COMMENTS

//R1 has condition X


//B has 1st number-w

//perform addition
//B has carry

// reset borrow flag


//B indicates borrow
//FF indicates negative no.

//16 bit produce in AB with


having lower byte

//quotient in A & remainder in B

LAST:

HERE:

INC R0
MOVX @R0,A
INC R0
MOVX @R0,A
INC R0
MOV A,B
MOVX @R0,A
SJMP HERE
END

RESULT:
Before Execution: ADD
After Execution : ADD
Before Execution : MUL

SUB
After Execution : SUB
After Execution : MUL

ASSEMBLY PROGRAM ILLUSTRATEING SQUARE AND CUBE OPERATIONS


//cube is an example of 16 bit arithmetic operations//depending on flag condition ,square or
cube is performed//flag is a bit in the bit addressable RAM ,say 1 st bit of location 20h is used
,then bit address is 01

PROGRAM -6:
AIM : An eight bit number X is stored in external memory location 9000h. write an ALP to
compute (1)the square of the number X if LSB of data RAM 20H(bit address01H)is set (2) the
cube of the number X if LSB of data RAM 20H(bit address 01H) is reset. Store your result at
location 9001, 9002, 9003H.
LABEL

MNEMONICS
ORG 0000H
SJMP 30H
ORG 30H
MOV DPTR,#9000H
MOVX A,@DPTR
MOV R0,A
MOV B,A
MUL AB
CLR C
JB 01,LAST

COMMENT

//get number X
//store in R0
//square it X^2
//for storing result
//if bit 01 is set then END, else do
cube
//store upper part of square
//B-lower part of X2
// A-X
//X*LOWER X^2

PUSH B
MOV B,A
MOV A,R0
MUL AB
INC DPTR
MOVX @DPTR,A
MOV A,B
MOV R2,A

//store partial result


//upper part of X*LOWER X^2 in R2
//get back upper part of square
//A-X
//X*upper X^2
// add to partial result

POP B

LAST:

MOV A,R0
MUL AB
ADD A,R2
INC DPTR
MOVX @DPTR,A
MOV A,B
ADDC A,#00

//add carry to b (for square


result,c=0)

INC DPTR
MOVX @DPTR,A
SJMP $
END

10

RESULT:
CUBE of 56H is 9B498 which is stored as 98,B4,09 (Lower Byte First)

To get SQUARE make the D1 bit of data memory 20H high ,say FF,02,06 etc. The bit address is
01 .similarly bit address 78H correspond to D0 bit of data RAM location 2FH.

PROGRAMMING ILLUSTRATING BIT MANIPULATIONS


PROGRAM -7
AIM: Two eight bit numbers NUM1 & NUM2 are stored in external memory locations 8000H &
8001H respectively. Write an ALP to compare the 2 numbers. Reflect your results as:
IF NUM1<NUM2, SET LSB of data RAM 2F (bit address 78H)
IF NUM1>NUM2, SET MSB of data RAM 2F (bit address 7FH)
IF NUM1=NUM2, Clear both LSB & MSB of bit addressable memory location 2FH.

LABEL

MNEMONICS
ORG 0000H
SJMP 30H
ORG 30H
MOV DPTR,#8000H
MOVX A,@DPTR
MOV R0,A
INC DPTR
MOVX A,@DPTR
CLR C
SUBB A,R0

COMMENTS

11

JZ EQUAL
JNC BIG
SETB 78H
SJMP END1
SETB 7FH
SJMP END1
CLR 77H
CLR 7FH
SJMP END1
END

BIG:
EQUAL:
END1:

RESULT:
1. Before Execution : X : 08000H = 45 & X :8001 = 35
After Execution: D : 02FH = 01
2. Before Execution : X : 08000H = 25 & X :8001 = 35
After Execution: D : 02FH = 80
3. Before Execution: X : 08000H = 45 & X :8001 = 45
After Execution: D : 02FH = 00

(III)

COUNTERS

ASSEMBLY PROGRAM ILLUSTRATING HEX UP/DOWN COUNTERS


//counter program hex/binary counters

PROGRAM -8
AIM: Write an ALP to implement (display) an eight bit up/down binary (hex) counters on watch
window.
LABEL

MNEMONICS
ORG 0H
SJMP 30H
ORG 0H
MOV a,#00

COMMENTS

12

BACK:

HERE:
DELAY:
DECR1:
DECR:

ACALL DELAY
INC a
JNZ BACK
SJMP HERE

//DEC a for binary down counter

MOV r1,#0FFH
MOV r2,#0FFH
MOV r3,#0FFH
DJNZ r3,$
DJNZ r2,DECR
DJNZ r1,DECR1
RET
END

RESULT:
Acccumulator A is incremented in binary from 00, 01, 02,. . . . . . 09, 0A, 0B,. . . ,0F, 10,
11, . . . FF.

ASSEMBLY PROGRAM ILLUSTRATING BCD UP/DOWN COUNTERS


//counter program BCD up/down counters

PROGRAM -9
AIM: Write an ALP to implement (display) an eight bit up/down BCD counters on watch
window.
LABEL

HERE:

MNEMONICS
ORG 0H
SJMP 30H
ORG 30H
MOV a,#00
ACALL DELAY
ADD a,#99H
DA A
JNZ BACK
SJMP HERE

DELAY:

MOV r1,#0FFH

BACK:

COMMENTS

//ADD 01 for BCD up counter


//for BCD counter

13

DECR1:
DECR:

MOV r2,#0FFH
MOV r3,#0FFH
DJNZ r3,$
DJNZ r2,DECR
DJNZ r1,DECR1
RET
END

RESULT:
Acccumulator A is incremented in BCD from 00, 01, 02,. . . . . . 09, 10, 11,. . . ,99.

(V) CONVERSION PROGRAMS

PROGRAM-10:
AIM: Write an ALP to implement decimal to hex conversion.
LABEL

HERE:

MNEMONICS
ORG 0000H
SJMP 30H
ORG 30H
MOV DPTR,#40H
MOVX A,@DPTR
ANL A,#0F0H
SWAP A
MOV B,#0AH
MUL AB
MOV r1,a
MOVX A,@DPTR
ANL A,#0FH
ADD A,R1
INC DPTR
MOVX @DPTR,A
SJMP HERE
END

COMMENTS

//2-digit decimal number to be converted is given


in data memory 40H
//Obtain upper decimal digit
//Bring to the units place
//MULTIPLY tens digit with #0A-to get tens in hex
//Temporarily store the converted tens value
//Get the decimal no. again
//Obtain the unit digit
//Add to the converted tens value
//Increment data address
//Converted hexadecimal number in next location

14

RESULT:
Before Execution: X:0040H=45(Decimal/BCD)
After Execution: X:0041H=2D(hex value)

PROGRAM -11:
AIM: Write an ALP to implement hex to decimal conversion.
LABEL

HERE:

MNEMONICS
ORG 0000H
SJMP 30H
ORG 30H
MOV DPTR,#9000H
MOVX A,@DPTR
MOV B,#10
DIV AB
INC DPTR
XCH A,B
MOVX @DPTR,A
XCH A,B
MOV B,#10
DIV AB
INC DPTR
XCH A,B
MOVX @DPTR,A
XCH A,B
INC DPTR
MOVX @DPTR,A
SJMP HERE
END

COMMENTS

//Get hex number


//Divide by 10(0AH)

//Store the reminder (in B) In units place


//Divide the quotient in A by 10

// Store the reminder (in B) In tens place

//Store the quotient (in A) in hundreds place

RESULT: Before Execution: 9000H=FF(HEX NUMBER)


After Execution: 9001 to 9003H=unpacked BCD number(decimal)-5,5,2(i.e., 255 stored
Lower digit first)

15

PROGRAM-12
AIM: Write an ALP to implement BCD to ASCII conversion.

LABEL

MNEMONICS
ORG 0000H
SJMP 30H
ORG 30H
MOV R1,#50H
MOV A,@R1
MOV R2,A
ANL A,#0FH
ORL A,#30H
INC R1
MOV @R1,A
MOV A,R2
SWAP A
ANL A,#0FH
ORL A,#30H
INC R1
MOV @R1,A
SJMP HERE
END

HERE:

COMMENTS

//Get BCD data byte from RAM location 50H


//Store in R2
//Get the lower nibble
//Add/or with 30H i.e., 0-9converted to 30-39H
//Store the lower digits ASCII code
//Get back the number
//Swap nibbles in A
//Get the upper BCD digit
//Convert to ASCII
//Store the upper digits ASCII code

RESULT:
The BCD code 28 at D : 0050H is converted to 2 ASCII codes- 38H 32H

PROGRAM -13:
AIM: Write an ALP to implement Hexadecimal to ASCII conversion.
//This program also illustrates conditional branching (JNC), call and return instructions.

16

LABEL

ASCII:

SKIP:

MNEMONICS
ORG 0000H
SJMP 30H
ORG 30H
MOV R1,#50H
MOV A,@R1
MOV R2,A
ANL A,#0FH
ACALL ASCII
INC R1
MOV @R1,A
MOV A,R2
SWAP A
ANL A,#0FH
ACALL ASCII
INC R1
MOV @R1,A
SJMP $
MOV R4,A
CLR C
SUBB A,#0AH
MOV A,R4
JNC SKIP
ADD A,#07H
ADD A,#30H
RET
END

COMMENTS

//Get hexadecimal data byte from RAM location


50H
//Store in R2
//Get the lower nibble
//Convert to ASCII
//Store the lower digits ASCII code
//Get back the number
//Swap nibbles in A
//Get the upper BCD digit
//Convert to ASCII
//Store the upper digits ASCII code

//Store a
//Check if digit>=0A

//Add 07 if>09
//Else add only30H for 0-9

RESULT: The BCD code 2C at D : 0050H is converted to 2 ASCII codes- 43H (for 0C) & 32H (for
02)

Another Example BA

17

PROGRAM-14
AIM: Write an ALP to implement ASCII to Hexadecimal conversion.
LABEL

SKIP:

MNEMONICS
ORG 0000H
SJMP 30H
ORG 30H
MOV R1,#50H
MOV A,@R1
CLR C
SUBB A,#41H
MOV A,@R1
JC SKIP
CLR C
SUBB A,#07H
CLR C
SUBB A,#30H
INC R1
MOV @R1,A
SJMP $
END

COMMENTS

//Get ASCII byte from RAM location 50H

//Store the hex code

RESULT:
The ASCII code 45 at D : 0050H is converted to hexadecimal -0E at 51H.

18

Das könnte Ihnen auch gefallen