8051 Assignments Array Addition, Multiplication, Block Transfer
8051 Assignments Array Addition, Multiplication, Block Transfer
– 10
Assignment No.:10 a(i)
Aim: To write a program to add n, 8 bits numbers found in internal RAM locations 40H
onwards and store results in R6 and R7.
Theory: The 8051 memory is divided into the following 4 physical parts as shown in
figure 1.
1. Internal RAM
2. Internal special function registers
3. External RAM
4. Internal and external ROM
Addresses
7F General
Purpose
30 Area
2F Bit
Address
20 Area
1F Register
18 Bank 3
17 Register
10 Bank 2
0F Register
08 Bank 1
07 Register
00 Bank 0
Fig. 1Internal RAM
Here we need to access internal memory to take input and to store the output.
Internal RAM: Internal memory organization is shown in fig.1. It is divided into three
distinct areas:
1) 32 bytes from address 00H to 1FH that make up 32 working registers organized as 4
banks of eight registers each. The 4 registers banks are numbered 0 to 3 and are made up
of 8 registers named R0 to R7. Each register can be addressed by name or by its RAM
address. Bits RS0 and RS1 in the PSW determine which bank of registers is currently in
use at any time when the program is running. Bank 0 is selected on reset.
7 6 5 4 3 2 1 0
CY AC F0 RS1 RS0 OV ------ P
The Program Status Word (PSW) Special Function Register
2) A bit addressable area of 16 bytes occupies RAM byte addresses 20H to 2FH forming
a total of 128 addressable bits. An addressable bit may be specified by its bit address of
00H to 7FH or byte address from 20H to 2FH. Addressable bits are useful when the
program need only remember a binary event.
3) A general purpose RAM area above the bit area from 30H to 7FH, addressable as
bytes.
Instructions used:
Some instructions include the carry flag as an additional source of a single bit that is
included in the operation at the least significant bit position.
ADDC A, #n Add the contents of A, the immediate number n and the C flag, put the
sum in A
C, AC and OV flags behave exactly as they do for the ADD commands.
e .g. MOV A, #1AH
ADDC A, #10H if C=1,A=2BH
ADDC is normally used to add a carry after the LSBY addition in a multi-byte process.
3) INC byte
This instruction adds 1 to the register or memory location specified by the operand.Cy is
not affected even if value FF is incremented to 00. It supports accumulator, register,
direct and register indirect addressing modes.
4) JNC target
Jump if no carry. The instruction examines the CY flag and if it is zero it will jump to
target address.
e.g. JNC radd Jump relative address if the Carry flag is reset to 0.
Input: n 8 bit numbers stored in internal RAM location 40H onwards. First location
should contain value of n.
Algorithm:
Calculations:
i) Do calculation for addition of 5(n) numbers stored in internal memory.
FAQs.
1) What is the difference between the following two instructions in terms of
addressing mode and function performed?
MOV A, #46H
MOV A, 46H
2) What address is assigned to register A?
3) How to change register bank in 8051?
References:
1. Ayala,”The 8051 Micro Controller 3rd Edition”, IE
2. Mazidi M.Gillipse J. “The 8051 Microcontroller and Embedded Systems”, Pearson
Education, 2002, ISBN-81-7808-574-7
Aim: To write a program to multiply 16 bit number by 8 bit number and store the result
in internal memory location.
Theory:
Normal multiplication
Consider 25 x 16
Step1: First multiply by the least significant digit 25 x 6= 150
Here take 15 as a carry since it exceeds the number of digits in “6” i. e. 1
Keep 0 as the least significant digit of the answer.
Step 3: Add carry FE01 + FE= FEFF It is addition of a 16 bit number and an 8 bit
number. Therefore addition will take two steps if carry gets generated by addition of
lower byte.
Instructions used:
1) MOV Rr, #n Copy the 8 bit number n into register Rr of the current register
bank
For e.g. MOV R0, #40H Put 8-bit memory address in register R0
Where 40H is internal memory location
Here immediate addressing mode is used.
Here indirect addressing mode is used. Any register R0 to R7 can be used to hold address
of the data. Here register R0 or R1 is often called a data pointer to hold address of one of
the data locations in RAM from address 00H to 7FH
For e.g. MOV A,@R0 Copy the contents of the address in R0 to the A register
MOV @R0, A Copy the data in A to internal memory address in R0
5) Addition
All addition is done with the A register as the destination of the result.
ADD A, Rr Add A and register Rr put the sum in A
If there is a carry out of bit position 7, it is cleared to 0 otherwise
AC flag is set to 1 if there is a carry out of bit position 3, it is cleared otherwise
OV flag is set to 1 if there is a carry out of position 7.
e.g. ADD A, R4 Add A and register R4 put the sum in A
Some instructions include the carry flag as an additional source of a single bit that is
included in the operation at the least significant bit position.
ADDC A, #n Add the contents of A, the immediate number n and the C flag, put the
sum in A
C, AC and OV flags behave exactly as they do for the ADD commands.
e .g. MOV A, #1AH
ADDC A, #10H if C=1, A=2BH
ADDC is normally used to add a carry after the LSBY addition in a multi-byte process.
6) Multiplication
Multiplication operations use registers A and B as both source and destination addresses
for the operation. The unsigned number in register A is multiplied by the unsigned
number in register B as follows
Output: Result of 16 bit x 8 bit multiplication stored at internal memory locations from
50H onwards
BBEE x FF = BB3212
Algorithm:
Input is in 40H to 42H
Output is in internal memory 50H to 52H
1) Initialize pointer to internal memory and copy the memory location content
(input)of corresponding location to corresponding internal registers
2) Initialize memory pointer to internal memory to store the output
3) Load the lower byte of the 16 bit number into accumulator A and 8 bit number
into register B
4) Multiply them using instruction MUL AB
5) Store lower byte of product in internal memory
6) Store the higher byte of product in one of general purpose register
7) Get higher byte of 16 bit number into accumulator and 8 bit number into register B
8) Multiply them using instruction MUL AB.
9) Add lower byte of the product with higher byte of previous multiplication.
10) Store the result into internal memory
11) Add carry with the higher byte and store it into memory
Calculations:
Multiplication of BBEE x FF
Step 1: EE x FF= ED12H
Keep ED as carry and 12 as the lower byte of the answer.
Step 2: BB x FF=BA45H
Conclusion: Internal memory accesses are studied by multiplying 16 bit number with 8
bit number
FAQs:
1) Give significance of MUL instruction.
2) How to check zero flag in 8051?
3) What is bit addressable area? How many bits are addressable? What is their
significance?
References:
1. Ayala,”The 8051 Micro Controller 3rd Edition”, IE
2. Mazidi M.Gillipse J. “The 8051 Microcontroller and Embedded Systems”, Pearson
Education, 2002, ISBN-81-7808-574-7
Prepared By: M. I. T., Pune
Assignment No.10 (b)
Objective: To study the various concept of internal/ external memory and there
addressing mode.
A) Related Theory: The 8051 architecture consists of Internal ROM and RAM.
1. Internal RAM of 128 bytes
2. Internal ROM or EPROM, 0-4K.
1. Internal RAM of 128 byte:
a) Four register bank, each contain eight registers (Working Register).
b) Sixteen byte, which may be addressed at the bit level (Bit Addressable)
c) Eighty byte of general purpose data memory. (General Purpose)
Internal RAM is organized into three areas. Thirty-two bytes from address 00h to 1fh
that makes up 32 working registers organized as four bank of eight registers each.
The four register banks are numbered 0 to 3 and are made up of eight registers name
R0 to R7.
A bit- addressable area of 16 byte occupies RAM byte addresses 20h to 2F, forming a
total of 128 addressable bits. An addressable bit may be specified by its bit address of
00h to 7Fh.A general purpose RAM area above the bit area, from 30h to 7Fh,
addressable as bytes.
Instructions Used:
1) MOV: This instruction is used to move immediate / indirect data from register
to register or register to accumulator or register to a memory location.
Ex. MOV A, @ R0
MOV @ R1,A
2) DPTR: This is 16- bit data pointer, use full to hold the address of the data byte
in external RAM. DPTR registers can address the maximum RAM space of
0000h to 0FFFFh.
Ex MOVX A, @ Rp
MOVX A, @ DPTR
MOVX @ Rp, A
MOVX @ DPTR, A
MOVX is normally used with external RAM or I/O addresses.
3) DJNZ Rr, target: DJNZ decrement the register by one and jump to the relative
address if the result is not zero, no flages are affected
Input: The number of bytes in the block to be transfer for source address location
to destination address location of internal memory.
Output: The data has been transferred from source address location to destination
Conclusion: Thus we have transfer 8-bit data from internal RAM to another location in
RAM, internal memory to external memory location , external memory
location to internal memory, but external to external memory data transfer
is not possible because two DPTR is not available in microcontroller.