0️⃣0️⃣ Assembly -> Reverse
https://guyinatuxedo.github.io/01-intro_assembly/assembly/index.html
How do processors work?
What are CPUs?
Definition: A Central Processing Unit (CPU) is the primary component of a computer that performs most of the processing inside the computer. It executes instructions from programs through basic arithmetic, logic, control, and input/output (I/O) operations specified by the instructions.
Key Points:
Often referred to as the "brain" of the computer.
Contains components like the Arithmetic Logic Unit (ALU) and Control Unit (CU).
Executes a sequence of stored instructions called a program.
Manages data processing, memory access, and task scheduling.
What are Registers?
Definition: Registers are small, fast storage locations within the CPU that hold data, addresses, or instructions temporarily. They are used by the CPU to quickly access and manipulate data during instruction execution.
Key Points:
Faster than main memory (RAM).
Include general-purpose registers (e.g., EAX, EBX, ECX, EDX in x86 architecture) for arithmetic and data storage.
Special-purpose registers include the Instruction Pointer (EIP) which holds the address of the next instruction to execute, and the Stack Pointer (ESP) which points to the top of the stack.
![](https://felix-billieres.gitbook.io/~gitbook/image?url=https%3A%2F%2F2346124906-files.gitbook.io%2F%7E%2Ffiles%2Fv0%2Fb%2Fgitbook-x-prod.appspot.com%2Fo%2Fspaces%252FO8Hp1HtgjLpE41gU26RP%252Fuploads%252FHiioR7tOGL6jkvZ4EspA%252Fimage.png%3Falt%3Dmedia%26token%3D37348438-6afc-422e-93da-d55faaa1d225&width=768&dpr=4&quality=100&sign=f0eb3087c89c0fc227a03541f5f852b9e76cc84c3162da62a473b2d8f87b0fca)
What is the Stack?
Definition: The stack is a data structure used in computer memory to store data temporarily. It operates on a Last In, First Out (LIFO) basis, meaning the last element added is the first to be removed.
Key Points:
Used for function call management, storing return addresses, local variables, and passing parameters.
Managed via the Stack Pointer (ESP in x86), which points to the top of the stack.
Stack operations include
PUSH
(to add data to the stack) andPOP
(to remove data from the stack).Crucial for managing function calls and supporting recursion.
What is ELF?
Definition: ELF stands for Executable and Linkable Format. It is a standard file format for executables, object code, shared libraries, and core dumps used in Unix-like operating systems.
Key Points:
Purpose: Facilitates the storage and loading of binary executables and shared libraries. It standardizes how these files are structured.
Assembly Code (ex1.asm
)
ex1.asm
)Line-by-Line Explanation:
global _start
:This line declares the
_start
label as global, which means it can be used as an entry point when linking. This is necessary because the linker needs to know where the execution of the program should begin.
_start:
:This label marks the beginning of the program. The linker will set this as the entry point for the executable.
mov eax, 1
:This instruction moves the value
1
into theeax
register. In Linux system calls, settingeax
to1
indicates that we want to call theexit
system call.
mov ebx, 42
:This instruction moves the value
42
into theebx
register. For theexit
system call,ebx
holds the exit status code. Here,42
is the exit code that the program will return.
int 0x80
:This instruction triggers a software interrupt, signaling the kernel to execute a system call. The
eax
register value determines which system call to perform, andebx
provides the argument for the system call.
Commands
nasm -f elf32 ex1.asm -o ex1.o
:This command assembles the assembly source file
ex1.asm
into an object fileex1.o
using the Netwide Assembler (NASM).-f elf32
: Specifies the output format as 32-bit ELF (Executable and Linkable Format).-o ex1.o
: Specifies the output file name.
ld -m elf_i386 ex1.o -o ex1
:This command links the object file
ex1.o
to create an executable namedex1
.-m elf_i386
: Specifies the target architecture as 32-bit Intel (i386).-o ex1
: Specifies the output file name for the executable.
./ex1
:This command runs the newly created executable
ex1
.
echo $?
:This command prints the exit status of the last executed command (
./ex1
in this case). The exit status is the value set in theebx
register when theexit
system call was invoked.
Expected Output
When ./ex1
runs, the following happens:
The program calls the
exit
system call with42
as the exit status.The
echo $?
command then prints42
, the exit status of./ex1
.
Play with the code ->
the sub ebx, 29 part will substract 29 to the ebx register wich is the output register, altering the output to 13 here
Here are some other assembly instructions:
![](https://felix-billieres.gitbook.io/~gitbook/image?url=https%3A%2F%2F2346124906-files.gitbook.io%2F%7E%2Ffiles%2Fv0%2Fb%2Fgitbook-x-prod.appspot.com%2Fo%2Fspaces%252FO8Hp1HtgjLpE41gU26RP%252Fuploads%252FzsXfREBpoOmMkvWD4xCb%252Fimage.png%3Falt%3Dmedia%26token%3Dc805d25a-a6d9-46f9-8e49-2253833010af&width=768&dpr=4&quality=100&sign=5eb68c7deaf9afa7744cdda11274379176eec026ec76e037d29f239a31b0343f)
When you use the
mul
instruction with a register (e.g.,ebx
), it implicitly useseax
as the left operand.The right operand is the register or memory location you specify (e.g.,
ebx
).The instruction
mul ebx
performs the following multiplication:
The result is stored in the EDX:EAX
pair, where:
EAX
holds the lower 32 bits of the result.EDX
holds the upper 32 bits of the result (if the result is larger than 32 bits).
and for div
Hello world!
Line-by-Line Explanation:
global _start
:This line declares the
_start
label as global, making it the entry point for the program. The linker needs this to know where the execution begins.
section .data
:This section is used to declare initialized data or constants. Data in this section does not change at runtime.
msg db "Hello, world!", 0x0a
:msg
is a label for the data "Hello, world!" followed by a newline character (0x0a
in hexadecimal, which is the ASCII code for a newline).
len equ $ - msg
:len
is defined as the length of the stringmsg
.$
represents the current address, andmsg
is the address of the beginning of the string. The difference gives the length of the string.
section .text
:This section contains the code (instructions) of the program.
_start:
:This label marks the entry point of the program. The execution starts here.
System Call to Write
mov eax, 4
:Moves the value
4
into theeax
register. In Linux,4
is the syscall number forsys_write
.
mov ebx, 1
:Moves the value
1
into theebx
register.1
is the file descriptor number for standard output (stdout).
mov ecx, msg
:Moves the address of
msg
(the string "Hello, world!\n") into theecx
register.ecx
will point to the data to be written.
mov edx, len
:Moves the value of
len
(the length of the stringmsg
) into theedx
register.edx
specifies the number of bytes to write.
int 0x80
:Triggers a software interrupt to invoke the kernel. The kernel interprets the values in the registers and performs the
write
syscall, writing "Hello, world!\n" to the standard output.
System Call to Exit
mov eax, 1
:Moves the value
1
into theeax
register. In Linux,1
is the syscall number forsys_exit
.
mov ebx, 0
:Moves the value
0
into theebx
register.0
is the exit status code.
int 0x80
:Triggers a software interrupt to invoke the kernel. The kernel interprets the values in the registers and performs the
exit
syscall, terminating the program with a status code of0
.
Last updated