What is Assembly?

Assembly was developed by Kathleen (1922-2022) and Andrew Booth (1918-2009) and released in 1947. It quickly became the default programming language for many mainframes and their OSs since the language allowed easier access to system calls (syscalls) — requests to the OS kernel.

The development of hobby OSs has caught my attention, but it seems that more new OSs are written in assembly than C (for example, Unix, Linux and derivatives) or any other language. This has started my curiosity of learning assembly although fairly antiquated given today's languages and programming standards, but I have no idea which assembly dialect would be best (easiest) for me. After all, there are various options in the market for any number of processors.

By Swtpc6800 en:User:Swtpc6800 Michael Holley — Own work, Public Domain, https://commons.wikimedia.org/w/index.php?curid=15345825

In any case, I have opted for NASM because of the vast amount of information available on-line related to Linux and easy installation on Cygwin for quick and easy cross-reference (emphasis on easy) — instead of GASM included in the GNU Compiler Collection (GCC), which is included in most Linux distros.

Even after watching an IEEE crash-course video on NASM and some other videos, I still have trouble understanding this language. That is no surprise. In the worst case scenario that I do not make sense of this language, I can always go back to C.

For example, the following is a Hello World program written in NASM, which makes some sense to me after watching several videos and reading some material on-line.

        
        ; based on `Hello World` using Netwide Assembly (NASM),
        ; courtesy of Ray Toal, Loyola Marymount University
        ; https://cs.lmu.edu/~ray/notes/nasmtutorial/
        
        global _start                   ; start of `global`
                                        ;
        section .text                   ; start of `text` section (program)
                                        ;
        _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                     ; invoking operating system to do the write
            mov rax, 60                 ; system call for exit
            xor rdi, rdi                ; exiting code 0
            syscall                     ; invoking operating system to exit
                                        ;
        section .data                   ; start of the data section (variables)
        message: db "Hello World!", 10  ; note the newline at the end
      

I ran the following statement in order to compile the code above.

        
        nasm -f elf32 -o nasm_hello_world_2.obj nasm_hello_world_2.asm
      

Unfortunately, I got the following error and I am back to zero (0).

        
        nasm_hello_world.asm:9: error: instruction not supported in 32-bit mode
        nasm_hello_world.asm:10: error: instruction not supported in 32-bit mode
        nasm_hello_world.asm:11: error: instruction not supported in 32-bit mode
        nasm_hello_world.asm:12: error: instruction not supported in 32-bit mode
        nasm_hello_world.asm:15: error: instruction not supported in 32-bit mode
        nasm_hello_world.asm:16: error: instruction not supported in 32-bit mode
      

At the same time, the following is a similar program written in MASM, which I does not make too much sense to me as a beginner (a total noob).

        
        ; based on `How to print your name in assembly language using
        ; DOSBox & MASM` using Microsoft Macro Assembly (MASM),
        ; courtesy of https://www.youtube.com/watch?v=3FWqNVHuq60
        
        .model small                    ; MASM assembly
        .stack 100h                     ; starting memory allocation
        .data                           ; data section
        .code                           ; code section
        main proc                       ; main section to write code
            mov ah, 2                   ; to print output
            mov dl, 'H'                 ; moving `H` to `stdout`, same for each character
            int 21h                     ; making sure code worked okay, same after each character
            mov dl, 'e'                 ; moving `e` to `stdout`
            int 21h                     ; error check
            mov dl, 'l'                 ; moving `l` to `stdout`
            int 21h                     ; error check
            mov dl, 'l'                 ; moving `l` to `stdout`
            int 21h                     ; error check
            mov dl, 'o'                 ; moving `o` to `stdout`
            int 21h                     ; error check
            mov dl, ' '                 ; moving space to `stdout`
            int 21h                     ; error check
            mov dl, 'W'                 ; moving `W` to `stdout`
            int 21h                     ; error check
            mov dl, 'o'                 ; moving `o` to `stdout`
            int 21h                     ; error check
            mov dl, 'r'                 ; moving `r` to `stdout`
            int 21h                     ; error check
            mov dl, 'l'                 ; moving `l` to `stdout`
            int 21h                     ; error check
            mov dl, 'd'                 ; moving `d` to `stdout`
            int 21h                     ; error check
            mov dl, '!'                 ; moving `!` to `stdout`
            int 21h                     ; error check
            mov ah, 4ch                 ; to return control to OS
            int 21h                     ; error check
        main endp                       ; end of `proc`
        end main                        ; end of `main` block
      

I could not run the code above since I do not have MASM in my machine.

If you have experience in assembly, you might be laughing at me. Of course, if you have not had any experience with assembly and/or similar languages as in my case, you might also be scratching your head trying to make sense of the differences between these two dialects.

Not a safe language:

The only problem with older languages like C, including C++ (one language extension I really like), and assembly is that has come up as of the National Security Agency (NSA) 11/10/2022 press release is that some languages are not "memory safe" (chance of code being exploited). Needless to say, C and assemble are not in this safe list.

Memory issues in software comprise a large portion of the exploitable vulnerabilities in existence. NSA advises organizations to consider making a strategic shift from programming languages that provide little or no inherent memory protection, such as C/C++, to a memory safe language when possible. Some examples of memory safe languages are C#, Go, Java, Ruby™, and Swift®. Memory safe languages provide differing degrees of memory usage protections, so available code hardening defenses, such as compiler options, tool analysis, and operating system configurations, should be used for their protections as well. By using memory safe languages and available code hardening defenses, many memory vulnerabilities can be prevented, mitigated, or made very difficult for cyber actors to exploit.
https://media.defense.gov/2022/Nov/10/2003112742/-1/-1/0/CSI_SOFTWARE_MEMORY_SAFETY.PDF

Although not listed in the quote above, Python, a language I am interested in, is considered safe.