Wednesday | 04 OCT 2023


Learning Assembly - Day 1

This nasm tutorial looks quite fun.

NASM Assembly Language Tutorials

The plan is to write an http server at the end of it. I'm not sure if I'll be able to but I think it would be fun to try.

Lesson 1 - sys_write

The first lesson mentions linux system calls which doesn't make sense to me. It says that calls are provided by the kernel and that it is built in to the operating system. Shouldn't it be built into the CPU itself. The CPU doesn't have an operating system. Maybe the author means system calls in general and that the Linux ones are stable?

It could be that the assembly I'm writing is Linux specific and isn't for the CPU directly.

You load the EAX register with a number and that gives you the function call that you can do. Very cool.

EAX - Register 1 - Function call to sys_writeEBX - Register 2 - STDOUT/STDERRECX - Register 3 - StringEDX - Register 4 - String Length

The example in my eyes looks reversed but I'm sure there is a reason. Why don't we load eax with the function to call, then the output, then the string and finally the string length?

Lesson 2 - sys_exit

What does the int 80h interrupt really do?

Without the int 80h at the end the program seg faults.

int 80h seems to be acting like some sort of run command to execute the previous chunk of code. That doesn't sound right.

It actually is that. The mov commands set up the registers and the interrupt is what will get the kernel to read the registers and do things.

This explains the segfault, even though the registers are filled for sysexit the kernel never gets to execute the sysexit call.

Lesson 3 - Looping

Looping uses the compare and jump keywords. This is a neat way of doing loops.

Lesson 4 - Subroutines

Interesting that they call functions subroutines here. CALL and RET are used instead of using jumps. This is because call and ret manipulate the stack.

The use of functions is pretty cool. It looks pretty straightforward though painful to juggle things. This reminds me alot of how I was managing my stacks in BASIC.

Lesson 5 - External Files

This was fun to see. I wish I could give it a shot first. i think that would be a fun way to learn. I started debugging why my second message was being output twice. I realized that was by design, there is a bug in the code.

Compiling hello.asm was enough to automatically pull in the included functions. I'm guessing there is a preprocessing step that moves the code in functions.asm into hello.asm. I'm sure there is a dynamic version of this where you have to compile functions.asm as well.

Lesson 6 - Null bytes

I actually fixed the above bug by accident by adding in the null byte. Does the memory that I have in assembly get nulled out at the beginning? This must be happening by the kernel.

Lesson 7 - Print New Line

Pretty cool to see how printing with a new line is really just calling the print logic and loading in a newline character into eax, pushing it onto the stack and then using the address when passing it to syswrite. syswrite needs an address and registers apparently don't have addresses.