<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Weslley Neves</title>
    <description>The latest articles on DEV Community by Weslley Neves (@wesurage).</description>
    <link>https://dev.to/wesurage</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F2470054%2Fc1d525a1-6f22-4638-a318-f4943fff1e0e.png</url>
      <title>DEV Community: Weslley Neves</title>
      <link>https://dev.to/wesurage</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/wesurage"/>
    <language>en</language>
    <item>
      <title>Your FIRST STEPS on the ASSEMBLY Programming Language!</title>
      <dc:creator>Weslley Neves</dc:creator>
      <pubDate>Sat, 23 Nov 2024 00:42:52 +0000</pubDate>
      <link>https://dev.to/wesurage/your-first-steps-on-the-assembly-programming-language-51o5</link>
      <guid>https://dev.to/wesurage/your-first-steps-on-the-assembly-programming-language-51o5</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;I will not focus on history. I will be assuming you are a beginner on the Assembly programming language. And assuming that, I will explain it for anybody who's interested to understand an assembly code to really understand it. The &lt;a href="https://en.wikipedia.org/wiki/Assembly_language#Assembler" rel="noopener noreferrer"&gt;assembler&lt;/a&gt; that will be used on this article is &lt;a href="https://en.wikipedia.org/wiki/Netwide_Assembler" rel="noopener noreferrer"&gt;nasm&lt;/a&gt; (Netwide Assembler) and we will be coding in &lt;a href="https://en.wikipedia.org/wiki/X86-64" rel="noopener noreferrer"&gt;x86_64&lt;/a&gt; Linux assembly.&lt;/p&gt;




&lt;h2&gt;
  
  
  Why Assembly?
&lt;/h2&gt;

&lt;p&gt;Assembly is often called "The Father of Programming Languages" because it serves as a bridge between high-level languages and the raw machine code executed by a computer's hardware, providing precise control over system resources and performance.&lt;/p&gt;

&lt;p&gt;So, Assembly should be chosen not only for those who are coming from C/C++, but also for those who want to understand about what happens on your code and computer at bit-level.&lt;/p&gt;

&lt;h2&gt;
  
  
  Today's code
&lt;/h2&gt;

&lt;p&gt;The code we are taking a look today is this one:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;section .data
  say db "Say something!", 0xA, 0
  say_len equ $ - say

  input_char db "&amp;gt; ", 0
  input_char_len equ $ - input_char

  said db "You said: "
  said_len equ $ - said

section .bss
  input resb 128

section .text
global _start

_start:
  mov rax, 1
  mov rdi, 1
  mov rsi, say
  mov rdx, say_len
  syscall

  mov rax, 1
  mov rdi, 1
  mov rsi, input_char
  mov rdx, input_char_len
  syscall

  mov rax, 0
  mov rdi, 0
  mov rsi, input
  mov rdx, 128
  syscall

  mov rax, 1
  mov rdi, 1
  mov rsi, said
  mov rdx, said_len
  syscall

  mov rax, 1
  mov rdi, 1
  mov rsi, input
  mov rdx, 128
  syscall

  mov rax, 60
  mov rsi, 0
  syscall
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;At the end of this article, you will be able to read it again and understand what it means and how it works. But first we need to understand some things.&lt;/p&gt;




&lt;h2&gt;
  
  
  Assembly's code structure
&lt;/h2&gt;

&lt;p&gt;The Assembly code is devided in segments/sections (I rather to say sections) and each section has it's own responsabilities. We have the section&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;.data&lt;/code&gt;: Where initialized data (variable pointer) is put.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;.rodata&lt;/code&gt;: Where read-only initialized data (constant pointer) is put.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;.bss&lt;/code&gt;: Where uninitialized data is put (also a pointer).&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;.text&lt;/code&gt;: Where the actual executable code is put.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Defined and Reserved bytes
&lt;/h2&gt;

&lt;p&gt;Where something like &lt;strong&gt;variables&lt;/strong&gt; and &lt;strong&gt;constants&lt;/strong&gt; are created.&lt;/p&gt;

&lt;h3&gt;
  
  
  Defined bytes
&lt;/h3&gt;

&lt;p&gt;Defined bytes are created when you assign a value to an identifier. You need three elements:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The name of the identifier (label).&lt;/li&gt;
&lt;li&gt;The size in bytes.&lt;/li&gt;
&lt;li&gt;The value.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;For example, in C, you might write &lt;code&gt;char* hello = "Hello, World!";&lt;/code&gt;. In Assembly, the equivalent is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;hello db "Hello, World!"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;db&lt;/code&gt; directive stands for "define byte". With &lt;code&gt;db&lt;/code&gt;, it means that every chunk of value of this "variable" will be stored in the size of &lt;strong&gt;1 byte&lt;/strong&gt; each one.&lt;/p&gt;

&lt;p&gt;And you can also define it in chunks of higher value bytes if you need to. These are the possible values:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;db&lt;/code&gt;: Defines in chunks of &lt;strong&gt;1 byte&lt;/strong&gt; (8 bits).&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;dw&lt;/code&gt;: Define word. Defines in chunks of &lt;strong&gt;2 bytes&lt;/strong&gt; (16 bits).&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;dd&lt;/code&gt;: Define double-word. Defines in chunks of &lt;strong&gt;4 bytes&lt;/strong&gt; (32 bits).&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;dq&lt;/code&gt;: Define quad-word. Defines in chunks of &lt;strong&gt;8 bytes&lt;/strong&gt; (64 bits).&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Reserved bytes
&lt;/h3&gt;

&lt;p&gt;They are pointers to undefined values. An these are the directives:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;resb&lt;/code&gt;: Reserves in chunks of &lt;strong&gt;1 byte&lt;/strong&gt; (8 bits).&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;resw&lt;/code&gt;: Reserve word. Reserves in chunks of &lt;strong&gt;2 bytes&lt;/strong&gt; (16 bits).&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;resd&lt;/code&gt;: Reserve double-word. Reserves in chunks of &lt;strong&gt;4 bytes&lt;/strong&gt; (32 bits).&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;resq&lt;/code&gt;: Reserve quad-word. Reserves in chunks of &lt;strong&gt;8 bytes&lt;/strong&gt; (64 bits).&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Registers
&lt;/h2&gt;

&lt;p&gt;Registers are like volatile boxes that have a value assigned to it. And since we are coding in x86_64, these are the registers of this family:&lt;/p&gt;

&lt;h3&gt;
  
  
  General Purpose Registers
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;RAX&lt;/code&gt;: Accumulator (arithmetic operations and function calls).&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;RBX&lt;/code&gt;: Base (general-purpose, preserved across function calls).&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;RCX&lt;/code&gt;: Counter (used in loops and some instructions like &lt;code&gt;rep&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;RDX&lt;/code&gt;: Data (arithmetic operations and I/O).&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;RSI&lt;/code&gt;: Source Index (string operations and general-purpose).&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;RDI&lt;/code&gt;: Destination Index (string operations and general-purpose).&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;RBP&lt;/code&gt;: Base Pointer (used to access local variables, preserved across function calls).&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;RSP&lt;/code&gt;: Stack Pointer (points to the top of the &lt;strong&gt;stack&lt;/strong&gt;, used in function calls and flow control).&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;R8&lt;/code&gt; to &lt;code&gt;R15&lt;/code&gt;: Additional general-purpose registers.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Deeper look into registers
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxbi9v8j09hleyrw4bpmu.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxbi9v8j09hleyrw4bpmu.jpg" alt="Image description" width="735" height="417"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Taking &lt;strong&gt;RAX&lt;/strong&gt; as an example for knowing more about the registers:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;RAX&lt;/strong&gt;: Re-extended &lt;strong&gt;ax&lt;/strong&gt;. As previously said, it is used in arithmetic operations and function calls.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;EAX&lt;/strong&gt;: Extended &lt;strong&gt;ax&lt;/strong&gt;. The 32-bit version of &lt;strong&gt;RAX&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;AX&lt;/strong&gt;: 16-bit version of &lt;strong&gt;RAX&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;AL&lt;/strong&gt;: 8-bit subdivision of &lt;strong&gt;AX&lt;/strong&gt; (&lt;strong&gt;least&lt;/strong&gt; significant bit of &lt;strong&gt;AX&lt;/strong&gt;).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;AH&lt;/strong&gt;: 8-bit subdivision of &lt;strong&gt;AX&lt;/strong&gt; (&lt;strong&gt;most&lt;/strong&gt; significant bit of &lt;strong&gt;AX&lt;/strong&gt;). &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Learn more about &lt;strong&gt;least&lt;/strong&gt; and &lt;strong&gt;most&lt;/strong&gt; significant bit &lt;a href="https://en.wikipedia.org/wiki/Bit_numbering" rel="noopener noreferrer"&gt;here&lt;/a&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  System Calls
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;System calls&lt;/strong&gt;, or &lt;strong&gt;syscalls&lt;/strong&gt;, are the interface between a user program and the operating system &lt;a href="https://en.wikipedia.org/wiki/Kernel_(operating_system)" rel="noopener noreferrer"&gt;kernel&lt;/a&gt;. They allow programs to request services from the kernel, such as reading from or writing to files, allocating memory, or terminating a process. In x86_64 Assembly, the &lt;code&gt;syscall&lt;/code&gt; instruction is used to invoke these services.&lt;/p&gt;




&lt;h3&gt;
  
  
  How System Calls Work
&lt;/h3&gt;

&lt;p&gt;When a program makes a syscall:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The program sets specific values in &lt;strong&gt;registers&lt;/strong&gt; to indicate the syscall number and its parameters.&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;syscall&lt;/code&gt; instruction is executed.&lt;/li&gt;
&lt;li&gt;The operating system processes the request and returns a result, typically in a register.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;You can consult the &lt;strong&gt;Linux Syscalls Table&lt;/strong&gt; &lt;a href="https://chromium.googlesource.com/chromiumos/docs/+/master/constants/syscalls.md#x86_64-64_bit" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;h4&gt;
  
  
  Registers Used in Syscalls
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;RAX&lt;/code&gt;&lt;/strong&gt;: Contains the syscall number (identifies the service to invoke).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;RDI&lt;/code&gt;&lt;/strong&gt;: The first argument for the syscall.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;RSI&lt;/code&gt;&lt;/strong&gt;: The second argument for the syscall.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;RDX&lt;/code&gt;&lt;/strong&gt;: The third argument for the syscall.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;R10&lt;/code&gt;&lt;/strong&gt;: The fourth argument for the syscall.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;R8&lt;/code&gt;&lt;/strong&gt;: The fifth argument for the syscall.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;R9&lt;/code&gt;&lt;/strong&gt;: The sixth argument for the syscall.&lt;/li&gt;
&lt;li&gt;The return value of the syscall is stored in &lt;strong&gt;&lt;code&gt;RAX&lt;/code&gt;&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  Example: Writing to Standard Output
&lt;/h3&gt;

&lt;p&gt;Below is a simple example where the program writes "Hello, World!" to the terminal using the &lt;code&gt;write&lt;/code&gt; syscall:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;section .data
    hello db "Hello, World!", 0xA, 0 ; The message to write, followed by a newline and a null terminator

section .text
global _start

_start:
    ; Syscall: write
    mov rax, 1          ; Syscall number for write (1)
    mov rdi, 1          ; File descriptor for standard output (1)
    mov rsi, message    ; Address of the message
    mov rdx, 15         ; Length of the message
    syscall             ; Make the syscall

    ; Syscall: exit
    mov rax, 60         ; Syscall number for exit (60)
    mov rdi, 0          ; Exit status (0)
    syscall             ; Make the syscall

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;mov&lt;/code&gt; instruction moves values between registers and memory addresses.&lt;/p&gt;




&lt;h2&gt;
  
  
  Labels
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Labels&lt;/strong&gt; in Assembly are identifiers followed by a &lt;code&gt;:&lt;/code&gt; (colon). They act as markers in the code, serving as references for jumps, loops, or points to access data. Think of them as "raw functions" or "bookmarks" within your program.&lt;/p&gt;

&lt;h3&gt;
  
  
  Types of Labels
&lt;/h3&gt;

&lt;p&gt;There are two main types of labels in Assembly:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Local Labels&lt;/strong&gt;: Used within a specific section of the program and cannot be accessed globally. These are typically written with a leading &lt;code&gt;.&lt;/code&gt; (dot) to signify they are local.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Global Labels&lt;/strong&gt;: Accessible throughout the program and often marked with the &lt;code&gt;global&lt;/code&gt; keyword for external visibility.&lt;/li&gt;
&lt;/ol&gt;




&lt;h3&gt;
  
  
  Declaring Labels
&lt;/h3&gt;

&lt;p&gt;A label is simply an identifier followed by a colon (&lt;code&gt;:&lt;/code&gt;). For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;start:         ; This is a global label
.loop:         ; This is a local label
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Using labels for control flow
&lt;/h3&gt;

&lt;p&gt;Labels are often used with control flow instructions like &lt;code&gt;jmp&lt;/code&gt; (unconditional jump) or &lt;code&gt;je&lt;/code&gt; (jump if equal). Here's an example of how labels are used in a loop:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;section .text
global _start

_start:
    mov ecx, 5          ; Set the counter (ecx) to 5

.loop:                  ; Start of the loop
    dec ecx             ; Decrement the counter
    jnz .loop           ; Jump back to .loop if ecx != 0

    ; Exit the program
    mov rax, 60         ; Syscall for exit
    mov rdi, 0          ; Exit code 0
    syscall
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;.loop&lt;/code&gt; is a local label.&lt;/li&gt;
&lt;li&gt;The program jumps back to &lt;code&gt;.loop&lt;/code&gt; while the counter (&lt;code&gt;ecx&lt;/code&gt;) is greater than zero.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Using Global Labels
&lt;/h3&gt;

&lt;p&gt;Global labels can be accessed across files when linking multiple Assembly files. To declare a global label, use the &lt;code&gt;global&lt;/code&gt; directive:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;File 1: function.asm&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;section .text
global my_function

my_function:
    ; Code for the function
    ret ; returns to the caller 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;File 2: call_function.asm&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;extern my_function     ; Declare the external function

section .text
global _start

_start:
    call my_function   ; Call the external function

    ; Exit the program
    mov rax, 60
    mov rdi, 0
    syscall

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;global _start&lt;/code&gt; on each main file tells the linker where is the main entry for it to link.&lt;/p&gt;




&lt;h2&gt;
  
  
  Offset calculation
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;say db "Say something!", 0xA, 0&lt;/code&gt;&lt;/strong&gt;:

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;0xA&lt;/code&gt;&lt;/strong&gt;: Adds a newline character (&lt;code&gt;\n&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;0&lt;/code&gt;&lt;/strong&gt;: Null terminator (&lt;code&gt;\0&lt;/code&gt;) to mark the string's end.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;This creates the string &lt;code&gt;"Say something!\n\0"&lt;/code&gt; in memory.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;say_len equ $ - say&lt;/code&gt;&lt;/strong&gt;:

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;equ&lt;/code&gt;&lt;/strong&gt;: Defines a constant value.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;$&lt;/code&gt;&lt;/strong&gt;: Represents the current memory address (after the string ends).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;$ - say&lt;/code&gt;&lt;/strong&gt;: Calculates the length of the string in bytes by subtracting the starting address (&lt;code&gt;say&lt;/code&gt;) from the current address (&lt;code&gt;$&lt;/code&gt;).&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;This computes the total string length, including the characters, newline (&lt;code&gt;0xA&lt;/code&gt;), and null terminator (&lt;code&gt;0&lt;/code&gt;).&lt;/p&gt;




&lt;h2&gt;
  
  
  And we're done.
&lt;/h2&gt;

&lt;p&gt;Now read again the code we wrote at the beginning: &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;prompter.asm:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;section .data
  ; Define initialized data
  say db "Say something!", 0xA, 0          ; The string "Say something!" followed by a newline (0xA) and null terminator (0)
  say_len equ $ - say                      ; Calculate the length of the string (current address minus 'say' label)

  input_char db "&amp;gt; ", 0                    ; The prompt string "&amp;gt; " followed by a null terminator
  input_char_len equ $ - input_char        ; Calculate the length of the prompt string

  said db "You said: ", 0                  ; The string "You said: " followed by a null terminator
  said_len equ $ - said                    ; Calculate the length of the "You said: " string

section .bss
  ; Define uninitialized data
  input resb 128                           ; Reserve 128 bytes of space for storing user input

section .text
global _start                              ; Define the program's entry point

_start:
  ; Display the message "Say something!"
  mov rax, 1                               ; Syscall number for 'write'
  mov rdi, 1                               ; File descriptor: 1 (stdout)
  mov rsi, say                             ; Address of the string "Say something!"
  mov rdx, say_len                         ; Length of the string
  syscall                                  ; Make the system call

  ; Display the prompt "&amp;gt; "
  mov rax, 1                               ; Syscall number for 'write'
  mov rdi, 1                               ; File descriptor: 1 (stdout)
  mov rsi, input_char                      ; Address of the prompt string "&amp;gt; "
  mov rdx, input_char_len                  ; Length of the prompt string
  syscall                                  ; Make the system call

  ; Read user input (up to 128 bytes)
  mov rax, 0                               ; Syscall number for 'read'
  mov rdi, 0                               ; File descriptor: 0 (stdin)
  mov rsi, input                           ; Address to store user input
  mov rdx, 128                             ; Max number of bytes to read
  syscall                                  ; Make the system call

  ; Display the string "You said: "
  mov rax, 1                               ; Syscall number for 'write'
  mov rdi, 1                               ; File descriptor: 1 (stdout)
  mov rsi, said                            ; Address of the string "You said: "
  mov rdx, said_len                        ; Length of the "You said: " string
  syscall                                  ; Make the system call

  ; Display the user input
  mov rax, 1                               ; Syscall number for 'write'
  mov rdi, 1                               ; File descriptor: 1 (stdout)
  mov rsi, input                           ; Address of the user input
  mov rdx, 128                             ; Max number of bytes to display
  syscall                                  ; Make the system call

  ; Exit the program
  mov rax, 60                              ; Syscall number for 'exit'
  mov rsi, 0                               ; Exit code: 0
  syscall                                  ; Make the system call

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Run the code
&lt;/h2&gt;

&lt;p&gt;First we need to &lt;strong&gt;assemble&lt;/strong&gt; it with &lt;code&gt;nasm&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;nasm &lt;span class="nt"&gt;-f&lt;/span&gt; elf64 prompter.asm &lt;span class="nt"&gt;-o&lt;/span&gt; prompter.o
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then link the object file with &lt;code&gt;ld&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;ld prompter.o &lt;span class="nt"&gt;-o&lt;/span&gt; prompter
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And run the code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;./prompter
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Final considerations
&lt;/h2&gt;

&lt;p&gt;You learned what Assembly is like and how to use it. But, that's only the peek of the iceberg. Thanks for reading and see you in the next article!&lt;/p&gt;

</description>
      <category>assembly</category>
      <category>beginners</category>
      <category>lowlevel</category>
    </item>
  </channel>
</rss>
