SlideShare a Scribd company logo
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

More Related Content

What's hot

Programming with 8085
Programming with 8085Programming with 8085
Programming with 8085
Shehrevar Davierwala
 
Chapter 6 - Introduction to 8085 Instructions
Chapter 6 - Introduction to 8085 InstructionsChapter 6 - Introduction to 8085 Instructions
Chapter 6 - Introduction to 8085 Instructions
cmkandemir
 
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
Saumitra Rukmangad
 
Micro task1
Micro task1Micro task1
Micro task1
ChetanShahukari
 
Abc2
Abc2Abc2
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
cmkandemir
 
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 instructions
hepzijustin
 
Class10
Class10Class10
Logical instruction of 8085
Logical instruction of 8085Logical instruction of 8085
Logical instruction of 8085
vishalgohel12195
 
Chapter 8
Chapter 8Chapter 8
Chapter 8
deval patel
 
Instruction set of 8085
Instruction set  of 8085Instruction set  of 8085
Instruction set of 8085
shiji v r
 
Intel 8085 mp
Intel 8085 mpIntel 8085 mp
Intel 8085 mp
Shaswata Chowdhury
 
8085 instruction set
8085 instruction set8085 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)
Basil John
 
Sap 1
Sap 1Sap 1
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 2
Apar Pramod
 
Microprocessor lab manual
Microprocessor lab manualMicroprocessor lab manual
Microprocessor lab manual
Dhaval Shukla
 

What's hot (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
 

Viewers also liked

Class9
Class9Class9
Class8
Class8Class8
Class5
Class5Class5
Chapter 7 8051 programming in c
Chapter 7  8051 programming in cChapter 7  8051 programming in c
Chapter 7 8051 programming in c
Abdelrahman Elewah
 
Microcontroller 8051 and its interfacing
Microcontroller 8051 and its interfacingMicrocontroller 8051 and its interfacing
Microcontroller 8051 and its interfacing
Ankur Mahajan
 
8051 Microcontroller Notes
8051 Microcontroller Notes8051 Microcontroller Notes
8051 Microcontroller NotesDr.YNM
 

Viewers also liked (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
 

Similar to Class4

Lecture 03 Arithmetic Group of Instructions
Lecture 03 Arithmetic Group of InstructionsLecture 03 Arithmetic Group of Instructions
Lecture 03 Arithmetic Group of Instructions
Zeeshan Ahmed
 
6 arithmetic logic inst and prog
6 arithmetic logic inst and prog6 arithmetic logic inst and prog
6 arithmetic logic inst and prog
Channabasappa Kudarihal
 
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.pdf
GafryMahmoud
 
Microprocessor Part 3
Microprocessor    Part  3Microprocessor    Part  3
Microprocessor Part 3
Sajan Agrawal
 
unit5_8051_microcontroller presentation.pdf
unit5_8051_microcontroller presentation.pdfunit5_8051_microcontroller presentation.pdf
unit5_8051_microcontroller presentation.pdf
tchandoo1
 
12 chapter06 math_instructions_fa14
12 chapter06 math_instructions_fa1412 chapter06 math_instructions_fa14
12 chapter06 math_instructions_fa14
John 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 :P
Jathin 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-2
Jathin Kanumuri
 
Microcontroller 8051- soft.ppt
Microcontroller 8051- soft.pptMicrocontroller 8051- soft.ppt
Microcontroller 8051- soft.ppt
steffydean
 
180410227 ae2406-lab-manual-doc
180410227 ae2406-lab-manual-doc180410227 ae2406-lab-manual-doc
180410227 ae2406-lab-manual-doc
homeworkping10
 
Addressing modes
Addressing modesAddressing modes
Addressing modes
karthiga selvaraju
 
The 8051 microcontroller
The 8051 microcontrollerThe 8051 microcontroller
The 8051 microcontroller
PallaviHailkar
 
ARITHMETIC LOGIC UNIT.ppt
ARITHMETIC LOGIC UNIT.pptARITHMETIC LOGIC UNIT.ppt
ARITHMETIC LOGIC UNIT.ppt
RAJESH S
 
Instruction 4.pptx
Instruction 4.pptxInstruction 4.pptx
Instruction 4.pptx
HebaEng
 
INTEL 8085 DATA FORMAT AND INSTRUCTIONS
INTEL 8085 DATA FORMAT AND INSTRUCTIONSINTEL 8085 DATA FORMAT AND INSTRUCTIONS
INTEL 8085 DATA FORMAT AND INSTRUCTIONS
Swapnil 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.pdf
eklavya0304
 
Comp Arithmetic Basic.ppt
Comp Arithmetic Basic.pptComp Arithmetic Basic.ppt
Comp Arithmetic Basic.ppt
skatiarrahaman
 

Similar to 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
 
unit5_8051_microcontroller presentation.pdf
unit5_8051_microcontroller presentation.pdfunit5_8051_microcontroller presentation.pdf
unit5_8051_microcontroller presentation.pdf
 
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
 

Recently uploaded

CW RADAR, FMCW RADAR, FMCW ALTIMETER, AND THEIR PARAMETERS
CW RADAR, FMCW RADAR, FMCW ALTIMETER, AND THEIR PARAMETERSCW RADAR, FMCW RADAR, FMCW ALTIMETER, AND THEIR PARAMETERS
CW RADAR, FMCW RADAR, FMCW ALTIMETER, AND THEIR PARAMETERS
veerababupersonal22
 
6th International Conference on Machine Learning & Applications (CMLA 2024)
6th International Conference on Machine Learning & Applications (CMLA 2024)6th International Conference on Machine Learning & Applications (CMLA 2024)
6th International Conference on Machine Learning & Applications (CMLA 2024)
ClaraZara1
 
RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...
RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...
RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...
thanhdowork
 
The Role of Electrical and Electronics Engineers in IOT Technology.pdf
The Role of Electrical and Electronics Engineers in IOT Technology.pdfThe Role of Electrical and Electronics Engineers in IOT Technology.pdf
The Role of Electrical and Electronics Engineers in IOT Technology.pdf
Nettur Technical Training Foundation
 
Fundamentals of Induction Motor Drives.pptx
Fundamentals of Induction Motor Drives.pptxFundamentals of Induction Motor Drives.pptx
Fundamentals of Induction Motor Drives.pptx
manasideore6
 
Planning Of Procurement o different goods and services
Planning Of Procurement o different goods and servicesPlanning Of Procurement o different goods and services
Planning Of Procurement o different goods and services
JoytuBarua2
 
Building Electrical System Design & Installation
Building Electrical System Design & InstallationBuilding Electrical System Design & Installation
Building Electrical System Design & Installation
symbo111
 
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdfHybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
fxintegritypublishin
 
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdfAKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
SamSarthak3
 
DfMAy 2024 - key insights and contributions
DfMAy 2024 - key insights and contributionsDfMAy 2024 - key insights and contributions
DfMAy 2024 - key insights and contributions
gestioneergodomus
 
Gen AI Study Jams _ For the GDSC Leads in India.pdf
Gen AI Study Jams _ For the GDSC Leads in India.pdfGen AI Study Jams _ For the GDSC Leads in India.pdf
Gen AI Study Jams _ For the GDSC Leads in India.pdf
gdsczhcet
 
Railway Signalling Principles Edition 3.pdf
Railway Signalling Principles Edition 3.pdfRailway Signalling Principles Edition 3.pdf
Railway Signalling Principles Edition 3.pdf
TeeVichai
 
Final project report on grocery store management system..pdf
Final project report on grocery store management system..pdfFinal project report on grocery store management system..pdf
Final project report on grocery store management system..pdf
Kamal Acharya
 
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
MdTanvirMahtab2
 
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Dr.Costas Sachpazis
 
Student information management system project report ii.pdf
Student information management system project report ii.pdfStudent information management system project report ii.pdf
Student information management system project report ii.pdf
Kamal Acharya
 
Water billing management system project report.pdf
Water billing management system project report.pdfWater billing management system project report.pdf
Water billing management system project report.pdf
Kamal Acharya
 
Tutorial for 16S rRNA Gene Analysis with QIIME2.pdf
Tutorial for 16S rRNA Gene Analysis with QIIME2.pdfTutorial for 16S rRNA Gene Analysis with QIIME2.pdf
Tutorial for 16S rRNA Gene Analysis with QIIME2.pdf
aqil azizi
 
Harnessing WebAssembly for Real-time Stateless Streaming Pipelines
Harnessing WebAssembly for Real-time Stateless Streaming PipelinesHarnessing WebAssembly for Real-time Stateless Streaming Pipelines
Harnessing WebAssembly for Real-time Stateless Streaming Pipelines
Christina Lin
 
Recycled Concrete Aggregate in Construction Part III
Recycled Concrete Aggregate in Construction Part IIIRecycled Concrete Aggregate in Construction Part III
Recycled Concrete Aggregate in Construction Part III
Aditya Rajan Patra
 

Recently uploaded (20)

CW RADAR, FMCW RADAR, FMCW ALTIMETER, AND THEIR PARAMETERS
CW RADAR, FMCW RADAR, FMCW ALTIMETER, AND THEIR PARAMETERSCW RADAR, FMCW RADAR, FMCW ALTIMETER, AND THEIR PARAMETERS
CW RADAR, FMCW RADAR, FMCW ALTIMETER, AND THEIR PARAMETERS
 
6th International Conference on Machine Learning & Applications (CMLA 2024)
6th International Conference on Machine Learning & Applications (CMLA 2024)6th International Conference on Machine Learning & Applications (CMLA 2024)
6th International Conference on Machine Learning & Applications (CMLA 2024)
 
RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...
RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...
RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...
 
The Role of Electrical and Electronics Engineers in IOT Technology.pdf
The Role of Electrical and Electronics Engineers in IOT Technology.pdfThe Role of Electrical and Electronics Engineers in IOT Technology.pdf
The Role of Electrical and Electronics Engineers in IOT Technology.pdf
 
Fundamentals of Induction Motor Drives.pptx
Fundamentals of Induction Motor Drives.pptxFundamentals of Induction Motor Drives.pptx
Fundamentals of Induction Motor Drives.pptx
 
Planning Of Procurement o different goods and services
Planning Of Procurement o different goods and servicesPlanning Of Procurement o different goods and services
Planning Of Procurement o different goods and services
 
Building Electrical System Design & Installation
Building Electrical System Design & InstallationBuilding Electrical System Design & Installation
Building Electrical System Design & Installation
 
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdfHybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
 
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdfAKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
 
DfMAy 2024 - key insights and contributions
DfMAy 2024 - key insights and contributionsDfMAy 2024 - key insights and contributions
DfMAy 2024 - key insights and contributions
 
Gen AI Study Jams _ For the GDSC Leads in India.pdf
Gen AI Study Jams _ For the GDSC Leads in India.pdfGen AI Study Jams _ For the GDSC Leads in India.pdf
Gen AI Study Jams _ For the GDSC Leads in India.pdf
 
Railway Signalling Principles Edition 3.pdf
Railway Signalling Principles Edition 3.pdfRailway Signalling Principles Edition 3.pdf
Railway Signalling Principles Edition 3.pdf
 
Final project report on grocery store management system..pdf
Final project report on grocery store management system..pdfFinal project report on grocery store management system..pdf
Final project report on grocery store management system..pdf
 
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
 
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
 
Student information management system project report ii.pdf
Student information management system project report ii.pdfStudent information management system project report ii.pdf
Student information management system project report ii.pdf
 
Water billing management system project report.pdf
Water billing management system project report.pdfWater billing management system project report.pdf
Water billing management system project report.pdf
 
Tutorial for 16S rRNA Gene Analysis with QIIME2.pdf
Tutorial for 16S rRNA Gene Analysis with QIIME2.pdfTutorial for 16S rRNA Gene Analysis with QIIME2.pdf
Tutorial for 16S rRNA Gene Analysis with QIIME2.pdf
 
Harnessing WebAssembly for Real-time Stateless Streaming Pipelines
Harnessing WebAssembly for Real-time Stateless Streaming PipelinesHarnessing WebAssembly for Real-time Stateless Streaming Pipelines
Harnessing WebAssembly for Real-time Stateless Streaming Pipelines
 
Recycled Concrete Aggregate in Construction Part III
Recycled Concrete Aggregate in Construction Part IIIRecycled Concrete Aggregate in Construction Part III
Recycled Concrete Aggregate in Construction Part III
 

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