0% found this document useful (0 votes)
8 views

Lecture 2 Basics

Uploaded by

mythemyaseen6
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Lecture 2 Basics

Uploaded by

mythemyaseen6
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 32

CMP3010

EMBEDDED SYSTEMS
PROGRAMMING
LECTURE 2 BASICS
Preliminaries
Binary, hex conversion.
Resistor calculations.
Encoding Byte Values
• Byte = 8 bits
0 0 0000
• Binary 000000002 to 111111112 1 1 0001
2 2 0010
• Decimal: 010 to 25510
3 3 0011
• Hexadecimal 0016 to FF16 4 4 0100
5 5 0101
• Base 16 number representation
6 6 0110
• Use characters ‘0’ to ‘9’ and ‘A’ to ‘F’ 7 7 0111
• Write FA1D37B16 in C as 8 8 1000
9 9 1001
• 0xFA1D37B A 10 1010
• 0xfa1d37b B 11 1011
C 12 1100
D 13 1101
E 14 1110
F 15 1111
Byte Ordering
 How should bytes within a multi-byte word be ordered in memory?
 Conventions
 Big Endian: Sun, PPC Mac, Internet
 Least significant byte has highest address

 Little Endian: x86


 Least significant byte has lowest address
Byte Ordering Example

Big Endian 0x100 0x101 0x102 0x103


01 23 45 67

Little Endian 0x100 0x101 0x102 0x103


67 45 23 01
15213
0011 1011 0110 1101

Representing Integer 3 B 6 D

int A = 15213;
long int C = 15213;
IA32, x86-64 Sun
IA32 x86-64 Sun
6D 00
6D 6D 00
3B 00
3B 3B 00
00 3B
00 6D 00 00 3B
00 00 6D
00
int B = -15213; 00
00
IA32, x86-64 Sun
00
93 FF
C4 FF
FF C4
FF 93 Two’s complement representation
(Covered later)
Representing Strings
Represented by array of characters
Each character encoded in ASCII format Linux/Alpha Sun
Standard 7-bit encoding of character set
31 31
Character “0” has code 0x30
Digit i has code 0x30+i 38 38
String should be null-terminated 32 32
Final character = 0 34 34
Compatibility 33 33
Byte ordering not an issue 00 00

char S[6] = "18243";


Bits, Bytes, and Integers
Representing information as bits
Bit-level manipulations
Integers
Representation: unsigned and signed
Conversion, casting
Expanding, truncating
Addition, negation, multiplication, shifting
Boolean Algebra
Developed by George Boole in 19th Century
Algebraic representation of logic
Encode “True” as 1 and “False” as 0
Boolean Algebra
And Not
 A&B = 1 when both A=1 and B=1
 ~A = 1 when A=0

Or Exclusive-Or (Xor)
 A|B = 1 when either A=1 or B=1  A^B = 1 when either A=1 or B=1, but not both
General Boolean Algebras
Operate on Bit Vectors
Operations applied bitwise

01101001 01101001 01101001


& 01010101 | 01010101 ^ 01010101 ~ 01010101
01000001 01111101 00111100 10101010

All of the Properties of Boolean Algebra Apply


Representing & Manipulating Sets
• Representation
• Width w bit vector represents subsets of {0, …, w–1}
• aj = 1 if j ∈ A
• 01101001 { 0, 3, 5, 6 }
• 76543210
• 01010101{ 0, 2, 4, 6 }
• 76543210
• Operations

• & Intersection 01000001 { 0, 6 }


• | Union 01111101 { 0, 2, 3, 4, 5, 6 }
• ^ Symmetric difference 00111100 { 2, 3, 4, 5 }
• ~ Complement 10101010 { 1, 3, 5, 7 }
Bit-Level Operations in C
• Operations &, |, ~, ^ Available in C
• Apply to any “integral” data type
• long, int, short, char, unsigned
• View arguments as bit vectors
• Arguments applied bit-wise
• Examples (Char data type)
• ~0x41 = 0xBE
• ~010000012 = 101111102
• ~0x00 = 0xFF
• ~000000002 = 111111112
• 0x69 & 0x55 = 0x41
• 011010012 & 010101012 = 010000012
• 0x69 | 0x55 = 0x7D
• 011010012 | 010101012 = 011111012
Contrast: Logic Operations in C
• Contrast to Logical Operators
• &&, ||, !
• View 0 as “False”
• Anything nonzero as “True”
• Always return 0 or 1
• Early termination

• Examples (char data type)


• !0x41 = 0x00
• !0x00 = 0x01
• !!0x41 = 0x01

• 0x69 && 0x55 = 0x01


• 0x69 || 0x55 = 0x01
Shift Operations
• Left Shift: x << y
• Shift bit-vector x left y positions Argument x 01100010
• Throw away extra bits on left << 3
• Fill with 0’s on right
Log. >> 2
• Right Shift: x >> y
• Shift bit-vector x right y positions Arith. >> 2
• Throw away extra bits on right
• Logical shift Argument x 10100010
• Fill with 0’s on left
<< 3
• Arithmetic shift
• Replicate most significant bit on right Log. >> 2

• Undefined Behavior Arith. >> 2


• Shift amount < 0 or ≥ word size
Integers : Encoding
Unsigned Two’s Complement
w1 w2
B2U(X)   xi 2 i
B2T(X)   xw1 2 w1
  xi 2 i
i0 i0

short int x = 15213;


short int y = -15213; Sign
Bit
• C short 2 bytes long
Decimal Hex Binary
x 15213 3B 6D 00111011 01101101
y -15213 C4 93 11000100 10010011

• Sign Bit
• For 2’s complement, most significant bit indicates sign
• 0 for nonnegative, 1 for negative
Weight 15213 -15213
1 1 1 1 1
Encoding Example (Cont.) 2
4
0
1
0
4
1
0
2
0
8 1 8 0 0
16 0 0 1 16
32 1 32 0 0
64 1 64 0 0
x = 15213: 00111011 01101101 128 0 0 1 128
y = -15213: 11000100 10010011 256 1 256 0 0
512 1 512 0 0
1024 0 0 1 1024
2048 1 2048 0 0
4096 1 4096 0 0
8192 1 8192 0 0
16384 0 0 1 16384
-32768 0 0 1 -32768
Sum 15213 -15213
Precision and alternatives
 Precision is the number of distinct or different values. We express precision in
alternatives, decimal digits, bytes, or binary bits.
 Alternatives are defined as the total number of possibilities. For example, an 8-
bit number format can represent 256 different numbers.
 An 8-bit digital to analog converter (DAC) can generate 256 different analog
outputs. An 8-bit analog to digital converter (ADC) can measure 256 different
analog inputs.
Introduction to Electronics
Current, I, is defined as the movement of electrons. In particular, 1 ampere (A) of current is
6.241×1018 electrons per second, or one coulomb per second.
Current is measured at one point as the number of electrons travelling per second.
Current has an amplitude and a direction.
Because electrons are negatively charged, if the electrons are moving to the left, we define
current as flowing to the right.
Voltage
Voltage, V, is an electrical term representing the potential difference between two points.
The units of voltage are volts (V), and it is always measured as a difference.
Voltage is the electromotive force or potential to produce current.
Resistor
We will see two types of conducting media: a wire and a resistor.
Wires, made from copper, will allow current to freely flow, but forcing current to flow through
a resistor will require energy.
The electrical property of a resistor is resistance in ohms (Ω).
Ideally, a wire is simply a resistor with a resistance of 0 Ω.
Ohm’s Law
The basic relation between voltage, current, and resistance for a resistor is known as Ohm’s
Law, which can be written three ways:
𝑉 = 𝐼 ×𝑅

𝑉
𝐼 =
𝑅

𝑉
𝑅 =
𝐼
Ohm’s Law
Analogous systems
Power
The power (P in watts) dissipated in a resistor can be calculated from voltage (V in volts),
current (I in amps), and resistance (R in ohms).
Power has neither a polarity nor a direction.

P=V*I Power = Voltage * Current


P = V2 / R Power = Voltage2 / Resistance
P = I2 * R Power = Current2 * Resistance
Electric circuits
A switch is an element used to modify the behavior of the circuit.
If the switch is pressed, its resistance is 0, and current can flow across the switch.
If the switch is not pressed, its resistance is infinite, and no current will flow.
The classic electrical circuit involves a battery, a light bulb (modeled in this circuit as a 100Ω
resistor), and a switch.
Kirchhoff's Current Law (KCL)
• The sum of the currents into a node equal the sum of the
currents leaving a node as shown in the figure.
Series resistance
• If resistor R1 is in series with resistor R2, this combination
behaves like one resistor with a value equal to R1+R2.
• By KCL, the currents through the two resistors are the
same. These two facts can be used to derive the voltage
divider rule
• V2 = I*R2 = (V/R)*R2 = V*R2/(R1+R2)
Parallel resistance
• Parallel resistance. If resistor R1 is in parallel with resistor
R2, this combination behaves like one resistor with a
value equal to
Introduction to Digital Logic
• A binary bit can exist in one of two possible states.
• In positive logic, the presence of a voltage is called the
‘1’, true, asserted, or high state.
• The absence of a voltage is called the ‘0’, false, not
asserted, or low state.
High and low voltages
• A voltage between 2 and 5 V is considered high, and a
voltage between 0 and 1.3 V is considered low,

• Separating the two regions by 0.7 V allows digital


logic to operate reliably at very high speeds.
• Digital data exist as binary bits and encoded as
high and low voltages.
Binary Representations
0 1 0
5V
2.8V

0.5V
0.0V

You might also like