Skip to content

A repository to keep my files while learning intel x86 assembly

Notifications You must be signed in to change notification settings

GGAlanSmithee/learning-intel-x86-assembly

Repository files navigation

learning-intel-x86-assembly

A repository to keep my files while learning Intel x86 assembly. I am using the NASM assembler, which uses the standard Intel syntax for writing x86 assembly code. The examples are ordered chronologically by the time they were added, starting with 001. This means that the examples are not necesarily ordered by complexity. Each example includes a small boot loader header, so that they are identified as bootable and can mounted and run using the qemu emulator.

The isolated header can be found in the first example, 001_header.

I will be adding a markdown file for each example, but this is more as a way for me to solidify what I am learning then something worth reading. Feel free to look through them though.

DISCLAIMER

This repository serves as a learning experience for me and will contain errors, missconceptions, misinformation, unoptimized code etc. Feel free to create an issue, PR or discussion with improvements.

Instructions

Before running an example you need to make sure that the two executables nasm and qemu are available in the system path. This can be done in a variaty of ways, depending on your operating system. The way I did it was to download SASM (which is great for debugging assembly) and qemu. I then made sure the (windows) environment variables path included their respective directories.

To run an example, simply type run.sh {example}, for example run.sh 001_header. A shell script, .sh, is a way to run scripts on linux, but you can execute it on windows by, for example, installing the git bash, or by using the Windows subsystem for Linux.

If you wish to create a new example, the create_example.sh script will help you get set up. Simply call it with ./crete_example.sh {name_of_example} which will add a new folder with an, othervise empty, but bootable script and a readme file.

Manuals

Additional resources

Examples

Ideas for future examples

  • non blocking keyboard input (0x16, AH = 1)
  • Indexes and pointers
  • calculator
  • GDT
  • IDT
  • Protected mode
  • Passing parameters to procedure
  • structs and passing structs by reference
  • local labels
  • data structures
  • local variables
  • section
  • data and code sections
  • calling conventions: cdecl, syscall, optlink

Notes

Operating modes

As explained in vol. 1, ch. 3 of the intel manual, the x86 architecture has three operting modes: real-address mode, protected mode, and system management mode.

real-address mode

The mode that the processor enters upon startup is called real-address mode. In this mode we have access to the programming environment of the processor, which lets us execute instructions, access memory and switch to protected mode. All our examples is written in this mode. While real address mode gives us direct control over memory and a handy set of interrupts, it is also very limiting. For example, it has less than 1MB of RAM available and no built in security mechanisms. See real address mode for more info.

By a lack of understanding on my part, I started running examples with the assembly directive BITS 32. This caused incompatabilities that became apparent when I tried to add an example of call procedures, which uses the stack. The procedure calls would behave very randomly, not returning to the correct addresses etc. The reason for this is that real address mode always run in 16 bit mode. I have since updated the examples.

protected mode

From real-address mode, we can enter protected mode. This is done by setting up a descriptor table and enabling the Protection Enabled (PE) bit in the control register 0 (CR0) (more on this in a later example). In this mode we can do things such as setting up different memory models using virtual memory addresses. It also gives us access to a lot more memory. The limit on a 32-bit system is 4GB (4,294,967,295 bytes) which is the max number of a 32-bit unsigned integer (2^32-1). See protected mode for more info.

Other

  • The names x86 and IA-32 are used interchangeably by Intel

About

A repository to keep my files while learning intel x86 assembly

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published