EECS 388: Embedded Systems
Lecture 6 – Memory Organization &
UARTs
Fall 2024
Jacob Schoonover
Lecture notes based in part on slides created by Alex Fosdick,
Heechul Yun, Mohammad Alian, and Tamzidul Hoque
1
Agenda & Announcements
• Memory Organization
• Group Activity
• UARTs
2
Context
Chapter 2,
Application Program Section 7 in
Computer
Operating
Compiler System Organization and
Machine Language (ISA)
Design by
Digital Logic
Patterson &
Hennessy
Electronic Circuits
Transistors
3
Memory Layout of C Programs
A typical memory representation of a C program
consists of three broad regions.
1. Static
-Text/code segment
-Initialized data segment
-Uninitialized data segment
2. Stack
3. Heap
https://courses.engr.illinois.edu/cs225/sp2022/resources/stack-heap/
4
Text Segment
• Contains executable instructions.
• Placed below the heap or stack in
order to prevent heaps and stack
overflows from overwriting it.
• Text segment is often read-only to
prevent a program from
accidentally modifying its
instructions.
5
Initialized Data Segment:
• Also called just `data segment’
• contains the global variables and
static variables that are
initialized in the code
• Data segment is not read-only,
since the values of the variables
can be altered at run time.
• Example: Int i=5;
6
Uninitialized Data Segment:
• often called the “bss”
• contains all global variables and
static variables that are initialized to
zero
• or do not have explicit initialization in
source code.
• Example:
• int i;
• Int i=0;
7
Static Variable
• Static variables preserve their previous
value in their previous scope and are not
initialized again in the new scope.
Output: 1, 2 Output: 1, 1
https://mazzo.li/posts/c-performance-anecdote.html
8
Heap Segment: 0xFFFF_FFFF
• Used for dynamic memory
allocation
• Grows from lower to higher
memory addresses
• Example:
• int * int_ptr;
• int_ptr = (int *) malloc (100*sizeof(int));
• free(int_ptr);
0x0000_0000 9
Stack 0xFFFF_FFFF
• Stack is in memory →need a register to point to it
• MIPS uses stack pointer register ($sp)
• Stack grows down from higher to lower addresses
• Push decreases sp
• Pop increases sp
• $sp points to top of stack (last pushed element)
• Rule of using stack:
• Can use stack at any time, but leave it as you found
it!
0x0000_0000
10
Stack Example
int main(){
//…
f1();
return 0;
Stack of Main() }
Stack of f1()
int f1(){
Stack of f2()
//…
Stack of f3() f2();
}
Void f2(){
//…
f3();
}
Void f3(){
//…
}
11
Pushing onto the Stack
• To push elements onto the stack: word 1
• Move the stack pointer $sp down to $sp word 2
make room for the new data.
• Store the elements into the stack.
• For example, to push registers $t1
and $t2 onto the stack:
sub $sp, $sp, 8 Before Storing onto stack
sw $t1, 4($sp)
sw $t2, 0($sp) word 1
• An equivalent (but less common) word 2
sequence is: $t1
sw $t1, -4($sp) $sp $t2
sw $t2, -8($sp)
sub $sp, $sp, 8
After Storing onto stack
• Slide taken and modified from CS232: Computer Architecture II. University of Illinois at Urbana-Champaign.
12
Accessing and popping elements
• You can access any element in the stack (not just word 1
the top one) if you know where it is relative to word 2
$sp.
$t1
• For example, to retrieve the value of $t1: $sp $t2
lw $s0, 4($sp)
• You can pop, or “erase,” elements simply by
adjusting the stack pointer upwards.
• To pop the value of $t2, yielding the stack shown word 1
at the bottom: word 2
addi $sp, $sp, 4 $sp $t1
• Note that the popped data is still present in $t2
memory, but data past the stack pointer is
• considered invalid.
Slide taken and modified from CS232: Computer Architecture II. University of Illinois at Urbana-Champaign.
13
MIPS memory map layout
14
15
Slides from: https://courses.cs.vt.edu/~cs2505/fall2010/Notes/pdf/T25.MIPSStack.pdf
16
Examples
https://medium.com/@saidsadaoy/memory-management-in-c-78797950efaa 17
Data Memory (Data Segment)
• Stack: temporary data like local
B int globA;
variables D int globB = 1;
• Heap: dynamically allocated int main () {
data S int varA;
S int varB = 10;
• malloc (similar to new in C++) B static int varC = 0;
• Data: non-zero initialized global D static int varE = 1;
S char *varD;
and static data H varD = (char*)malloc(8);
• BSS (Block Started by Symbol): varA = varB + varC;
zero initialized and uninitialized return varA;
}
global and static data
18
Group Activity – Code Reviews
19
What is wrong with this code?
20
What is wrong with this code?
21
Context
• Recommended reading: Chapter 9 of “Introduction to
Computing,” Patt, Patel
Memory
MDR MAR
Input Output
• push button • LED
• Keyboard • Monitor
• Sensors • I/O
• Disk • Disk
Processing Unit
ALU Reg
Control Unit
Program Counter (PC) Instruction Register (IR)
** Picture in the slides are in part taken from “Introduction to Computing From bits & gates to C and beyond” Patt, Patel 22
Simplified Processing unit – I/O device
interaction
• Using two registers:
• Data register: Hold the data being transferred
• Status register: status of the device (e.g., busy,
need attention, etc.)
I/O Device
Data Status
Register Register
23
Serial Communication Standard: RS-232
• Asynchronous communication protocol (no source
clock)
• First introduced in 1962 to connect
teletypes to modems
• Because the standard was set long before
the advent of the TTL logic family, its input
and output voltage levels are not TTL
compatible
• TTL: transistor-transistor logic
• A 1 is represented by −3 to −25 V
• A 0 is represented by +3 to +25 V
• Low voltage denotes 1
• Large voltage range makes it less susceptible
to noise, interference, and degradation.
24
source
RS-232 Pins
DB-9 as the standard connector of RS232
Pin Description
1 Data carrier detect
2 Received data
3 Transmitted data
4 Data terminal ready
5 Signal ground
6 Data set ready
7 Request to send
8 Clear to send
9 Ring indicator
25
RS-232
• The idle state of the RS232 lines is logic 1 (-12V)
• To signal a start condition the line is set logic 0 (+12V) for 1 bit
period (please note high voltage means 0).
• Cause a 1 to 0 transition →indicates valid data is coming
26
This Photo by Unknown Author is licensed under CC BY-SA
RS-232 Frame Format
0 b0 b1 bn p s1 s2
Start bit
Parity Stop bit
1111010000011111
data
Idle
27
Parity bit
• Used for detecting errors in bits
• There are two types of parity checking technique: ‘even’
and ‘odd’
• Even parity: if total number of 1’s are even, the parity bit
value is set to 0. For odd parity its opposite.
7 bits of data (count of 1- 8 bits including parity
bits) even odd
0000000 0 00000000 00000001
1010001 3 10100011 10100010
28
Universal Asynchronous
Receiver/Transmitter (UART)
• Convert parallel content of an 8-bit register to a bit
sequence ready to be transmitted over a serial port
e.g., RS-232
• Processor pins →UART→volt conversion → RS232
• UART acts as the interface between a CPU and a
serial port like RS232
29
UART Speed (Baudrate)
Both sender and receiver must use agreed upon
transmission speed (baudrate)
• For a baud rate of 2400 (2400
bps) the frequency is 2400Hz
and the bit period is 1/2400 or
416.6us.
• This is the information that a
receiver uses to recover the
bits from the data stream.
416.6us 30
This Photo by Unknown Author is licensed under CC BY-SA
Question
• Suppose you are sending data over a
UART channel at a baud rate of
115200 bps. How long does it take to
send a single 8-bit character over the
channel?
• Assume 2 stop bit, 1 parity bit, and 1
start bit
31
Memory Map of
SiFive FE310
Memory mapped I/O
regions
32
33
34
35
36
Lab
• Does our code continually check for
data (polling?) or does it get informed
when data is available (interrupt?).
• What happens when we read the byte?
https://en.wikipedia.org/wiki/FIFO_%28computing_and_electronics%29 37