Journey learning 8086 assembly
Hey everyone. Well, whosoever is fun enough to read this :). I basically just had to learn x86 assembly language. Why is the language so important and what’s it all about; let’s discuss.
Starting with, why did I have to learn it? Well, it all happened during a night with a bro (credits to Jay). He saw a wrapped (github, lol) amd we decoded to check ours out. However, this years wrapped was nonsense for both of us. Why? Good question, for him, Java was his most used language, a language he just started exploring and for me, lol, scilab was my third most used languae. Really, whatttt? I dont even write scilab, it’s mostly generated for something. Well, that’s not the major problem really. I mean, who cares? I dooooo. Well,, let’s ignore that. However, my most used language for the year is assembly. Whatttt? Not bad of a language. However, I don’t write 8086 that much. Of course, I wrote some samples while learning Operating systems but those were just basic stuffs and I barely understood some instructions I used. I mean, why care so much about register names. Pssst, I slept that night but woke up with a different initiative. I felt, if that be the case, why not end the year with the fun of understanding the architecture. Yhh, I write assembly as well but of another architecture. In fact, just a learning form (the computer I’m learning to build, thanks to the elements of the computing system). It’s a weird architecture but helps me learn how assembly works in depth and I knew I could pick 8086 quickly, which I did.
So, with that, I picked up 8086 from tutorials point and printed out the pages on the 14th of this month. i read 50% same day, thanks to the other architecture I write already. I mean, it was basically comparison. However, things changed as I started the other 50%. I mean what’s stosb
or whatsoever. I mean, what is push
, did they really implement a stack on 8086 ( I didn’t know there was a stack inbuilt). Wow, this is fun. I started reading more and I learned something called endianess (literally means what end are you reading your bits from, RTL or LTR). I mean, it was basically fun.
The joy of learning assembly is that you get to appreciate High Level programming and how pointers work in a higher language like C or CPP. I mean, passing data by reference and all those basically appear as normal now. Thanks to the stack on 8086, you can quickly push data to it and quickly move some other things to say the eax
register. I mean, checkout printing “hello world” in assembly.
section .data ; data segement (this is a comment)
msg db "Hello world" ; the message we wanna print
len equ $ - msg ; $ denotes memory address of end of the msg and we subtract it from the beginning to get length
section .text ; this is where our instruction lives
global main ; this is required for gcc to compile as starting point (just like c right)
main: ; now you see the starting point
mov eax, 4 ; this tells the machine we wanna write out, .i.e sys_write call to the kernel
mov ebx, 1 ; this is the file descriptor .i.e std_out
mov ecx, msg ; the message we want to write out
mov edx, len ; remember the len variable we declared above, we telling the machine size of bytes to write out
int 0x80 ; this is an interrupt call to the kernel to execute our instruction
exit:
mov eax, 1 ; this is telling the kernel we want to exit, .i.e sys_exit call
int 0x80 ; basically, kernel execute again
; tadaa! that's all you need to print hello world
Remember our discussion on trap
instruction for the kernel, it’s how interrupt works, check here.
See how easy it is to print a message in 8086. Sweet enough, we have procedures in 8086 which can function just like code body labels. This is useful in creating loops or passing control conditionally. Another beautiful feature of 8086 is macros. This is basically just what you’ll call a function in a high level language. You can pass an argument to it and makes things easier. An example of a macro to implement the print function will be the following if we need to reduce the stress of duplicating registers values will be the following
%macro print 2 ; macro named print that takes in two variables, let's use message and length
; we first want to push all registers we are going to mutate to the stack
push eax
push ebx
push ecx
push edx
; with this, we can now mutate the registers and use
mov eax, 4 ; sys_write instruction
mov ebx, 1 ; file descriptor of std_out
mov ecx, %1 ; this is to use the first variable passed which we take as message
mov edx, %2 ; second variable passed which is length; the validity of course, ensure your code is correct
int 0x80 ; execute kernel man
nwln ; just a new line macro predefined, lol
; now let's restore the values of the registers so that the caller don't have to worry
pop edx
pop ecx
pop ebx
pop eax
; NOTE the reverse direction of the pop from the stack, remember a stack uses LIFO; .i.e the last value stored on it
; will be the first one to be popped
$endmacro
; Now let's use the macro
section .text
mov eax, 45 ; we just stored a value in the eax register, this is actually sys_brk for allocating memory
; hey let's print out a message
print hello_msg len
; after doing this, although the eax register was mutated, it still contains 45
mov eax, 1 ; exit
int 0x80 ; interrupt kernel
section .data
hello_msg db "Hello my good friend", 0
len equ $ - hello_msg
Wooops, that’s using macros in 8086.
Some other assenbly programs are here
So many other things learned but I still have to read more on the assembly strings. I get the concept actually but I just feel like I need to learn more, xD.
8086 is the architecture most of our PCs admit although the switch to ARM is looking true. However, I really enjoyed reading this 8086. Today is 17th and I took a break on 16th. Roughly 3 days (definitely very small hours of actively learning, flex right!<).
Plus, I’m currently watching Naruto, hence I want to go train my chakra. Bye, see y’all next time.