I tried to do this without using the example and got to a pretty similar solution. Fun little project. I need to build out a list of registers and what they mean and also what the various system calls are, I think having a cheatsheet would be massively helpful.
The idea of replacing the running process with something else is pretty cool and it's one I've probably used before in some abstracted way. I know that fork and execute are heavily involved in shells so it would be cool to start building out a shell in assembly.
I was able to wire up a version of this lesson where you can enter in the command to run. This was a bit of fun as it builds off the lesson in a different way. The arguments can be passed in by pushing them onto a stack and then passing in the esp pointer. I wonder if this is the correct way of doing things.
A simple example of something that could become a shell:
%include "functions.asm"
SECTION .data
prompt db "$ ",0h
SECTION .bss
input: resb 255
SECTION .text
global _start
_start:
mov eax, prompt
call sprint
mov eax, 3
mov ebx, 0
mov ecx, input
mov edx, 255
int 80h
mov byte[ecx+eax-1], 0 ; remove the newline character from input
mov eax, 11 ; exec syscall
mov ecx, 0h ; null terminator
push ecx ; push on to the stack
mov ebx, input ; move the address of input into ebx
push ebx ; push ebx onto the stack as well
mov ecx, esp ; the stack now contains the args
mov edx, 0 ; environment is set to 0
int 80h
call quit
nasm -f elf shell.asm && ld -m elf_i386 shell.o -o shell && ./shell
The lesson was a bit too short, I know that forking is important but this doesn't really highlight it. This lesson very much just shows how the fork works. I assume that you can check if it is the child process and then run the exec. This way you can keep the parent shell process going while the child process becomes whatever program that got sent to it.
A very simple lesson showing how to use the time syscall.
Very simple lesson of creating a file. The interesting thing here is that permissions are specified and given in octal. I wonder what a system where permissions are fully ignored would look like. I would love to make my system calls.
This uses sys_write but now we can specify a different output than just stdout.
Very straightforward. The lessons repeat the creating and writing code and I think it would be better to remove that chunk and print out the file descriptor itself.
This lesson is when I can see how a program like cat could be written using assembly. I think it would be fun to build a few of the core utilities in assembly.
A straightforward lesson on closing a file descriptor.
This was straightforward but I know that I'll need some practice before the whence and offset stuff make sense. I can see the case for setting the offset to 0 and the whence to the end of the file. This will let you append. Setting the whence to the beginning of the file lets you have a positive offset to put things directly into a file. This will be useful when you update spots directly but inserting data needs to be done some other way.
A simple lesson showing to delete a file.
The next set of lessons will be on sockets which I think will be quite fun.