Lecture 6 - Memory Organization, Stack, Heap, UART EECS 388
Lecture 6 - Memory Organization, Stack, Heap, UART EECS 388
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
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:
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
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
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)
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)
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
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.
29
UART Speed (Baudrate)
Both sender and receiver must use agreed upon
transmission speed (baudrate)
31
Memory Map of
SiFive FE310
https://en.wikipedia.org/wiki/FIFO_%28computing_and_electronics%29 37