Mind blowing journey
grafeno30
Posted on May 13, 2023
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.
Algorithm Sum
Var
Sum = 0
Begin
Read a, b
Sum = a + b
Write Sum
End
Executable (Binary machine code)
Machine code instructions for a physical CPU.
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)
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) -
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:
We are going to use the following system calls:
- 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
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 -
Let’s install compiler gcc:
sudo apt install build-essential
sudo apt install gcc
gcc --version
gcc, 15 million lines of code in 2019 (it is one of the biggest free program)
Let's create a C program:
- nano hello.c:
#include <stdio.h>
int main (void)
{
printf ("Hello, world!\n");
return 0;
}
gcc -Wall hello.c -o hello2
./hello2
Programming language - C++ language -
Let's create a C program:
1) To install cpp compiler
sudo apt install g++
g++ --version
2) nano hello_cpp.cpp:
#include <iostream>
int main(int argc, char** argv) {
std::cout << "Hello, world!" << std::endl;
return 0;
}
3) g++ -Wall -pedantic -o hello2 hello_cpp.cpp
4)./hello2
Programming language - Java language -
Let's create a Java program:
1) Install jdk and jre:
sudo apt install default-jdk
sudo apt install default-jre
2) nano hello.java:
class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
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)
6) java HelloWorld
Output:
Hello, World!
Programming language - Python -
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:
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
November 29, 2024