chapter 2 arm - sonoma state university...what you should know….. before coming to this class...

44
Chapter 2 Number System Embedded Systems with ARM Cortext-M Updated: Tuesday, January 16, 2018

Upload: others

Post on 07-Mar-2020

6 views

Category:

Documents


0 download

TRANSCRIPT

Chapter2NumberSystemEmbeddedSystemswithARMCortext-M

Updated:Tuesday,January16,2018

Whatyoushouldknow…..Beforecomingtothisclass

Decimal Binary Octal Hex0 0000 00 0x01 0001 01 0x12 0010 02 0x23 0011 03 0x34 0100 04 0x45 0101 05 0x56 0110 06 0x67 0111 07 0x78 1000 010 0x89 1001 011 0x910 1010 012 0xA11 1011 013 0xB12 1100 014 0xC13 1101 015 0xD14 1110 016 0xE15 1111 017 0xF

See the review notes!

Whatyoushouldknow…..Beforecomingtothisclass

See the review notes!http://web.sonoma.edu/users/f/farahman/sonoma/courses/es310/resources/Engineering_Notation.pdf

Terminologies

• Bit(1,0)• BYTE

• 8bitsisequivalenttoonebyte

• NIBBLE• 4bitsisequivalenttoonenibble

• WORD• 16bitsisequivalenttooneword

1. 128 bits is equivalent to how many bytes? 2. What is the maximum number that can be represented by 1 bytes?

8-BIT ME!

128/8 = 1628 - 1 = 255

Bit,Byte,Half-word,Word,Double-Word

5

SwitchStateandDigitalRepresentation

0000000100100011=291= 123

0100010101100111=17767=4567

1000100110101011=35243=89AB

1100110111101111=52719=CDEF

In each case we have 16 switches. 1- What Binary/Decimal/Hexadecimal number does each switch represent?2- What is the maximum binary number we can represent using these switches?

Maximum number: 216-1=65536-1=65535=64K in Computer terms!

DataTypes

OneWord

Double-Word

DataRepresentation

• DatarepresentationcanbeDigitalorAnalog• InAnalogrepresentationvaluesarerepresentedoveracontinuous range

• InDigitalrepresentationvaluesarerepresentedoveradiscrete range

• Digitalrepresentationcanbe• Decimal• Binary• Octal• Hexadecimal We need to know how to use

and convert from one to another!

DigitalDataRepresentations:Decimal(base10),Binary(base2),Hex(base16)

Decimal Binary Octal Hex0 0000 00 0x01 0001 01 0x12 0010 02 0x23 0011 03 0x34 0100 04 0x45 0101 05 0x56 0110 06 0x67 0111 07 0x78 1000 010 0x89 1001 011 0x910 1010 012 0xA11 1011 013 0xB12 1100 014 0xC13 1101 015 0xD14 1110 016 0xE15 1111 017 0xF

DigitalDataRepresentations:Powerof2

UsingBinaryRepresentation

• Digitalsystemsarebinary-based• Allsymbolsarerepresentedinbinaryformat• EachsymbolisrepresentedusingBits• Abitcanbeone orzero (on oroffstate)

• ComparingBinaryandDecimalsystems:• InDecimaladigitis[0-9]– base-10• InBinaryadigitis[0-1]– base-2• InDecimaltwo digitscanrepresent[0-99] à102-1• InBinarytwo digitscanrepresent[0-3] à 22-1

BinaryCounting

23 22 21 20

0 0 0 0 00 0 0 1 10 0 1 0 20 0 1 1 30 1 0 0 40 1 0 1 5

2-1 2-2 2-3 2-4

0 0 0 0 01 0 0 0 0.50 1 0 0 0.250 0 1 0 0.1250 1 1 0 0.3751 0 1 0 0.625

1 0 0 1 1 0 1 9.625Binary Representation Decimal Representation

Binary Point

CountinginDifferentNumberingSystems

• Decimal• 0,1,2,3,4,5,6,7,8,9,10,11,12…,19,20,21,…,29,30,…,39….

• Binary• 0,1,10,11,100,101,110,111,1000,….

• Octal• 0,1,2,3,4,5,6,7,10,11,12…,17,20,21,22,23…,27,30,…

• Hexadecimal• 0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,10,…,1F,20,…,2F,30,..…

LearningNumberConversion

Decimal

OctalHexasdecimal

Binary

BCD/ ASCII

Binary-to-DecimalConversionsDecimal

OctalHexasdecimal

Binary

BCD

1001234 27128162121202121 =+++=´+´+´+´+´

11011 à Decimal

11

33

22

11

00012321 ...)...( -

--- ´+´+´+´+´¾¾ ®¾ NN

ConvertbNN bnbnbnbnbnnnnnnn

In the above example: Binary is base-2 (b=2) n0=1n1=1n2=0n3=1n4=1

Q: What is 11011.11In Decimal?

Ans: =27+(1x2-1+ 1x2-2)=27+0.5+0.25=27.75

Decimal-to-BinaryConversionsDecimal

OctalHexasdecimal

Binary

BCD

1__Re02/10__Re12/20__Re22/40__Re42/80__Re82/160__Re162/321__Re322/65

ofmainderofmainderofmainderofmainderofmainderofmainderofmainder

+=+=+=+=+=+=+= 65 à Binary

1000001LSBMSB

What if you are using a calculator? Last one should be “0”

65/2 = 32.50.5 x 2 (base-2) = 1LSB = Least Significant Bit

MSB= Most Significant Bit

Octal/DecimalConversionsDecimal

OctalHexasdecimal

Binary

BCD

10012 2501287643828783 =´+´+´=´+´+´

372 à Decimal

Decimal

OctalHexasdecimal

Binary

BCD4__Re08/41__Re48/332__Re338/266

ofmainderofmainderofmainder

+=+=+=

266 à Octal

11

33

22

11

00012321 ...)...( -

--- ´+´+´+´+´¾¾ ®¾ NN

ConvertbNN bnbnbnbnbnnnnnnn

Binary is base-8 (b=8)

412LSBMSB

What about 372.28? Ans: =250+(2x8-1)=250.25

Decimal-to-Octal:

Hex-to-DecimalConversionsDecimal

OctalHexasdecimal

Binary

BCD

10012 6871616051216151610162 =++=´+´+´

2AF à Decimal

11

33

22

11

00012321 ...)...( -

--- ´+´+´+´+´¾¾ ®¾ NN

ConvertbNN bnbnbnbnbnnnnnnn

Binary is base-16 (b=16)

Remember A=10, F=15

In the above example: Binary is base-16 (b=16) n0=F which is 15n1=A which is 10n2=2

Decimal-to-HexConversions

Decimal

OctalHexasdecimal

Binary

BCD

1__Re016/110__Re116/267__Re2616/423

ofmainderofmainderofmainder

+=+=+=

423à Hex

1A7LSBMSB

214à Hex

13__Re016/136__Re1316/214

ofmainderofmainder

+=+=

D6

Remember 13 in Hex is DLast one should be “0”

Remember 10 in Hex is A

ConvertingfromHex-to-Octal

Decimal

OctalHexasdecimal

Binary

BCD

Decimal

OctalHexasdecimal

Binary

BCD

124Hex à ??Oct

Ans: =124 Hex= 0001 0010 0100= 000 100 100 100=0 4 4 4=444 Oct

Always convert to Binary first and then from binary to Oct.

Counting

• Decimal• 0,1,2,3,4,5,6,7,8,9,10,11,12…,19,20,21,…,29,30,…,39….

• Binary• 0,1,10,11,100,101,110,111,1000,….

• Octal• 0,1,2,3,4,5,6,7,10,11,12…,17,20,21,22,23…,27,30,…

• Hexadecimal• 0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,10,…,1F,20,…,2F,30,..…

BCDAddition

• Anysymbolcanberepresentedbyacode• Example: Binary-Coded-decimal(BCD)

• Eachdigit hasitsownbinarycode• Example:610=0110,1610=00010110(Inbinary16is?)

• BDCnumbers• Theyarebetween0and9• Henceeachdecimalnumberisrepresentedby4bits

• Addeachnumberbetween0-9individually

n 0 1 1 0 ß BCD for 60 1 1 1 ß BCD for 7_________________1 1 0 1 ß 13 but invalid! 0 1 1 0 ß Add 6 to correct

________________________0 0 0 1 0 0 1 1 ß BDC for 13!!

ASCIICode• Example:AmericanStandardCodeforInformationInterchange (ASCII)

• Eachsymbolisrepresentedbyaseven-bitcode(Howmanysymbolscanberepresented?– 127)

• Example:A=1000001=41inHex,1=0110000=31inHex,$=0100100=24inHex(Whatis“DAD”inASCII?)

Look at the ASCII code listing – Don’t memorize!

AmericanStandardCodeforInformationInterchange

Dec Hex Char Dec Hex Char Dec Hex Char Dec Hex Char0 00 NUL 32 20 SP 64 40 @ 96 60 ‘1 01 SOH 33 21 ! 65 41 A 97 61 a2 02 STX 34 22 " 66 42 B 98 62 b3 03 ETX 35 23 # 67 43 C 99 63 c4 04 EOT 36 24 $ 68 44 D 100 64 d5 05 ENQ 37 25 % 69 45 E 101 65 e6 06 ACK 38 26 & 70 46 F 102 66 f7 07 BEL 39 27 ’ 71 47 G 103 67 g8 08 BS 40 28 ( 72 48 H 104 68 h9 09 HT 41 29 ) 73 49 I 105 69 i10 0A LF 42 2A * 74 4A J 106 6A j11 0B VT 43 2B + 75 4B K 107 6B k12 0C FF 44 2C , 76 4C L 108 6C l13 0D CR 45 2D - 77 4D M 109 6D m14 0E SO 46 2E . 78 4E N 110 6E n15 0F SI 47 2F / 79 4F O 111 6F o16 10 DLE 48 30 0 80 50 P 112 70 p17 11 DC1 49 31 1 81 51 Q 113 71 q18 12 DC2 50 32 2 82 52 R 114 72 r19 13 DC3 51 33 3 83 53 S 115 73 s20 14 DC4 52 34 4 84 54 T 116 74 t21 15 NAK 53 35 5 85 55 U 117 75 u22 16 SYN 54 36 6 86 56 V 118 76 v23 17 ETB 55 37 7 87 57 W 119 77 w24 18 CAN 56 38 8 88 58 X 120 78 x25 19 EM 57 39 9 89 59 Y 121 79 y26 1A SUB 58 3A : 90 5A Z 122 7A z27 1B ESC 59 3B ; 91 5B [ 123 7B {28 1C FS 60 3C < 92 5C \ 124 7C |29 1D GS 61 3D = 93 5D ] 125 7D }30 1E RS 62 3E > 94 5E ^ 126 7E ~31 1F US 63 3F ? 95 5F _ 127 7F DEL

SavingASCIICharacters

24

Memory Address

Memory Content Letter

str + 12 ® 0x00 \0str + 11 ® 0x79 ystr + 10 ® 0x6C lstr + 9 ® 0x62 bstr + 8 ® 0x6D mstr + 7 ® 0x65 estr + 6 ® 0x73 sstr + 5 ® 0x73 sstr + 4 ® 0x41 Astr + 3 ® 0x20 spacestr + 2 ® 0x4D Mstr + 1 ® 0x52 R

str ® 0x41 A

charstr[13]=“ARMAssembly”;//Thelengthhastobeatleast13//eventhoughithas12letters.The//NULLterminatorshouldbeincluded.

ConverttoUpperCase

25

A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

41 42 43 44 45 46 47 48 49 4A 4B 4C 4D 4E 4F 50 51 52 53 54 55 56 57 58 59 5A

a b c d e f g h i j k l m n o p q r s t u v w x y z

61 62 63 64 65 66 67 68 69 6A 6B 6C 6D 6E 6F 70 71 72 73 74 75 76 77 78 79 7A

‘a’ – ‘A’ = 0x61 – 0x41 = 0x20 = 32

DigitalArithmetic

BinaryArithmetic• Digitalcircuitsarefrequentlyusedforarithmeticoperations

• Fundamentalarithmeticoperationsonbinarynumbersanddigitalcircuitswhichperformarithmeticoperationswillbeexamined.

• Binarynumbersareaddedlikedecimalnumbers.

• Indecimal,whennumberssummorethan9acarryresults.

• Inbinarywhennumberssummorethan1acarrytakesplace.

• Additionisthebasicarithmeticoperationusedbydigitaldevicestoperformsubtraction,multiplication,anddivision.

Binary Arithmetic

Operations (Adding, Subtracting, Inverting)

Representation Positive and Negative Numbers

Binary Numbers

If the numbers are unsigned and positive add them as follow:

1 + 0 = 1 with carry of 01 + 1 = 0 with carry of 11 + 1 + 1 = 1 with carry of 1

0 - 0 = 0 with borrow 00 - 1 = 1 with borrow 11 - 0 = 1 with borrow 01 + 1 = 0 with borrow 0

AddingUnsignedNumbers

• Examples:X 190 10111110

Y 141 10001101

X+Y 331 101001011

X 127 1111111

Y 63 111111

X+Y 190 10111110

X 170 10101010

Y 85 1010101

X+Y 255 11111111

AddingUnsignedNumbers

• Examples:X 190 10111110

Y 141 10001101

X+Y 331 101001011

X 127 1111111

Y 63 111111

X+Y 190 10111110

X 170 10101010

Y 85 1010101

X+Y 255 11111111

RepresentingSignedNumbers

• Sinceitisonlypossibletoshowmagnitudewithabinarynumber,thesign(+or-)isshownbyaddinganextra“sign”bit.

• Asignbitof0indicatesapositivenumber.

• Asignbitof1indicatesanegativenumber.

• The2’scomplement systemisthemostcommonlyusedwaytorepresentsignednumbers.

0 1 1 1

1 0 0 1

à

à

à

à

à

0 0 0 0

1 1 1 1

+7

0

-1

-7

1 0 0 0-8

Sign-bit

Magnitude bits

Examples of 2’s complement representation

+2^0+2^1+2^2-2^3+2 +1+4-8

Comparison– presenting+/- Numbers

31

00000/10000

0 12

34

5

6

7

8 01000

0000100010

00011

00100

00101

00110

00111

9

10

11

12

131415

01001

01010

01011

01100

0110101110

01111

-8

-7

-6

-5

-4-3

-2 -1

10111

10110

10101

10100

1001110010

10001

-15-14-13

-12

-11

-10

-9

11000

1111111110

11101

11100

11011

11010

11001

00000/11111

0 12

34

5

6

7

8 01000

0000100010

00011

00100

00101

00110

00111

9

10

11

12

131415

01001

01010

01011

01100

0110101110

01111

-8

-7

-6

-5

-4-3

-2 -1

11000

11001

11010

11011

1110011101

11110

-15-14-13

-12

-11

-10

-9

10111

1000010001

10010

10011

10100

10101

10110

-16

00000

0 12

34

5

6

7

8 01000

0000100010

00011

00100

00101

00110

00111

9

10

11

1213

1415

01001

01010

01011

01100

0110101110

01111

-8

-7

-6

-5

-4-3

-2 -1

11001

11010

11011

11100

1110111110

11111

10000

-15-14

-13-12

-11

-10

-9

11000

1000110010

10011

10100

10101

10110

10111

One’scomplementrepresentationNegative=invertallbitsofapositive

Two’sComplementrepresentationTC=invertallbits,thenplus1

Signedmagnituderepresentation0 =positive1 =negative

RepresentingSignedNumbersConvertingto2’sComplement

• Inordertochangeabinarynumberto2’scomplementitmustfirstbechangedto1’scomplement.

• Toconvertto1’scomplement,simplychangeeachbittoitscomplement(opposite).

• Toconvert1’scomplementto2’scomplement add1tothe1’scomplement.

• Apositivenumberistruebinarywith0inthesignbit.

• Anegativenumberisin2’scomplementformwith1inthesignbit.

• Abinarynumbercanbenegated bytakingthe2’scomplementofit.

For example: +9 à 01001 (sign bit = 0, indicating +)2’s complement of 9 à01001 à 10110 10110 + 1 à 10111 = -9

This is The sign

BIT

When the sign-bit is zero à Positive number If the sign-bit is set à Negative number

Remember: 2’s complement is just a conventional way of representing signed

numbers in digital arithmetic – Don’t ask why!

2’sComplementRepresentation

• AssumingN+1 bitsrepresentinga2’sComplement(thatisrepresentingthenumberwithNbitsandonebitisdedicatedtoindicatethesign):

• Largestpositivenumberwillbe2N-1• Smallestsignednumber(largestnegativenumber)willbe-2N

• Totalnumbers(includingzero)thatcanberepresentedwillbe2N+1

For example:Assume 3+1 bitsLargest pos. number will be 0111= +7Smallest number will be 1000 = -8Remember: 1111= -1Zero is represented by 0000 = Zero

For example:Assume 6+1 bitsLargest pos. number will be ??Smallest signed number will be ??Zero is represented by ??

0 111 111 = 63

1 000 000 = -64

0 000 000 = 0

MoreExamplesInteger

2's ComplementSigned Unsigned

5 5 0000 0101

4 4 0000 0100

3 3 0000 0011

2 2 0000 0010

1 1 0000 0001

0 0 0000 0000

-1 255 1111 1111

-2 254 1111 1110

-3 253 1111 1101

-4 252 1111 1100

-5 251 1111 1011

Integer2's Complement

Signed

7 0111

6 0110

5 0101

4 0100

3 0011

2 0010

1 0001

0 0000

-1 1111

-2 1110

-3 1101

-4 1100

-5 1011

-6 1010

-7 1001

-8 1000

Remember: Always know how many bits are provided!

2’sComplementRepresentationInteger

2's ComplementSigned

7 0111

6 0110

5 0101

4 0100

3 0011

2 0010

1 0001

0 0000

-1 1111

-2 1110

-3 1101

-4 1100

-5 1011

-6 1010

-7 1001

-8 1000

FlagsCarry,Borrow,Overflow

• TheresultofaArithmeticOperationinamicroprocessorcanoftenbeexpressedbyflags

• Carry:4+7=1carry1• Borrow4- 7=7Borrow1• Overflow8+9=7overflow1(ifonlyonedigitcanbeused)

Flags• Whenaddingtwounsignednumbersinann-bitsystem,a

carryoccursiftheresultislargerthanthemaximumunsignedinteger thatcanberepresented(i.e. 2" − 1).

• Whensubtractingtwounsignednumbers,borrowoccursiftheresultisnegative,smallerthanthesmallestunsignedintegerthatcanberepresented(i.e. 0).

• OnARMCortex-M3 processors,thecarryflagandtheborrowflagarephysicallythesameflagbitinthestatusregister.• Foranunsignedsubtraction,Carry=NOTBorrow

Carry/borrowflagbitforunsignednumbers

38

16

00000

0 12

34

5

6

7

8 01000

0000100010

00011

00100

00101

00110

00111

9

10

11

1213

1415

01001

01010

01011

01100

0110101110

01111

24

25

26

27

2829

30 31

11001

11010

11011

11100

1110111110

11111

10000

1718

1920

21

22

23

11000

1000110010

10011

10100

10101

10110

10111

Carry+6

28 + 6

Acarryoccurswhenadding28and6

Ifthetraversecrossestheboundarybetween0and𝟐𝒏 − 𝟏,thecarryflagissetonadditionandisclearedonsubtraction.

• Carryflag=1,indicatingcarryhasoccurredonunsignedaddition.

• Carryflagis1 becausetheresultcrossestheboundarybetween31 and0.

1 1 1 0 00 0 1 1 0+

1 0 0 0 1 0

1 1 1 0 0Carry

5-bit resultExtra bit is discarded.

286+2

NOT32!

Carry/borrowflagbitforunsignednumbers

39

16

00000

0 12

34

5

6

7

8 01000

0000100010

00011

00100

00101

00110

00111

9

10

11

1213

1415

01001

01010

01011

01100

0110101110

01111

24

25

26

27

2829

30 31

11001

11010

11011

11100

1110111110

11111

10000

1718

1920

21

22

23

11000

1000110010

10011

10100

10101

10110

10111

Carry

-5

3 - 5

Aborrowoccurswhensubtracting5from3.

Ifthetraversecrossestheboundarybetween0and𝟐𝒏 − 𝟏,thecarryflagissetonadditionandisclearedonsubtraction.

• Carryflag=0,indicatingborrowhasoccurredonunsignedsubtraction.

• Forsubtraction,carry=NOTborrow.

0 0 0 1 10 0 1 0 1-

1 1 1 1 0

1 1 1 0 0Borrow

5-bit result

35-30

ArithmeticOperationsusing2’sComplement

• Inverting• Apositivenumbertoanegativenumber• Anegativenumbertoapositivenumber

• Eithercasejusttakethe2’scomplement

• AddingAandBà A+B (assumingNbitsrepresentthemagnitudeandonebitisdedicatedasthesign-bit)

• SubtractingBfromAà A-B• Justtakethe2’sComplementofB• AddAandB(A+B)

• NOTE: WhenaPositiveandaNegativenumberareaddedtogether,theoverflowwillbeasignedoverflowanditisok!

X 180 10110100

Y 85 01010101

X+Y 265 100001001

X 6 0110

Y -3 1101

X-Y 3 1 0011

X 4 0100

Y -7 1001

X-Y -3 1101

X -3 1101

Y -6 1010

X+Y -9 1 0111

X 7 0 111

Y 7 0 111

X+Y 14 1 110

Magnitude Overflow:(max unsigned number that can be represented using 8 bits is 255)

The carry will be ignored

Overflow:(max signed number that can be represented using 4 bits is -8)

Overflow:(max signed number that can be represented using 4 bits is 7)

Note the value is negative

X -4 11100

Y -9 10111

X+Y -13 1 10011

The overflow can be discarded If Carry into and out of the sign bit are the same

BCDAddition

• BDCnumbers• Theyarebetween0and9• Henceeachdecimalnumberisrepresentedby4bits

• Addeachnumberbetween0-9individually

n 0 1 1 0 ß BCD for 60 1 1 1 ß BCD for 7_________________1 1 0 1 ß 13 but invalid! 0 1 1 0 ß Add 6 to correct

________________________0 0 0 1 0 0 1 1 ß BDC for 13!!

BCDAddition– AnotherExample

• BDCnumbers• Theyarebetween0and9• Henceeachdecimalnumberisrepresentedby4bits

• Addeachnumberbetween0-9individually

10 1 0 1 1 0 0 1 ß BCD for 590 0 1 1 1 0 0 0 ß BCD for 38________________1 0 0 1 0 0 0 1 ß 91 but invalid!

0 1 1 0 ß Add 6 ________________________

1 0 0 1 0 1 1 1 ß BDC for 97!!

HexArithmetic• Addition ofHexnumbersissimilartodecimaladdition

• Rememberthelargestvalueis16(F)andNOT9!

• Carrya1ifthenumberislargerthanF

• Usethefollowingsteps• Addthehexdigitsindecimal.• Ifthesumis15orlessexpressitdirectlyinhexdigits.

• Ifthesumisgreaterthan15,subtract16andcarry1tothenextposition.

58+ 4B= A3

3AF+ 23C= 5EB

8+B=8+(11)=19à19-16=3,

Carry 1

9+ 8

In Hex=11

9+ 8

In Decimal =1717-16 =1

In Hex = 1116

HexArithmetic• Subtraction ofhexnumbersfollowsasimilarmethodasbinarynumbers

• Makesureyoutakethe2’scomplementofthenegativehexnumber

• Method1:• SubtractthenumberfromFFFF…• Writetheresultsinhex• Addonetothefinalanswer• Forexample:2’scomplementof3A5is:

• FFF-3A5=C5Aà C5A+1=C5B

• Method2:• ConvertthenumbertoBinary• Takethe2’scomplement• Convertbacktheresultsintohexvalue

• Simplyaddthevaluestogether

592- 3A5à592+ C5B

= 11ED

67F- 2A4à67F+ D5C= 3DB

5+C=2+(12)=17à17-16=1,

Carry 1 –Must be

discarded!

FFF- 3A5=(15-3),(15-10),(15-5) = 12, 5, 10 à

C5A+1=C5B

F+C=(15)+(12)=27à27-16=11 or B,

Carry 1