Writing your first Assembly Program

Writing your first Assembly Program

Assembly Tutorial 01

ยท

3 min read

Hello, readers! ๐Ÿ‘‹

I am Ayush Bulbule, and I am excited to start a new blog series that will serve as a tutorial for learning Assembly Language. I know some of you might think that Assembly Language is complex and hard to understand, but don't worry, I am here to guide you through the process. It may require patience, but just think about it - you are going to learn a language that is closer to the computer's brain! Isn't that amazing?

So, let's get started with a positive approach and try to grasp the concepts together. We'll work through it step-by-step, and soon you'll realize that Assembly Language is not as complicated as it seems.

Before stating we will see just 3 reasons to learn assembly:

  1. Low-level programming

  2. A better understanding of computer architecture

  3. Reverse engineering and security analysis

Understanding the Structure of the Assembly Program

  1. Data section:

    This section is used to declare or initialize data or constants.
    Syntax:

     section .data
    
  2. Bss Section:
    This section is used to declare variables.

     section .bss
    
  3. Text Section:

    This section is where the actual code is present.

     section .text
    

An Assembly language program always starts with global _start which tells the kernel from where to start the execution of the program.

First Assembly Program which prints text on the screen.

;simple program to print hello ayush code

global _start

section .data
    msg db "Hello! AyushCodes",0xA
    msglen equ $ -msg

section .text
_start:
    mov rax, 01
    mov rdx, 01
    mov rsi, msg
    mov rdx, msglen
    syscall

EXIT:
    mov rax, 60
    mov rdx, 60
    syscall

Now save program as hello.asm

Tu run your program in terminal you have to run:

nasm -f elf64 hello.asm

ld -o h hello.o

./h

The fist will assemble your program.

Next command will link object file.

Third command will execute that file.

Now we will understand the program line by line:

global _start

This line declares the label _start as a global symbol, which is required for the linker to find it when it creates the executable file.

section .data 
    msg db "Hello! AyushCodes",0xA 
    msglen equ $ -msg

These lines declare a section of the program's memory called ".data" and define two variables inside it. The first variable, "msg", is a string that contains the message to be printed, followed by a newline character (0xA). The second variable, "msglen", is defined as the difference between the current position of the assembly counter ($) and the starting position of the "msg" variable. This effectively calculates the length of the string.

section .text
 _start: 
    mov rax, 01 
    mov rdx, 01 
    mov rsi, msg 
    mov rdx, msglen 
    syscall

These lines declare a section of the program's memory called ".text" and define the entry point of the program as the "_start" label. The program then moves the value 1 into the "rax" register, which is the syscall number for "write". It then moves the value 1 into the "rdx" register, which is the length of the message to be printed. The address of the "msg" variable is then moved into the "rsi" register, which is the pointer to the message buffer. Finally, the program invokes the "syscall" instruction, which will perform the write operation and print the message to the console.

EXIT: 
mov rax, 60 
mov rdx, 60 
syscall

These lines define a label called "EXIT" and use the "syscall" instruction to terminate the program. The value 60 is moved into the "rax" register, which is the syscall number for "exit". The value 60 is also moved into the "rdx" register, which is the exit status code. The program then invokes the "syscall" instruction, which will terminate the program with the specified exit status.

Let me know your views on this blog in the comments section!

ย