:: krowemoh

Thursday | 26 DEC 2024
Posts Links Other About Now

previous
next

Learning Assembly - Day 5

2023-09-09

Lesson 18 - FizzBuzz

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.

Lesson 19 - Execute

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

Lesson 20 - Forking

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.

Lesson 21 - Time

A very simple lesson showing how to use the time syscall.

Lesson 22 - Creating a file

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.

Lesson 23 - Writing to a file

This uses sys_write but now we can specify a different output than just stdout.

Lesson 24 - Opening a file

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.

Lesson 25 - Reading a file

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.

Lesson 26 - Closing a file

A straightforward lesson on closing a file descriptor.

Lesson 27 - Seeking a file

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.

Lesson 28 - Deleting a file

A simple lesson showing to delete a file.

The next set of lessons will be on sockets which I think will be quite fun.