28. Userland assembly

Programs under userland/arch/<arch>/ are examples of userland assembly programming.

This section will document ISA agnostic concepts, and you should read it first.

ISA specifics are covered at:

Like other userland programs, these programs can be run as explained at: Section 2.8, “Userland setup”.

As a quick reminder, the fastest setups to get started are:

However, as usual, it is saner to build your toolchain as explained at: Section 11.1, “QEMU user mode getting started”.

The first examples you should look into are:

The add examples in particular:

  • introduce the basics of how a given assembly works: how many inputs / outputs, who is input and output, can it use memory or just registers, etc.

    It is then a big copy paste for most other data instructions.

  • verify that the venerable ADD instruction and our assertions are working

Now try to modify modify the x86_64 add program to see the assertion fail:

LKMC_ASSERT_EQ(%rax, $4)

because 1 + 2 tends to equal 3 instead of 4.

And then watch the assertion fail:

./build-userland
./run --userland userland/arch/x86_64/add.S

with error message:

assert_eq_64 failed
val1 0x3
val2 0x4
error: asm_main returned 1 at line 8

and notice how the error message gives both:

  • the actual assembly source line number where the failing assert was

  • the actual and expected values

Other infrastructure sanity checks that you might want to look into include: