Chapter No. 01
Chapter No. 01
Organization &
Assembly Language
www.ilmirah.com
Waqas Ahmed Khilji
PhD(Progress..) + Research Scholar
Artificial Intelligent Developer(PIAIC Karachi )
(Data Science) Specialist Diploma (India)
10+ Years Professional Experience
HELLO!
Welcome to All 2
Google Classroom
https://classroom.google.com/h
Course Outline
Course Description: This course provides an introduction to the fundamental concepts of computer organization and architecture, as
well as the basics of assembly language programming. Topics covered include digital logic circuits, CPU architecture, memory systems,
input/output systems, instruction sets, addressing modes, and programming in assembly language.
Course Objectives:
To understand the architecture and organization of modern computer systems.
To gain proficiency in low-level programming using assembly language.
To explore the interaction between hardware and software at the system level.
To develop problem-solving skills through hands-on programming assignments and projects.
4
Course Outline
Presentation +
Theory Practical Live Coding +
Assignment
9
Why ?
• Computer organization and assembly language are fundamental topics in the field of computer science and engineering,
offering several key importance:
• Understanding Computer Architecture: Studying computer organization provides insights into the inner workings of computer systems,
including the CPU, memory hierarchy, input/output systems, and peripheral devices. This understanding is crucial for computer engineers
and programmers to develop efficient software and hardware systems.
• Efficient Programming: Assembly language programming allows programmers to write code that interacts directly with the hardware
components of a computer system. By understanding assembly language, programmers can optimize code for performance and efficiency,
especially in resource-constrained environments such as embedded systems and real-time applications.
• Low-Level Optimization: Knowledge of computer organization and assembly language enables programmers to write highly optimized
code by taking advantage of specific hardware features and optimizations. This level of optimization is essential for tasks such as real-time
processing, numerical computation, and system-level programming.
• Embedded Systems Development: Many embedded systems, such as microcontrollers and IoT devices, require low-level programming to
control hardware peripherals and interface with external devices. Understanding computer organization and assembly language is crucial for
developing firmware and software for embedded systems.
10
Why ?
• Reverse Engineering and Security: Knowledge of assembly language is essential for reverse engineering binary executable and analyzing
malware. Understanding computer organization and assembly language enables security researchers to identify vulnerabilities, analyze
exploits, and develop defensive strategies against cyber threats.
• Operating System Development: Operating systems require a deep understanding of computer organization to manage hardware
resources efficiently. Operating system developers need to write code that interacts closely with hardware components, such as device
drivers and system services, often using assembly language for performance-critical tasks.
• Career Opportunities: Proficiency in computer organization and assembly language opens up various career opportunities in fields such as
embedded systems development, firmware engineering, system programming, cybersecurity, and computer architecture design.
• Foundation for Advanced Studies: Computer organization and assembly language serve as foundational topics for advanced studies in
areas such as computer architecture, compiler design, operating systems, and embedded systems. A strong understanding of these topics
is essential for pursuing higher education and research in computer science and engineering.
__________________________________________________________________________________________________________________
• In summary, computer organization and assembly language are essential components of computer science and engineering education,
providing students with a deep understanding of how computers work at the hardware level and empowering them to develop efficient and
optimized software and hardware systems.
11
Lecture #01
Chapter N0.01 Introduction to computer systems:
12
An Introduction to Computer Organization & Assembly Language
• A computer is a device that can receive, store, process, and output data according to programmed
instructions. Essentially, it's an electronic machine that manipulates information, performs tasks, and
executes programs.
• Computer Architecture: Think of it as the blueprint or design of a computer. It decides what the
computer can do and how fast it can do it. It includes things like what tasks the computer can handle,
how it stores and retrieves information, and how it processes data.
• Computer Organization: This is like the physical arrangement of the parts inside the computer. It's
about how all the different pieces (like the processor, memory, and storage) are put together and
connected to make the computer work the way it's supposed to.
• Assembly language is a type of programming language that uses human-readable instructions, called
mnemonics, to represent the low-level operations performed by a computer's processor. It allows
programmers to write code that directly controls hardware components, making it powerful but also more 13
complex compared to high-level languages.
Classes of Computers
▰ Personal computers
▻ General purpose, variety of software, subject to cost/performance
▰ Server computers
▻ Network based, high capacity, performance, and reliability
▻ Range from small servers to building sized
▰ Supercomputers
▻ High-end scientific and engineering calculations
▻ Highest capability but only a small fraction of the computer market
▰ Embedded computers
▻ Hidden as components of systems
▻ Stringent power/performance/cost constraints
14
Classes of Computers
A computer system consists of hardware and systems software that work together to run application programs. Specific
implementations of systems change over time, but the underlying concepts do not. All computer systems have similar experience
hardware and software components that perform similar functions.
17
Information Is Bits + Context
18
Information Is Bits + Context
This excerpt describes the representation of a C program, "hello.c", in terms of bits and characters. Let me summarize the key points:
1. Text File /Source Program: The "hello" program starts as a source program created by a programmer using an editor and saved as a text file named
"hello.c". The source program consists of a sequence of bits, each representing a character in the program.
2. Bytes and Characters: The bits are organized into 8-bit chunks called bytes, with each byte representing a text character. Modern systems typically
use the ASCII standard to represent characters, where each character is mapped to a unique byte-sized integer value.
3. ASCII Representation: The ASCII representation of the "hello.c" program is shown in Figure 1.2. Each character in the program is represented by its
corresponding ASCII integer value.
4. Text Files vs. Binary Files: Files like "hello.c" that consist exclusively of ASCII characters are known as text files, while all other files are considered
binary files.
5. Information Representation: The fundamental idea presented is that all information in a computer system, including disk files, programs in memory,
user data, and network-transferred data, is represented as bits. The context in which we view these bits determines the interpretation of the data.
6. Understanding Machine Representations: Programmers need to understand machine representations of numbers because they differ from integers
and real numbers. They are finite approximations that can behave unexpectedly.
19
Programs Are Translated by Other Programs into Different Forms
The transformation of a high-level C program, such as "hello.c," into a form executable by the computer.
1. High-Level C Program: Initially, the program exists in a high-level language, such as C. This version is readable and understandable by
humans. For example, the "hello.c" program you provided earlier is written in C and is readable by programmers.
2. Translation to Machine Language: To execute the program on a computer system, the high-level C statements must be translated into low-
level machine-language instructions. This translation is typically performed by a compiler, which is a program designed to translate source
code into machine code.
3. Executable Object Program: Once translated, the machine-language instructions are packaged into an executable object program, often
referred to as an executable object file. This file contains the binary representation of the program that the computer can directly execute.
20
Programs Are Translated by Other Programs into Different Forms
4. Storage as Binary Disk File: The executable object program is then stored as a binary disk file on the system's storage device. This
file contains the machine-language instructions necessary to run the program.
5. Execution: When the program is executed, the operating system loads the executable object program into memory and directs the
processor to execute its instructions. As a result, the "hello, world" message is printed to the console.
6. On Unix systems, the translation from the source file ("hello.c") to the object file is typically performed by a compiler
driver
21
Compilation Systems Work
The compilation process of a C program, "hello.c," using the GCC compiler driver.
1. Preprocessing Phase: The preprocessor (cpp) reads the source file, "hello.c," and processes directives starting with '#' symbols. For example, the
'#include <stdio.h>' directive instructs the preprocessor to include the contents of the system header file "stdio.h" into the program. The output of this
phase is another C program, typically with the ".i" suffix, called "hello.i".
2. Compilation Phase: The compiler (cc1) translates the preprocessed C program, "hello.i", into an assembly-language program stored in a text file named
"hello.s". Each statement in the assembly-language program corresponds to one low-level machine-language instruction.
3. Assembly Phase: The assembler (as) takes the assembly-language program, "hello.s", and translates it into machine-language instructions. These
instructions are packaged into a relocatable object program, which is stored in the binary file "hello.o". This file contains binary representations of
machine-language instructions.
4. Linking Phase: The linker (ld) combines the object file "hello.o" with any necessary precompiled object files, such as "printf.o", which contains the
implementation of the printf function from the standard C library. This merging process produces the final executable object file, "hello", which is
ready to be loaded into memory and executed by the system.
This compilation process involves several stages, each performed by a specific program or component, resulting in the transformation of the high-level C
source code into a binary executable file that can be run on the system.
22
It Pays to Understand How Compilation Systems Work
Several important reasons why programmers should understand how compilation systems work:
1. Optimizing Program Performance: While modern compilers generally produce efficient code, understanding how C code translates into
machine instructions helps programmers make informed decisions for writing efficient code. This includes understanding the performance
implications of different language constructs (like switch vs. if-else), memory access patterns, and loop optimizations.
2. Understanding Link-Time Errors: Linker errors can be challenging, especially in large software projects. Understanding linker behavior, such
as resolving references, managing global variables, and handling libraries, is crucial for effective software development. Additionally,
understanding why some errors only appear at runtime is important.
3. Avoiding Security Holes: Buffer overflow vulnerabilities have historically been significant security risks in software. Understanding stack
discipline, buffer overflow vulnerabilities, and security measures at the programmer, compiler, and operating system levels is essential for
writing secure code.
23
Processors Read and Interpret Instructions Stored in Memory
how to run the executable file generated from the "hello.c" source program on a Unix system using the shell:
1. Compilation to Executable Object File: After translating the "hello.c" source program into an executable object file named "hello", the file is
stored on disk. This executable file contains the machine-language instructions necessary to run the program.
2. Running the Executable File: To run the program, you type its name (in this case, "hello") into the shell prompt. For example:
3. Shell Interpretation: The shell, which is a command-line interpreter, receives the command. It prints a prompt and waits for you to type a
command line. When you enter "./hello", the shell interprets it as a request to execute the "hello" program.
4. Execution by the Shell: If the first word of the command line does not correspond to a built-in shell command, the shell assumes it is the
name of an executable file to load and run. Therefore, the shell loads the "hello" program and executes it.
5. Program Execution: The "hello" program executes, printing its message to the screen ("hello, world"), and then terminates.
6. Shell Prompt: After the program terminates, the shell prints another prompt and waits for the next input command line, allowing further 24
interactions with the user.
Hardware Organization of a System
• To understand what happens to our hello program when we run it, we need to understand the hardware organization of a typical system
25
Hardware Organization of a System
Buses
• Function: Buses are electrical conduits that facilitate the transfer of bytes of information between different components of the system.
• Word Size: Buses typically transfer fixed-sized chunks of bytes known as words. The number of bytes in a word, known as the word size, varies
across systems. Common word sizes include 4 bytes (32 bits) or 8 bytes (64 bits).
• Role of I/O Devices: I/O devices serve as the system's interface with the external world, enabling users to input data and receive output.
Examples include keyboards, mice, displays, and disk drives for long-term data storage. Initially, the executable hello program resides on the disk.
• Connection to I/O Bus: Each I/O device is connected to the I/O bus through either a controller or an adapter. Controllers are chipsets within the
device or on the motherboard, while adapters are cards that plug into motherboard slots. Despite the packaging difference, both serve the purpose
of transferring information between the I/O bus and the device.
26
Hardware Organization of a System
Main memory
Main memory serves as a temporary storage device in a computer system, holding both the program instructions and the data being manipulated
by the processor during program execution. Physically, it comprises a collection of dynamic random access memory (DRAM) chips. Logically,
memory is organized as a linear array of bytes, each with a unique address starting at zero.
In general, machine instructions making up a program can vary in size, and the sizes of data items corresponding to C program variables differ
based on their types. For instance, on an IA32 machine running Linux, a short data type requires two bytes, while int, float, and long require four
bytes, and double requires eight bytes.
Processor
The processor, or CPU, is the core component of a computer system responsible for executing instructions stored in main memory. It interprets
instructions using a program counter (PC), executing them in a sequential manner. Basic operations include loading data from memory into registers,
performing arithmetic/logic operations, and storing results back in memory. While processors seem to operate on a simple instruction set
architecture, modern processors employ complex mechanisms to enhance performance. Understanding the processor's operation is essential for
comprehending program execution and optimizing performance.
27
Running the hello Program
In running a program like "hello," the processor executes instructions from the program's binary representation stored in memory. Key
operations include loading data from memory to registers, performing arithmetic/logic operations, storing results back in memory, and changing
the program flow through jumps. Despite appearing simple, modern processors employ complex mechanisms to optimize performance. When
executing the "hello" program, the shell loads the executable file into memory, where the processor reads and executes its machine-language
instructions, ultimately displaying the desired output on the screen. This process involves various steps, including data transfer from disk to
memory using direct memory access (DMA).
28
Running the hello Program
29
Running the hello Program
30
Caches Matter
• A system spends a lot of time moving information from one place to another. from storage devices like disk drives to main memory and
ultimately to the processor for execution. It highlights the overhead associated with these copying operations, which can slow down program
execution and impact system performance.
• larger storage devices are slower than smaller ones. For example, accessing data from disk drives can be millions of times slower than accessing
data from main memory. Similarly, while register files store only a few hundred bytes of information, the processor can read data from them
much faster than from main memory.
• To bridge the processor-memory gap, system designers incorporate cache memories as smaller, faster storage devices.
• Cache memories serve as temporary staging areas for frequently accessed information, helping to reduce the time spent accessing slower main
memory. The excerpt explains the structure of cache memories, including L1 and L2 caches, and how they exploit the principle of locality to
improve memory access times.
31
Storage Devices Form a Hierarchy
The concept of a memory hierarchy in computer systems, where storage devices are organized in levels based on their speed, size, and cost per byte.
At the top of the hierarchy is the register file (level 0 or L0), followed by multiple levels of cache memory (L1 to L3), main memory (level 4), and
secondary storage devices such as disks (level 5 and beyond).
The main idea behind the memory hierarchy is that storage at one level serves as a cache for storage at the next lower level. For example, the
register file acts as a cache for the L1 cache, which in turn acts as a cache for the L2 cache, and so on. Ultimately, the lower levels of the hierarchy
serve as caches for data stored in slower, larger, and cheaper storage devices like disks.
32
Storage Devices Form a Hierarchy(Continue..)
33
The Operating System Manages the Hardware
The role of the operating system (OS) in a computer system.
Fundamental abstractions shown in Figure 1.11: processes, virtual memory, and files. As this figure suggests, files are
abstractions for I/O devices, virtual memory is an abstraction for both the main memory and disk I/O devices, and
processes are abstractions for the processor, main memory, and I/O devices.
35
The Operating System Manages the Hardware
In the context of the operating system, there are three fundamental abstractions: processes, virtual memory, and files. These
abstractions serve as mechanisms for the operating system to protect the hardware from misuse and provide a uniform interface for
application programs to interact with different low-level hardware devices.
Processes: The illusion of single user on a busy system
• A process is an abstraction that represents a running program.
• The OS creates the illusion that each process has exclusive use of the CPU, memory, and I/O devices.
• In reality, the OS rapidly switches between processes (context switching) making it seem like they all run concurrently (at the same
time).
• This allows multiple programs to seemingly run at once, even on a system with just one CPU.
Context Switching: The magic behind multitasking
• The OS keeps track of each process's state (context) including program counter (PC), registers, and memory contents.
• When the OS decides to switch processes, it saves the current process's context and restores the context of the new process.
• This allows the new process to pick up exactly where it left off, creating the illusion of uninterrupted execution.
36
Processes
37
Processes
38
Threads
The concept of threads within a process in modern computer systems. While traditionally processes were considered to have a single
control flow, modern systems allow processes to consist of multiple execution units called threads.
Threads within Processes:
• While traditionally processes were seen as single-threaded, modern systems allow them to have multiple threads.
• Shared Resources: Threads within a process share the same code and global data, enabling efficient communication and data
exchange.
• Importance of Threads: There are several reasons why threads are becoming increasingly important:
• Concurrency Needs: Network servers heavily rely on concurrency, and threads provide a good way to handle multiple requests
simultaneously.
• Data Sharing: Sharing data between threads within the same process is simpler compared to sharing data between separate
processes.
• Efficiency: Threads are generally lighter-weight than processes, leading to better performance, especially when dealing with
multiple processors
39
Virtual Memory
Virtual memory is an abstraction that provides each process with the illusion that it has exclusive use of the main memory.
Each process has the same uniform view of memory, which is known as its virtual address space. The virtual address space
for Linux processes is shown in Figure 1.13.
40
Virtual Memory
Heap. The code and data areas are followed immediately by the run-time heap. Unlike the code and data areas, which are fixed in size
once the process begins running, the heap expands and contracts dynamically at run time as a result
of calls to C standard library routines such as malloc and free.
. Shared libraries. Near the middle of the address space is an area that holds the code and data for shared libraries such as the C
standard library and the math library. The notion of a shared library is a powerful but somewhat difficult concept.
. Stack. At the top of the user’s virtual address space is the user stack that the compiler uses to implement function calls. Like the heap,
the user stack expands and contracts dynamically during the execution of the program. In particular, each time we call a function, the stack
grows. Each time we return from a function,
. Kernel virtual memory. The kernel is the part of the operating system that is always resident in memory. The top region of the address
space is reserved for the kernel. Application programs are not allowed to read or write the contents of this area or to directly call functions
defined in the kernel code.
41
File
A file is a sequence of bytes, nothing more and nothing less. Every I/O device, including disks, keyboards, displays, and even
networks, is modeled as a file. All input and output in the system is performed by reading and writing files, using a small set of
system calls known as Unix I/O This simple and elegant notion of a file is nonetheless very powerful because it provides
applications with a uniform view of all of the varied I/O devices that might be contained in the system. For example, application
programmers who manipulate the contents of a disk file are blissfully unaware of the specific disk technology. Further, the same
program will run on different systems that use different disk technologies.
42
System Communication with Other Systems Using Networks
• Introduction to Networked Systems: The passage begins by highlighting that modern systems are often
interconnected through networks rather than operating in isolation. It describes how data transfer across networks is
analogous to input/output (I/O) operations, where data is transferred between main memory and peripheral devices.
43
System Communication with Other Systems Using Networks
• Example with Telnet: The text presents an example scenario using the telnet application to run a program remotely on
another machine. It outlines the process of initiating a telnet session, sending a command to the remote machine, executing
the command remotely, and receiving the output back to the local machine. This example illustrates the typical interaction
44
THANKS!
Any questions?
You can find me at
45