SlideShare une entreprise Scribd logo
1  sur  24
Télécharger pour lire hors ligne
Pari vallal Kannan
Center for Integrated Circuits and Systems
University of Texas at Dallas
8051 Programming: Arithmetic
and Logic
EE4380 Fall 2002
Class 4
5-Sep-02 2
Topics
l Signed and Unsigned arithmetic
l Binary and BCD coded numbers
l Addition Instructions
l Subtraction
l Multiplication
l Division
l Logic Operations
l Rotate and Swap operations
l Bit addressable memory and single bit instructions
5-Sep-02 3
Unsigned Addition
l add a, source ; A = A + source
l Carry (if any) will be in CY flag
mov A, #0F5H
add A, #0BH ; A = F5 + B0 = 00, CY=1
l 16 bit addition
– addc A, source ; A = A + source + CY
– Add the lower bytes using add
– Save the result
– Add the upper bytes using addc
5-Sep-02 4
Unsigned Addition (contd.)
l Example of 16 bit addition
l Add UUVV and PPQQ
clr C
mov A, QQ
add A, VV
mov r6, A
mov A, PP
addc A, UU
mov r7, A
l Final 16 bit result in r7:r6 and CY flag
5-Sep-02 5
BCD Addition
l BCD – Binary Coded Decimal
– 4 bits are used to represent a decimal number from 0-9
l Packed BCD has two such numbers in one byte
– 17 PBCD = 17decimal = 11hex
l Packed BCD addition may not yield a valid BCD. Use
decimal adjust instruction (da A) for correcting it
l After adding two Packed BCD numbers call da to get
valid PBCD
mov A, #47H ; first BCD = 47d
mov B, #25H ; second BCD = 25d
add A,B ; A = 6CH (binary addition of 47H and 25H)
da A ; A = 72H (BCD result of addition)
5-Sep-02 6
BCD Addition (contd.)
l To correct an invalid BCD, add 6 to the digit
that is greater than 9
l What da does
– If lower nibble is > 9 or AC=1 then add 6 (0110) to
the lower nibble
– If upper nibble is > 9 or CY=1 then add 6 to the
upper nibble
l da will work for ADD only. For other operations
(inc, sub etc), this correction has to be done
manually
5-Sep-02 7
Unsigned Subtraction
l subb x, y ; x = x-y with borrow from CY
l Operation:
– Take 2’s complement of the subtrahend (y)
– Add it to the minuend (x)
– If the CY flag is set after the subb operation, then
the result is negative and the destination has the 2’s
complement of the result
l subb performs subtract with borrow, if CY is set
before the call. Used for 16bit subtraction
– To get plain sub, clear CY before calling subb
5-Sep-02 8
Unsigned Subtraction (contd.)
l Example
clr c ; clear CY for sub operation
mov A, #4CH ;
subb A, #6EH ; two operands, do 4C – 6E
jnc done ; if CY==0 result is positive
cpl A ; CY=1, result negative. So find 2’s complement
inc A ; by complementing A and adding 1 to it
done: mov R1, A ; final result in R1
l 16 bit subtraction 2762H – 1296H
clr C ; clear Cy
mov A, #62H ;
subb A, #96H ; 62H – 96H = CCH and CY=1
mov R7, A ; store the lower byte of the result in R7
mov A, #27H ; now subtract the upper bytes
subb A, #12H ; 27H – 12H – 1 = 14H
mov R6, A ; store upper byte of result in R6.
; Final 16bit result is in R6:R7
5-Sep-02 9
Multiplication and Division
l MUL AB ; A x B, place result in BA
mov A, #25H ; operand1: 25H
mov B, #65H ; operand2: 65H
mul AB ; 25H * 65H = E99H
; B = 0EH, A = 99H
l DIV AB ; A/B, place quotient in A and
remainder in B
mov A, #95H
mov B, #10
div AB ; A = 9 (quotient), B = 5 (remainder)
5-Sep-02 10
Signed Arithmetic - Concepts
l Representation of the sign
– Allocate one bit of all numeric quantities for the sign
– Usually MSB (most significant bit) is assigned for
the sign
– The remaining bits represent the magnitude
l 8051 has only 8-bit registers
– Signed numbers can have only a 7 bit magnitude
– Positive numbers in 8051 = 0 to +127 (7 bits)
– Negative numbers -128 to –1
– Why ?
5-Sep-02 11
Signed Arith. – Negative Numbers
l Negative Number representation in signed
arithmetic
– The sign bit (MSB) is 1
– Magnitude is in 2’s complement form
l Examples
Represent –5
5 = 0000 0101
Cpl = 1111 1010
+1 = 1111 1011
Hex = FBH
Hence –5 = FBh
Represent –34H
34H = 0011 0100
Cpl = 1100 1011
+1 = 1100 1100
Hex = CCH
Hence –34H = CCH
Represent –128
128 = 1000 0000
Cpl = 0111 1111
+1 = 1000 0000
Hex = 80H
Hence –128 = 80H
Range
-128 = 80H
-127 = 81H
…..
-1 = FFH
0 = 00H
1 = 01H
+127 = 7FH
5-Sep-02 12
Signed Numbers - Usage
l Application may require a specific quantity be
represented as a signed number
– Temperature measurement –20deg, +10deg etc
– Water/Gas/Beer level measurement in a tank
l Data is collected and stored as an array of signed
numbers
– Some of the array elements can be negative, while others are
positive
– Identify negative numbers by the MSB. If MSB=1, the number
is negative
l Same arithmetic operations (add, sub, mul, div etc)
may need to be performed on the array elements, and
the result can be positive or negative.
5-Sep-02 13
8051 – Signed Arithmetic
l 8051 uses negative number representation in
the sub instruction. Not enough!
l When signed numbers are needed,
programmer has to take care of signed
arithmetic
l Overflow has to be dealt with. Carry flag is not
enough, because only 7 bits carry the
magnitude in signed numbers
l The 8051 provides another flag – OV
(Overflow) for this purpose.
5-Sep-02 14
8051 - Signed Arithmetic (contd.)
l Addition A+B A = 01H, B = FFH
A = +1, B = -1
A = 0000 0001
B = 1111 1111
+ = 1 0000 0000
A+B = 0H
A+B A = FEH, B = FFH
A = -2, B = -1
A = 1111 1110
B = 1111 1111
+ = 1 1111 1101
A+B = FDH = -3
A-B A = 01H, B = FFH
A = +1, B = -1
2’s(B) = 0000 0000 +1 = 0000 0001
A = 0000 0001
2’s(B) = 0000 0001
+ = 0 0000 0010
A-B = 02H
A-B A = FEH, B = 01H
A = -2, B = +1
2’s(B) = 1111 1110 +1 = 1111 1111
A = 1111 1110
2’s(B) = 1111 1111
+ = 1 1111 1101
A-B = FDH = -3
l Subtraction
5-Sep-02 15
8051 Signed Arith. - Overflow
l Overflow can occur from
the magnitudes of the
signed numbers, which
can change the sign bit.
l OV Flag is to be
checked for error in
signed arithmetic
l Example
A+B, A=+96 (60H), B=+70(46H)
A = 0110 0000
B = 0100 0110
+ = 1010 0110 = A6H = -90 (wrong)
OV = 1, CY=0
96+70 = 166 > +127
5-Sep-02 16
8051 – OV Flag
l After arithmetic operations, OV is set if
– Carry from D6 to D7 but no carry from D7
– Carry from D7 but no carry from D6 to D7
– These cases indicate a wrong result due to signed
arithmetic
l After arithmetic operation involving signed
numbers, check OV flag, for error detection
– Use jb PSW.2 or jnb PSW.2
– PSW.2 = OV
5-Sep-02 17
8051 Logic Instructions
l AND
– anl dest, source ; dest = dest AND source
– Commonly used to mask out (set to 0) a few bits in an operand
l OR
– orl dest, source ; dest = dest OR source
– Commonly used to set a few bits in an operand
l XOR
– xrl dest, source ; dest = dest XOR source
– Commonly used to clear a register, check if two registers have
the same value and toggle a few bits
l Complement
– cpl A ; A = A’
l None of these instructions affect any flags
5-Sep-02 18
8051 – Compare Instruction
l CJNE
– Cjne dest, source, rel address
– Compare dest and source and jump to relative address if not
equal
– Basically a subtract operation which does not change the
operands but affects the CY flag
– dest > source è CY=0
– dest < source è CY=1
l Example
– Monitor P1 continuosly and
– exit if P1=63H
Loop: mov A, P1
cjne A, #63, loop
Cmp: cjne R5, #80, NEQ
EQ: …. ;R5= #80
NEQ: jnc GREAT
LESS: … ;R5< #80
GREAT: …. ;R5 > #80
5-Sep-02 19
8051 – Rotate and Swap
l Bitwise rotation is required in many apps like serial
comm., control etc.
l Rotate right
– rr A ; rotate right A
l Rotate left
– rl A ; rotate left A
l Rotate right/left with Carry
– Use CY in the rotate sequence (9 bit rotate)
– rlc A : D7 à CY àD0
– rrc A: D0 à CY à D7
l Swap nibbles
– swap A ; swaps D7-D4 with D3-D0
Example for RR
mov A, #AAH
rr A ; now A = 55H
rr A ; now A = 2AH
Example for RL
mov A, #55H
rl A ; now A = AAH
rl A ; now A = 54H
5-Sep-02 20
8051 – Single Bit Instructions
l Set a bit
– set bit_name ;bit = 1
l Clear a bit
– clr bit_name ;bit = 0
l Complement a bit
– cpl bit_name ;bit = bit’
l Conditional Jump on bit value
– jb (jump if bit=1), jnb (jump if bit=0), jbc (jump if
bit=1 and clear the bit)
5-Sep-02 21
Bit addressable Regs and Memory
l I/O ports (P0 – P3), B, PSW, IP, IE, ACC, SCON and
TCON are bit addressable registers (BARs)
l The bits of BARs can be referred to as Register.bitnum
(P0.1, PSW.2, IE.4 etc) or by their bit address
l Bit address is the base address of the register + the bit
number
– ACC Base address is E0H, hence ACC.1=E1H, ACC.7=E7H
– P0, Base address is 80H, hence P0.0=80H, P0.5=84H and so
on
l 16 bytes of the internal RAM is bit addressable
– 20H to 2FH has a bit address of 00H to 7FH
– clr 67H ; clear bit D7H of RAM location 2CH
– setb 05H ; set bit 5 of RAM location 20H
5-Sep-02 22
Single Bit Operations with CY flag
l 8051 has special instructions that directly
manipulate CY flag
– setb C; clr C; cpl C; mov b,C; mov C,b; jnc, jc, anl
C,b; anl C,/b; orl C,b; orl C,/b
– anl C, /b ;C = CY AND b’
l Example: Turn ON fan (P2.2) and turn OFF
light (P2.3) Fan_on: setb C
orl C,P2.2 ;CY = CY OR P2.2
mov P2.2, C;turn on fan if not already ON
Light_off: clr C
anl C,P2.3 ;CY = CY AND P2.3
mov P2.3,C ;turn off light if not already OFF
5-Sep-02 23
Class-4: Review
l Signed and Unsigned arithmetic
l Binary and BCD coded numbers
l Addition, Subtraction, Multiplication, Division
l Signed Number representation and arithmetic
l Logic Operations
l Rotate and Swap operations
l Bit addressable memory and single bit
instructions
5-Sep-02 24
Thanks

Contenu connexe

Tendances

Chapter 6 - Introduction to 8085 Instructions
Chapter 6 - Introduction to 8085 InstructionsChapter 6 - Introduction to 8085 Instructions
Chapter 6 - Introduction to 8085 Instructionscmkandemir
 
8085 Paper Presentation slides,ppt,microprocessor 8085 ,guide, instruction set
8085 Paper Presentation slides,ppt,microprocessor 8085 ,guide, instruction set8085 Paper Presentation slides,ppt,microprocessor 8085 ,guide, instruction set
8085 Paper Presentation slides,ppt,microprocessor 8085 ,guide, instruction setSaumitra Rukmangad
 
Chapter 7 - Programming Techniques with Additional Instructions
Chapter 7 - Programming Techniques with Additional InstructionsChapter 7 - Programming Techniques with Additional Instructions
Chapter 7 - Programming Techniques with Additional Instructionscmkandemir
 
Microprocessor and Microcontroller Lab Manual!
Microprocessor and Microcontroller Lab Manual!Microprocessor and Microcontroller Lab Manual!
Microprocessor and Microcontroller Lab Manual!PRABHAHARAN429
 
Microprocessor instructions
Microprocessor instructionsMicroprocessor instructions
Microprocessor instructionshepzijustin
 
Logical instruction of 8085
Logical instruction of 8085Logical instruction of 8085
Logical instruction of 8085vishalgohel12195
 
Instruction set of 8085
Instruction set  of 8085Instruction set  of 8085
Instruction set of 8085shiji v r
 
Introduction to 8085 & it's description(includes basic lab experiments)
Introduction to 8085 & it's description(includes basic lab experiments)Introduction to 8085 & it's description(includes basic lab experiments)
Introduction to 8085 & it's description(includes basic lab experiments)Basil John
 
8086-instruction-set-ppt
 8086-instruction-set-ppt 8086-instruction-set-ppt
8086-instruction-set-pptjemimajerome
 
The 8051 assembly language
The 8051 assembly languageThe 8051 assembly language
The 8051 assembly languagehemant meena
 
SAP II ARTICTURE ,SAP 2
SAP II ARTICTURE ,SAP 2SAP II ARTICTURE ,SAP 2
SAP II ARTICTURE ,SAP 2Apar Pramod
 
Microprocessor lab manual
Microprocessor lab manualMicroprocessor lab manual
Microprocessor lab manualDhaval Shukla
 

Tendances (20)

Programming with 8085
Programming with 8085Programming with 8085
Programming with 8085
 
Chapter 6 - Introduction to 8085 Instructions
Chapter 6 - Introduction to 8085 InstructionsChapter 6 - Introduction to 8085 Instructions
Chapter 6 - Introduction to 8085 Instructions
 
8085 Paper Presentation slides,ppt,microprocessor 8085 ,guide, instruction set
8085 Paper Presentation slides,ppt,microprocessor 8085 ,guide, instruction set8085 Paper Presentation slides,ppt,microprocessor 8085 ,guide, instruction set
8085 Paper Presentation slides,ppt,microprocessor 8085 ,guide, instruction set
 
Micro task1
Micro task1Micro task1
Micro task1
 
Abc2
Abc2Abc2
Abc2
 
Chapter 7 - Programming Techniques with Additional Instructions
Chapter 7 - Programming Techniques with Additional InstructionsChapter 7 - Programming Techniques with Additional Instructions
Chapter 7 - Programming Techniques with Additional Instructions
 
Microprocessor and Microcontroller Lab Manual!
Microprocessor and Microcontroller Lab Manual!Microprocessor and Microcontroller Lab Manual!
Microprocessor and Microcontroller Lab Manual!
 
Microprocessor instructions
Microprocessor instructionsMicroprocessor instructions
Microprocessor instructions
 
Class10
Class10Class10
Class10
 
Logical instruction of 8085
Logical instruction of 8085Logical instruction of 8085
Logical instruction of 8085
 
Chapter 8
Chapter 8Chapter 8
Chapter 8
 
Instruction set of 8085
Instruction set  of 8085Instruction set  of 8085
Instruction set of 8085
 
Intel 8085 mp
Intel 8085 mpIntel 8085 mp
Intel 8085 mp
 
8085 instruction set
8085 instruction set8085 instruction set
8085 instruction set
 
Introduction to 8085 & it's description(includes basic lab experiments)
Introduction to 8085 & it's description(includes basic lab experiments)Introduction to 8085 & it's description(includes basic lab experiments)
Introduction to 8085 & it's description(includes basic lab experiments)
 
Sap 1
Sap 1Sap 1
Sap 1
 
8086-instruction-set-ppt
 8086-instruction-set-ppt 8086-instruction-set-ppt
8086-instruction-set-ppt
 
The 8051 assembly language
The 8051 assembly languageThe 8051 assembly language
The 8051 assembly language
 
SAP II ARTICTURE ,SAP 2
SAP II ARTICTURE ,SAP 2SAP II ARTICTURE ,SAP 2
SAP II ARTICTURE ,SAP 2
 
Microprocessor lab manual
Microprocessor lab manualMicroprocessor lab manual
Microprocessor lab manual
 

En vedette (7)

Class9
Class9Class9
Class9
 
Class8
Class8Class8
Class8
 
Class5
Class5Class5
Class5
 
Chapter 7 8051 programming in c
Chapter 7  8051 programming in cChapter 7  8051 programming in c
Chapter 7 8051 programming in c
 
Microcontroller 8051 and its interfacing
Microcontroller 8051 and its interfacingMicrocontroller 8051 and its interfacing
Microcontroller 8051 and its interfacing
 
8051 Microcontroller Notes
8051 Microcontroller Notes8051 Microcontroller Notes
8051 Microcontroller Notes
 
Class7
Class7Class7
Class7
 

Similaire à Class4

Lecture 03 Arithmetic Group of Instructions
Lecture 03 Arithmetic Group of InstructionsLecture 03 Arithmetic Group of Instructions
Lecture 03 Arithmetic Group of InstructionsZeeshan Ahmed
 
Arithmetic & logical operations in 8051
Arithmetic & logical operations in 8051Arithmetic & logical operations in 8051
Arithmetic & logical operations in 8051Jay Patel
 
Unit-8-Computer-Arithmetic.pdf
Unit-8-Computer-Arithmetic.pdfUnit-8-Computer-Arithmetic.pdf
Unit-8-Computer-Arithmetic.pdfGafryMahmoud
 
Microprocessor Part 3
Microprocessor    Part  3Microprocessor    Part  3
Microprocessor Part 3Sajan Agrawal
 
12 chapter06 math_instructions_fa14
12 chapter06 math_instructions_fa1412 chapter06 math_instructions_fa14
12 chapter06 math_instructions_fa14John Todora
 
microp-8085 74 instructions for mct-A :P
microp-8085 74 instructions for mct-A :Pmicrop-8085 74 instructions for mct-A :P
microp-8085 74 instructions for mct-A :PJathin Kanumuri
 
microp-8085 74 instructions for mct-A :P-2
microp-8085 74 instructions for mct-A :P-2microp-8085 74 instructions for mct-A :P-2
microp-8085 74 instructions for mct-A :P-2Jathin Kanumuri
 
Microcontroller 8051- soft.ppt
Microcontroller 8051- soft.pptMicrocontroller 8051- soft.ppt
Microcontroller 8051- soft.pptsteffydean
 
180410227 ae2406-lab-manual-doc
180410227 ae2406-lab-manual-doc180410227 ae2406-lab-manual-doc
180410227 ae2406-lab-manual-dochomeworkping10
 
The 8051 microcontroller
The 8051 microcontrollerThe 8051 microcontroller
The 8051 microcontrollerPallaviHailkar
 
ARITHMETIC LOGIC UNIT.ppt
ARITHMETIC LOGIC UNIT.pptARITHMETIC LOGIC UNIT.ppt
ARITHMETIC LOGIC UNIT.pptRAJESH S
 
Instruction 4.pptx
Instruction 4.pptxInstruction 4.pptx
Instruction 4.pptxHebaEng
 
INTEL 8085 DATA FORMAT AND INSTRUCTIONS
INTEL 8085 DATA FORMAT AND INSTRUCTIONSINTEL 8085 DATA FORMAT AND INSTRUCTIONS
INTEL 8085 DATA FORMAT AND INSTRUCTIONSSwapnil Mishra
 
Computer organization and architecture lab manual
Computer organization and architecture lab manual Computer organization and architecture lab manual
Computer organization and architecture lab manual Shankar Gangaju
 
ARMbuilt-inshift_2a6f2cdd75038e8c46c6d481aac833ec.pdf
ARMbuilt-inshift_2a6f2cdd75038e8c46c6d481aac833ec.pdfARMbuilt-inshift_2a6f2cdd75038e8c46c6d481aac833ec.pdf
ARMbuilt-inshift_2a6f2cdd75038e8c46c6d481aac833ec.pdfeklavya0304
 
Comp Arithmetic Basic.ppt
Comp Arithmetic Basic.pptComp Arithmetic Basic.ppt
Comp Arithmetic Basic.pptskatiarrahaman
 
Assembly Language Instructions & Programming.pptx
Assembly Language Instructions & Programming.pptxAssembly Language Instructions & Programming.pptx
Assembly Language Instructions & Programming.pptxVineetKukreti1
 

Similaire à Class4 (20)

Hemanth143
Hemanth143 Hemanth143
Hemanth143
 
Lecture 03 Arithmetic Group of Instructions
Lecture 03 Arithmetic Group of InstructionsLecture 03 Arithmetic Group of Instructions
Lecture 03 Arithmetic Group of Instructions
 
6 arithmetic logic inst and prog
6 arithmetic logic inst and prog6 arithmetic logic inst and prog
6 arithmetic logic inst and prog
 
Arithmetic & logical operations in 8051
Arithmetic & logical operations in 8051Arithmetic & logical operations in 8051
Arithmetic & logical operations in 8051
 
Unit-8-Computer-Arithmetic.pdf
Unit-8-Computer-Arithmetic.pdfUnit-8-Computer-Arithmetic.pdf
Unit-8-Computer-Arithmetic.pdf
 
Microprocessor Part 3
Microprocessor    Part  3Microprocessor    Part  3
Microprocessor Part 3
 
12 chapter06 math_instructions_fa14
12 chapter06 math_instructions_fa1412 chapter06 math_instructions_fa14
12 chapter06 math_instructions_fa14
 
microp-8085 74 instructions for mct-A :P
microp-8085 74 instructions for mct-A :Pmicrop-8085 74 instructions for mct-A :P
microp-8085 74 instructions for mct-A :P
 
microp-8085 74 instructions for mct-A :P-2
microp-8085 74 instructions for mct-A :P-2microp-8085 74 instructions for mct-A :P-2
microp-8085 74 instructions for mct-A :P-2
 
Microcontroller 8051- soft.ppt
Microcontroller 8051- soft.pptMicrocontroller 8051- soft.ppt
Microcontroller 8051- soft.ppt
 
180410227 ae2406-lab-manual-doc
180410227 ae2406-lab-manual-doc180410227 ae2406-lab-manual-doc
180410227 ae2406-lab-manual-doc
 
Addressing modes
Addressing modesAddressing modes
Addressing modes
 
The 8051 microcontroller
The 8051 microcontrollerThe 8051 microcontroller
The 8051 microcontroller
 
ARITHMETIC LOGIC UNIT.ppt
ARITHMETIC LOGIC UNIT.pptARITHMETIC LOGIC UNIT.ppt
ARITHMETIC LOGIC UNIT.ppt
 
Instruction 4.pptx
Instruction 4.pptxInstruction 4.pptx
Instruction 4.pptx
 
INTEL 8085 DATA FORMAT AND INSTRUCTIONS
INTEL 8085 DATA FORMAT AND INSTRUCTIONSINTEL 8085 DATA FORMAT AND INSTRUCTIONS
INTEL 8085 DATA FORMAT AND INSTRUCTIONS
 
Computer organization and architecture lab manual
Computer organization and architecture lab manual Computer organization and architecture lab manual
Computer organization and architecture lab manual
 
ARMbuilt-inshift_2a6f2cdd75038e8c46c6d481aac833ec.pdf
ARMbuilt-inshift_2a6f2cdd75038e8c46c6d481aac833ec.pdfARMbuilt-inshift_2a6f2cdd75038e8c46c6d481aac833ec.pdf
ARMbuilt-inshift_2a6f2cdd75038e8c46c6d481aac833ec.pdf
 
Comp Arithmetic Basic.ppt
Comp Arithmetic Basic.pptComp Arithmetic Basic.ppt
Comp Arithmetic Basic.ppt
 
Assembly Language Instructions & Programming.pptx
Assembly Language Instructions & Programming.pptxAssembly Language Instructions & Programming.pptx
Assembly Language Instructions & Programming.pptx
 

Dernier

Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance BookingCall Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Bookingroncy bisnoi
 
UNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular ConduitsUNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular Conduitsrknatarajan
 
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Christo Ananth
 
result management system report for college project
result management system report for college projectresult management system report for college project
result management system report for college projectTonystark477637
 
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordCCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordAsst.prof M.Gokilavani
 
University management System project report..pdf
University management System project report..pdfUniversity management System project report..pdf
University management System project report..pdfKamal Acharya
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Christo Ananth
 
Thermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptThermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptDineshKumar4165
 
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...Call Girls in Nagpur High Profile
 
UNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its PerformanceUNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its Performancesivaprakash250
 
data_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdfdata_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdfJiananWang21
 
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Bookingdharasingh5698
 
Generative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPTGenerative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPTbhaskargani46
 
UNIT-IFLUID PROPERTIES & FLOW CHARACTERISTICS
UNIT-IFLUID PROPERTIES & FLOW CHARACTERISTICSUNIT-IFLUID PROPERTIES & FLOW CHARACTERISTICS
UNIT-IFLUID PROPERTIES & FLOW CHARACTERISTICSrknatarajan
 
Intze Overhead Water Tank Design by Working Stress - IS Method.pdf
Intze Overhead Water Tank  Design by Working Stress - IS Method.pdfIntze Overhead Water Tank  Design by Working Stress - IS Method.pdf
Intze Overhead Water Tank Design by Working Stress - IS Method.pdfSuman Jyoti
 

Dernier (20)

Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance BookingCall Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Booking
 
UNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular ConduitsUNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular Conduits
 
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
 
result management system report for college project
result management system report for college projectresult management system report for college project
result management system report for college project
 
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort ServiceCall Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
 
NFPA 5000 2024 standard .
NFPA 5000 2024 standard                                  .NFPA 5000 2024 standard                                  .
NFPA 5000 2024 standard .
 
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordCCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
 
University management System project report..pdf
University management System project report..pdfUniversity management System project report..pdf
University management System project report..pdf
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
 
Thermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptThermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.ppt
 
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
 
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
 
UNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its PerformanceUNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its Performance
 
data_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdfdata_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdf
 
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
 
Roadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and RoutesRoadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and Routes
 
Generative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPTGenerative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPT
 
UNIT-IFLUID PROPERTIES & FLOW CHARACTERISTICS
UNIT-IFLUID PROPERTIES & FLOW CHARACTERISTICSUNIT-IFLUID PROPERTIES & FLOW CHARACTERISTICS
UNIT-IFLUID PROPERTIES & FLOW CHARACTERISTICS
 
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
 
Intze Overhead Water Tank Design by Working Stress - IS Method.pdf
Intze Overhead Water Tank  Design by Working Stress - IS Method.pdfIntze Overhead Water Tank  Design by Working Stress - IS Method.pdf
Intze Overhead Water Tank Design by Working Stress - IS Method.pdf
 

Class4

  • 1. Pari vallal Kannan Center for Integrated Circuits and Systems University of Texas at Dallas 8051 Programming: Arithmetic and Logic EE4380 Fall 2002 Class 4
  • 2. 5-Sep-02 2 Topics l Signed and Unsigned arithmetic l Binary and BCD coded numbers l Addition Instructions l Subtraction l Multiplication l Division l Logic Operations l Rotate and Swap operations l Bit addressable memory and single bit instructions
  • 3. 5-Sep-02 3 Unsigned Addition l add a, source ; A = A + source l Carry (if any) will be in CY flag mov A, #0F5H add A, #0BH ; A = F5 + B0 = 00, CY=1 l 16 bit addition – addc A, source ; A = A + source + CY – Add the lower bytes using add – Save the result – Add the upper bytes using addc
  • 4. 5-Sep-02 4 Unsigned Addition (contd.) l Example of 16 bit addition l Add UUVV and PPQQ clr C mov A, QQ add A, VV mov r6, A mov A, PP addc A, UU mov r7, A l Final 16 bit result in r7:r6 and CY flag
  • 5. 5-Sep-02 5 BCD Addition l BCD – Binary Coded Decimal – 4 bits are used to represent a decimal number from 0-9 l Packed BCD has two such numbers in one byte – 17 PBCD = 17decimal = 11hex l Packed BCD addition may not yield a valid BCD. Use decimal adjust instruction (da A) for correcting it l After adding two Packed BCD numbers call da to get valid PBCD mov A, #47H ; first BCD = 47d mov B, #25H ; second BCD = 25d add A,B ; A = 6CH (binary addition of 47H and 25H) da A ; A = 72H (BCD result of addition)
  • 6. 5-Sep-02 6 BCD Addition (contd.) l To correct an invalid BCD, add 6 to the digit that is greater than 9 l What da does – If lower nibble is > 9 or AC=1 then add 6 (0110) to the lower nibble – If upper nibble is > 9 or CY=1 then add 6 to the upper nibble l da will work for ADD only. For other operations (inc, sub etc), this correction has to be done manually
  • 7. 5-Sep-02 7 Unsigned Subtraction l subb x, y ; x = x-y with borrow from CY l Operation: – Take 2’s complement of the subtrahend (y) – Add it to the minuend (x) – If the CY flag is set after the subb operation, then the result is negative and the destination has the 2’s complement of the result l subb performs subtract with borrow, if CY is set before the call. Used for 16bit subtraction – To get plain sub, clear CY before calling subb
  • 8. 5-Sep-02 8 Unsigned Subtraction (contd.) l Example clr c ; clear CY for sub operation mov A, #4CH ; subb A, #6EH ; two operands, do 4C – 6E jnc done ; if CY==0 result is positive cpl A ; CY=1, result negative. So find 2’s complement inc A ; by complementing A and adding 1 to it done: mov R1, A ; final result in R1 l 16 bit subtraction 2762H – 1296H clr C ; clear Cy mov A, #62H ; subb A, #96H ; 62H – 96H = CCH and CY=1 mov R7, A ; store the lower byte of the result in R7 mov A, #27H ; now subtract the upper bytes subb A, #12H ; 27H – 12H – 1 = 14H mov R6, A ; store upper byte of result in R6. ; Final 16bit result is in R6:R7
  • 9. 5-Sep-02 9 Multiplication and Division l MUL AB ; A x B, place result in BA mov A, #25H ; operand1: 25H mov B, #65H ; operand2: 65H mul AB ; 25H * 65H = E99H ; B = 0EH, A = 99H l DIV AB ; A/B, place quotient in A and remainder in B mov A, #95H mov B, #10 div AB ; A = 9 (quotient), B = 5 (remainder)
  • 10. 5-Sep-02 10 Signed Arithmetic - Concepts l Representation of the sign – Allocate one bit of all numeric quantities for the sign – Usually MSB (most significant bit) is assigned for the sign – The remaining bits represent the magnitude l 8051 has only 8-bit registers – Signed numbers can have only a 7 bit magnitude – Positive numbers in 8051 = 0 to +127 (7 bits) – Negative numbers -128 to –1 – Why ?
  • 11. 5-Sep-02 11 Signed Arith. – Negative Numbers l Negative Number representation in signed arithmetic – The sign bit (MSB) is 1 – Magnitude is in 2’s complement form l Examples Represent –5 5 = 0000 0101 Cpl = 1111 1010 +1 = 1111 1011 Hex = FBH Hence –5 = FBh Represent –34H 34H = 0011 0100 Cpl = 1100 1011 +1 = 1100 1100 Hex = CCH Hence –34H = CCH Represent –128 128 = 1000 0000 Cpl = 0111 1111 +1 = 1000 0000 Hex = 80H Hence –128 = 80H Range -128 = 80H -127 = 81H ….. -1 = FFH 0 = 00H 1 = 01H +127 = 7FH
  • 12. 5-Sep-02 12 Signed Numbers - Usage l Application may require a specific quantity be represented as a signed number – Temperature measurement –20deg, +10deg etc – Water/Gas/Beer level measurement in a tank l Data is collected and stored as an array of signed numbers – Some of the array elements can be negative, while others are positive – Identify negative numbers by the MSB. If MSB=1, the number is negative l Same arithmetic operations (add, sub, mul, div etc) may need to be performed on the array elements, and the result can be positive or negative.
  • 13. 5-Sep-02 13 8051 – Signed Arithmetic l 8051 uses negative number representation in the sub instruction. Not enough! l When signed numbers are needed, programmer has to take care of signed arithmetic l Overflow has to be dealt with. Carry flag is not enough, because only 7 bits carry the magnitude in signed numbers l The 8051 provides another flag – OV (Overflow) for this purpose.
  • 14. 5-Sep-02 14 8051 - Signed Arithmetic (contd.) l Addition A+B A = 01H, B = FFH A = +1, B = -1 A = 0000 0001 B = 1111 1111 + = 1 0000 0000 A+B = 0H A+B A = FEH, B = FFH A = -2, B = -1 A = 1111 1110 B = 1111 1111 + = 1 1111 1101 A+B = FDH = -3 A-B A = 01H, B = FFH A = +1, B = -1 2’s(B) = 0000 0000 +1 = 0000 0001 A = 0000 0001 2’s(B) = 0000 0001 + = 0 0000 0010 A-B = 02H A-B A = FEH, B = 01H A = -2, B = +1 2’s(B) = 1111 1110 +1 = 1111 1111 A = 1111 1110 2’s(B) = 1111 1111 + = 1 1111 1101 A-B = FDH = -3 l Subtraction
  • 15. 5-Sep-02 15 8051 Signed Arith. - Overflow l Overflow can occur from the magnitudes of the signed numbers, which can change the sign bit. l OV Flag is to be checked for error in signed arithmetic l Example A+B, A=+96 (60H), B=+70(46H) A = 0110 0000 B = 0100 0110 + = 1010 0110 = A6H = -90 (wrong) OV = 1, CY=0 96+70 = 166 > +127
  • 16. 5-Sep-02 16 8051 – OV Flag l After arithmetic operations, OV is set if – Carry from D6 to D7 but no carry from D7 – Carry from D7 but no carry from D6 to D7 – These cases indicate a wrong result due to signed arithmetic l After arithmetic operation involving signed numbers, check OV flag, for error detection – Use jb PSW.2 or jnb PSW.2 – PSW.2 = OV
  • 17. 5-Sep-02 17 8051 Logic Instructions l AND – anl dest, source ; dest = dest AND source – Commonly used to mask out (set to 0) a few bits in an operand l OR – orl dest, source ; dest = dest OR source – Commonly used to set a few bits in an operand l XOR – xrl dest, source ; dest = dest XOR source – Commonly used to clear a register, check if two registers have the same value and toggle a few bits l Complement – cpl A ; A = A’ l None of these instructions affect any flags
  • 18. 5-Sep-02 18 8051 – Compare Instruction l CJNE – Cjne dest, source, rel address – Compare dest and source and jump to relative address if not equal – Basically a subtract operation which does not change the operands but affects the CY flag – dest > source è CY=0 – dest < source è CY=1 l Example – Monitor P1 continuosly and – exit if P1=63H Loop: mov A, P1 cjne A, #63, loop Cmp: cjne R5, #80, NEQ EQ: …. ;R5= #80 NEQ: jnc GREAT LESS: … ;R5< #80 GREAT: …. ;R5 > #80
  • 19. 5-Sep-02 19 8051 – Rotate and Swap l Bitwise rotation is required in many apps like serial comm., control etc. l Rotate right – rr A ; rotate right A l Rotate left – rl A ; rotate left A l Rotate right/left with Carry – Use CY in the rotate sequence (9 bit rotate) – rlc A : D7 à CY àD0 – rrc A: D0 à CY à D7 l Swap nibbles – swap A ; swaps D7-D4 with D3-D0 Example for RR mov A, #AAH rr A ; now A = 55H rr A ; now A = 2AH Example for RL mov A, #55H rl A ; now A = AAH rl A ; now A = 54H
  • 20. 5-Sep-02 20 8051 – Single Bit Instructions l Set a bit – set bit_name ;bit = 1 l Clear a bit – clr bit_name ;bit = 0 l Complement a bit – cpl bit_name ;bit = bit’ l Conditional Jump on bit value – jb (jump if bit=1), jnb (jump if bit=0), jbc (jump if bit=1 and clear the bit)
  • 21. 5-Sep-02 21 Bit addressable Regs and Memory l I/O ports (P0 – P3), B, PSW, IP, IE, ACC, SCON and TCON are bit addressable registers (BARs) l The bits of BARs can be referred to as Register.bitnum (P0.1, PSW.2, IE.4 etc) or by their bit address l Bit address is the base address of the register + the bit number – ACC Base address is E0H, hence ACC.1=E1H, ACC.7=E7H – P0, Base address is 80H, hence P0.0=80H, P0.5=84H and so on l 16 bytes of the internal RAM is bit addressable – 20H to 2FH has a bit address of 00H to 7FH – clr 67H ; clear bit D7H of RAM location 2CH – setb 05H ; set bit 5 of RAM location 20H
  • 22. 5-Sep-02 22 Single Bit Operations with CY flag l 8051 has special instructions that directly manipulate CY flag – setb C; clr C; cpl C; mov b,C; mov C,b; jnc, jc, anl C,b; anl C,/b; orl C,b; orl C,/b – anl C, /b ;C = CY AND b’ l Example: Turn ON fan (P2.2) and turn OFF light (P2.3) Fan_on: setb C orl C,P2.2 ;CY = CY OR P2.2 mov P2.2, C;turn on fan if not already ON Light_off: clr C anl C,P2.3 ;CY = CY AND P2.3 mov P2.3,C ;turn off light if not already OFF
  • 23. 5-Sep-02 23 Class-4: Review l Signed and Unsigned arithmetic l Binary and BCD coded numbers l Addition, Subtraction, Multiplication, Division l Signed Number representation and arithmetic l Logic Operations l Rotate and Swap operations l Bit addressable memory and single bit instructions