<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Ratnadeep Bhattacharyya on Medium]]></title>
        <description><![CDATA[Stories by Ratnadeep Bhattacharyya on Medium]]></description>
        <link>https://medium.com/@ratnadeepofghato?source=rss-d57e0b4026ab------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*fygitDqwtJvqDIAP.</url>
            <title>Stories by Ratnadeep Bhattacharyya on Medium</title>
            <link>https://medium.com/@ratnadeepofghato?source=rss-d57e0b4026ab------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Thu, 19 Nov 2026 11:21:56 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@ratnadeepofghato/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Learn how computers work by learning FASM: Part 1]]></title>
            <link>https://medium.com/@ratnadeepofghato/learn-how-computers-work-by-learning-fasm-part-1-23757f83778d?source=rss-d57e0b4026ab------2</link>
            <guid isPermaLink="false">https://medium.com/p/23757f83778d</guid>
            <category><![CDATA[x86-64]]></category>
            <category><![CDATA[x86]]></category>
            <category><![CDATA[x64]]></category>
            <category><![CDATA[assembly]]></category>
            <category><![CDATA[fasm]]></category>
            <dc:creator><![CDATA[Ratnadeep Bhattacharyya]]></dc:creator>
            <pubDate>Tue, 17 Oct 2023 14:15:51 GMT</pubDate>
            <atom:updated>2023-10-17T14:15:51.460Z</atom:updated>
            <content:encoded><![CDATA[<p>I am software engineer with 5 years of professional experience and 18 years of hands-on experience in programming. I coded in mostly all of the languages professionally or for personal projects used like Python, Javascript/Typescript, Golang, Rust, and even on C/C++ in fewer cases.</p><p>I am always fascinated by how computers can convert human words into binary instructions. I have worked with assembly in my college days specially during micro-controllers and micro-processors subject and it fascinated me always how these work. I tried <em>NASM</em> at that time but with headache of fetching a job, it never worked out learning how these things work.</p><p>I am starting a new journey of learning and understanding how assembly and microprocessors (only x86_64, for now) internally, storing, retrieving and manipulating data using registers and beyond that.</p><h4><strong>Registers</strong></h4><p>Not diving deep, I understand it as nearest warehouse where data can be stored but in small amount and can be accessed fast (blazing fast) by processing unit and write back results in there for further distribution into the system.</p><p>If you want definition of it then here it is:</p><blockquote>A <strong>processor register</strong> is a quickly accessible location available to a computer’s processor. Registers usually consist of a small amount of fast storage, although some registers have specific hardware functions, and may be read-only or write-only.<br>- Wikipedia</blockquote><p>Here is how registers look conceptually</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Vsl623EEo9sD_UQ7K_BpnA.png" /></figure><ul><li><em>rax</em> is the 64-bit, “long” size register. It was added in 2003 during the transition to 64-bit processors.</li><li><em>eax</em> is the 32-bit, “int” size register. It was added in 1985 during the transition to 32-bit processors with the 80386 CPU.</li><li><em>ax</em> is the 16-bit, “short” size register. It was added in 1979 with the 8086 CPU, but is used in DOS or BIOS code to this day.</li><li><em>al</em> and <em>ah</em> are the 8-bit, “char” size registers. al is the low 8 bits, ah is the high 8 bits. They’re pretty similar to the old 8-bit registers of the 8008 back in 1972.</li></ul><p>Here is table to show what each registers are for:</p><pre><br>| Name |                                             Notes                                              |   Type    | 64-bit long | 32-bit int |<br>|------|------------------------------------------------------------------------------------------------|-----------|-------------|------------|<br>| rax  | Value returned from functions in this register                                                 | scratch   | rax         | eax        |<br>| rcx  | Typical scratch register. Some instruction also use it as counter                              | scratch   | rcx         | ecx        |<br>| rdx  | Scratch regiser                                                                                | scratch   | rdx         | edx        |<br>| rbx  | Preserved register: don&#39;t use it without saving it                                             | preserved | rbx         | ebx        |<br>| rsp  | The stack register: Points to top of stack                                                     | preserved | rsp         | esp        |<br>| rbp  | Preserved register: Sometimes used to store the old value of the stack pointer, or the &quot;base&quot;. | preserved | rbp         | ebp        |<br>| rsi  | Scratch regiser. Also used to pass argument #2 in 64-bit linux                                 | scratch   | rsi         | esi        |<br>| rdi  | Scratch regiser. Function argument #1 in 64-bit Linux                                          | scratch   | rdi         | edi        |<br>| r8   | Scratch register.  These were added in 64-bit mode, so they have numbers, not names.           | scratch   | r8          | r8d        |<br>| r9   | Scratch register.                                                                              | scratch   | r9          | r9d        |<br>| r10  | Scratch register.                                                                              | scratch   | r10         | r10d       |<br>| r11  | Scratch register.                                                                              | scratch   | r11         | r11d       |<br>| r12  | Preserved register.  You can use it, but you need to save and restore it.                      | preserved | r12         | r12d       |<br>| r13  | Preserved register.                                                                            | scratch   | r13         | r13d       |<br>| r14  | Preserved register.                                                                            | scratch   | r14         | r14d       |<br>| r15  | Preserved register.                                                                            | scratch   | r15         | r15d       |</pre><h4>Sycalls</h4><p>To do anything with hardware we need to ask kernel to do stuff for us, like writing in terminal, reading input, opening socket for tcp connection. We will be writing for x86_64 system so I will referring this link for syscall codes <a href="https://chromium.googlesource.com/chromiumos/docs/+/master/constants/syscalls.md">Chrome Docs</a>.</p><h4>FASM</h4><p>Lots of theory up there related to computer science, lets start writing something and as ritual we will write hello world but first we need a assembler, I will be using <a href="https://flatassembler.net/">FASM</a> (Flat Assembler) and I will be using Linux to create executable, for Windows and Mac things will be different. If you’re on windows you can use WSL to have Linux VM.</p><p>When a program exits it gives a exit code, in POSIX systems 0 is treated as success and anything else is failure. So, lets write a program to give number be want to be the exit code.</p><pre>; file: hello.asm<br>; comment starts with semi-colon here<br>format ELF64 executable 3 ; defines that it will a ELF64 type executable<br><br>entry start               ; defines which address to look for when program starts<br><br>; following section is marked as readable and executable<br>; this section will be executed<br>segment readable executable<br>start:<br>    mov rax, 60           ; in x86 60 is code for syscall to exit program<br>    mov rdi, 19           ; exit code of program<br>    syscall               ; do syscall for code defined in rax here it is exit</pre><p>You can compile it using <em>fasm hello.asm</em> it will create a executable <em>hello</em>. If you run it nothing much you can see, but if you run echo $? which shows last command exit code, you can see 19 as output. Congrats, you wrote your first assembly program.</p><p><em>mov</em> instruction moves value provided to given register.</p><p><em>syscall</em> instruction does the syscall for given syscall code in rax.</p><p>Now, lets print something. Giving just exit code is somewhat boring, lets modify it to print “Hello World!” in terminal.</p><pre>; file: hello.asm<br>; comment starts with semi-colon here<br>format ELF64 executable 3 ; defines that it will a ELF64 type executable<br><br>entry start               ; defines which address to look for when program starts<br><br>; following section is marked as readable and executable<br>; this section will be executed<br>segment readable executable<br>start:<br>    mov rax, 1            ; x86 syscall code for write<br>    mov rdi, 1            ; file descriptor, here it is stdout<br>    mov rsi, msg          ; address of our data byte<br>    mov rdx, msg_size     ; address of msg_size<br>    syscall               ; do syscall for code defined in rax here it is write<br><br>    mov rax, 60           ; in x86 60 is code for syscall to exit program<br>    mov rdi, 0            ; exit code of program, changed to 0 for success<br>    syscall               ; do syscall for code defined in rax here it is exit<br><br>; section for storing values, thats why readable and writable<br>segment readable writeable<br>msg db &quot;Hello World!&quot;, 10 ; store address of string in msg which is of type db (data byte) and end with new line <br>msg_size = $ - msg        ; msg_size stores length of msg</pre><p>Now, compile with <em>fasm hello.asm</em> and run <em>hello</em>, you should see “Hello World!” printed at terminal.</p><p>So, you learned how to create a simple “Hello World!” program in FASM but its just tip of iceberg. We will cover different things in assembly and some features of FASM also like macros.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=23757f83778d" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>