Sei sulla pagina 1di 8

Experiment 1

BT-40
Deboleena Roy 09EC3203 Shruti Khatri 09EC1057

Experiment 1: Familiarization with 8051 kit

Objective:
Study about the kit components and its interface with the PC. Write the following programs in the
assembly language of 8051.
1. Find the second largest of an array of 100 numbers stored in external memory from location
9000H onwards. The number be stored at location 9500H. Show the contents of memory
locations before and after running the program to establish correctness of it
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 and Software
1. ESA51E 8051 Trainer Board
2. Compiler Kiel Vision4
3. Driver Win51E

Design (Hardware)
ESA51E is trainer kit that is interfaced with PC through RS-232-C serial port connection. It is provided
external power supply of +5V. The compiler converts the assembly code into a .hex file which is then
loaded on the code space of 8051 using driver Win51E

Design (Software)
Part 1: Obtain second largest number from an array of 100 numbers

Design
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



Experiment 1
BT-40
Deboleena Roy 09EC3203 Shruti Khatri 09EC1057

Software Code

ORG 8100H
MOV DPTR,#9000H
MOV R0,#99

MOVX A,@DPTR
MOV R1,A

INC DPTR
MOVX A,@DPTR

MOV 60H,R1
CJNE A,60H,NEXT
MOV R2,A
SJMP LOOP

NEXT: JNC SHIFT
MOV R2,A
SJMP LOOP

SHIFT: MOV R3,A
MOV A,R1

MOV R2,A
MOV A,R3

MOV R1,A

LOOP: DJNZ R0,MAIN
SJMP FINISH

MAIN: INC DPTR
MOVX A,@DPTR
MOV 60H,R1
CJNE A,60H,NEXT2
MOV R2,A
SJMP LOOP

NEXT2: JNC SHIFT
MOV 60H,R2
CJNE A,60H,NEXT3
SJMP LOOP
NEXT3: JNC MOVR2A
SJMP LOOP

MOVR2A: MOV R2,A
SJMP LOOP
Experiment 1
BT-40
Deboleena Roy 09EC3203 Shruti Khatri 09EC1057


FINISH: MOV DPTR,#9500H
MOV A,R2
MOVX @DPTR,A

HERE: SJMP HERE
END

Part 2: Find the LCM of 2 given numbers

Design
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 Bs 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



Software Code

ORG 8100H

MOV DPTR,#9000H
MOVX A,@DPTR
MOV R1,A
INC DPTR
MOVX A,@DPTR
MOV R2,A
MOV 60H,R1
CJNE A,60H, NEXT1
SJMP LEAVE

NEXT1: JNC SWITCH
MOV A,R1
MOV B,R2
SJMP LOOP

SWITCH: MOV A,R1
Experiment 1
BT-40
Deboleena Roy 09EC3203 Shruti Khatri 09EC1057

MOV R3,A
MOV A,R2
MOV R1,A
MOV A,R3
MOV R2,A
MOV A,R1
MOV B,R2

LOOP: MOV R4,B
DIV AB
MOV A,B
JZ GCDOUT
MOV A,R4
SJMP LOOP

GCDOUT: MOV A,R1
MOV B,R4
DIV AB
MOV B,R2
MUL AB

LEAVE: INC DPTR
MOVX @DPTR,A
INC DPTR
MOV A,B
MOVX @DPTR,A

HERE: SJMP HERE
END

Part 3: Find out whether a given string is a palindrome

Design
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 at location 70H and corresponding mirror image in A. Compare and 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


Experiment 1
BT-40
Deboleena Roy 09EC3203 Shruti Khatri 09EC1057

Software Code

ORG 8100H
MOV R2,#0H
MOV DPTR,#8FFFH ;1 less than starting value

STRING:
INC R2
INC DPTR
MOVX A,@DPTR
CJNE A,#0H,STRING
DEC DPL
MOV R0,DPL

MOV A,R2
MOV B,#2H
DIV AB
MOV R2,A

MOV DPTR,#9000H

LOOP:
MOVX A,@DPTR
MOV R4,DPL
MOV 70H,A
MOV DPL,R0
MOVX A,@DPTR
CJNE A,70H,LEAVE
DEC DPL
MOV R0,DPL
MOV DPL,R4
INC DPTR
DJNZ R2,LOOP

MOV DPTR,#9500H
MOV A,#01H
MOVX @DPTR,A
LJMP NEXT

LEAVE:
MOV DPTR,#9500H
MOV A,#0H
MOVX @DPTR,A

NEXT: SJMP NEXT
END


Experiment 1
BT-40
Deboleena Roy 09EC3203 Shruti Khatri 09EC1057

Observation
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.

Part 1:

User Input
Address Value
9000H 00H
9001H 50H
9002H 60H
9003H 70H
9004H 00H
.
.
.
9063H 00H

Output
Address Value
9500H 60H

Part 2:

User Input
Address Value
9000H 06H (6)
9001H 0AH (10)

Output
Address Value
9002H 1EH (30)


Experiment 1
BT-40
Deboleena Roy 09EC3203 Shruti Khatri 09EC1057

Part 3:

Case 1

User Input
Address Value
9000H 03H
9001H 02H
9002H 01H
9003H 02H
9004H 03H
9005H 00H


Output
Address Value
9500H 01H (it is a palindrome)

Case 2

User Input
Address Value
9000H 03H
9001H 02H
9002H 00H
9003H 02H
9004H 03H
9005H 00H

Output
Address Value
9500H 00H (it is not palindrome)

The program reads the string up to 9001H as it encounters the null character at 9002H. So it sees the
string as 03 02 which is not a palindrome



Experiment 1
BT-40
Deboleena Roy 09EC3203 Shruti Khatri 09EC1057

Discussion
Deboleena Roy 09EC3203
1. The program is written in assembly language. Usual conditional constructs like if else, for,
do while are replaced by intuitive register transfers and jumps.
2. Mathematical operations, transfer of values with external RAM (MOVX), comparisons are
restricted to Register Accumulator (A)
3. Each program is executed by first setting the program counter to start of the location of the
code in code space. In our case it is 8100H
4. Each program terminates with an infinite loop of the type
NEXT: JUMP NEXT
so that the PC remains stuck there and no further instructions in other parts of the code
space gets executed.
5. CJNE function was abundantly used in all 3 parts. The syntax is CJNE value#1, value#2.
Value#1 must always be A (accumulator). Carry bit is set to 1 if A < value#2 else it is set to
zero.
Shruti Khatri 09EC1057
1. In this experiment, we wrote and burned basic programs on the 8051 micro-controller.
2. Syntax used in assembly language is not very intuitive. Most operations don't work on
registers and the accumulator has to be used during the operation and the result needs to
be copied in the register. Same is the case with operations like compare which doesn't work
on registers.
3. When writing the program we should always make sure that the program ends with an
infinite loop otherwise on running the code some instructions that are already present at
memory locations that we haven't re-written might be executed. We should press break to
stop the execution of the infinite loop and see the results.
4. When performing compare operation the value of carry varies depending on whether
greater than or less than is true and this can be used in the program.
5. The program counter should be set in the WIN51E environment at the location from which
we want the program to execute.