Lecture 2 Basics
Lecture 2 Basics
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
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
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
• 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.
0.5V
0.0V