Name: Vani Verma
Registration Number: 23BAI0016
Lab Assesment-2
Question 1:
Get the Data from Port P1 and Send it to Port P2, Note: P1asinputPortand P2 as Output Port. Take
input as last 2 digits of your Reg. no
Code:
;VANI VERMA 23BAI0016
MOV A, #0FFH ; A=FFH
MOV P1, A ; make P1 an input port
HERE: MOV A, P1 ; get data from P1
MOV P2, A ; send it to P2
SJMP HERE ; keep doing this
END
Output:
Question 2:
Write and assemble a program to toggle all the bits of P0, P1, and P2 continuously by
sending 55H and AAH to these ports.
Put a time delay between the "on" and "off" states. Then using the simulator, single-step
through the program and examine the ports. Do not single-step through the time delay
call.
Code:
ORG 0000H ; Set the origin (starting address) at 0000H
HERE:
MOV P0, #55H ; Load 0x55 (01010101 in binary) into Port 0
MOV P1, #55H ; Load 0x55 into Port 1
MOV P2, #55H ; Load 0x55 into Port 2
ACALL DELAY ; Call the delay subroutine
MOV P0, #0AAH ; Load 0xAA (10101010 in binary) into Port 0
MOV P1, #0AAH ; Load 0xAA into Port 1
MOV P2, #0AAH ; Load 0xAA into Port 2
ACALL DELAY ; Call the delay subroutine
SJMP HERE ; Jump back to HERE, creating an infinite loop
; Delay Subroutine
DELAY:
MOV R1, #04H ; Load outer loop counter with 4
BACK:
MOV R2, #20H ; Load inner loop counter with 32 (hex 20H)
AGAIN:
DJNZ R2, AGAIN ; Decrement R2, repeat until R2 = 0
DJNZ R1, BACK ; Decrement R1, repeat outer loop until R1 = 0
RET ; Return from the subroutine
END ; End of the program
Output:
Question 3:
A switch is connected to pin P1.7. Write a program to check the status of the switch and perform
the following: (a)If switch = 0, send letter ‘N’ to P2. (b)If switch = 1, send letter ‘Y’ to P2. • Use the
carry flag to check the switch status.
Code:
ORG 0000H ; Start of the program
SETB P1.7 ; Make P1.7 an input (not strictly required for P1)
AGAIN: MOV C, P1.2 ; Read the switch at P1.2 into the Carry flag
JC OVER ; If switch is HIGH (1), jump to OVER
MOV P2, #'N' ; Switch is LOW (0), send 'N' to Port 2
SJMP AGAIN ; Keep monitoring
OVER: MOV P2, #'Y' ; Switch is HIGH (1), send 'Y' to Port 2
SJMP AGAIN ; Keep monitoring
END ; End of program
Output:
Question 4:
Write an 8051 program to get the length (8-bit number) and breadth(8-bitnumber) of a rectangle
from port P0 and P1 respectively.
Calculate area and perimeter of the rectangle and send them through port P2 and P3respectively.
Note: assume values of the inputs are such that the outputs do not exceed 8-bits
• Take LENGTH as first 2 digits of Reg no
• Take BREADTH as second 2 digits of Reg no
• Do STEP Simulation
Code:
;VANI VERMA 23BAI0016
MOV A, P0 ; Load length (L) from Port P0 into accumulator
MOV B, P1 ; Load breadth (B) from Port P1 into register B
MOV R0, A ; Move length to R0 as temporary memory
MOV R1, B ; Move breadth to R1 as temporary memory
; Calculate Area (L * B)
MUL AB ; Multiply A and B, result in A (low) and B (high)
MOV P2, A ; Send the low byte of the area to Port P2 (result <= 8 bits)
; Calculate Perimeter (2 * (L + B))
MOV A, R0 ; Reload length into accumulator
MOV B, R1 ; Reload breadth into accumulator
ADD A, B ; Add breadth to length (L + B)
MOV R3, A ; Move value of A to R3 as temporary memory
ADD A, R3 ; Multiply the sum by 2 by adding 2 times(2 * (L + B))
MOV P3, A ; Send the perimeter to Port P3
END ; End of program
Output:
Question 5:
Create a square wave of 66% duty cycle on bit 3 of port 1. The 66% duty cycle means the “on” state
is twice the “off” state.
Code:
;VANI VERMA 23BAI0016
ORG 0000H ; Set origin at address 0000H
BACK: SETB P1.3 ; Set P1.3 HIGH (Turn ON LED)
LCALL DELAY ; Call delay subroutine
LCALL DELAY ; Extra delay for longer ON time
CLR P1.3 ; Clear P1.3 LOW (Turn OFF LED)
LCALL DELAY ; Call delay again
SJMP BACK ; Repeat the process indefinitely
; ---------- DELAY Subroutine ----------
DELAY: MOV R7, #25 ; Load R7 with 25 (Outer loop)
D1: MOV R6, #25 ; Load R6 with 25 (Inner loop)
D2: DJNZ R6, D2 ; Decrement R6 until it reaches 0
DJNZ R7, D1 ; Decrement R7 and repeat inner loop
RET ; Return from subroutine
END ; End of program
Output:
Question 6:
Write an 8051 assembly language program to get 8-bit number(x)from port P0. If x is odd number
compute y = x ^ 2 + 5 and if x is even number compute y = x / 2 + 2x Transfer the value y to port P1.
Note: The value of x is such that y does not exceed 8-bits.
Code:
;VANI VERMA 23BAI0016
ORG 0000H ; Program start
MOV A, P0 ; Read x from Port 0 into Accumulator
MOV R0, A ; Store x in R0 for later use
ANL A, #01H ; Check LSB to determine even/odd
JNZ ODD_CASE ; If LSB is 1, x is odd ? jump
; ---------- EVEN CASE: y = x/2 + 2x ----------
MOV A, R0 ;A=x
RR A ;A=x/2
MOV R1, A ; R1 = x / 2
MOV A, R0 ;A=x
ADD A, R0 ; A = x + x = 2x
ADD A, R1 ; A = 2x + x/2
MOV P1, A ; y ? Port 1
SJMP DONE
; ---------- ODD CASE: y = x^2 + 5 ----------
ODD_CASE:
MOV A, R0 ;A=x
MOV B, R0 ;B=x
MUL AB ;A=x*x
ADD A, #05H ; A = x^2 + 5
MOV P1, A ; y ? Port 1
DONE:
SJMP DONE ; Infinite loop
END
Output: