Skip to content

Commit f67b825

Browse files
committed
os, co
- stack segment - stack pointer
1 parent 6ac8a9e commit f67b825

File tree

3 files changed

+29
-21
lines changed

3 files changed

+29
-21
lines changed

content/Computer Organisation/Processor/Register.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ tags:
77
- OS
88
- computer_organisation
99
Creation Date: 2023-11-20T10:58:00
10-
Last Date: 2024-02-20T13:21:32+08:00
10+
Last Date: 2024-04-01T19:23:27+08:00
1111
References:
1212
---
1313
## Abstract
@@ -30,8 +30,7 @@ References:
3030
- CSRs are typically used to control various aspects of the processor's operation, such as interrupt handling, memory management, and power management etc
3131

3232
### Stack Pointer
33-
- A [[Register]]
34-
- Holds the address of the top of the [[Address Space#Stack Segment]] in the current **execution context**
33+
- A [[Register]] that holds the [[Memory Address]] of the top of the [[Address Space#Stack Segment]] in the current **execution context**. Here is the [[stack_segment.png|Diagram]]
3534

3635
>[!info] `offset($sp)`
3736
> Used to **access a memory location** **relative** to the **current top** of the stack

content/OS/Process/Address Space.md

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ tags:
99
- c
1010
- rust
1111
Creation Date: 2023-10-19T17:15:00
12-
Last Date: 2024-03-17T13:59:16+08:00
12+
Last Date: 2024-04-01T19:53:43+08:00
1313
References:
1414
description: Stack (automatic memory management for function variables), Heap (dynamic memory management), Data (stores pre-defined variables shipped with the program) and Text (stores unchangeable program codes).
1515
---
@@ -23,7 +23,8 @@ description: Stack (automatic memory management for function variables), Heap (d
2323
- Data in [[#Text Segment]] (the **orange** block shown above) and [[#Data Segment]] (the **red** block shown above) are shipped with the program
2424
- Data in [[#Heap Segment]] (the **blue** block shown above) and [[#Stack Segment]] (the **green** block shown above) are allocated dynamically when the program is running
2525

26-
### Heap Segment
26+
## Heap Segment
27+
---
2728
- **Dynamically allocated region** where the [[Process (进程)]] can create new data structures as needed
2829
- **Grows** and **shrinks** as the **process** allocates and deallocates memory
2930

@@ -50,33 +51,37 @@ description: Stack (automatic memory management for function variables), Heap (d
5051
>
5152
>Refer to this [section of article](https://rust-book.cs.brown.edu/ch04-01-what-is-ownership.html#rust-does-not-permit-manual-memory-management) for more details
5253
53-
#### Memory leak
54+
### Memory leak
5455
- Happens when we **forget to release** data in heap memory using `free()` in the example of C
5556
- This can eventually lead to the exhaustion of available [[Main Memory]], resulting in **degraded performance** or even **program crashes**
56-
### Stack Segment
57+
## Stack Segment
58+
---
59+
![[stack_segment.png|300]]
5760
- **Dynamically allocated region** used to store **function calls**, local variables, and temporary data etc
58-
- Made up of [[#Stack Frame]], follow a [[Stack]] structure
59-
- **Grows** as functions are called and **shrinks** as they return
61+
- Made up of [[#Stack Frame]], following a [[Stack]] structure
62+
- **Expands** as functions are called and **shrinks** as they return
63+
- We can obtain the default stack size assigned by the system using `ulimit -s`
6064

6165
>[!question]- Grows Downwards
62-
>Stack Segment starts at a higher [[Memory Address]], then memory address decreases as we add in **Stack Frame**, thus **growing downwards** in terms of Memory Address, so to remove stack frame, we need to increment the [[Register#Stack Pointer]]
66+
>Stack Segment starts at a higher [[Memory Address]], then **memory address decreases** as we add in **Stack Frame**, thus **growing downwards** in terms of Memory Address, so to remove stack frame, we need to increment the [[Register#Stack Pointer]].
6367
6468
>[!info]- Fun Fact Regarding Grow Downwards
65-
> Growing downwards is a convention from when computers had small memories and the stack was placed at the end of the [[#Data Segment]]. Nowadays the stack can be anywhere, but the convention stuck on, at the end of the day it makes no difference
69+
> Growing downwards is a convention from when computers had small memories and the stack was placed at the end of the [[#Data Segment]]. Nowadays the stack can be anywhere, but the convention stuck on, at the end of the day it makes no difference.
70+
71+
>[!bigbrain] Variables live in the stack
72+
> When assigning one variable to another variable, data is **duplicated**.
73+
>
74+
> For example, `a=1` and `b=a`, the value `1` is duplicated and assigned to `b`. A nice visualisation can be found [here](https://rust-book.cs.brown.edu/ch04-01-what-is-ownership.html#variables-live-in-the-stack)
6675
6776

68-
- When assigning one variable to another variable, data is **duplicated**
69-
- For example, `a=1` `b=a`, the value `1` is duplicated and assigned to `b`
70-
- A nice visualisation can be found [here](https://rust-book.cs.brown.edu/ch04-01-what-is-ownership.html#variables-live-in-the-stack)
7177

7278
>[!success] Data management in Stack Segment is more efficient than Heap Segment
7379
>1. Stack memory is allocated and deallocated in a **Last In, First Out (LIFO) manner**, making it faster than heap memory. This is because all it needs to do is move the [[Register#Stack Pointer]] up or down, while heap memory requires more complex memory management
74-
>2. No overhead of complex [[Synchronization (同步)]], unlike data inside heap segment, data inside the stack segment is usually dedicated to that particular [[Process (进程)]] or [[Thread]]. Thus, manipulation of data inside the stack segment doesn't require the complex synchronisation
80+
>2. No overhead of complex [[Synchronization (同步)]], unlike data inside heap segment, data inside the stack segment is dedicated to that particular [[Thread]]. Thus, manipulation of data inside the stack segment doesn't require the complex synchronisation
7581
76-
>[!caution] Stack Overflow
77-
>Happens when the **size of all the stack frame** is **over** the **default fixed size** of the stack segment
7882

79-
>[!caution] Variable Size Defined before Compilation
83+
>[!caution] Attention
84+
> The size of variable on the stack is defined before Compilation
8085
8186
>[!example]- XV6-RISCV Kernel Stack
8287
>```c {13}
@@ -133,14 +138,18 @@ description: Stack (automatic memory management for function variables), Heap (d
133138
>- We load the base address of the **stack segment**
134139
>- Then based on the core id (0-7), we set the [[Register#Stack Pointer]] for each [[CPU]]. We can see the stack pointer starting point is obtained by adding `(hartid * 4096)` to the base address, this is because **stack segment grows downwards** when we are adding values to the stack
135140
136-
#### Stack Frame
141+
### Stack Frame
137142
- A section of the [[#Stack Segment]] dedicated to a **specific function call**
138143
139-
### Data Segment
144+
### Stack Overflow
145+
- Happens when the **size of all the stack frame** is **over** the **default fixed size** of the stack segment
146+
## Data Segment
147+
---
140148
- This region stores **global** and **static variables** and **constants** used by the program, pre-defined before the execution of the program
141149
- Can be both read and write, allowing the [[Process (进程)]] to manipulate the data as needed
142150
143-
### Text Segment
151+
## Text Segment
152+
---
144153
- Stores program codes, **unchangeable**, **read-only**
145154
146155
132 KB
Loading

0 commit comments

Comments
 (0)