ASM our language in which we communicate
We all know what ASM is. Am i right? The code goes through a compiler which becomes an abstract structure etc. I looked at compilers, hybrids and interpreters. What I found? It is very hard to understand. Why people like me want to develop a compiler? BECAUSE, I'm currently developing on a compiler. I don't think that make any sense. But the journey doesn't end here.
Why is ASM the best programming language? (Meant ironically)
Everything that is controlled: keyboard, mouse, computer, etc. is written in ASM. Therefore, compilers have to rewrite the code. You can say that the compiler is simply a translator.
Let's see what really happens:
Our code:
let x = 1 + 2;
let y = 8;
let z = x + y + 2;
exit(z);
The code in ASM (NASM):
global _start
_start:
mov rax, 1
push rax
mov rax, 2
push rax
; Add
pop rax
pop rbx
add rax, rbx
push rax
mov rax, 8
push rax
; Call var: x
push QWORD [rsp + 8]
; Call var: y
push QWORD [rsp + 8]
mov rax, 2
push rax
; Add
pop rax
pop rbx
add rax, rbx
push rax
; Add
pop rax
pop rbx
add rax, rbx
push rax
; Call var: z
push QWORD [rsp + 0]
; Exit by var.
mov rax, 60
pop rdi
syscall
mov rax, 60
mov rdi, 0
syscall
Really hard to read, isn't it? Let's look.
The code is first converted into tokens, then into an abstract form, and finally into code. (Very simplified)
The code (see above) is in [let; ID; Eq; int_lit; Plus; int_lit; half; ...] converted. What we see are individual tokens. Then it is placed in a structure. [let->value->id], [let->value->id->int_lit]...
This structure can be read by the compiler. At the end of the process, the game pieces or puzzle pieces are put into a shape. That's how easy it can be. Um. (Not really. This process resulted in 1000 lines of C++ code.) Sometimes you should just be on the Internet and know how difficult it is to pull off something like this.
Yeah, after the journey you now know how the compiler works with very basics of knowledge of compiler. I will create another journey about interpreters and hybrids. I know that thos is very short. Bye!
We live with code!
Top comments (0)