You are on page 1of 6

Group 33, Tuesday Batch ( Avigyan Sinha -09EC1046, Gaurav Singh 09EC1047 ) Experiment 1 Familiarization with 8051 Kit

t Objective : To use assembly language to perform the following actions and study about the kit components and its interface with the PC 1. Find the second largest of an array of 100 numbers stored in external memory from location 9000H onwards. The number should be stored at location 9500H. 2. Compute the LCM of two numbers. Assume the numbers to be stored at location 9000H and 9001H. Store the result at 9002H. 3. Check whether a NULL terminated string stored from location 9000H is a palindrome or not. If the string is a palindrome, store 1 at location 9500H, else clear the location. Apparatus + Software : 1) ESA51E 8051 Trainer Board 2) Compiler Keil uvision 4 3) Driver - Win 51E Hardware circuitry: Using 8051 kit: The program is written in a standard editor and then the program is built to form a .hex file. The .hex file can then be transferred onto a 8051 microcontroller. The microcontroller is connected to the computer through a cable and when the connection is established, the program is loaded on the microcontroller. Data input: To work with user given data, and to verify the functionality of the code, we can use the software package to view and edit the registers of the microcontroller. Next, by running the program over the inserted values we can validate the code. Software Code: Part 1.1 - The starting memory address of the program is 8100H. R0 is the total count of numbers that are considered. The second largest no. is stored in R2 and the largest one in R1. The number at the current value of data pointer is read and compared with the second largest no. upto that point. If it is less than Second highest no. then the steps are repeated with the next no. , otherwise it is compared with the highest number. If it is less than the highest no., then the second highest no., R0, is replaced by it. Else if it is greater than the highest no. then the latter is replaced with it and the earlier highest no. replaces the earlier second highest no. The process is repeated for the entire array. ORG MOV 8100H A,#00H

MOV MOV MOV MOV MOV

R0,#64H R2,#00H R1,#00H R5,#00H DPTR,#9000H A,@DPTR

COMPARE: MOVX SUBB JC

A,R2 DEC

MOVX A,@DPTR MOV SUBB JC MOV MOV MOV MOV MOV MOV DEC : INC DJNZ LAST: MOV MOV R2,A A,R1 DEC A,R2 R5,A A,R1 R2,A A,R5 R1,A DPTR R0, COMPARE DPTR,#9500H A,R2

MOVX @DPTR,A HERE: SJMP END HERE

Part 1.2 The larger of the 2 nos. is stored in R1and the smaller in R2. The larger no. is also stored in R3 and A and the smaller one in B. A is divided with B. If the remainder is zero, then LCM is R3. Otherwise A and R3 is stored with R3+R1, and B is stored with the smaller no. The process of division is repeated until the remainder is zero.

ORG 8100H MOV DPTR,#9000H MOVX A,@DPTR MOV R1,A MOV DPTR,#9001H MOVX A,@DPTR MOV R2,A MOV A,R1 MOV B,R2 MOV R3,A DIVIDE: DIV AB MOV A,B JNZ CONTINUE SJMP COMPLETE

CONTINUE:MOV A,R3 ADD A,R1 MOV R3,A MOV B,R2 SJMP DIVIDE COMPLETE: MOV DPTR,#9002H MOV A,R3

MOVX @DPTR,A HERE: SJMP HERE END Part 1.3 The string is written manually in target memory locations, ending the string with Null element. DPTR is set to 9000H. DPH is stored into R2 and R4, DPL into R3 and R5. R1, R7, A are cleared. The value stored at each incremented value of DPTR is checked. If it is non zero, the values of R1, R5 are incremented until a NULL is encountered. At the end of the loop, R1 has the string length. DPH and DPL of the start of the string are stored in R2, R3 respectively and DPH, DPL of the string end is stored in R4 and R5. The DPTR value is moved with R2 and R3, the value stored in this address is moved to A and then stored in R7. The DPTR value is now moved with R4 and R5, and the value stored in this address into A. R7 is subtracted from A to compare the elements of the string at equal distances from the 2 ends. If the result is 0, R3 is incremented, R5 and R1( string length ) decremented. The above steps are repeated until R1 is 0. R1= 0 indicates that all corresponding elements from front and rear of string have matched and it is a palindrome. 1 is moved to 9500. If R1 is not zero finally then the loop is ended and 0 is moved to 9500 to indicate that it is not palindrome.

ORG 8100H MOV DPTR,#9000H MOV A,#00H MOV R1,A MOV R2,DPH MOV R3,DPL MOV R4,DPH MOV R5,DPL MOV R7,A READ: MOVX A,@DPTR CJNE A,#00H,INCRE SJMP ENDPTV INCRE: INC DPTR INC R1

INC R5 SJMP READ ENDPTV: DEC R5 MOV DPTR,#9000H CHECK: MOV DPH,R2 MOV DPL,R3 MOVX A,@DPTR MOV R7,A MOV DPH,R4 MOV DPL,R5 MOVX A,@DPTR SUBB A,R7 JNZ NOTPAL INC R3 DEC R5 DJNZ R1,CHECK PAL: MOV DPTR,#9500H MOV A,#01H MOVX @DPTR,A JMP HERE NOTPAL: MOV DPTR,#9500H MOV A,#00H MOVX @DPTR,A HERE: SJMP HERE END

Dicussion: 1) The experiment introduces us to the basics of programming 8051.

AVIGYAN SINHA - 09EC1046

2) The starting address of our code in program memory should always be 8100H. We should also define the end of the program using an infinite loop at the end. The keyword END should not be used as it does not work. If the end of the program is not defined properly then the compiler will enter into the code space of a different program which will lead to erroneous result. 3) Since we are writing codes in assembly it is important to remember all the programming logical constructs and its limitations. For example MOV operation cannot be used to move data between two registers of register bank such as MOV R1, R2 is invalid. Such intricacies of the assembly language commands are to be kept in mind. 4) To do a simple task of comparing two numbers and perform some logical operations based on the comparison (IF- ELSE operation), in assembly language, we need to properly branch out and also come back to appropriate lines of code. Proper use of labelling must be known. 5) The syntax of assembly often has many limitations and subtleties, which the programmer should be aware of. As an example, the CJNE command cannot be used to compare registers, and even when comparing with accumulator, A should be operand 1. Such intricacies should be kept in mind.

DISCUSSION In the experiment we learned about the instruction set of

GAURAV SINGH 09EC1047: the 8051 mirocontroller.

The microcontroller board does not really stop at the END command but continues to carry on the operation again and again overwriting the output. Thus to see the output one needs to put an infinite loop in the end and put a manual break in the program run and see the values of the registers. It is important to keep the track of the flow of programs, jumps can lead us to some other part of program flow but after the end of the codes under it, the sequential route is followed. Proper knowledge of the syntax is very important for the programming. For example, one cannot compare a register with a register and many such cases must be known to be a swift 8051 programmer The WIN51E environment should be carefully setup for simulation. Details include setting the PC, and initializing the start address of the memory. It is important at branches to make sure that the branch terminates from all routes (by looping back or looping to another construct)

You might also like