0% found this document useful (0 votes)
171 views42 pages

Prac 1 W

The document describes using Keil software to transfer data between memory locations using assembly language instructions. It discusses different types of data transfer instructions in the 8051 architecture like MOV, MOVX, MOVC, PUSH, and POP. The aim is to use these instructions to transfer 10 bytes of data from memory addresses 30h to 39h to memory addresses 40h to 49h.
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)
171 views42 pages

Prac 1 W

The document describes using Keil software to transfer data between memory locations using assembly language instructions. It discusses different types of data transfer instructions in the 8051 architecture like MOV, MOVX, MOVC, PUSH, and POP. The aim is to use these instructions to transfer 10 bytes of data from memory addresses 30h to 39h to memory addresses 40h to 49h.
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
You are on page 1/ 42

Practical no: 1

Aim: introduction to keil software.

Apparatus: keil software.

Theory :

What is keil?

Keil development tools for the 8051 Microcontroller Architecture support every level of software
developer from the professional applications engineer to the student just learning about embedded
software development.

The industry-standard Keil C Compilers, Macro Assemblers, Debuggers, Real-time Kernels, Single-board
Computers, and Emulators support all 8051 derivatives and help you get your projects completed on
schedule.

8051 Microcontroller Architecture

The 8051 Microcontroller is an 8 – bit Microcontroller i.e. it can read, write and process 8 – bit Data.
There are a bunch of manufacturers like Atmel, NXP, TI, who manufacture their own versions of 8051
Microcontroller. Irrespective of the manufacturer, the internal hardware design i.e. the 8051
Microcontroller Architecture remains more or less the same. The following image shows the 8051
Microcontroller Architecture in a block diagram style.
The block diagram of the 8051 Microcontroller Architecture shows that 8051 Microcontroller consists of a
CPU, RAM (SFRs and Data Memory), Flash (EEPROM), I/O Ports and control logic for communication
between the peripherals.

All these different peripherals inside the 8051 Microcontroller will communicate with each other via the 8
– bit Data Bus, also known as the internal data bus. 

8051 Microcontroller Architecture Features

We have seen the internal architecture of the 8051 Microcontroller in the above section. Now, we will see
the features of the 8051 Microcontroller Architecture.

NOTE: Some of the features like internal ROM and RAM will vary with the specific model of the 8051
Microcontroller.

 8 – bit CPU with two Registers A (Accumulator) and B.


 Internal ROM of 8K Bytes – It is a flash memory that supports in – system programming.
 Internal RAM of 256 Bytes – The first 128 Bytes of the RAM i.e. 00H to 7FH is again divided in
to 4 banks with 8 registers (R0 – R7) in each bank, 16 bit addressable registers and 80 general
purpose registers. The higher 128 Bytes of the RAM i.e. 80H to FFH consists of SFRs or Special
Function Registers. Using SFRs we can control different peripherals like Timers, Serial Port, all I/O
Ports, etc.

 32 I/O Pins (Input / Output Pins) – Arranged as 4 Ports: P0, P1, P2 and P3.
 8- bit Stack Pointer (SP) and Processor Status Word (PSW).
 16 – bit Program Counter (PC) and Data Pointer (DPTR).
 Two 16 – bit Timers / Counters – T0 and T1.
 Control Registers – SCON, PCON, TCON, TMOD, IP and IE.
 Serial Data Transmitter and Receiver for Full – Duplex Operation – SBUF.
 Interrupts: Two External and Three Internal.
 Oscillator and Clock Circuit. 

In this tutorial, we have seen a brief note on the 8051 Microcontroller Architecture. In the next 8051
Tutorial, we will see the 8051 Memory Organization and I/O Ports.  

Procedure:

1. Open Keil from the Start menu

2. The Figure below shows the basic names of the windows referred in this document
3. Select New μVision Project from the Project Menu.

4. Name the project with any name.

5. Click on the Save Button.

6. The device window will be displayed.

7. Select the part you will be using to test


with. For now we will use the Atmel part at89C51
8. Double Click on the Atmel.

9. Scroll down and select the AT89C514 Part


Click OK. Choose No

10. Click File Menu and select New. A new window will open up in the Keil IDE.

11. As we selected the AT89C51 device, a particular target is created.


12. By right clicking the target we set the crystal frequency to 12 mhz. and create a hex file.
13. Now your Project pane on the Kiel IDE would look something like this

14. Adding Files to Keil Project

15. Right Click on the Source Group 1 folder on your Project pane and select Add New Item to Group
'Source Group1'.
16. Selected the file and add it to the keil project.
17. After whatever you have typed out the above program to your main. A file, you can compile
the A file by pressing F7 key or by going to ' Project -> Build Target ' on the IDE menu bar.

18. If there are no errors the code will compile and you can view the output on the Build Output pane.
Hence after the compilation of code we run the program.

Applications of keil
It used for c compliers, macro assembler.

It is used as debuggers and real time kernels.

It is used for single board computers, and for emulator support for all 8051 derivatives.

Conclusion:
Hence we have studied the keil software and also know the steps of how to executed for various programs.
Practical no: 2
Aim: To understand addressing modes

Apparatus: keil software.

Theory: To perform any operation we have to give corresponding instruction to 8051. In each instruction we have to

give 3 information.

1] Operation to be performed.

2] Address of source of data.

3] Address of destination of result.

The method by which address of source of data or address of destination of result is given in the instruction called

addressing mode.

Different types of addressing modes are as follows:

1] Immediate addressing mode

 In Immediate Addressing mode, the operand, which follows the Opcode, is a constant data of either 8 or 16

bits. The name Immediate Addressing came from the fact that the constant data to be stored in the memory

immediately follows the Opcode.

 The 8 or 16 bit data required for execution of instruction is directly given in the instruction, called as

immediate addressing mode. Therefore the data must be followed by a hash “#” sign.

 Immediate Addressing is very fast as the data to be loaded is given in the instruction itself.

Example:

Mov a, #40h

Before Execution
After Execution

2] Direct addressing mode

 In Direct Addressing Mode, the address of the data is specified as the Operand in the

 Instruction. 8 bit data required for execution of instruction is present in the internal data memory
 I.e. Ram location and address of that memory location given in the instruction is called direct addressing

mode.

 This can be used to access internal ram and sfr’s.

Example: MOV P1, 0D0H

After Execution

3] Register indirect addressing mode:

 In the Indirect Addressing Mode or Register Indirect Addressing Mode, the address of the Operand is

specified as the content of a Register.

 Therefore an 8 bit data required for execution of the instruction is present in memory location (internal or

external) and addressing of that memory location is pointed by pointer register which is present in the

instruction, called as indirect addressing mode.


 Pointer register must be followed by “@” sign.

 8 bit pointer register are R0 and R1 and 16 bit pointer register is dptr.

 We can’t use sfr by this mode.

(1) Example: MOV A, @R1 (2) example: mov 23h, #0AAH

MOV R1, #23H

MOV A,@R1

END

After Execution
4] Register direct addressing mode

 In the 8051, Microcontroller Memory Organization Tutorial, we have seen the organization of RAM and

four banks of Working Registers with eight Registers in each bank.

 Therefore 8 or 16 bit data required for execution of instruction is present in register and name of the

register is mentioned in the instruction, called as register addressing mode or register direct addressing

mode.

Example:

Mov b, # (data); for initialization.

Mov A, R5

Mov R0, B
After Execution

5] Index addressing mode:

 With Indexed Addressing Mode, the effective address of the Operand is the sum of a base register and an

offset register. If the data present in memory and address of memory location is obtained by adding 16bit

no. of given base register DPTR/PC with 8bit no. of index register accumulator, then it is called as base

index addressing mode.

 Only program memory can be accessed with index addressing mode .this addressing mode is used to read

look up table in EEPROM and in jump instruction.

Example: mov DPTR, # (DATA) (16BIT); Base register

Mov A, # (DATA) (8BIT); offset

Movc A, A+@DPTR
END

After Execution

 CONCLUSION:

 Hence we have studied and executed the various types of addressing modes of microcontroller 8051.

 Result:

 An Addressing Mode is a way to locate a target Data, which is also called as Operand.

 Therefore addressing modes allows flexibility in accessing the register memory locations, and it is the

methods by which address of data or address of destination of result is given in the instructions.
PRACTICAL NO: 3(a)

TO UNDERSTAND DATA TRANSFER VIA AS WELL AS EMBEDDED C

AIM: To transfer 10 bytes of data from 0x30-0x39 to 0x40-0x49.(i.e.30h-39h to 40h-49h)

Apparatus: keil software

Theory: data transfer instruction of 8051 does not affect any of PSW flags. Except a pop or mov directly to the
psw.

Therefore various data transfer instructions are: mov, movx, movc, push, pop and exchange (XCHG).

Data transfer operations are divided into three types:

1] General purpose.

2] Accumulator –specific.

3] address-object.

General purpose registers:

 MOV: mov performs a bit or byte transfer from the source operand to the destination operand.
 PUSH: push increments the SP register and then transfers a byte from the source operand to the stack
location currently addressed by sp.

Accumulator specific transfers:

 XCH: it exchanges the byte source operand with register A.


 XCHD: XCHD exchanges the lower order nibble of the byte source operand with the low order nibble A.
 MOVX: it performs a byte move between the external data memory and the accumulator .the external
address can be specified by the DPTR register or the R1 or R0 register (8bit).
 MOVC: it moves the program memory to the accumulator .the operand in A is used as an index into a 256
byte table pointed to by the base register (DPTR or PC). The byte accessed is transferred to the
accumulator.

Address-object transfer:

 MOV DPTR, data loads 16 bits of immediate data into a pair of destination registers, DPH, DPL.

PROGRAM:

ADDRESS MNEOMICS OPCODES EXPLATIONS


Cx0000 MOV 40h,30H 853040 MOVE DIRECT BYTE
TO BYTE(3BYTE)
Cx0003 MOV 41H,31H 853141 DATA OF 31H IS
TRANSFERRED TO
41H
Cx0006 MOV 42H,32H 853242 DATA OF 32H IS
TRANSFERRED TO
42H
Cx0009 MOV 43H,33H 853343 DATA OF 33H IS
TRANSFERRED TO
43H
Cx000C MOV 44H,34H 853444 DATA OF 34H IS
TRANSFERRED TO
44H
Cx000F MOV 45H,35H 853545 DATA OF 35H IS
TRANSFERRED TO
45H
Cx0012 MOV 46H,36H 853646 DATA OF 36H IS
TRANSFERRED TO
46H
Cx0015 MOV 47H,37H 853747 DATA OF 37H IS
TRANSFERRED TO
47H
Cx0018 MOV 48H,38H 853848 DATA OF 38H IS
TRANSFERRED TO
48H
Cx001B MOV 49H,39H 853949 DATA OF 39H IS
TRANSFERRED TO
49H
Cx001C END 00 END OF PROGRAM
Conclusion: therefore in microcontroller 8051 there are different 28 instructions under data transfer and total

there are 79 Op-codes .flags are not affected by using data transfer instructions. Hence we have studied and

performed the data transfer instruction.


PRACTICAL NO: 3(B)

AIM: TRANSFER 10 BYTES OF DATA FROM 0X30-0X39 TO 0X35-0X3E (i.e.30H-39H TO 35H-3EH) .

Apparatus: keil software.

Program:

ADDRESS MNEOMICS OPCODES EXPLATIONS


0x0000 MOV 3EH,39H 85393E Reverse program.

Data of 39h is

transferred to 3EH

MOVE DIRECT BYTE

TO BYTE(3BYTE)
0x0003 MOV 3DH,38H 85383D Data of 38Hh is

transferred to 3DH
0x0006 MOV 3CH,37H 85373C Data of 37h is

transferred to 3CH

0x0009 MOV 3BH,36H 85363B Data of 36h is

transferred to 3BH

0x000C MOV 3AH,35H 85353A Data of 35h is

transferred to 3AH

0x000F MOV 39H,34H 893439 Data of 34h is

transferred to 39H

0x0012 MOV 38H,33H 853338 Data of 33h is

transferred to 38H

0x0015 MOV 37H,32H 853237 Data of 32h is


transferred to 37H

0x0018 MOV 36H,31H 853136 Data of 31h is

transferred to 36H

0x001B MOV 35H,30H 853035 Data of 30h is

transferred to 35H

0x001E END 00 END OF PROGRAM


CONCLUSION: HENCE DATA TRANSFER INSTUCTION IS EXECUTED AND STUDIED.
PRACTICAL NO: 3(C)

AIM: EXCHANGE DATA BYTES PRESENT IN MEMORY LOACTION FROM 30H-39H TO 40H-49H

APPARATUS: KEIL SOFTWARE.

PROGRAM:

ADDRESS MNEOMICS OPCODES EXPLATIONS


0X000 MOV A,30H E530 Load the contents of

memory location

30h into acc


0x0002 MOV 30H,40H 854030 Store the memory

the product data

into 30h location.


0x0005 MOV 40H,A F540 Store the product

byte in memory

location 40h
0x0007 MOV A,31H E531 Load the contents of

memory location

31h into acc


0x0009 MOV 31H,41H 854131 Store the memory

the product data of

41h into ,31h

memory location
0x000C MOV 41H,A F541 Store the product

byte in memory

location 41h
0x000E MOV A,32H E532 Load the contents of

memory location

32h into acc


0x0010 MOV 32H,42H 854232 Store the memory
the product data of

42h into ,32h

memory location
0x0013 MOV 42H,A F542 Store the product

byte in memory

location 42h
0x0015 MOV A,33H E533 Load the contents of

memory location

33h into acc


0x0017 MOV 33H,43H 854333 Store the memory

the product data of

43h into ,33h

memory location
0x001A MOV 43H,A F543 Store the product

byte in memory

location 43h
0x001C MOV A,34H E534 Load the contents of

memory location

34h into acc


0x001E MOV 34H,44H 854434 Store the memory

the product data of

44h into ,34h

memory location
0x0021 MOV 44H,A F544 Store the product

byte in memory

location 44h
0x0023 MOV A,35H E535 Load the contents of

memory location

35h into acc


0x0025 MOV 35H,45H 854535 Store the memory
the product data of

45h into ,35h

memory location
0x0028 MOV 45H,A F545 Store the product

byte in memory

location 45h
0x002A MOV A,36H E536 Load the contents of

memory location

36h into acc


0x002C MOV 36H,46H 854636 Store the memory

the product data of

46h into ,36h

memory location
0x002F MOV 46H,A F546 Store the product

byte in memory

location 46h
0x0031 MOV A,37H E537 Load the contents of

memory location

37h into acc


0x0033 MOV 37H,47H 854737 Store the memory

the product data of

47h into ,37h

memory location
0x0036 MOV 47H,A F547 Store the product

byte in memory

location 47h
0x0038 MOV A,38H E538 Load the contents of

memory location

38h into acc


0x003D MOV 38H,48H 854838 Store the memory
the product data of

48h into ,38h

memory location
0x003F MOV 48H,A F548 Store the product

byte in memory

location 48h
0x0041 MOV A,39H E539 Load the contents of

memory location

39h into acc


0x0044 MOV 39H,49H 854939 Store the memory

the product data of

49h into ,39h

memory location
0x0046 MOV 49H,A F549 Store the product

byte in memory

location 49h
0x0047 END 00 End of program
CONCLUSION: HENCE THE EXCHANGE DATA BYTES PRESENT IN MEMORY LOACTION FROM 30H-39H TO

40H-49H IS BEING PERFORMED AND EXECUTED.


Practical no: 4(d)

Aim: to find the largest number from the 10 bytes of data in memory location 30h-39h.store the largest

byte in R3 and store the address of largest byte in R4.

APPARATUS: keil software

Program:

Address mnemonics opcodes expiations


0x002D ORG 0000H 00 Assembler directive
0x002E SJMP MAIN 00 2byte instruction ,1

machine cycle
0x002F ORG 30H 802E Assembler directive

address
0x0030 MAIN: MOV 7809 2 byte instruction

R0,#09H one machine cycle.


0x0032 MOV DPTR,#30H 900030 3byte and 2

machine cycle
0x0035 MOVX A,@DPTR E0 2byte instruction
0x0036 MOV B,A F5F0 2byte instruction

&one machine cycle


0x0038 INC DPTR A3
0x0039 MOVX A,@DPTR E0 3byte and 2
machine cycle
0x003A SUBB A,B 95F0 Subtract a & b
0x003C JNC BACK 5006
0x003E DJNZ R0,L2 D8F8 2 byte instruction,

2machine cycle
0x0040 MOV R3,B ABF0 One byte instruction

and one machine

cycle.
0x0042 HERE: SJMP HERE 80FE
0x0044 BACK:MOVX E0 3byte and 2

A,@DPTR machine cycle


0x0045 MOV B,A F5F0 2byte instruction

&one machine cycle


0x0047 MOV R4,DPL AC82 3byte and 2

machine cycle
0X0049 MOV R5,DPH AD83 3byte and 2

machine cycle
0x004b SJMP L1 80f1
0x004d END 00 End of program
Conclusion: Thus, the program is executed for finding the largest number.

This program demonstrates that specific data parameter can be obtained by using

Conditional instructions.
Practical no: 4(A)

Aim: addition/subtraction/multiplication/division of two 8 bit numbers .i.e.((((a+b)-c)*d)/e).

Apparatus: keil software

Theory:

Addition:

Inc : (increment adds one source operand and put the result in the operand.

ADD: adds A to the source operand and returns to A.

ADDC: (add with carry): it adds A and the source operand and then adds 1 if CY is set and puts the result in A.

DAA (decimal adjust for bcd): corrects the sum which results from the binary addition of two, two-digit operands.
The packed bcd decimal sum formed by DA is returned to A. cy is set if bcd results greater than 99, otherwise cy is
cleared.

Subtract:

Subb: subtract with borrow: it subtracts the source operand from the first operand (the accumulator), subtracts 1
if CY is set and returns the result to A.

DEC :( DECREMENT): SUBTRACTS 1 from the source operand and returns the results to the operand.

Multiplication:

Mul performs an unsigned multiplication of register A register with B register, resulting in a two –byte result. A
receives the lower order byte and B receives the higher order byte

Division:
Div performs an unsigned division of A register by B register and returns the integer quotient to A and remainder
to the B register.

Program:

Address Mnemonics Opcode Explanation


00H ORG 0000H 80 Jump to the address
SJMP MAIN pointed by the label
‘MAIN’.
40H ORG 0040H 78 Transfer the
MAIN: MOV R0, contents of memory
#40H location 40H into R0
register of Bank 0.
42H MOV A, @R0 E6 Transfer the
contents of R0
register into
Accumalator.
43H INC R0 08 Increment the
memory location by
one.
44H MOV B, @R0 86 Transfer the
contents of current
memory location
into B register.
46H ADDC A, B 35 Add with carry data
present in
Accumalator with
data present in B
register.
48H MOV 50H, A F5 Transfer the result
of addition into
memory location
50H.
4AH INC R0 08 Increment the
current memory
location by one.
4BH MOV A, @R0 E6 Transfer the data
present in current
memory location
from R0 register into
Accumalator.
4CH MOV B, A F5 Transfer the data
from Accumalator
into B register.
4EH MOV A, 50H E5 Transfer the result
of addition of
previous two
numbers from
memory location
50H into
Accumalator.
50H SUBB A, B 95 Subtract the data
present in
Accumalator with
data present in B
register.
52H MOV 51H, A F5 Transfer the result
of subtraction into
memory location
51H.
54H INC R0 08 Increment the
current memory
location by one.
55H MOV A, @R0 E6 Transfer the
contents of present
memory location
into Accumalator.
56H MOV B, A F5 Transfer the
contents of
Accumalator into B
register.
58H MOV A, 51H E5 Transfer the result
of subtraction from
51H into
Accumalator.
5AH MUL AB A4 Multiply the data
present in
Accumalator with
data present in B
register.
5BH MOV 52H, A F5 Transfer the result
of multiplication
into memory
location 52H.
5DH INC R0 08 Increment the
present memory
location by one.
5EH MOV A, @R0 E6 Transfer the
contents of present
memory location
into Accumalator.
5FH MOV B, A F5 Transfer data from
Accumalator into B
register.
61H MOV A, 52H E5 Transfer the result
of multiplication
from 52H into
Accumalator.
63H DIV AB 84 Divide the data
present in
Accumalator by data
present in B register.
64H MOV 90H, A F5 Transfer the
quotient of result
into Port 1 i.e. 90H.
66H MOV 80H, B 85 Transfer the
remainder of the
result into Port 0 i.e.
80H.
END NOP End of code.
Quotient Remainder

Conclusion:

The equation ((((A+B)-C)*D)/E) has been performed .

The value of quotient is stored at port 1 & the value of remainder is stored at port 0.
Practical no: 4(B)

Aim: addition/subtraction of two 16 bit numbers. i.e. (A+B)-C.

Apparatus: keil software

Theory:

Addition:

Inc : (increment adds one source operand and put the result in the operand.

ADD: adds A to the source operand and returns to A.

ADDC: (add with carry): it adds A and the source operand and then adds 1 if CY is set and puts the result in A.

DAA (decimal adjust for bcd): corrects the sum which results from the binary addition of two, two-digit operands.
The packed bcd decimal sum formed by DA is returned to A. cy is set if bcd results greater than 99, otherwise cy is
cleared.

Subtract:

Subb: subtract with borrow: it subtracts the source operand from the first operand (the accumulator), subtracts 1
if CY is set and returns the result to A.

DEC :( DECREMENT): SUBTRACTS 1 from the source operand and returns the results to the operand.

Program:
Address Mnemonics opcodes explanation
0x0000 mov dptr,#1000h 901000 3 byte instruction and
2machinne cycle.
0x0003 movx a,@ dptr E0 1byte instruction, 2
machine cycle
0x0004 mov b,a F5F0 1byte instruction and
1 machine cycle.
0x0006 mov dptr,#1002h 901002 3 byte instruction and
2machinne cycle.
0x0009 movx a,@ dptr E0 1byte instruction, 2
machine cycle
0x000A add a,b 25F0 1byte instruction and
1 machine cycle.
0x000C mov dptr,#3000h 903000 3 byte instruction and
2machinne cycle
0x000F movx @ dptr ,a F0 1byte instruction, 2
machine cycle
0x0010 mov dptr,#1001h 901001 3 byte instruction and
2machinne cycle.
0x0013 movx a,@ dptr E0 1byte instruction, 2
machine cycle
0x0016 mov b,a 901003 1byte instruction and
1 machine cycle.
0x0019 mov dptr,#1003h E0 3 byte instruction and
2machinne cycle.
0x001A movx a,@ dptr 35F0 1byte instruction, 2
machine cycle
0x001C addc a,b 35F0 1byte instruction, 1
machine cycle
0x001F mov dptr,#3001h 903001 3 byte instruction and
2machinne cycle.
0x0020 movx @ dptr ,a F0 1byte instruction, 2
machine cycle
0x0022 mov a,#00h 7400 2 byte instruction ,1
machine cycle
0x0024 addc a,#00h 3400 2 byte instruction ,1
machine cycle
0x0024 mov dptr,#3002h 903002 3 byte instruction and
2machinne cycle.
0x0027 movx @ dptr,a F0 1byte instruction, 2
machine cycle
0x0028 movx @ dptr,a 901004 1byte instruction, 2
machine cycle
0x002B movx a,@ dptr E0 1byte instruction, 2
machine cycle
0x002C Mov b,a F5F0 1byte instruction, 1
machine cycle
0x002E mov dptr,#3000h 903000 3 byte instruction and
2machinne cycle.
0x0031 movx a,@ dpt E0 1byte instruction, 2
machine cycle
0x0032 subb a,b 95f0 1byte instruction,
1machine cycle
0x0034 mov dptr,#1006h 901006 3 byte instruction and
2machinne cycle.
0x0037 movx @ dptr ,a F0 1byte instruction, 2
machine cycle
Conclusion: (A+B)-C

1] Ain 1000h & 1001h

2] B in 1002h & 1003h

3[ C in 1004h & 1005h

Therefore the result is stored in 1006h &1007h & 1008h.

Practical no 4: (C)

AIM: conversion of packed BCD to unpacked BCD.

Apparatus: keil software.

Theory :

What is the BCD?


Binary coded decimal (BCD) is a system of writing numerals that assigns a four-digit binary code to each
digit 0 through 9 in a decimal (base-10) numeral.
The four-bit BCD code for any particular single base-10 digit is its representation in binary notation, as
follows: 0 = 0000.1 = 0001.

Why BCD code is used?


The main advantage of the Binary Coded Decimal system is that it is a fast and efficient system to convert
the decimal numbers into binary numbers as compared to the pure binary system. But the BCD code is
wasteful as many of the 4-bit states (10-to-16) are not used but decimal displays have important
applications.

What is BCD code?

We have seen here that Binary Coded Decimal or BCD is simply the 4-bit binary code representation of


a decimal digit with each decimal digit replaced in the integer and fractional parts with
its binary equivalent. BCD Code uses four bits to represent the 10 decimal digits of 0 to 9.

Packed BCD:

 Packed BCD Numbers are stored in two digits to a byte, in 4 bit groups referred to as nibbles. ALU is
capable of performing only binary addition and subtraction, but

by adjusting the sum or difference the correct result in packed BCD format.

Unpacked BCD: 

Unpacked BCD Format: In Unpacked BCD, there is only one digit per byte and because of this,
unpacked multiplication and division can be done.

Binary operations act on single bytes and the results are adjusted. For Division, the adjustment is
done before the binary division.

Program:

Address mnemonics opcodes Explanations

0x0000 Sjmp main 803E 2byte instruction, 24


oscillator period.
0X0040 Main: mov r0,#40h 7840 2byte instruction, 1
machine cycle.
0x0042 Mov a,@r0 E6 Move indirect ram to
accumulator, 1byte
instruction.
0x0043 Da a 1 byte instruction 1
machine cycle
0x0044 Mov b,a F5f0 1 byte instruction 1
machine cycle
0x0046 Anl a,#0fh 540f 2byte instruction
0x0048 Mov a,b E5f0 1 byte instruction 1
machine cycle
0x004A Anl a,#0f0h 54f0 2byte instruction
0x004c Swap a C4 1 byte instruction 1
machine cycle
0x004f Mov 42h,a F542 2byte instruction and
12 oscillator period.
0x0051 End 00 End op program
Conclusion:

Therefore data in 40h.

Unpacked data in 41h & 42h.

Therefore, the main advantage of the Binary Coded Decimal system is that it is a fast and efficient system
to convert the decimal numbers into binary numbers as compared to the pure binary system.

Practical no 4(D)

AIM: Find 2’s complement of a number. i.e. data in 40h & find 2's comp in 41h

Apparatus: keil software

Theory:

1’s complement:

1’s complement of a binary number is another binary number obtained by toggling all bits in it, i.e., transforming
the 0 bit to 1 and the 1 bit to 0.

Examples:

Let numbers be stored using 4 bits:

1's complement of 7 (0111) is 8 (1000).

1's complement of 12 (1100) is 3 (0011).

2’s complement:
In 2’s complement of a binary number is 1 added to the 1’s complement of the binary number.

Examples:

Let numbers be stored using 4 bits:

2's complement of 7 (0111) is 9 (1001).

2's complement of 12 (1100) is 4 (0100).

The main difference between 1′ s complement and 2′ s complement is that 1′ s complement has two


representations of 0 (zero) – 00000000, which is positive zero (+0) and 11111111, which is negative zero
(-0); whereas in 2′ s complement, there is only one representation for zero – 00000000 (+0) because if we
add 1 to 11111111 (-1), we get 00000000 (+0) which is the same as positive zero.
This is the reason why 2′ s complement is generally used.
Another difference is that while adding numbers using 1′ s complement, we first do binary addition, then
add in an end-around carry value. But, 2′ s complement has only one value for zero, and doesn’t require
carry values.
Program:
address Mnemonics Opcodes explanations
0x0000 Mov r0,#40h 7840 2byte instruction, 1
machine cycle.
0x0002 Mov a, @ro E6
0x0003 Cpl a F4 1byte instruction, 1
machine cycle.
0x0004 Add a,#01h 2401 2byte instruction, 1
machine cycle.
0x0006 Mov 41h,a F541 2byte instruction, 1
machine cycle.
0x0008 end 00 End of program.
Conclusion: Therefore, 1’s complement of a binary number is another binary number obtained by toggling all
bits in it, i.e., transforming the 0 bit to 1 and the 1 bit to 0.

 2's complement of a binary number is 1 added to the 1's complement of the binary number.

You might also like