You are on page 1of 25

ESE 350

Embedded Systems/Microcontroller
Laboratory

Lecture 4
Computer Architecture & C Programming
Professor Rahul Mangharam
Electrical and Systems Engineering
University of Pennsylvania
Computer Architecture-Office Analogy
Memory in computers is like a filing cabinet in an office.
File folders have tab labels and contain contents.

16-bit address 8-bit data


Address Data
$FFFF $C0

$0002 $AA
$0001 $01
$0000 $1F

CPU changes contents of memory like a dedicated secretary.


CPU contains registers like workspace desk of secretary.
2
Slots
Files

Accumulator
Desk

CPU (Secretary) Memory (Filing Cabinet)

Accumulator is the legal pad in front of the secretary.


Secretary
―Write down something on pad.
―Take paper from pad and put into file number 10.
3
Actions of the secretary=instructions.
An instruction consists of VERB + OBJECT
Write down #31 LDAA #$1F
Put Into File 10 STAA $0A

VERB=Op code (1 byte)


Object=Operand (0-2 bytes)
Multiple instructions=Program
[Op code Operand] [Op code Operand] [Op code
Operand] …

The secretary
With Machine language, writes down all numbers.
With Assembly language, writes down mnemonics.
With C language, writes an abstract statement to represent a set of
mnemonics that are normally used together.
4
Architecture-Office Analogy
Diagram
Secretary
CPU

ALU
Incoming Mail Outgoing Mail
Slots Input Control Output Slots

Memory

File cabinets

5
Memory Mapped I/O
Memory mapped I/O implies that I/O devices and memory
share the same map with some addresses reserved for I/O
and others for memory.
Advantage-Program flexibility: same instructions are
used by the CPU to read/write data to I/O devices as to
RAM and ROM (memory)
Disadvantage-Limit on amount of memory & I/O devices
due to sharing.

6
MC9S12C12
8 Memory
Map
A Memory Mapped I/O-PortB
Output port B:
Address=$1004

Pins:
B7 B6 B5 B4 B3 B2 B1 B0
Each pin can be HIGH=1, +5V or LOW=0, 0V

[B7…B0] 8-bit number

8
Isolated I/O
CPU treats memory and I/O devices separately, using
different control signals for each.
Advantage-CPU can communicate with 28=256 I/O
devices and memory can use all 65536 addresses (16-bit
address)
Disadvantage-Special program instructions to perform I/O
operations.

9
Isolated I/O-Intel 8085

Memory

CPU

Address Bus

RD I/O
WR
IO/M

Data Bus

To read from I/O: RD=0, WR=1, IO/M=1


10
Comparison of Programming Languages
Level of Efficient Use of Programming
C Language Abstraction Processor/Memory Difficulty

High Low Low

Assembly Language

Hex Machine Language

(Binary) Machine Language Low High High

11
Language Translation
Regardless of what language a program is written with, the only language the
computer understands is machine language, that is, 0s and 1s.

C Language
Compiler/Interpreter

Assembly Language
Source
Program

Assembler
Hex Machine Language

Object
(Binary) Machine Language Program

Compiler translates the complete C program into an machine language object program.
Interpreter translates small chunk of the C program and executes as it goes along without
creating an object program.
Assembler converts (assembles) the Assembly language into machine language object
program. 12
Same Task-Different Languages
Add two numbers and save the result.

Hex (Binary) Machine


Machine Language
C Language Assembly Language Language

8000 LDAA $C300 B6 10110110


8003 ADDA $E0FF C3 11000011
c=a+b; 8006 STAA $C4AA 00 00000000
BB 10111011
E0 11100000
FF 11111111
B7 10110111
C4 11000100
AA 10101010

Hand assembly: LDAA $C300->B6 C3 00-> 1011 0110 1100 0011 0000 0000
13
Review of C Programming
#include <stdio.h> Include standard library definitions file
/*Comments*/
main() Function main implicitly returns int value
{
int a,b,c; 3 variables (int: 16-bit for MC9S12C128)
a=3;
b=5;
c=a+b; Library function that converts,
formats, and prints its argument
printf(“a+b=%d\r\n”,c);
on the standard output
return 0;
}
Predefined constant:
newline and carriage return
Character group

Matches with open brace


Conversion character
14
Helpful Hints
To ensure that the cursor behaves as you output text to the terminal,
add \r a carriage return to the ends of your printf statements.
Example:
printf(“Hello, world!\r\n”);

The more print statements you use the longer it will take your code to
compile and the slower it will execute, so if timing is an issue, reduce
the number of them.

Use LEDs as visual clues as to what signals are being input and
output.

15
Data Types & Flow Control
Data Types:

char c; 8-bit variable


Int x; 16-bit variable (short int: 16-bit, long int:32-bit)

Flow Control:
if(a!=0){
b=1;
}
else {
b=0;
}

16
Conditional Loops
While and for loops test condition at the beginning of the loop.
If nonzero, execute statement cycle continues until
expression becomes zero, in which case execution
resumes after the while condition block of
statements
n=0;
while(n<5)
{
printf(“n=%d\r\n”,n);
n++; Same as n=n+1
}

for(n=0;n<5;n++)
{
printf(“n=%d\r\n”,n);
}
17
Switches
Switch allows multiway decision based on control expression.
Switch(n){ Possible values for n
case 1: printf(“.”);
break;
case 2: printf(“..”);
break;
case 3: printf(“…”);
Break keyword forces the
break;
program flow to drop out of the
default: switch statement so that only the
statements under the
} corresponding case label are
executed. If any break statement
is missing, then all statements
from that case label until next
break statement within the same
switch statement will be
18
executed.
Functions
A function processes information passed to it by the calling
part of the program and returns a single value.
Return type is void int add1(int x)
if function does not
{
return any value to
caller. int y; Returns information desired
y=x+1; by function call
return y;
}

int main()
{
int a, b;
a=2;
b=add1(a);
return 0;
} 19
Arrays
Arrays are multiple data items that have common characteristics.
― Share same name
― Can be characters, integers, floats, etc.
― Elements of an array must be same type
Memory:
$C000 1
$C002 10
$C004 20

int x[10]; Data_type array_name [expr]


Can be initialized when defined
X[0]=1;
X[1]=10;
X[2]=20;
1 dimensional array; higher dimensions possible
20
Pointers
A pointer is a variable that holds the address of a variable.

Variable Address Value


a $D000 2
int a, b;
type_name *pointer_name
Variable p is a pointer to an integer.
int *p;
a=2; Assigns the address of the variable a to the pointer p.
p=&a; Therefore, p=$D000.
b=*p;
Dereferencing operator assigns value pointed to by
pointer p to variable b. Therefore b=2.

21
Bitwise Operators
A bitwise operator performs bit-by-bit operation on variable(s).
char a,b; 5 B
01011011 a
a=0x5B;
b=0x33;
00110011 b

3 3

c=a|b; OR
c=a&b; AND
c=~a; NOT
c=a^b; XOR (Exclusive-OR)
22
Boolean Logic-Truth Table

X Y X OR Y X AND Y NOT X NOT Y X XOR Y


0 0 0 0 1 1 0
0 1 1 0 1 0 1
1 0 1 0 0 1 1
1 1 1 1 0 0 0

23
Bitwise Operators
Applying the previous Boolean 5 B
Logic truth table:
01011011 a
c=a|b; 0111 1011=0x7B
c=a&b; 0001 0011=0x13 00110011 b
c=~a; 1010 0100=0xA4
c=a^b; 0110 1000=0x68 3 3

24
Bit Shift Operations
Shift left 1 bit: x 11000110
x=0xC6; 0
y=(x <<1); y 10001100

8 C

Shift right 2 bits: x 11000110


x=0xC6;
y=(x >>2); y 00110001
00
3 1

25

You might also like