Mind blowing journey

grafeno30

grafeno30

Posted on May 13, 2023

Mind blowing journey

Warning. You must have a linux distribution to follow this post

TOC:

Pseudocode
Executable (Binary machine code)
Scripts - Bash script (text file) -
Assembler -Netwide Assembler (NASM) -
programming language - C language -
programming language - C++ language -
programming language - Java language -
programming language - Python -

Pseudocode

Pseudocode is a plain language description of the steps in an algorithm. It is intended for human reading.

Image description

Image description

Algorithm Sum
Var
    Sum = 0
Begin
    Read a, b
    Sum = a + b
    Write Sum
End
Enter fullscreen mode Exit fullscreen mode

Executable (Binary machine code)

Machine code instructions for a physical CPU.

Image description

Image description

EXE is a common filename extension denoting an executable file for Microsoft Windows

ELF: The Executable and Linkable Format is a common standard file format for executable files, object code, shared libraries….

If you run the command file you will get information about the executable. Try the following:

file /usr/bin/ls (Please, pause and try it!)

“ls” is a command but it is also executable program (ELF)

To display file headers of an elf file:

readelf -h /usr/bin/ls (Please, pause and try it!)

Magic numbers is the name given to constant sequences of bytes (usually) at the beginning of files, used to mark those files as being of a particular file format. They serve a similar purpose to file extensions.

How to see the executable inside?

hexdump -C /usr/bin/ls | less (Please, pause and try it!)

You could see what system calls are made using the command “strace”

strace /usr/bin/ls | more (Please, pause and try it!)

Scripts - Bash script (text file)

Image description

Follow the next steps:

1)nano hello.sh

2) Add the following code:

#!/bin/bash
echo "Hello World"

3) Set the script executable permission by running chmod command:

chmod +x hello.sh

4) Run or execute the script using following syntax:

./hello.sh

You can also do step 2 and 3 as follows: bash ./hello.sh

Assembler -Netwide Assembler (NASM) -

Image description

Let’s install NASM:

sudo apt update
sudo apt install nasm

The Netwide Assembler (NASM) is an assembler for the Intel x86 architecture. It can be used to write 16-bit, 32-bit and 64-bit (x86-64) programs.

List of available records:

Image description

We are going to use the following system calls:

Image description

  1. nano hello.asm:
global    _start


          section   .text


_start:   mov  rax, 1      ; system call for write
          mov  rdi, 1      ; file handle 1 is stdout
          mov  rsi, message ; address of string to output
          mov  rdx, 13                 ; number of bytes
          syscall   ;invoke operating system to do the write
          mov  rax, 60                 ; system call for exit
          xor  rdi, rdi                ; exit code 0
          syscall        ; invoke operating system to exit


          section   .data
message:  db        "Hello, World", 10     
 ; note the newline at the end

Enter fullscreen mode Exit fullscreen mode

Execute the following command:

nasm -felf64 hello.asm && ld hello.o && ./a.out

From the txt file called hello.asm an object file “hello.o” is created and then “ld” command creates an executable file “a.out” (ld, command to link object files)

To display file headers elf file:

readelf -h ./a.out

Programming language - C language -

Image description

Let’s install compiler gcc:

sudo apt install build-essential
sudo apt install gcc 
gcc --version
Enter fullscreen mode Exit fullscreen mode

gcc, 15 million lines of code in 2019 (it is one of the biggest free program)

Let's create a C program:

  1. nano hello.c:
#include <stdio.h>

int main (void)
{
  printf ("Hello, world!\n");
  return 0;
}
Enter fullscreen mode Exit fullscreen mode
  1. gcc -Wall hello.c -o hello2

  2. ./hello2

Programming language - C++ language -

Image description

Let's create a C program:

1) To install cpp compiler

sudo apt install g++
g++ --version
Enter fullscreen mode Exit fullscreen mode

2) nano hello_cpp.cpp:

#include <iostream>

int main(int argc, char** argv) {
  std::cout << "Hello, world!" << std::endl;
  return 0; 
}

Enter fullscreen mode Exit fullscreen mode

3) g++ -Wall -pedantic -o hello2 hello_cpp.cpp

4)./hello2

Programming language - Java language -

Image description

Let's create a Java program:

1) Install jdk and jre:

sudo apt install default-jdk
sudo apt install default-jre

Enter fullscreen mode Exit fullscreen mode

2) nano hello.java:

class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello, World!"); 
    }
}
Enter fullscreen mode Exit fullscreen mode

3) javac hello.java

This creates a file called hello.class (it is in bytecode, which is code that can be executed by the Java VM).

4) file HelloWorld.class (this command is optional, it is only for didactic purposes)
HelloWorld.class: compiled Java class data, version 55.0

5) hexdump -C HelloWorld.class (this command is optional, it is only for didactic purposes)

Image description

6) java HelloWorld
Output:
Hello, World!

Programming language - Python -

Image description

Don't have python installed? Use the following script:

Let's create a python program:

1) nano hello.py:
print("Hello world!")
2)python3 hello.py
Output:
Hello world!

Sources:

💖 💪 🙅 🚩
grafeno30
grafeno30

Posted on May 13, 2023

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related