You are on page 1of 8

 

Experiment #1
Familiarization with the basic 8051 Kit 
 
 
Submitted By: 
Wednesday Group # 13 
07EC1041 Aamod Shanker  
07EC1043 Jayant 
 

Objective:
To use assembly language to perform the following actions 
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. 
 
Methodology:
Apparatus and Software: 
• ESA51E 8051 Trainer Board 
• Compiler – Keil uvision 4 
• Driver : Win51E 
 
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 Design and Algorithm:

Part 1: Second largest number from the 100 element array. 
1. For the first number, read it and store it in R1. 
2. Now, input the next number in R3. 
3. Copy content of R1 at some random location, say 60h. 
4. If R3 = value at 60h, R2 = R3. 
5. If R3 > value at 60h, R1 = R3; R2 = value at 60h. 
6. If R3 < value at 60h, and R3 > R2; R2 = R3. 
7. Loop 
8. END 

CODE: 

// Code for experiment 1A (SECOND LARGEST NUMBER IN ARRAY)

ORG 8100H // The starting address of the program code in memory

MOV DPTR,#9000H // Initialize DPTR to store the result of addition


MOV R0,#99 // loop index

MOVX A,@DPTR
MOV R1,A // R1 stores the first/largest number

INC DPTR
MOVX A,@DPTR //read second number in buffer
MOV 60h,R1
CJNE A,60h,NEXT //compare largest with buffer
MOV R2,A //if buffer and largest equal, put buffer in second largest
SJMP LOOP //enter loop

NEXT: JNC SHIFT //if largest smaller than buffer, replace largest with buffer
MOV R2,A //if not then put buffer in second largest
SJMP LOOP //enter loop

SHIFT: MOV R3,A //put buffer in temp


MOV A,R1 //put buffer in temp

MOV R2,A //put largest in second largest


MOV A,R3

MOV R1,A //put buffer in largest

LOOP: DJNZ R0,MAIN


SJMP FINISH

MAIN: INC DPTR //Read next value


MOVX A,@DPTR

MOV 60h,R1
CJNE A,60h,NEXT2 //compare largest with buffer
MOV R2,A //update R2
SJMP LOOP

NEXT2: JNC SHIFT


MOV 60h,R2
CJNE A,60h,NEXT3 //compare with second largest
SJMP LOOP //do nothing

NEXT3: JNC MOVR2A


SJMP LOOP

MOVR2A: MOV R2,A


SJMP LOOP

FINISH: MOV DPTR,#9500H


MOV A,R2
MOVX @DPTR,A
HERE: SJMP HERE
END
Part 2: LCM of two given numbers. 
1. Input the numbers in R1 and R2. 
2. If R1 < R2, interchange the values. 
3. Store A = R1, B = R2. 
4. Perform the following until B = 0 is satisfied. 
a. Store B at some location say 60h 
b. Divide A by B. 
c. Store B’s value in A, and Store the remainder in B. 
d. Loop 
5. GCD = value at 60h. 
6. Obtain LCM by using the formula,     . 
7. END 

CODE: 

//CODE FOR EXPERIMENT 1B , LCM OF TWO NUMBERS


ORG 8100H

MOV DPTR,#9000h //read first value


MOVX A,@DPTR
MOV R1,A //first value in R1
INC DPTR
MOVX A,@DPTR
MOV R2,A // second value in R2

MOV 60H,R1
CJNE A,60H, NEXT1 //compare the two values
SJMP LEAVE // if equal, lcm is the number

NEXT1: JNC SWITCH //if R2>R1 swap


MOV A,R1
MOV B,R2 /load the divide registers for GCD
SJMP LOOP

SWITCH: MOV A,R1 //if R2>R1


MOV R3,A //switch using temp R3
MOV A,R2
MOV R1,A
MOV A,R3
MOV R2,A
//R1- larger value , R2 - smaller value
MOV A,R1
MOV B,R2 //load the divide registers for GCD

LOOP: MOV R4,B


DIV AB
MOV A,B
JZ GCDOUT //if remainder zero , exit with gcd in R4
MOV A,R4 //move old divisor in A , remainder stored in B
SJMP LOOP //repeat if B is not zero

GCDOUT: MOV A,R1 //Multiply numbers


MOV B,R4
DIV AB //value stored as BA
MOV B,R2
MUL AB

//read product as R6/R5

LEAVE: INC DPTR


MOVX @DPTR,A
INC DPTR
MOV A,B
MOVX @DPTR,A
HERE: SJMP HERE
END

 
 

       

 
Part 3: Check if the given string is a palindrome. 
1. Obtain the string length by reading till NULL is obtained, and running a counter 
alongside. 
2. Start reading character by character.  
3. Store 1st character in R1 and corresponding mirror image in A. If same, continue to next 
character. If not, exit loop. Go to step 5. 
4. If the entire string is scanned (R1=0), then exit loop. Move #1 to 9500h. 
5. END 
 
CODE: 
ORG 8100H

MOV R1,#0
MOV DPTR , #8FFFh

//Calculate String Length


NULLFIND: INC DPTR
MOVX A,@DPTR
INC R1 //Strlen stored in R1
CJNE A,#0,NULLFIND

DEC R1
MOV DPTR,#9000H //Initialize

PALCHECK: MOVX A,@DPTR //Move 1st value to R1


MOV R2,A

MOV A,R1
MOVC A,@A+DPTR //Move mirror value to A

MOV 60h,R2
CJNE A,60h,FINISH //Compare

INC DPTR //Next value


DJNZ R1,PALCHECK

RESULT: MOV DPTR,#9500H // If R1 reduces to zero (with all comparisons satisfied), move 1
to 9500h
MOV A,#1
MOVX @DPTR,A

FINISH: SJMP FINISH


END
 
 
 
 
DISCUSSION 
 
 JAYANT 07EC1043: 
• In the experiment we learned about the instruction set of 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, 
 
AAMOD SHANKER (07ec1041) 
• The experiment involved some fundamental programming on the 8051 platform in 
assembly. 
• The code is written in assembly constructs, and hence associated logical constructs had 
to be used. 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. 
• 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) 
• While running the code on board, the WIN51E driver runs through the entire code 
memory. To prevent this, an infinite loop was put at the end of the code, which was 
manually terminated. 
 
 
 
 

 
 

You might also like