Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Chapter-2 ARM

Download as pdf or txt
Download as pdf or txt
You are on page 1of 44

Chapter

2
Number System
Embedded Systems with ARM Cortext-M
Updated: Tuesday, January 16, 2018
What you should know…..
Before coming to this class
Decimal Binary Octal Hex
0 0000 00 0x0
1 0001 01 0x1
2 0010 02 0x2
3 0011 03 0x3
4 0100 04 0x4
5 0101 05 0x5
6 0110 06 0x6
7 0111 07 0x7
8 1000 010 0x8
9 1001 011 0x9
10 1010 012 0xA
11 1011 013 0xB
12 1100 014 0xC See the review
13
14
1101
1110
015
016
0xD
0xE notes!
15 1111 017 0xF
What you should know…..
Before coming to this class

See the review notes!


http://web.sonoma.edu/users/f/farahman/sonoma/courses/es310/resources/Engineering_Notation.pdf
Terminologies
• Bit (1,0) 8-BIT
• BYTE ME!

• 8 bits is equivalent to one byte


• NIBBLE
• 4 bits is equivalent to one nibble
• WORD
• 16 bits is equivalent to one word

1. 128 bits is equivalent to how many bytes? 128/8 = 16


2. What is the maximum number that can be represented by 1 bytes? 28 - 1 = 255
Bit, Byte, Half-word, Word, Double-Word

5
Switch State and Digital Representation

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?
0000000100100011=291= 123

0100010101100111=17767=4567

1000100110101011=35243=89AB

1100110111101111=52719=CDEF

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


Data Types

One Word

Double-Word
Data Representation
• Data representation can be Digital or Analog
• In Analog representation values are represented over a
continuous range
• In Digital representation values are represented over a
discrete range
• Digital representation can be
• Decimal
• Binary
• Octal
• Hexadecimal We need to know how to use
and convert from one to another!
Digital Data Representations:
Decimal (base 10), Binary (base 2), Hex(base 16)
Decimal Binary Octal Hex
0 0000 00 0x0
1 0001 01 0x1
2 0010 02 0x2
3 0011 03 0x3
4 0100 04 0x4
5 0101 05 0x5
6 0110 06 0x6
7 0111 07 0x7
8 1000 010 0x8
9 1001 011 0x9
10 1010 012 0xA
11 1011 013 0xB
12 1100 014 0xC
13 1101 015 0xD
14 1110 016 0xE
15 1111 017 0xF
Digital Data Representations:
Power of 2
Using Binary Representation
• Digital systems are binary-based
• All symbols are represented in binary format
• Each symbol is represented using Bits
• A bit can be one or zero (on or off state)
• Comparing Binary and Decimal systems:
• In Decimal a digit is [0-9] – base-10
• In Binary a digit is [0-1] – base-2
• In Decimal two digits can represent [0-99] à102-1
• In Binary two digits can represent [0-3] à 22-1
Binary Counting
23 22 21 20 2-1 2-2 2-3 2-4
0 0 0 0 0 0 0 0 0 0
0 0 0 1 1 1 0 0 0 0.5
0 0 1 0 2 0 1 0 0 0.25
0 0 1 1 3 0 0 1 0 0.125
0 1 0 0 4 0 1 1 0 0.375
0 1 0 1 5 1 0 1 0 0.625

Binary Representation Decimal Representation


1 0 0 1 1 0 1 9.625

Binary Point
Counting in Different Numbering Systems

• 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,..…
Learning Number Conversion

Decimal

Hexasdecimal Octal

Binary

BCD/ ASCII
Binary-to-Decimal Conversions
11011 à Decimal
Decimal

Hexasdecimal Octal

1´ 2 4 + 1´ 23 + 0 ´ 2 2 + 1´ 21 + 1´ 20 = 16 + 8 + 2 + 1 = 2710
Binary

BCD

¾¾® n0 ´ b 0 + n1 ´ b1 + n2 ´ b 2 + n3 ´ b 3 ... + nN -1 ´ b N -1
(nN -1nN - 2 ...n3n2 n1n0 )b ¾Convert

In the above example: Q: What is 11011.11


Binary is base-2 (b=2) In Decimal?
n0=1
n1=1 Ans: =27+(1x2-1+ 1x2-2)
n2=0 =27+0.5+0.25
n3=1 =27.75
n4=1
Decimal-to-Binary Conversions
Decimal

65 / 2 = 32 + Re mainder _ of _ 1 65 à Binary Hexasdecimal Octal

32 / 2 = 16 + Re mainder _ of _ 0 Binary

16 / 2 = 8 + Re mainder _ of _ 0
8 / 2 = 4 + Re mainder _ of _ 0 1000001
BCD

4 / 2 = 2 + Re mainder _ of _ 0
MSB LSB
2 / 2 = 1 + Re mainder _ of _ 0
1 / 2 = 0 + Re mainder _ of _ 1

What if you are using a calculator?


Last one should be “0”
65/2 = 32.5
LSB = Least Significant Bit
0.5 x 2 (base-2) = 1
MSB= Most Significant Bit
Octal/Decimal Conversions
372 à Decimal
Decimal

Binary is base-8 (b=8) Hexasdecimal Octal

3 ´ 82 + 7 ´ 81 + 2 ´ 80 = 3 ´ 64 + 7 ´ 8 + 2 ´1 = 25010
Binary

BCD

¾¾® n0 ´ b 0 + n1 ´ b1 + n2 ´ b 2 + n3 ´ b 3 ... + nN -1 ´ b N -1
(nN -1nN - 2 ...n3n2 n1n0 )b ¾Convert
What about 372.28? Ans: =250+(2x8-1)=250.25

Decimal-to-Octal:
266 à Octal Decimal

266 / 8 = 33 + Re mainder _ of _ 2
Hexasdecimal Octal

33 / 8 = 4 + Re mainder _ of _ 1
412
Binary

4 / 8 = 0 + Re mainder _ of _ 4 BCD

MSB LSB
Hex-to-Decimal Conversions
2AF à Decimal
Decimal

Binary is base-16 (b=16) Hexasdecimal Octal

2 ´16 2 + 10 ´161 + 15 ´16 0 = 512 + 160 + 16 = 68710


Binary

BCD

¾¾® n0 ´ b 0 + n1 ´ b1 + n2 ´ b 2 + n3 ´ b 3 ... + nN -1 ´ b N -1
(nN -1nN - 2 ...n3n2 n1n0 )b ¾Convert

In the above example:


Binary is base-16 (b=16)
Remember A=10, F=15 n0=F which is 15
n1=A which is 10
n2=2
Decimal-to-Hex Conversions
Decimal

423à Hex Hexasdecimal Octal

Binary

423 / 16 = 26 + Re mainder _ of _ 7
26 / 16 = 1 + Re mainder _ of _ 10 1A7
BCD

1 / 16 = 0 + Re mainder _ of _ 1
MSB LSB
Remember 10 in Hex is A
214à Hex
214 / 16 = 13 + Re mainder _ of _ 6
13 / 16 = 0 + Re mainder _ of _ 13 D6

Remember 13 in Hex is D
Last one should be “0”
Converting from Hex-to-Octal
Decimal

124Hex à ??Oct Hexasdecimal Octal

Binary

BCD

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


Decimal

Ans: =124 Hex


Hexasdecimal Octal

= 0001 0010 0100


= 000 100 100 100 Binary

=0 4 4 4
=444 Oct
BCD
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,..…
BCD Addition
n 0 1 1 0 ß BCD for 6
0 1 1 1 ß BCD for 7
• Any symbol can be represented by a code _________________
• Example: Binary-Coded-decimal (BCD) 1 1 0 1 ß 13 but invalid!
• Each digit has its own binary code 0 1 1 0 ß Add 6 to correct
• Example: 610=0110, 1610=0001 0110 (In binary 16 is?)
________________________
• BDC numbers 0 0 0 1 0 0 1 1 ß BDC for 13!!
• They are between 0 and 9
• Hence each decimal number is represented by 4 bits

• Add each number between 0-9 individually


Dec Hex Char Dec Hex Char Dec Hex Char Dec Hex Char
0 00 NUL 32 20 SP 64 40 @ 96 60 ‘

ASCII Code
1 01 SOH 33 21 ! 65 41 A 97 61 a
2 02 STX 34 22 " 66 42 B 98 62 b
3 03 ETX 35 23 # 67 43 C 99 63 c
4 04 EOT 36 24 $ 68 44 D 100 64 d
5 05 ENQ 37 25 % 69 45 E 101 65 e

• Example: American Standard Code for 6


7
06
07
ACK
BEL
38
39
26
27
&

70
71
46
47
F
G
102
103
66
67
f
g
Information Interchange (ASCII) 8
9
08
09
BS
HT
40
41
28
29
(
)
72
73
48
49
H
I
104
105
68
69
h
i
• Each symbol is represented by a seven- 10 0A LF 42 2A * 74 4A J 106 6A j
bit code (How many symbols can be 11
12
0B
0C
VT
FF
43
44
2B
2C
+
,
75
76
4B
4C
K
L
107
108
6B
6C
k
l
represented? – 127) 13 0D CR 45 2D - 77 4D M 109 6D m
• Example: A=100 0001 = 41 in Hex, 1=011 14
15
0E
0F
SO
SI
46
47
2E
2F
.
/
78
79
4E
4F
N
O
110
111
6E
6F
n
o
0000 = 31 in Hex, $=010 0100 = 24 in Hex 16 10 DLE 48 30 0 80 50 P 112 70 p
(What is “DAD” in ASCII?) 17 11 DC1 49 31 1 81 51 Q 113 71 q
18 12 DC2 50 32 2 82 52 R 114 72 r
19 13 DC3 51 33 3 83 53 S 115 73 s
American 20 14 DC4 52 34 4 84 54 T 116 74 t
21 15 NAK 53 35 5 85 55 U 117 75 u
Standard 22 16 SYN 54 36 6 86 56 V 118 76 v
Code for 23 17 ETB 55 37 7 87 57 W 119 77 w
24 18 CAN 56 38 8 88 58 X 120 78 x
Information 25 19 EM 57 39 9 89 59 Y 121 79 y
Interchange 26 1A SUB 58 3A : 90 5A Z 122 7A z
27 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 }
Look at the ASCII code listing – Don’t memorize! 30 1E RS 62 3E > 94 5E ^ 126 7E ~
31 1F US 63 3F ? 95 5F _ 127 7F DEL
Saving ASCII Characters
Memory Memory
Letter
Address Content
str + 12 ® 0x00 \0
str + 11 ® 0x79 y
str + 10 ® 0x6C l
char str[13] = “ARM Assembly”; str + 9 ® 0x62 b
// The length has to be at least 13 str + 8 ® 0x6D m
str + 7 ® 0x65 e
// even though it has 12 letters. The str + 6 ® 0x73 s
// NULL terminator should be included. str + 5 ® 0x73 s
str + 4 ® 0x41 A
str + 3 ® 0x20 space
str + 2 ® 0x4D M
str + 1 ® 0x52 R
str ® 0x41 A

24
Convert to Upper Case
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

25
Digital Arithmetic
Binary Numbers

Binary Arithmetic Binary Arithmetic

• Digital circuits are frequently used for


arithmetic operations
• Fundamental arithmetic operations on Representation Operations
Positive and Negative Numbers (Adding, Subtracting, Inverting)
binary numbers and digital circuits which
perform arithmetic operations will be
examined. If the numbers are unsigned and
• Binary numbers are added like decimal positive add them as follow:
numbers.
1+0=1 with carry of 0
• In decimal, when numbers sum more 1+1=0 with carry of 1
than 9 a carry results.
1+1+1=1 with carry of 1
• In binary when numbers sum more than
1 a carry takes place. 0 -0=0 with borrow 0
• Addition is the basic arithmetic operation 0 -1=1 with borrow 1
used by digital devices to perform 1-0=1 with borrow 0
subtraction, multiplication, and division. 1+1=0 with borrow 0
Adding Unsigned Numbers
• 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


Adding Unsigned Numbers
• 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


Representing Signed Numbers
• Since it is only possible to show Magnitude bits

magnitude with a binary number, +7 à 0 1 1 1


the sign (+ or -) is shown by adding 0 0 0 0
an extra “sign” bit. 0 à

• A sign bit of 0 indicates a positive -1 à 1 1 1 1


number. -7 à 1 0 0 1
• A sign bit of 1 indicates a negative
number. -8 à 1 0 0 0

• The 2’s complement system is the Sign-bit

most commonly used way to Examples of 2’s complement representation


represent signed numbers. -8 +4 +2 +1
-2^3 +2^2 +2^1 +2^0
Comparison – presenting +/- Numbers 00000/
00000/
11110 11111 00001
10001 10000 00001
11101 00010 11111 00000 00001
10010 00010 11110 00010
10011 00011 11100 00011 11101 00011
0 1 -1 0 1 0
-1 11011 -2 2 00100 -1 1
10100 -2 2 00100 11100 -2 2 00100
-3 3 -3 3 -3 3
10101 00101 11010 -4 4 00101 11011 00101
-4 4 -4 4
-5 -5 5 -5
5 11001 00110 5
10110 00110 11010 00110
-6 6 -6 6 -6 6
10111 00111 11000 00111 11001 00111
-7 7 -7 7 -7 7

-8 01000 11000 -8 01000


11000 -8 8 01000 10111 8 8

9 -9 9 -9 9
-9 10111 01001
11001 01001 10110 01001
-10 10 -10 10
-10 10
11010 01010 10101 01010 10110 01010
-11 11 -11 11 -11 11

10100 12 01011 10101 -12 12 01011


11011 -12 12 01011 -12
-13 13
-13 13 -13 13
11100 01100 10011 01100 10100 -14 14 01100
-14 14 -14 14 -15 -16 15
-15 15 -15 15
11101 01101 10010 01101 10011 01101
11110 01110 10001 01110 10010 01110
11111 01111 10000 01111 10001 10000 01111

Signed magnitude One’s complement Two’s Complement


representation representation representation
0 = positive Negative = invert all TC = invert all bits,
1 = negative bits of a positive then plus 1

31
Representing Signed Numbers
Converting to 2’s Complement
For example:
• In order to change a binary number to 2’s +9 à 01001 (sign bit = 0, indicating +)
complement it must first be changed to 1’s 2’s complement of 9 à
complement.
01001 à 10110
• To convert to 1’s complement, simply change each
bit to its complement (opposite). 10110 + 1 à 10111 = -9
• To convert 1’s complement to 2’s complement add 1
to the 1’s complement.
This is
• A positive number is true binary with 0 in the sign The sign
bit. BIT
• A negative number is in 2’s complement form with When the sign-bit is zero à Positive number
1 in the sign bit. If the sign-bit is set à Negative number
• A binary number can be negated by taking the 2’s
complement of it. Remember: 2’s complement is just a
conventional way of representing signed
numbers in digital arithmetic – Don’t ask why!
2’s Complement Representation
For example:
• Assuming N+1 bits representing Assume 3+1 bits
a 2’s Complement (that is Largest pos. number will be 0111= +7
Smallest number will be 1000 = -8
representing the number with N Remember: 1111= -1
bits and one bit is dedicated to Zero is represented by 0000 = Zero

indicate the sign):


For example:
• Largest positive number will be 2N-1 Assume 6+1 bits
• Smallest signed number (largest negative Largest pos. number will be ??
number) will be -2N Smallest signed number will be ??
• Total numbers (including zero) that can be Zero is represented by ??
0 111 111 = 63
represented will be 2N+1
1 000 000 = -64
0 000 000 = 0
More Examples
Integer Integer
2's Complement 2's Complement
Signed Signed Unsigned
7 0111
5 5 0000 0101
6 0110
4 4 0000 0100
5 0101
3 3 0000 0011
4 0100
3 0011 2 2 0000 0010

2 0010 1 1 0000 0001

1 0001 0 0 0000 0000


0 0000 -1 255 1111 1111
-1 1111
-2 254 1111 1110
-2 1110
-3 253 1111 1101
-3 1101
-4 252 1111 1100
-4 1100
-5 251 1111 1011
-5 1011
-6 1010
-7 1001 Remember: Always know how
-8 1000 many bits are provided!
2’s Complement Representation
Integer
2'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
Flags
Carry, Borrow, Overflow
• The result of a Arithmetic Operation in a microprocessor can often be
expressed by flags
• Carry: 4 + 7 = 1 carry 1
• Borrow 4 - 7 = 7 Borrow 1
• Overflow 8 + 9 = 7 overflow 1 (if only one digit can be used)
Flags
• When adding two unsigned numbers in an n-bit system, a
carry occurs if the result is larger than the maximum
unsigned integer that can be represented (i.e. 2" − 1).

• When subtracting two unsigned numbers, borrow occurs if


the result is negative, smaller than the smallest unsigned
integer that can be represented (i.e. 0).

• On ARM Cortex-M3 processors, the carry flag and the


borrow flag are physically the same flag bit in the status
register.
• For an unsigned subtraction, Carry = NOT Borrow
Carry/borrow flag bit for unsigned numbers
If the traverse crosses the boundary between 0 and 𝟐𝒏 − 𝟏, the carry flag is set on
addition and is cleared on subtraction.
11111 00000 00001
11110 00010
11101 00011 Carry 1 1 1 0 0
31 0 1
11100 2 00100
29
30
3 1 1 1 0 0 28
11011 28 4 00101

11010
27
+6
Carry
5
00110
+ 0 0 1 1 0 + 6
28 + 6
11001
26 6
00111
1 0 0 0 1 0 2
25 7
NOT 32!
11000 24 8 01000
Extra bit is 5-bit result
23 9
10111 01001 discarded.
22 10
10110 01010
21 11

10101 20 12 01011 • Carry flag = 1, indicating carry has occurred


19 13
10100 18
17 16 15
14 01100 on unsigned addition.
10011
01110
01101 • Carry flag is 1 because the result crosses
10010
10001 10000 01111 the boundary between 31 and 0.
A carry occurs when adding 28 and 6

38
Carry/borrow flag bit for unsigned numbers
If the traverse crosses the boundary between 0 and 𝟐𝒏 − 𝟏, the carry flag is set on
addition and is cleared on subtraction.
11111 00000 00001
11110 00010
11101 00011

11100
31 0 1
2 00100
Borrow 1 1 1 0 0
30
29 3
11011 28 -5
4 00101 0 0 0 1 1 3
Carry 3-5
11010
27 5
00110 - 0 0 1 0 1 - 5
26 6
11001
25 7
00111 1 1 1 1 0 30
11000 24 8 01000

23 9
10111 01001 5-bit result
22 10
10110 01010
21 11

10101 20 12 01011 • Carry flag = 0, indicating borrow has


19 13
10100 18
17 15
14 01100 occurred on unsigned subtraction.
16
10011 01101 • For subtraction, carry = NOT borrow.
10010 01110
10001 10000 01111

A borrow occurs when subtracting 5 from 3.

39
Arithmetic Operations using 2’s Complement
• Inverting X 180 10110100
Magnitude Overflow:
• A positive number to a negative number Y 85 01010101
(max unsigned number
that can be represented
• A negative number to a positive number using 8 bits is 255)
X+Y 265 100001001

• Either case just take the 2’s complement


X 6 0110
• Adding A and Bà A+B (assuming N bits represent
The carry will be ignored Y -3 1101
the magnitude and one bit is dedicated as the sign-
bit) X-Y 3 1 0011

X 4 0100
• Subtracting B from A à A-B Note the value is negative
Y -7 1001
• Just take the 2’s Complement of B
X-Y -3 1101
• Add A and B (A+B)

• NOTE: When a Positive and a Negative number are Overflow: X -3 1101


added together, the overflow will be a signed (max signed number that
Y -6 1010
can be represented using
overflow and it is ok! 4 bits is -8) X+Y -9 1 0111

X -4 11100
Overflow: X 7 0 111
Y -9 10111
(max signed number that
can be represented using Y 7 0 111
X+Y -13 1 10011
4 bits is 7) X+Y 14 1 110

The overflow can be discarded


If Carry into and out of the sign bit are the same
BCD Addition n 0 1 1 0 ß BCD for 6
0 1 1 1 ß BCD for 7
_________________
• BDC numbers 1 1 0 1 ß 13 but invalid!
• They are between 0 and 9 0 1 1 0 ß Add 6 to correct
• Hence each decimal number is represented by 4 bits ________________________
• Add each number between 0-9 individually 0 0 0 1 0 0 1 1 ß BDC for 13!!
BCD Addition – Another Example
1
• BDC numbers 0 1 0 1 1 0 0 1 ß BCD for 59
• They are between 0 and 9 0 0 1 1 1 0 0 0 ß BCD for 38
• Hence each decimal number is represented by 4 bits ________________
• Add each number between 0-9 individually 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!!
Hex Arithmetic
• Addition of Hex numbers is
similar to decimal addition +8
9
+8
9

• Remember the largest value is 16 (F) In Decimal =17


In Hex=11
17-16 =1
and NOT 9! In Hex = 1116
• Carry a 1 if the number is larger than
F
• Use the following steps
• Add the hex digits in decimal. 3AF 8+B=8+(11)= 58
• If the sum is 15 or less + 23C 19à19-16=3,
Carry 1 + 4B
express it directly in hex = 5EB = A3
digits.
• If the sum is greater than 15,
subtract 16 and carry 1 to the
next position.
Hex Arithmetic
• Subtraction of hex numbers follows a
similar method as binary numbers
• Make sure you take the 2’s complement of
the negative hex number
• Method 1:
• Subtract the number from FFFF…
592 67F
• Write the results in hex
• Add one to the final answer - 3A5 - 2A4
• For example: 2’s complement of 3A5 is: à592 à67F
• FFF-3A5=C5A à C5A+1 = C5B + C5B + D5C
• Method 2: = 11ED = 3DB
• Convert the number to Binary
• Take the 2’s complement FFF- 3A5=
5+C=2+(12)=
• Convert back the results into hex value (15-3),(15- F+C=(15)+(12)=27
17à17-16=1,
10),(15-5) = à27-16=11 or B,
Carry 1 –
• Simply add the values together Must be
12, 5, 10 à
C5A+1=C5B
Carry 1
discarded!

You might also like