Learn "x86 architecture learned with your own emulator" with Manjaro (Linux)

Introduction

I bought a book called "x86 architecture learned with my own emulator". The Makefile of this sample code refers to the included toolchain (gcc, etc.), and the tool was for Windows. I don't like developing on Windows for religious reasons, so I decided to develop an x86 emulator on my usual Manjaro (Archlinux-based Linux environment).

In this article, I will explain how to use the sample code modified for Manjaro and the modified part.

Preparing the learning environment

Installation of necessary tools (gcc, nasm, objdump, qemu)

$ sudo pacman -S git gcc nasm binutils qemu

Download sample code

$ git clone https://github.com/Kenta11/x86-emulator-book-Manjaro

Modification of sample code

The differences from the officially distributed sample code are as follows.

--Replace the tool referenced by the Makefile with the one you just installed -Developed boots for use in "4.5 Let's see PBR" with full scratch -Added Makefile so that "4.6 Let's run on the actual machine" can be done with QEMU. --Removed toolchain for Windows

Replace the tool referenced by the Makefile with the one you just installed

In the distributed sample code, gcc etc. were included and referred to them.

--Before (from tolset_p86 / emu4.2 / Makefile)

TARGET = px86.exe                                                                                                                               
OBJS = main.o emulator_function.o instruction.o modrm.o io.o bios.o
Z_TOOLS = ../z_tools

CC = $(Z_TOOLS)/mingw32-gcc/bin/gcc
CFLAGS += -Wall

.PHONY: all 
all :
    make $(TARGET)

%.o : %.c Makefile
    $(CC) $(CFLAGS) -c $<

$(TARGET) : $(OBJS) Makefile
    $(CC) -o $@ $(OBJS)

I replaced the referenced tool with the one installed with pacman, and added a rule that I thought would be more useful.

--After (from chapter4 / section2 / Makefile)

TARGET = px86                                                                                                                                   
PROGRAM = subroutine32.bin
OBJS = main.o emulator_function.o instruction.o modrm.o io.o bios.o

CC = gcc 
ASM = nasm
CFLAGS += -O2 -Wall -Wextra

.PHONY: all run clean
all : $(TARGET) $(PROGRAM)

run : $(TARGET) $(PROGRAM)
        ./$(TARGET) -q $(PROGRAM)

$(TARGET) : $(OBJS)
        $(CC) -o $@ $(OBJS)

%.o : %.c 
        $(CC) $(CFLAGS) -c $<

%.bin : %.asm
        $(ASM) -f bin -o $@ $<

clean :
        rm -f $(TARGET) $(PROGRAM) $(OBJS)

In the case of this Makefile, run and clean are added. run runs the test program on the emulator. Clean also removes products (object files, emulator binaries, test programs).

Developed boots for use in "4.5 Let's see PBR" with full scratch

Since boots was a binary for Windows without exception, I created a program that performs processing similar to the original (chapter4 / section5. tree / master / chapter4 / section5)). The book shows the contents of FAT16, but all the USB memory around me was formatted in FAT32. Therefore, FAT32 can also be displayed. (I wonder if the original boots can also see FAT32 ... I'll check later)

Added Makefile so that "4.6 Let's run on the actual machine" can be done with QEMU

In the book, the assembly program output by boots is modified to run the program that displays the character string on the actual machine. However, I was afraid that there was an accident that the computer broke due to a mistake in the assembly program, so I tried it instead with QEMU (emulator). Place the assembly program pbr.asm in chapter4 / section6 and run make to get QEMU. It reads pbr.bin and displays the same result as the actual machine.

-From chapter4 / section6 / Makefile

TARGET = pbr.bin
ASM = nasm
QEMU = qemu-system-x86_64

.PHONY: all clean
all : $(TARGET)
	$(QEMU) $<

%.bin : %.asm
	nasm $< -o $@

clean :
	rm -f $(TARGET)

Removed toolchain for Windows

The included gcc and boots are for Windows and are not needed for Manjaro, so I deleted them. I'm happy because the file size is smaller.

Summary

I worked on "x86 architecture learned with my own emulator" with Manjaro. I don't think you can enjoy the fun of the 80386 emulator being incrementally created as you read through the pages in other books (especially Japanese books!). The architecture of x86 seems complicated at first glance, but after reading this book it seems complicated: P Of course, this has happened for historical reasons, but as you read it, why? I was able to experience it. I would definitely recommend it to anyone who is interested in how computers work.

By replacing the tools referenced by the sample code Makefile with ones that can be installed with Manjaro's package manager, it is easier to reuse in a Linux environment while being smaller than the original sample code ZIP file. If you would like to work on the contents of this book on Linux, please use the sample code uploaded to GitHub.

Link

--Learning with your own emulator x86 architecture Product homepage: [https://book.mynavi.jp/ec/products/detail/id=41347](https://book.mynavi.jp/ec/products/detail/id= 41347) ――It seems that you can purchase the PDF version on this page. I didn't notice until just before I posted this article. --Sample code for Manjaro: https://github.com/Kenta11/x86-emulator-book-Manjaro

Recommended Posts

Learn "x86 architecture learned with your own emulator" with Manjaro (Linux)
Use WDC-433SU2M2 with Manjaro Linux
Make your own manual. [Linux] [man]
Solve your own maze with Q-learning
Train UGATIT with your own dataset
Solve your own maze with DQN
Your own Twitter client made with Django
Create your own Linux commands in Python
[Reinforcement learning] DQN with your own library
Learn data distributed with TensorFlow Y = 2X
Start arch Linux (Manjaro) minikube with KVM2
Create your own DNS server with Twisted
Create your own Composite Value with SQLAlchemy
X86 assembler on Linux (linkage with C)
To import your own module with jupyter
Publish your own Python library with Homebrew
Run Linux on ARM architecture with QEMU