Introduction to Assembly — Part-2/n

The_Sudo_
3 min readJun 11, 2021

segmented memory model divides the system memory into groups of independent segments referenced by pointers located in the segment registers. Each segment is used to contain a specific type of data

CPU memory consists of various number of storage locations of cells each of which has a numeric address.

There are different segments in which data gets stored and are layed out in following order

  1. Stack, it is used for quick allocation of static data.
  2. Heap, it holds dynamically allocated data
  3. .data, contains global and static data initialized to non zero values
  4. .bss, contains global and static data that is uninitialized or initialized to zero
  5. .text, contains the source code of the problem.
This is how memory laid out in Windows

STACK

Stacks are used for quick allocation of static data. Data is read and written as “last-in-first-out” . LIFO structure can be simply represented as a stack of plates . You can’t simply takeout 3rd plate from the top. you can only access the piece of data that’s on the top of the stack

User input should be stored in heap because the data has variable size . However the address of the data will be stored in stack for future reference

Two Registers are used to keep the track of the stack one is stack pointer, which is used to keep the track of top of the stack and another one is base pointer which keeps the track of base of the stack

Heap

Similar to the Stack but used for dynamic allocation and it is little slower to access . the heap is typically used for the data that is dynamic. Things such as structures and user input might be stored on the heap . When you ad data to heap, It grows towards higher addresses

Thread Environment Block

Thread Environment block stores information about the currently running Threads

Process Environment Block

Process Environment block stores information about the process and loaded modules . One piece of Information the PEB contains is “being debugged” which can be used to determine if the current process is being debugged

Stack frames are chunks of data for functions , They include local variables , Saved Base pointers , The return address of the caller and function parameters

consider following example,

main function is called first, Stack frame includes variable num before function call to square(). When main calls square() the base pointer and return addresses are both saved.

Base pointer points to the bottom of the stack. base pointer is saved because when a function is called, base pointer is updated to the base of that function’s Stack once the function returns, the base pointer is restored so it points to the base of the caller’s stack frame

return address is saved so once the function returns, program knows where to resume execution

Endianness

Endianness is the order or sequence of bytes of a word of digital data in computer memory. There are two ways computers can store data in memory

1 . Big Endianness

2. Little Endianness

Given the value 0xDEADBEEF

Big Endian , The most significant byte is stored first . this would be 0xDEADBEEF

Little Endian, The least significant byte is stored first . this would be 0xEFBEADDE

--

--