0% found this document useful (0 votes)
1 views

MC Lab Programs with Output

The document provides a comprehensive set of assembly language programs that demonstrate various operations such as data transfer, addition, subtraction, multiplication, and division of numbers, both 8-bit and 16-bit. It includes examples of addressing modes, BCD arithmetic, and algorithms for sorting and finding the smallest number in an array. Each program is detailed with input, output, and the status of the PSW register where applicable.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

MC Lab Programs with Output

The document provides a comprehensive set of assembly language programs that demonstrate various operations such as data transfer, addition, subtraction, multiplication, and division of numbers, both 8-bit and 16-bit. It includes examples of addressing modes, BCD arithmetic, and algorithms for sorting and finding the smallest number in an array. Each program is detailed with input, output, and the status of the PSW register where applicable.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

Assembly Language Programs

1. Transfer the data from source to the destination


a. 1 byte data from TCON (88h) to register R2 using direct addressing
mode
b. 1 byte data from TCON (88h) to register R2 using indirect addressing
mode

a.
No. of bytes
MOV R2, 88h 02
END

INPUT
88h = 22h

OUTPUT
R2 = 22h

b.
No. of bytes
MOV R1, #02h 02
MOV @R1, 88h 02
END

INPUT
R1 = 02h

OUTPUT
02h = 88h
2. Addition of two 8-bit numbers to get 8-bit sum using (i). Immediate
addressing mode and (ii). Direct addressing mode

i. Immediate addressing mode

No. of bytes
MOV A, #8Eh 02
ADD A, #2Eh 02
MOV R1, A 01
END

INPUT Output
A = 8Eh R1 = BCh
A = 2Eh

Status of PSW register:


Parity Flag = 1
OV Flag = 0
AC Flag = 1
Carry Flag = 0

ii. Direct addressing mode

No. of bytes
MOV A, 40h 02
ADD A, 41h 02
MOV 42h, A 02
END

INPUT Output
40h = Ah 42h = 18h
41h = Eh

Status of PSW register:


Parity Flag = 0
OV Flag = 0
AC Flag = 1
Carry Flag = 0
3. Addition of two 8-bit numbers to get 16-bit answer
No. of bytes
MOV A, 50h 02
ADD A, 51h 02
MOV 52h, A 02
MOV A, #00h 02
ADDC A, #00h 02
MOV 53h, A 02
END

INPUT Output
50h = AFh 52h = 8Ch
51h = DDh 53h = 01h

Status of PSW register:


Parity Flag = 1
OV Flag = 0
AC Flag = 0
Carry Flag = 0

4. Addition of two 16-bit numbers to get 16 bit sum


No. of bytes
MOV A, 40h 02
ADD A, 42h 02
MOV 44h, A 02
MOV A, 41h 02
ADDC A, 43h 02
MOV 45h, A 02
END

INPUT Output
40h = 33h 44h = 55h
42h = 22h 45h = 43h
41h = 32h
43h = 11h

5. Addition of two 16-bit numbers to get 24-bit sum

No. of bytes
MOV A, 50h 02
ADD A, 52h 02
MOV 54h, A 02
MOV A, 51h 02
ADDC A, 53h 02
MOV 55h, A 02
MOV A, #00h 02
ADDC A, #00h 02
MOV 56h, A 02
END

INPUT Output
50h = E1h 54h = 8Dh
52h = ACh 55h = 8Ch
51h = AFh 56h = 01h
53h = DCh

6. Subtraction of two 8-bit numbers

No. of bytes
MOV A, 40h 02
SUBB A, 41h 02
MOV 42h, A 02
END

INPUT Output
40h = AAh 42h = 98h
41h = 12h

Status of PSW register:


Parity Flag = 1
OV Flag = 0
AC Flag = 0
Carry Flag = 0

7. Subtraction of two 16-bit numbers

No. of bytes
MOV A, 40h 02
SUBB A, 42h 02
MOV 44h, A 02
MOV A, 41h 02
SUBB A, 43h 02
MOV 45h, A 02
END

INPUT Output
40h = 1Ch 44h = 08h
42h = 14h 45h = 04h
41h = B0h
43h = ACh

Status of PSW register:


Parity Flag = 1
OV Flag = 0
AC Flag = 1
Carry Flag = 0

8. Multiplication of two 8-bit numbers


No. of bytes
MOV A, 40h 02
MOV B, 41h 02
MUL AB 01
MOV 42h, A 02
MOV 43h, B 02
END

INPUT Output
40h = ABh 42h = B4h
41h = 1Ch 43h = 12h

Status of PSW register:


Parity Flag = 0
OV Flag = 1
AC Flag = 0
Carry Flag = 0
9. Division of 8-bit numbers
No. of bytes
MOV A, 40h 02
MOV B, 41h 02
DIV AB 01
MOV 42h, A 02
MOV 43h, B 02
END

INPUT Output
40h = 56h 42h = 04h
41h = 12h 43h = 0Eh

Status of PSW register:


Parity Flag = 1
OV Flag = 0
AC Flag = 0
Carry Flag = 0

10. To find the Square of a Number


No. of bytes
MOV A, 40h 02
MOV B, A 02
MUL AB 01
MOV 41h, A 02
MOV 43h, B 02
END

INPUT Output
A = AAh 41h = E4h
B = AAh 43h = 70h

Status of PSW register:


Parity Flag = 0
OV Flag = 1
AC Flag = 0
Carry Flag = 0
11. To find Cube of a Number
No. of bytes
MOV A, 40h 02
MOV B, A 02
MUL AB 01
MOV B, 40h 02
MUL AB 01
MOV 41h, A 02
MOV 42h, B 02
END

INPUT Output
40h = 05h 41h = 7Dh
B = 05h 42h = 00h

Status of PSW register:


Parity Flag = 0
OV Flag = 0
AC Flag = 0
Carry Flag = 0

12. Addition of 2 single bit BCD number


No. of bytes
MOV A, 40h 02
ADD A, 41h 02
DA A 01
MOV 42h, A 02
END

INPUT Output
40h = 2h 42h = 5h
41h = 3h

Calculation:
13. Addition of two 2 digit BCD numbers
No. of bytes
MOV A, 40h 02
ADD A, 41h 02
DA A 01
MOV 42h, A 02
CLR A 01
ADDC A, #00h 02
MOV 43h, A 02
END

INPUT Output
40h = 41h 42h = 78h
41h = 37h 43h = 00h

Calculation:

14. Addition of two 4 digit BCD numbers


No. of bytes
MOV A, 40h 02
ADD A, 42h 02
DA A 01
MOV 50h, A 02
MOV A, 41h 02
ADDC A, 43h 02
DA A 01
MOV 51h, A 02
CLR A 01
ADDC A, #00h 02
MOV 52h, A 02
END
INPUT Output
40h = 25h 50h = 90h
42h = 65h 51h = 37h
41h = 20h 52h = 00h
43h = 17h

Calculation:

15. Data transfer program from one block to another block within internal
RAM
No. of bytes
MOV R3, #05h 02
MOV R0, #50h 02
MOV R1, #60h 02
UP: MOV A, @R0 01
MOV @R1, A 01
INC R0 01
INC R1 01
DJNZ R3, UP 02
END

INPUT Output
50h = 11 60h = 11
51h = 12 61h = 12
52h = 13 62h = 13
53h = 14 63h = 14
54h = 15 64h = 15

16. Data transfer program between internal RAM and external RAM

No. of bytes
MOV R0, #40h 02
MOV DPTR, #9500 03
MOV R3, #05h 02
AGAIN: MOV A, @R0 01
MOVX @DPTR, A 01
INC R0 01
INC DPTR 01
DJNZ R3, AGAIN 02
END

INPUT Output
40h = 11 9500h = 11
41h = 12 9501h = 12
42h = 13 9502h = 13
43h = 14 9503h = 14
44h = 15 9504h = 15

17. Block of data Exchange


No. of bytes
MOV R0, #40H 02
MOV R1, #50H 02
MOV R7, #0AH 02
BACK: MOV A, @R0 01
XCH A, @R1 01
MOV @R0, A 01
INC R0 01
INC R1 01
DJNZ: R7, BACK 02
END

Input:

Address Data Address Data


40h 11 50h 25
41h 12 51h 26
42h 13 52h 27
43h 14 53h 28
44h 14 54h 29
Output:

Address Data Address Data


40h 25 50h 11
41h 26 51h 12
42h 27 52h 13
43h 28 53h 14
44h 29 54h 15

18. Program to arrange the data of an array in an ascending order


No. of bytes
ORG 0000H
MOV R4, 30H 02
MOV R0, #50H 02
L1: MOV B, R4 01
MOV R2, B 01
MOV A, R0 01
INC A 01
MOV R1, A 01
L2: MOV 10H, @R0 02
MOV A, @R1 02
CJNE A, 10H, L3 03
SJMP NEXT 02
L3: JC EXCHANGE 02
SJMP NEXT 02
EXCHANGE: MOV @R0, A 02
MOV @R1, 10H 02
NEXT: INC R1 01
DJNZ R2, L2 02
INC R0 01
DJNZ R4, L1 02
END

INPUT Output
50h = 37 50h = 04
51h = 24 51h = 12
52h = 12 52h = 19
53h = 19 53h = 24
54h = 04 54h = 37
19. Arranging data of an array in descending order

No. of bytes
ORG 0000H
MOV R4, 30H 02
MOV R0, #50H 02
L1: MOV B, R4 01
MOV R2, B 01
MOV A, R0 01
INC A 01
MOV R1, A 01
L2: MOV 10H, @R0 02
MOV A, @R1 02
CJNE A, 10H, L3 03
SJMP NEXT 02
L3: JNC EXCHANGE 02
SJMP NEXT 02
EXCHANGE: MOV @R0, A 02
MOV @R1, 10H 02
NEXT: INC R1 01
DJNZ R2, L2 02
INC R0 01
DJNZ R4, L1 02
END

INPUT Output
30h = 4
50h = 37 50h = 37
51h = 24 51h = 24
52h = 12 52h = 19
53h = 19 53h = 12
54h = 04 54 = 04
20. Program to add first 10 natural numbers
ORG 0000H
MOV A, #0h
MOV R2, #0Ah
MOV R0, #0h
AGAIN : INC R0
ADD A, R0
DJNZ R2, AGAIN
MOV 46h, A
END

21. Program to find the smallest number in the given array


ORG 0000H
MOV R0, #30H
MOV R1, #05H
MOV A, @R0
DEC R1
INC R0
LOOP: MOV B, @R0
CJNE A, B, CHECK
SJMP NEXT
CHECK: JC NEXT
MOV A, B
NEXT: INC R0
DJNZ R1, LOOP
MOV 40H, A
END

INPUT Output
30h = 37 40h = 04
31h = 24
32h = 12
33h = 19
34h = 04

22. Program to find the largest number in the given array


ORG 0000H
MOV R0, #30H
MOV R1, #05H
MOV A, @R0
DEC R1
INC R0
LOOP: MOV B, @R0
CJNE A, B, CHECK
SJMP NEXT
CHECK: JNC NEXT
MOV A, B
NEXT: INC R0
DJNZ R1, LOOP
MOV 40H, A
END
INPUT Output
30h = 37 40h = 37
31h = 24
32h = 12
33h = 19
34h = 04
23. Program to count number of 1’s in a byte
MOV A, 70h
MOV R5, #00h
MOV R3, #08h
BACK: RLC A
JNC SKIP
INC R5
SKIP: DJNZ R3, BACK
MOV 71h, R5
END

INPUT Output
70h = AA 71h = 04

24. Program to count number of 0’s in a byte


MOV A, 70h
MOV R5, #00h
MOV R3, #08h
BACK: RLC A
JC SKIP
INC R5
SKIP: DJNZ R3, BACK
MOV 71h, R5
END

INPUT Output
70h = BC 71h = 03

25. Program to count number of 1’s and 0’s in a byte


MOV A, 70h
MOV R5, #00h
MOV R6, #00h
MOV R3, #08h
BACK: RLC A
JC SKIP
INC R5
SJMP NEXT
SKIP: INC R6
NEXT: DJNZ R3, BACK
MOV 71h, R5
MOV 72h, R6
END

INPUT Output
70h = A8 71h = 05
72h = 03

26. Program to find the number of positive bytes in an array


MOV R0, #50h
MOV R7, #05h
MOV R5, #00h
BACK: MOV A, @R0
RLC A
JC NEXT
INC R5
NEXT: INC R0
DJNZ R7, BACK
MOV 71h, R5
END
INPUT Output
50h = 37 71h =
51h = 24
52h = 12
53h = 19
54h = 04

27. Program to find the number of negative bytes in an array


MOV R0, #50h
MOV R7, #05h
MOV R5, #00h
BACK: MOV A, @R0
RLC A
JNC NEXT
INC R5
NEXT: INC R0
DJNZ R7, BACK
MOV 71h, R5
END

INPUT Output
50h = 37 71h =
51h = 24
52h = 12
53h = 19
54h = 04

28. Program to find the number of positive and negative bytes in an array
MOV R0, #50H
MOV R7, #05H
MOV R5, #00H
MOV R6, #00H
BACK: MOV A, @R0
RLC A
JNC NEXT
INC R5
SJMP SKIP
NEXT: INC R6
SKIP: INC R0
DJNZ R7, BACK
MOV 71H, R5
MOV 72H, R6
END

INPUT Output
50h = 37 71h =
51h = 24 72h =
52h = 12
53h = 19
54h = 04

29. Program to find number of even bytes in an array


MOV R0, #50H
MOV R7, #05H
MOV R5, #00H
BACK: MOV A, @R0
RRC A
JC NEXT
INC R5
NEXT: INC R0
DJNZ R7, BACK
MOV 71H, R5
END

INPUT Output
50h = 37 71h = 03
51h = 24
52h = 12
53h = 19
54h = 04

30. Program to find number of odd bytes in an array


MOV R0, #50H
MOV R7, #05H
MOV R5, #00H
BACK: MOV A, @R0
RRC A
JNC NEXT
INC R5
NEXT: INC R0
DJNZ R7, BACK
MOV 71H, R5
END
INPUT Output
50h = 37 71h = 02
51h = 24
52h = 12
53h = 19
54h = 04

31. Program to find number of even and odd bytes in an array


MOV R0, #50H
MOV R7, #05H
MOV R5, #00H
MOV R6, #00H
BACK: MOV A, @R0
RRC A
JNC NEXT
INC R5
SJMP SKIP
NEXT: INC R6
SKIP: INC R0
DJNZ R7, BACK
MOV 71H, R5
MOV 72H, R6
END

INPUT Output
50h = 37 71h = 02
51h = 24 72h = 03
52h = 12
53h = 19
54h = 04

32. Program for Decade counter


ORG 0000H ;Program starts at address 0000H
MOV P1,#00H ; Clear Port 1 (Output port)
MOV R0,#00H ; R0 is our counter register, start at 0
MAIN: MOV A, R0; Move counter value to Accumulator
MOV P1, A ; Output counter value to Port 1
ACALL DELAY ; Call delay routine
INC R0 ;Increment counter
CJNE R0, #0AH, MAIN;If R0 != 10 (0Ah), jump to MAIN
MOV R0, #00H ; Reset counter back to 0
SJMP MAIN ; Repeat loop
;------------------------------------
; Delay Subroutine (simple loop)
;------------------------------------
DELAY:MOV R2, #255
D1: MOV R3, #255
D2: DJNZ R3, D2
DJNZ R2, D1
RET
END
Output:
00h to 09h

33. Program for Binary counter


ORG 0000H ; Program starts at address 0000H

MOV P1, #00H ; Clear Port 1 (set all bits to 0)


MOV A, #00H; Clear accumulator (start counter at 0)

MAIN:
MOV P1, A ; Send current value of accumulator to Port 1
ACALL DELAY; Call delay for visibility
INC A ; Increment the counter (A = A + 1)
SJMP MAIN ; Repeat forever

;------------------------------------
; Simple Delay Subroutine
;------------------------------------
DELAY:
MOV R2, #255h
D1: MOV R3, #255h
D2: DJNZ R3, D2
DJNZ R2, D1
RET

END

Output:
00h to FFh

34. Program for BCD Counter


; BCD Counter Program for 8051
; Assume Port 1 (P1) is connected to display/output
ORG 0000H ; Start address
MOV A, #00H ; Initialize Accumulator A to 00 (BCD start)
MOV R0, #00H ; Initialize R0 as tens digit (optional)
LOOP: MOV P1, A ; Output BCD value to Port 1
ACALL DELAY ; Small delay for visibility
ADD A, #01H ; Increment units place
; Check if units digit exceeds 9 (i.e., becomes 0x0A)
ANL A, #0FH ; Mask upper nibble (optional, precautionary)
CJNE A, #0AH, NO_CORRECTION
; Correction for BCD (units digit rolled over)
MOV A, P1 ; Reload current value
ADD A, #06H ; Add 6 to adjust BCD (0x0A -> 0x10)
NO_CORRECTION: MOV P1, A ; Output corrected value to port
; Check if counter reached 99
CJNE A, #100, LOOP
SJMP DONE ; Jump to DONE once 99 is reached
DELAY: ; Simple Delay Routine
MOV R2, #255
DELAY1: MOV R3, #255
DELAY2: DJNZ R3, DELAY2
DJNZ R2, DELAY1
RET
DONE: SJMP DONE ; Infinite loop at the end
END

Output:
00h to 09h
35. Program for BCD counter to a particular value
; BCD Counter up to a Specific Value (for 8051)
; Output BCD on Port 1 (P1)

ORG 0000H

MOV A, #00H ; Start from 00

START:
MOV P1, A ; Output current BCD value to Port 1
ACALL DELAY ; Short delay

CJNE A, #50h, CONTINUE ; Compare A with 50h


SJMP DONE ; If equal, done

CONTINUE:
; Increment BCD
ADD A, #01H ; Add 1 to the BCD value
; ANL A, #0FH ; Mask upper nibble (optional)

; Check if units digit overflows beyond 9


CJNE A, #0AH, SKIP_CORRECT
; Correct BCD if units place exceeded 9
MOV A, P1
ADD A, #06H
SKIP_CORRECT: MOV P1, A ; Update corrected value
SJMP START ; Repeat
; Delay Subroutine
DELAY:MOV R2, #200
D1: MOV R3, #255
D2: DJNZ R3, D2
DJNZ R2, D1
RET
DONE: NOP ;
END

Output:
P1 = 0x50 = 01010000
36. Program for Binary counter to a particular value
; Binary Counter up to a Specific Value (8051 Assembly)
; Output binary value to Port 1 (P1)

ORG 0000H

MOV A, #00H ; Start from 00h


START:
MOV P1, A ; Output A to Port 1
ACALL DELAY ; Short delay

CJNE A, #0FH, CONTINUE ; Compare A with target value


SJMP DONE ; If equal, done

CONTINUE:
INC A ; Simple binary increment
SJMP START ; Repeat

; Delay Subroutine
DELAY:
MOV R2, #200
D1: MOV R3, #255
D2: DJNZ R3, D2
DJNZ R2, D1
RET

DONE:
NOP ;

END

Output:
P1 = 0x0F = 00001111
37. Program to convert BCD to HEX
ORG 0000H

; Assume BCD input is in register A


MOV A, #45H ; Example: BCD 45H

; Separate upper and lower nibbles


MOV R0, A ; Save original BCD

ANL A, #0F0H ; Mask lower nibble


SWAP A ; Bring upper nibble to lower position
MOV R1, A ; Save upper digit

MOV A, R0 ; Get original BCD back


ANL A, #0FH ; Get lower digit
MOV R2, A ; Save lower digit

; Multiply upper digit by 10


MOV A, R1
MOV B, #0AH
MUL AB ; A = upper_digit × 10

; Add lower digit


ADD A, R2

; Now A has HEX equivalent


MOV P1, A ; Output HEX result to Port 1 (optional)

DONE:NOP
END

Output:
P1 = 0x2D = 00101101

38. Program to convert HEX to BCD


; Program: HEX to BCD Conversion (8051 Assembly)
; Converts HEX (in A register) to BCD (on P1)
ORG 0000H

; Load HEX input (for example: 3AH = 58 decimal)


MOV A, #3AH ; HEX input value (3AH = 58 decimal)

; Convert HEX to BCD


; Upper digit: A = 3 (first decimal digit)
; Lower digit: A = A (second decimal digit)

MOV B, #0AH ; B = 10 (used for division)


MOV R0, A ; Save original value in R0
DIV AB ; A = quotient (upper digit), B = remainder (lower digit)
MOV R1, A ; Store quotient (upper decimal digit)
MOV A, B ; Get remainder (lower decimal digit)
MOV R2, A ; Store remainder (lower decimal digit)

; Now:
; R1 = Upper decimal digit (most significant)
; R2 = Lower decimal digit (least significant)

; Output BCD to Port 1


; Combine R1 (upper) and R2 (lower) for BCD result
MOV P1, R1; Output upper decimal digit to Port 1 (for the most significant digit)
MOV P2, R2; Output lower decimal digit to Port 2 (for the least significant digit)

DONE:
NOP
END

Output:
P1 = 0x05 = 00000101

You might also like