Categories
Uncategorized

echo and printenv in x86 Assembly

This post contains implementations of echo and printenv in 32-bit x86 assembly for Linux.

echo is a Unix utility that prints its arguments to standard output.

printenv is a Unix utility that prints the environment to standard output.

The core functionality of these programs can be written in a few lines of C, where program arguments and the environment are passed as function arguments to main.

When a process is executed on Linux (or other Unix-like systems), its stack contains pointers to the program arguments and the environment, as shown below.

        |--------------------------|     Low
0(%esp) |      Argument Count      |  Addresses
        |--------------------------|
4(%esp) |     Argument Pointers    |
        |           ...            |
        |--------------------------|
        |            0             |
        |--------------------------|
        |   Environment Pointers   |
        |           ...            |
        |--------------------------|
        |            0             |
        |--------------------------|
        |     Additional Data      |
        |           ...            |     High
        |--------------------------|  Addresses