Category: Programming Languages

  • C Language Compiled Code

    C Language Compiled Code

    C Language Compiled Code refers to the machine-readable version of a program written in the C programming language. When you write C source code in human-readable files (.c and .h), it cannot be executed directly by a computer. A compiler translates this source code through several stages including preprocessing, compilation to assembly, assembling into object code, and linking with libraries to produce a standalone executable file. The resulting compiled code consists of binary instructions that the CPU can interpret and run directly. This makes compiled code fundamentally different from the original C source code, which is intended for humans to read, write, and modify.

    Source Code

    The code is in C language (.c and .h files)

    cat # Command used to display the contents of a file
    test.c # The C source file whose contents will be printed to the terminal

    (host) cat test.c

    #include <stdio.h> // Includes the Standard Input/Output library so we can use functions like printf()

    int main(){ // The main function: program execution starts here
      printf(“Hello World!”); // Prints the text “Hello World!” to the standard output (usually the terminal)
      return 0; // Ends the program and returns 0 to the operating system indicating successful execution
    }

    #include <stdio.h>

    int main(){
    printf("Hello World!");
    return 0;
    }

    Pre-Processor

    This step includes the headers and expand the macros, the generated file have the .i, .ii extensions  

    gcc # GNU C Compiler
    -E # Tells gcc to run only the preprocessor (expands #include, #define, etc.)
    test.c # The C source file being processed
    | # Pipe operator; sends the output of the command on the left to the command on the right
    head # Displays only the first 10 lines of the output

    (host) gcc -E test.c | head

    # 0 "test.c"
    # 0 "<built-in>"
    # 0 "<command-line>"
    # 1 "/usr/include/stdc-predef.h" 1 3 4
    # 0 "<command-line>" 2
    # 1 "test.c"
    # 1 "/usr/include/stdio.h" 1 3 4
    # 28 "/usr/include/stdio.h" 3 4

    Compiler

    The expanded code/preprocessed code gets converted into assembly code, the generated file have the .s, .asm extensions 

    gcc # GNU C Compiler
    -S # Tells gcc to compile the code into assembly language but not create an executable
    test.c # The C source file being compiled

    (host) gcc -S test.c

    cat # Command used to display the contents of a file
    test.s # The generated assembly language file from the compilation step

    (host) cat test.s

            .file   "test.c"
            .text
            .section        .rodata
    .LC0:
            .string "Hello World!"
            .text
            .globl  main
            .type   main, @function
    main:
    .LFB0:
            .cfi_startproc
            pushq   %rbp
            .cfi_def_cfa_offset 16
            .cfi_offset 6, -16
            movq    %rsp, %rbp
            .cfi_def_cfa_register 6
            leaq    .LC0(%rip), %rax
            movq    %rax, %rdi
            movl    $0, %eax
            call    printf@PLT
            movl    $0, %eax
            popq    %rbp
            .cfi_def_cfa 7, 8
            ret
            .cfi_endproc
    .LFE0:
            .size   main, .-main
            .ident  "GCC: (Debian 14.2.0-16) 14.2.0"
            .section        .note.GNU-stack,"",@progbits

    Assembler

    The assembly code gets converted into object code/machine code, the generated file have the .o, .obj extensions  

    gcc # GNU C Compiler
    -c # Compile the source code into an object file (machine code) without linking
    test.c # The C source file being compiled

    (host) gcc -c test.c

    cat # Outputs the contents of the file
    test.o # Object file containing compiled machine code
    | # Pipe operator; sends output of the left command to the right command
    xxd # Converts binary data into a hexadecimal (hex) and ASCII representation
    | # Pipe operator again
    head # Displays only the first 10 lines of the hex output

    (host) cat test.o | xxd | head

    00000000: cffa edfe 0c00 0001 0000 0000 0100 0000  ................
    00000010: 0400 0000 b801 0000 0020 0000 0000 0000  ......... ......
    00000020: 1900 0000 3801 0000 0000 0000 0000 0000  ....8...........
    00000030: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    00000040: 6800 0000 0000 0000 d801 0000 0000 0000  h...............
    00000050: 6800 0000 0000 0000 0700 0000 0700 0000  h...............
    00000060: 0300 0000 0000 0000 5f5f 7465 7874 0000  ........__text..
    00000070: 0000 0000 0000 0000 5f5f 5445 5854 0000  ........__TEXT..
    00000080: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    00000090: 3400 0000 0000 0000 d801 0000 0200 0000  4...............
    ...
    ...
    ...

    Linker

    The last step is combining the object code/machine code and .lib/.a static library files, the generated file have the .exe, elf, bin extensions

    gcc # GNU C Compiler used to compile and link the program
    test.c # The C source file being compiled
    -o # Option to specify the name of the output file
    test.bin # Name of the final executable binary file that will be created

    (host) gcc test.c -o test.bin

    cat # Outputs the contents of the file
    test.bin # The compiled executable binary file
    | # Pipe operator; sends output of one command to another
    xxd # Converts binary data into hexadecimal and ASCII representation
    | # Pipe operator again
    head # Displays the first 10 lines of the output

    (host) cat test.bin | xxd | head

    00000000: 7f45 4c46 0201 0100 0000 0000 0000 0000  .ELF............
    00000010: 0300 3e00 0100 0000 5010 0000 0000 0000  ..>.....P.......
    00000020: 4000 0000 0000 0000 9036 0000 0000 0000  @........6......
    00000030: 0000 0000 4000 3800 0e00 4000 1f00 1e00  ....@.8...@.....
    00000040: 0600 0000 0400 0000 4000 0000 0000 0000  ........@.......
    00000050: 4000 0000 0000 0000 4000 0000 0000 0000  @.......@.......
    00000060: 1003 0000 0000 0000 1003 0000 0000 0000  ................
    00000070: 0800 0000 0000 0000 0300 0000 0400 0000  ................
    00000080: 9403 0000 0000 0000 9403 0000 0000 0000  ................
    00000090: 9403 0000 0000 0000 1c00 0000 0000 0000  ...............
    ...
    ...
  • Python Introduction

    Python Introduction

    Computer Programming

    Computer Programming is the process of designing, creating, testing, and maintaining programs to perform specific tasks or solve particular problems. It involves translating logical solutions into a language computers can understand and execute. Programmers use languages like Python, Java, C++, or JavaScript to write code that instructs computers on how to carry out required operations.


    Python

    Python is a high-level, interpreted programming language known for its simplicity, readability, and versatility. It was first released in 1991 as Python 0.9.0 by Guido van Rossum. Python is cross-platform, so it can run on various operating systems like Windows, macOS, and Linux without significant code changes.

    Python supports object-oriented programming, allowing developers to design software using objects that encapsulate data and behavior. It also supports procedural and functional programming paradigms, making it highly flexible. As an interpreted language, Python executes code line by line, which facilitates rapid development, easier debugging, and interactive testing.


    Interpreted (?)

    Interpreted refers to a method of program execution where the source code is not directly compiled into machine code beforehand. Instead, the code is executed line by line or in small sections at runtime, often through an interpreter.

    Interpretation Process

    1. Source Code: The programmer writes Python code in a human-readable form.
    2. Compilation to Bytecode: Before execution, Python automatically translates the source code into bytecode, which is a low-level, platform-independent representation of the code.
    3. Execution by the Python Virtual Machine (PVM): The PVM reads and executes the bytecode, translating it into machine-level instructions that the computer can understand.

    Advantages

    • Cross-Platform Compatibility: Since the bytecode is platform-independent, the same Python program can run on any system with a compatible Python interpreter.
    • Ease of Debugging: Errors are caught during runtime, making testing and debugging easier, especially for beginners.
    • Interactive Development: Python supports interactive execution, allowing developers to test code snippets immediately without compiling the entire program.

    Compiled

    Compiled refers to a method of program execution where the source code written by the programmer is directly translated into machine code by a compiler before the program is run. Machine code consists of low-level instructions that a computer’s processor can execute directly.

    Compilation Process:

    1. Source Code: The programmer writes human-readable code in a programming language such as C, C++, or Rust.
    2. Compilation: The compiler analyzes the source code, checks it for errors, and translates it into machine code specific to the target processor and operating system. This results in an executable file.
    3. Linking: If the program uses external libraries or modules, the compiler links them together with the machine code to create a complete, runnable program.
    4. Execution: The final executable file can be run directly by the computer without needing an interpreter.

    Advantages

    • Faster Execution: Compiled programs generally run faster because the code is already translated into machine language.
    • Platform-Specific: The resulting executable is usually specific to the operating system and processor architecture it was compiled for.
    • Error Detection: Compilation detects many errors before the program is run, which can make debugging easier.
    • No Runtime Translation: Unlike interpreted languages, compiled code does not require an interpreter to execute.

    Cross-Platform

    Cross-Platform refers to software, applications, or programming languages that can run on multiple operating systems or hardware platforms with minimal modification. This means the same program can function on Windows, macOS, Linux, and sometimes even mobile operating systems like Android or iOS.


    High-Level

    A High-Level Programming Language is designed to be human-readable and easy to use. Unlike low-level languages, which are close to machine code and hardware operations, high-level languages use natural language elements, symbols, and abstractions that are intuitive for programmers.


    Object-Oriented

    OOP is a programming paradigm that organizes software design around objects—instances of classes. Objects are self-contained units containing data (attributes) and code (methods) that operate on that data. This approach models real-world entities, making programs easier to design, understand, and maintain.

    Concepts

    • Class: A blueprint or template for creating objects, defining attributes and methods.
    • Object: A specific instance of a class with unique attribute values but shared structure.
    • Encapsulation: Keeping an object’s data and methods together while restricting outside access, providing controlled interaction.
    • Inheritance: Allowing a subclass to inherit properties and methods from a superclass, promoting code reuse.
    • Polymorphism: Enabling different objects to respond differently to the same method call, allowing flexible and interchangeable code.
    • Abstraction: Hiding complex implementation details and exposing only necessary parts.

    Advantages

    • Modularity: Programs can be broken into reusable, independent objects.
    • Maintainability: Changes to one object typically do not affect others.
    • Reusability: Code is reused through inheritance and composition.
    • Real-World Modeling: Objects naturally map to real-world entities, making problem-solving intuitive.

    Kernel Mode v.s. User Mode

    The kernel is the core component of an operating system (OS) that manages system resources and facilitates communication between hardware and software. It acts as a bridge between user-level applications and computer hardware, ensuring safe and efficient use of resources like memory, CPU, and input/output devices.

    Kernel-Level Applications (Privileged Mode)

    • Run in kernel mode, also known as privileged mode, with full access to all hardware and system resources.
    • Considered trusted because improper behavior can crash the system or compromise security.
    • Examples: device drivers, memory management routines, core OS functions.

    User-Level Applications (Restricted Mode)

    • Run in user mode, a restricted environment that limits access to hardware and critical resources.
    • Considered less trusted, so the OS restricts their actions to prevent accidental or malicious damage.
    • Examples: web browsers, word processors, games.

    Interaction via System Calls

    • User-level applications cannot directly access hardware or kernel resources. They communicate with the kernel through system calls.
    • A system call is a controlled interface that allows programs to request services like file operations, network communication, or memory allocation.

    Kernel’s Role in Handling Requests

    • The kernel executes requested operations in privileged mode when it receives a system call.
    • After completing the task, the kernel returns control to the user-level application, maintaining system stability and security.

    The kernel is crucial for system stability, security, and efficiency. By separating user mode from kernel mode, operating systems ensure that user applications are restricted while trusted kernel processes manage hardware and resources safely. This separation prevents accidental or malicious misuse of critical system functions and enables multitasking by controlling access to shared resources.

    Example

    Using the standard C library to output Hello World! to the standard output device

    cat # Command used to display the contents of a file
    test.c # The C source file whose contents will be printed to the terminal

    (host) cat test.c

    #include <stdio.h> // Includes the Standard Input/Output library so we can use functions like printf()

    int main(){ // The main function: program execution starts here
      printf(“Hello World!”); // Prints the text “Hello World!” to the standard output (usually the terminal)
      return 0; // Ends the program and returns 0 to the operating system indicating successful execution
    }

    #include <stdio.h>

    int main(){
    printf("Hello World!");
    return 0;
    }

    gcc # GNU C Compiler program used to compile C source code
    test.c # The C source file being compiled
    -o # Option that tells the compiler to name the output file
    test.exe # Name of the compiled executable file that will be created

     (host) gcc test.c -o test.exe

    Output

    Hello World!

    Step by step of the process

    1. User-mode: Program Execution
      • test.exe is loaded into user mode memory.
      • main() function begins execution in user mode.
    2. User-mode: Calling the C Library
      • printf("Hello World!") function is called within main() function.
      • printf() function is part of the C standard library and runs in user mode.
    3. System Call: Switching to Kernel-mode
      • printf internally calls write() system call to send data to stdout.
      • The write() system call is invoked, which causes a context switch from user mode to kernel mode.
    4. Kernel-mode: Writing Output
      • The kernel’s write function is executed with the provided data.
      • The kernel accesses the terminal or console device buffer.
      • The string "Hello World!" is written to the hardware output (e.g., screen).
      • The kernel ensures that the operation is performed safely and that access control is maintained.
    5. Kernel-mode: Return to User-mode
      • After the write operation completes, the kernel returns the result (number of bytes written or error code) back to user mode.
    6. User-mode: Continuing Execution
      • printf() function receives the result from the kernel and completes its execution.
      • The main() function continues executing and returns 0, indicating successful termination.
    7. Program Exit
      • The program exits cleanly, and control is returned to the operating system.

    Some General-Purposes

    Automation and Testing

    Python is widely used for automating repetitive tasks such as file handling, data entry, and system monitoring. It is popular in software testing with frameworks like PyTest and unittest, allowing developers to write automated tests for applications.

    • Example: Automating data extraction from spreadsheets or websites.

    Web Development, Game Development, and Business Applications

    Python powers web development through frameworks like Django and Flask, enabling the creation of scalable and secure web applications. In game development, Python is used with libraries like Pygame to create 2D games and prototypes.

    • Examples: Inventory management, accounting, CRM systems, and desktop software, all benefiting from Python’s rapid development capabilities.

    Data Science, Machine Learning, and Deep Learning

    Python is the primary language for data science, offering libraries such as NumPy, Pandas, Matplotlib, and Seaborn for data analysis and visualization. In machine learning, libraries like scikit-learn, TensorFlow, and PyTorch enable developers to build predictive models and AI applications.

    • Deep learning frameworks in Python are used to create neural networks for tasks like image recognition, natural language processing, and other advanced AI applications.

    Why Python?

    • Easy to learn: Python mimics English; it employs a streamlined syntax focusing on natural language, making learning more straightforward for newcomers.
    • Flexible and reliable: A language for general-purpose programming, it can be used to develop any application with minimum effort.
    • Extremely active community: The community has all sorts of Python professionals that provide access to documentation or answer your questions.

    Python IDLE

    Python’s IDLE is the integrated development environment (IDE) that can be downloaded on different operating systems

    Install Python on macOS

    1. Download the latest Python version from here & Install it
    2. Go to Launchpad and look for IDLE (Integrated Development and Learning Environment)
    3. Or, go to Launchpad, then Others, then Terminal, and finally type python3 there

    Install Python on Windows 64bit

    1. Download the latest Python version from here
    2. When installing it, make sure that you check the “Add Python … to PATH”
    3. Go to Windows Search, then type IDLE (integrated development learning environment)

    Install Python on Linux-Based (Ubuntu)

    1. Go to Terminal, then run sudo apt-get install idle3
    2. Go to Show Applications and look for IDLE
    3. Or, go to Show Applications, look for Terminal, then type IDEL there

    Other options

    Native Apps

    Web Apps