Hey everyone, Today i will make a blogpost about introduction to programming. Because, why not. So, most time in programming, you are working in data. And you commonly have 4 options to store these data. These options are: cpu registers, cpu cache, random access memory and long term memory So, cpu registers are very, very small storage directly inside cpu. They work like so, that they can store commonly 1 number. For example in 32-bit architecture, you have 4 32-bit general purpose registers EAX, EBX, ECX, EDX. However you can also use 16-bit registers AX, BX, CX, DX, each being the lower half of the 32-bit counterpart. The 16-bit registers are also splitted into 2-8bit registers, al, ah, bl, and so on. Registers are made for quick math, or moving operations, because they are extreemly fast directly inside the cpu. What is the cache then? Cache is like a small amount of memory, that is also stored inside the cpu. Commonly when you are working with memory, the cpu automatically moves the currently used memory to cache, and when the piece of memory isnt used anymore, it replaces it with something else. The cache is also commonly few megabytes for modern cpus. You can't control cache however as a programmer, so it doesnt really matter that much. Now random access memory(RAM). This is not inside cpu direclty, if we're not talking about SoC's ofc. However, RAM is commonly closer to cpu than any other component on the motherboard. Thats because of latency. Ram has latency about a few nanoseconds. RAM has also a big disanvantage tho, and that is it resets as soon as the power to it is cut off. This is most commonly used type of storage in programming, because you can quickly access it etc. What about the long term storage? Well, there are multiple types of these, such as Floppy drives, HDD's or SSD's(solid state drive). Each having different capcity, and speed, so I wont cover all of those, but your program commonly uses them to store something like config files, larger assets etc. Anyway, programming is kindof like saying the computer which instructions to execute(by computer I mean eighter commonly CPU or GPU, or any other processing unit). So lets say you want to add 2 numbers. For simplier syntax lets use assembly First of all, we need to load data to the registers. You can load them from hard drive, or from memory, but I chosed to just hard code them like this: MOV EAX, 10 MOV EBX, 20 These two commands will(atleast in nasm syntax) move 10 and 20 to EAX and EBX registers. Now lets add them together ADD EAX, EBX This will result in EAX having value 30, and EBX having value 20. Preety simple right? Now, you want to execute some other hardware than just memory and cpu dont you? To this, we can use syscalls aka. interupts. Interupts were made in CPU's all the way from when the 8086 architecture was born. Well even earlier actually.. but thats computer history so I wont dive into those right now. Lets say you want to set a video mode that is 200x320 pixels, 256colors. Lets also assume that you are in real mode. MOV AX, 0x13 INT 0x10 This will initiate the 13h mode, with the parameters mentioned above. Kernels like linux have implemented their own interupts tho, to control it trough their API. On DOS its commonly interupt 25, on linux its interupt 0x80, and on windows.... ye its complicated. Now lets talk about conditions. If your program dont have conditions.. its kindof dumb. It always executes everything the same, which is not a bad thing, but still kindof limiting. How do we make a simple condition then? Lets see... MOV AX, 10 CMP AX, 10 JE a JNE b a: ; Handle if equal b: ; Handle if not equal As you can see here we are first moving 10 to ax, for it to be equal to the 10 mentioned below it CMP instructions simply compares the two, and stores the result in the CARRY register. You can then you this register to jump IF the result was equal(JE) or if it wasn't equal(JNE). There are several instructions CPU's provide us. JE = Jump if equal JNE= Jump if not equal JL = Jump if lesser(the first value is smaller than the second) JG = Jump if greater(the first value is bigger than the second) JLE= Jump if lesser OR equal etc. Now what exactly does jump do? jump changes the IP register in the cpu, which controls which instruction is currently being executed. So lets say you want to skip part of a program, so you do this: JMP skip ; The part of the program you want to skip skip: Or you can even do a simple loop like this: A: JMP A This will forever execute the code between the A: label and the JMP instruction, but we can also use conditional jumps to jump IF a condition is met, like this: MOV EAX, 0 A: ; Code inbetween INC A CMP EAX, 10 JLE A This will execute the code inbetween 10 times. Why? The INC instruction increases EAX by 1. So when you compare it aginst 10, it will execute 10 times. So now, lets stop using asm and look into higher level language, such as C. C is lowest level high level language that is still used today, and it looks like this. int a = 10; while(a < 10){ // Code inbetween a++; } As you can see, the a variable is now not register anymore, because its not called eax, or any other thing like it. So, the A is now variable in memory, not register anymore. However you can still instruct it to become one like this: register int a = 10; This will instruct the C compiler(aka translator from C to assembly, where assembler converts it to actual executable) to store A in register, if its possible. And thats about it! Now, how to assemble your assembly code? Just do: nasm -o output.out program.nasm And your executable is ready! Just remember to put _start: label at the start of your code, so the computer knows where the program starts. Ok, and thats it for today, see you soon!