Lecture 2 Part 1
Lecture 2 Part 1
ECE 3130
Microcomputer Systems
01 Architecture
Computer
1/22/2024
Outline
• Data representation
• Computer Hardware
• Memory
• Processor
• Program execution
1/22/2024
Data Representation
1/22/2024
Unsigned Integers
Unsigned Integers
Convert Decimal to Binary
Example 1 Example 2
• 𝒄=𝒂+𝒃
• Carry happens if c is too big to fit in n bits (i.e. c > 2 −1).
• 𝒄=𝒂−𝒃
• Borrow happens if 𝑐 < 0.
Borrow 1 1 1 0 0
0 0 0 1 1 3
- 0 0 1 0 1 - 5
1 1 1 1 0 30
5-bit result
One’s
Sign-and-Magnitude Two’s Complement
Complement
Range [−2 + 1, 2 − 1] [−2 + 1, 2 − 1] [−2 ,2 − 1]
Zero Two zeroes (±0) Two zeroes (±0) One zero
Unique
2 −1 2 −1 2
Numbers
1/22/2024
Signed Integers
Method 1: Signed Magnitude
Sign-and-Magnitude:
𝒗𝒂𝒍𝒖𝒆 = (−𝟏)𝒔𝒊𝒈𝒏 × 𝑴𝒂𝒈𝒏𝒊𝒕𝒖𝒅𝒆
• The most significant bit is the sign.
• The rest bits are magnitude.
Signed Integers
Method 2: One’s Complement
One’s Complement (𝜶):
𝜶 + 𝜶 = 𝟐𝒏 − 𝟏
Signed Integers
Method 3: Two’s Complement (TC)
Two’s Complement (𝜶):
𝜶 + 𝜶 = 𝟐𝒏
Example 1: TC(3)
Binary Decimal
Original number 0b00011 3
Step 1: Invert
0b11100
every bit
Step 2: Add 1 + 0b00001
Two’s complement 0b11101 -3
1/22/2024
Signed Integers
Method 3: Two’s Complement (TC)
Two’s Complement (TC)
𝜶 + 𝜶 = 𝟐𝒏
Example 2: TC(-3)
Binary Decimal
Original number 0b11101 -3
Step 1: Invert
0b00010
every bit
Step 2: Add 1 + 0b00001
Two’s complement 0b00011 3
1/22/2024
Comparison
• Overflow cannot occur when adding operands with different signs or when
subtracting operands with the same signs.
1/22/2024
-5 5
11010 00110
-6 6
11001 00111
-7 7
11000 -8 8 01000
-9 9
10111 01001
-10 10
10110 01010
-11 11
Overflow +5
10101 -12 12 01011
-13 13
10100 -14 14 01100
-15 -16 15
10011 01101
01110
1. On addition, overflow occurs if 𝑠𝑢𝑚 ≥ 2
10010
10001 10000 01111
-5 5
11010 00110
-6 6
11001 00111
-7 7
11000 -8 8 01000
-9 9
10111 01001
-10 10
10110 01010
-11 11
Overflow
10101 -12 -7 12 01011
-13 13
10100 -14 14 01100
-15 -16 15
10011 01101
10010 01110
10001 10000 01111
On addition, overflow occurs if
𝑠𝑢𝑚 < −2 when adding two
Overflow occurs when adding negatives.
two negative integers but
getting a positive result.
1/22/2024
Signed or Unsigned
a = 0b10000
b = 0b10000
c = a + b
Signed or unsigned
• Whether the carry flag or the overflow flag should be used depends on the programmer’s intention.
Signed or Unsigned
a = 0b10000
b = 0b10000
c = a + b
Are a and b signed or unsigned numbers?
uint a;
uint b; Check the carry flag!
…
c = a + b
…
C Program
1/22/2024
Signed or Unsigned
a = 0b10000
b = 0b10000
c = a + b
Are a and b signed or unsigned numbers?
int a;
int b; Check the overflow flag!
…
c = a + b
…
C Program
1/22/2024
-9 + 6
9 flip +1 -9
1 0 1 1 1 0 0 1 1 0 01001 10110 10111
23 6 Two’s
Complement
29 3
flip +1
1 1 1 0 1 00010 00011
Two’s Complement
-3 Counterpart
1/22/2024
17
15 flip +1 -15
1 0 0 0 1 01111 10000 10001
-15
1/22/2024
Condition Codes
𝑐 =𝑎±𝑏
Carry Overflow
(for unsigned) (for signed)
Add 𝐶𝑎𝑟𝑟𝑦 = 1 if 𝑐 is too
large to fit in. 𝑂𝑣𝑒𝑟𝑓𝑙𝑜𝑤 = 1 if 𝑐 is too
Subtract 𝐵𝑜𝑟𝑟𝑜𝑤 = 1, i.e. large or too small to fit in
𝐶𝑎𝑟𝑟𝑦 = 0 if 𝑎 < 𝑏.
• ARM Cortex-M has no dedicated borrow flag, • Signed Subtraction is converted to sign
carry flag is reused. addition
• For unsigned subtract, 𝐵𝑜𝑟𝑟𝑜𝑤 = 𝐶𝑎𝑟𝑟𝑦 • 𝑎 − 𝑏 = 𝑎 + (−𝑏)
1/22/2024
R = X + Y
Characters Dec
0
Hex
00
Char
NUL
Dec
32
Hex
20
Char
SP
Dec
64
Hex
40
Char
@
Dec
96
Hex
60
Char
‘
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
American 5 05 ENQ 37 25 % 69 45 E 101 65 e
Standard 6 06 ACK 38 26 & 70 46 F 102 66 f
7 07 BEL 39 27 ’ 71 47 G 103 67 g
Code for 8 08 BS 40 28 ( 72 48 H 104 68 h
Information 9 09 HT 41 29 ) 73 49 I 105 69 i
10 0A LF 42 2A * 74 4A J 106 6A j
Interchange 11 0B VT 43 2B + 75 4B K 107 6B k
12 0C FF 44 2C , 76 4C L 108 6C l
13 0D CR 45 2D - 77 4D M 109 6D m
14 0E SO 46 2E . 78 4E N 110 6E n
15 0F SI 47 2F / 79 4F O 111 6F o
• Standard ASCII 16 10 DLE 48 30 0 80 50 P 112 70 p
0 - 127 17 11 DC1 49 31 1 81 51 Q 113 71 q
18 12 DC2 50 32 2 82 52 R 114 72 r
• Extended ASCII 19 13 DC3 51 33 3 83 53 S 115 73 s
0 - 255 20 14 DC4 52 34 4 84 54 T 116 74 t
21 15 NAK 53 35 5 85 55 U 117 75 u
• ANSI 22 16 SYN 54 36 6 86 56 V 118 76 v
0 - 255 23 17 ETB 55 37 7 87 57 W 119 77 w
• Unicode 24 18 CAN 56 38 8 88 58 X 120 78 x
25 19 EM 57 39 9 89 59 Y 121 79 y
0 - 65535 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 }
30 1E RS 62 3E > 94 5E ^ 126 7E ~
31 1F US 63 3F ? 95 5F _ 127 7F DEL
Null-terminated String
String Comparison
Strings are compared based on their ASCII values
• “j” < “jar” < “jargon” < “jargonize”
• “CAT” < “Cat” < “DOG” < “Dog” < “cat” < “dog”
• “12” < “123” < “2”< “AB” < “Ab” < “ab” < “abc”
1/22/2024
String Length
• Strings are terminated with a null character (NULL, ASCII value 0x00)
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
Questions?