0% found this document useful (0 votes)
38 views4 pages

8051 Assembly Language Program Examples

Uploaded by

Aritra Ghosh
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views4 pages

8051 Assembly Language Program Examples

Uploaded by

Aritra Ghosh
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

//1ADDITION

ORG 0000H MOV A,#05H MOV B,#05H ADD A,B H: SJMP H END

//2 SUBTRACTION
ORG 0000H MOV A,#05H MOV B,#04H SUB A,B H: SJMP H END

//DIVISION
ORG 0000H MOV A,#05H MOV B,#05H MUL AB H: SJMP H END

//4 Write and assemble a program to add the following data and then use the
simulator to examine the CY flag. 92H, 23H, 66H, 87H, FSH
ORG 0000H MOV A, #92H MOV R0, #23H ADD A, R0 JNC L1 INC R3 L1: ADD A,
#66H JNC L2 INC R3 L2: ADD A, #87H JNC L3 INC R3 L3: ADD A, #0F5H JNC L4
INC R3 L4: SJMP L4 END

//5 Write and assemble a program to load values into each of registers R0 - R4
and then push each of these registers onto the stack. Single step the program
and examine the stack and the SP register after the execution of each
instruction.
ORG 000H MOV R0,#30H MOV R1,#40H MOV R2,#50H MOV R3,#60H MOV
R4,#70H PUSH 0 PUSH 1 PUSH 2 PUSH 3 PUSH 4 HERE: SJMP HERE END

//6 Write an 8051 assemble language program: (a) Set SP = 0D, (b) Put a
different value in each of RAM locations 0D, 0C, 0B, 0A, 09 and 08 (c) POP each
stack location into registers R0 - R4.
ORG 000H MOV SP,#0DH MOV 0DH,#10H MOV 0CH,#20H MOV 0BH,#30H MOV
0AH,#40H MOV 09H,#50H MOV 08H,#60H POP 0 POP 1 POP 2 POP 3 POP 4 POP
5 HERE: SJMP HERE END

//7 Write and assemble a program to load valuesinto each of registers R0 - R4


and then push each of these registers onto the stack and pop them back. Single
step the program, and examine the stack and the SP register after the execution
of each instruction.
ORG 0000H MOV R0, #10H MOV R1, #20H MOV R2, #30H MOV R3, #40H MOV
R4, #50H PUSH 0 PUSH 1 PUSH 2 PUSH 3 PUSH 4 POP 4 POP 3 POP 2 POP 1 POP
0 END
//8 Write a program to transfer a string of data from code space starting at
address 200H to RAM locations starting at 40H. The data is as shown below:
0200H: DB "VIT UNIVERSITY" Using the simulator, single step through the
program and examine the data transfer and registers.
ORG 0000H MOV DPTR, #0200H MOV R1, #0EH MOV R0, #40H LOOP: CLR A
MOVC A, @A+DPTR MOV @R0, A INC DPTR INC R0 DJNZ R1, LOOP HERE: SJMP
HERE ORG 0200H DB "VIT UNIVERSITY" END

//9 Write a program to add 10 bytes of data and store the result in registers R2
and R3. The bytes are stored in the ROM space starting at 200H. The data would
look as follows: MYDATA: DB 92, 34, 84, 129, ... Pick your own data. Notice that
you must first bring the data from ROM space into the CPU's RAM, and then add
them together. Use a simulator to single step the program and examine the data.
ORG 000H MOV DPTR, #200H MOV R0, #10H MOV R2, #00H MOV R3, #00H
LOOP: CLR A MOVC A, @A+DPTR ADD A, R2 JNC NEXT INC R3 NEXT: INC DPTR
MOV R2, A DJNZ R0, LOOP HERE: SJMP HERE ORG 200H DB 22H, 43H, 23H, 34H,
31H, 77H, 91H, 33H, 43H, 07H END

//10 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 HERE: MOV P0, #55H MOV P1, #55H MOV P2, #55H ACALL
DELAY MOV P0, #0AAH. MOV P1, #0AAH MOV P2, #0AAH ACALL DELAY SJMP
HERE DELAY: MOV R1, #04H BACK: MOV R2, #20H AGAIN: DJNZ R2, AGAIN DJNZ
R1, BACK RET END

//11 Get the Data from Port P1 and Send it to Port P2, Note: P1 as input Port and
P2 as Output Port
MOV A, #0FFh MOV P1, A HERE: MOV A, P1 MOV P2, A SJMP HERE END

//12 Write a program using timer 0 to generate a 500 Hz square wave frequency
on one of the pins of P1.0 Then examine the frequency using the KEIL IDE inbuilt
Logic Analyzer.
Frequency f = 500Hz
Total time period T = 2ms
Required delay = TON = TOFF = 1ms
[(FFFF - XXXX) + 1] * 1.085µs = 1ms
(65536 - X) * 1.085µs = 1ms
X = (64614)D
X = XXXX = FC66H
ORG 0000H MOV TMOD, #01H HERE: MOV TL0, #66H MOV TH0, #0FCH CPL P1.0
ACALL DELAY SJMP HERE DELAY: SETB TR0 AGAIN: JNB TF0, AGAIN CLR TR0 CLR
TF0 RET END

//13 Write a program using timer 1 to generate a 1 kHz square wave frequency
on one of the pins of P1. Then examine the frequency using the KEIL IDE inbuilt
Logic Analyzer.
Frequency f = 1 KHz
Total time period T = 1ms
Required delay = TON = TOFF = 0.5ms
[(FFFF - XXXX) + 1] * 1.085µs = 0.5ms
XXXX = FE33H
ORG 0000H MOV TMOD, #10H HERE: MOV TL1, #33H MOV TH1, #0FEH CPL P1.0
ACALL DELAY SJMP HERE DELAY: SETB TR1 AGAIN: JNB TF1, AGAIN CLR TR1 CLR
TF1 RET END

//14 Write a program using timer 1 to generate a 2 KHz square wave frequency
on one of the pins of P1.0.
ORG 0000H MOV TMOD, #20H HERE: MOV TH1, #19H BACK: SETB TR1 AGAIN:
JNB TF1, AGAIN CPL P1.0 CLR TF1 SJMP BACK END

//15 Assuming that clock pulses are fed into pin T1, write a program for counter 1
in mode 2 to count the pulses and display the state of the TL1 count on P2, which
connects to 8 LEDs.
MOV TMOD, #01100000B MOV TH1, #0 SETB P3.5 AGAIN: SETB TR1 BACK: MOV
A, TL1 MOV P2, A JNB TF1, BACK CLR TR1 CLR TF1 SJMP AGAIN END

//16 Write an 8051-assembly program to transfer data serially at baud rate 9600
with 8- bit data, one stop bit, and observe the transmi ed data in the serial
window of the simulator
ORG 0000H START: MOV DPTR, #MYDATA MOV TMOD, #20H MOV TH1, #-3 MOV
SCON, #50H SETB TR1 MOV R1, #14 AGAIN: CLR A MOVC A, @A+DPTR MOV
SBUF, A HERE: JNB TI, HERE CLR TI INC DPTR DJNZ R1, AGAIN SJMP START
MYDATA: DB 'VIT UNIVERSITY' END
//17 Generate a square wave signal from P1.2 which has 0.1ms off me and 0.1ms
on me use Timer 1 – mode 2 (Auto Reload mode)
ORG 0000H MOV TMOD, #20H HERE: MOV TH1, #0A4H CPL P1.2 LCALL DELAY
SJMP HERE DELAY: SETB TR1 AGAIN: JNB TF1, AGAIN CLR TF1 RET END

//18 Write an 8051 program to get data from port P0 and send it to port P1
continuously while an interrupt will do the following: Timer 0 will toggle the P2.1
bit every 100 microseconds.
ORG 0000H LJMP MAIN ORG 000BH CPL P2.1 RETI ORG 0030H MAIN: MOV
TMOD,#02H MOV P0,#0FFH MOV TH0,#0A4H MOV IE,#10000010B SETB TR0
BACK: MOV A,P0 MOV P1,A SJMP BACK END

//19 Write an 8051 program to get data from a single bit of P1.2 and send it to
P1.7 continuously while an interrupt will do the following: A serial interrupt
service routine will receive data from a PC and display it on P2 ports
ORG 0000H LJMP MAIN ORG 0023H LJMP DISP ORG 0050H MAIN: SETB P1.2 MOV
TMOD, #20H MOV TH1, #-3 MOV SCON, #50H MOV IE, #10010000B SETB TR1
BACK:MOV C, P1.2 MOV P1.7, C SJMP BACK DISP: MOV A, SBUF MOV P2, A CLR RI
RETI END

//20
Write an assembly-level program to display your roll number on LCD which is
interfaced with an 8051 microcontroller.
ORG 0000H MOV A, #38H ACALL COMMAND ACALL DELAY MOV A, #0EH ACALL
COMMAND ACALL DELAY MOV A, #01H ACALL COMMAND ACALL DELAY MOV A,
#82H ACALL COMMAND ACALL DELAY MOV DPTR, #string back: MOV A, #00H
MOVC A, @A+DPTR JZ AGAIN ACALL DATATRANSFER ACALL DELAY INC DPTR SJMP
back ACALLDELAY AGAIN: SJMP AGAIN COMMAND: MOV P2, A CLR P0.5 CLR P0.6
SETB P0.7 CLR P0.7 RET DELAY: MOV R3, #255 HERE: DJNZ R3, HERE RET
DATATRANSFER: MOV P2, A SETB P0.5 CLR P0.6 SETB P0.7 CLR P0.7 END

Common questions

Powered by AI

The program configures Counter 1 in Mode 2 (8-bit auto-reload) by setting TMOD to 36H. The TH1 register is initialized to zero for counting from zero. The process involves setting bit P3.5 to enable counter input from an external source connected to T1. TR1 is set to start the counter. As the counter counts clock pulses, the current count value from TL1 is moved to Port P2, updating LED states in real-time with the counter value. When TF1 is set upon overflow, the counter is halted, cleared, and immediately restarted, allowing continuous counting and updating of the LED display .

The mechanism involves using the DPTR register to point to the starting address (200H) in code space, and the R0 register to point to the starting address (40H) in RAM. The program uses the MOVC instruction to move a byte of data from code memory addressed by DPTR to the accumulator, and then moves the accumulator's content to the RAM location addressed by R0. The DPTR and R0 are incremented after each byte is transferred to move to the next byte and address respectively, and a loop continues this process for the length of the string, controlled by R1 which represents the length of the string .

The POP instruction functions by transferring data from the top of the stack to the specified register. It utilizes the stack pointer (SP) to identify the current top of the stack position in memory. Upon execution, the data at the memory address pointed to by SP is moved into the specified register, and then the SP is incremented to point to the next stack location. This operation effectively retrieves and removes the topmost value from the stack, adhering to the Last-In-First-Out (LIFO) principle .

The program toggles all the bits of ports P0, P1, and P2 by writing the constant values 55H and AAH to these ports alternately. This creates a square wave or toggling effect on all bits. The time delay implemented by the DELAY subroutine inserts a pause between the toggling operations to control the frequency of the waveforms generated on the ports. The delay loop uses two nested loops to create the desired time between toggling states .

The SJMP (Short Jump) instruction enables loop execution by directing program flow to a specified address relative to the current PC. This operation forms an effective mechanism for creating infinite or controlled loops, allowing repeated execution of block instructions. For example, in a program that repeatedly toggles port bits or reads an input continuously, SJMP can be used to loop back to the beginning of the instruction sequence thereby ensuring sustained operation until an external event interrupts the process. An example from the document is the use of SJMP HERE to lock the program in a continuous loop that transfers data from one port to another without termination .

The SJMP HERE instruction creates an infinite loop, keeping the microcontroller locked in a continuous cycle of reading data from Port P1 and sending it to Port P2. This ensures that as long as the program is running, the data flow from the input port P1 to the output port P2 is uninterrupted .

To generate a 1 kHz square wave on P1.0, Timer 1 is configured in Mode 2 (auto-reload mode). The TMOD register is set to 10H for Timer 1 in Mode 2. Timer 1's registers TL1 and TH1 are loaded with the values 33H and FEH respectively, which correspond to the required delay for generating a 0.5 ms half-period of the waveform. The CPL P1.0 instruction toggles the pin state, providing the square wave output. The timer is started using the TR1 bit and repeatedly checked for overflows using TF1 until a half-period delay elapses, at which time the TF1 flag is cleared and the process repeats .

The JNC (Jump if No Carry) instruction is used to check the carry flag after performing an addition of two 8-bit numbers. If the addition results in a carry (overflow beyond 8 bits), the carry flag will be set, and the JNC will not take the jump, allowing an increment operation on the designated overflow register (such as R3 for carry count). If no carry occurs, the addition process continues without incrementing the overflow register .

To use Timer 0 for toggling a bit of port P2 every 100 microseconds, Timer 0 is set in Mode 2 (8-bit auto-reload) by initializing TMOD with 02H. The TH0 register is loaded with a calculated preset value (A4H) to create a 100-microsecond delay according to the clock period. The Timer 0 interrupt is enabled to automate the toggling process using the interrupt vector, which executes an ISR (Interrupt Service Routine) to toggle P2.1 every time the timer overflows. The CPL P2.1 instruction is used within the ISR to perform the toggle operation .

The program sets up the serial communication by initializing the DPTR with the starting address of the string data, configures Timer 1 in Mode 2 for baud rate generation, and sets TH1 with the required value to achieve a baud rate of 9600 (calculated to be -3). The SCON register is configured for 8-bit data transfer with one stop bit. The data is sequentially moved from code memory into the A register using the MOVC instruction, then sent to the SBUF register for serial transmission. The transmission completion is monitored by checking the TI flag, which indicates the end of transmission, upon which the flag is cleared and the next byte is processed .

You might also like