Linux Format 255 (Sampler)

Page 24

Assembly language coding academy Part Two

Assembly

Missed part one? Get a back issue on page 66!

Low-level system calls with the GNU C library

John Schwartzman shows how to write assembly language code that calls Linux kernel services and the C run-time library. ast issue we used assembly language to access Linux kernel services. Now we’re going to use the C run-time library, glibc, instead of calling the kernel services directly. The glibc functions are in many cases thin wrappers around the Linux kernel services. This is the preferred way to access Linux kernel services. Kernel system calls are limited to six arguments, but that’s not enough for the C library. We use almost the same six registers that we used for kernel system calls: RDI, RSI, RDX, RCX (instead of R10), R8 and R9, but any number of additional arguments can be passed to C library functions on the stack. We populate the registers listed above with the arguments to the function. We then PUSH the remaining arguments onto the stack and remove them from the stack after the C library function returns. You’ll see this in environment.asm. When using the kernel system calls we called a common location using the software interrupt instruction SYSCALL and passed the ID of the specific service in the RAX register. When using the C library, we link to and call the specific function we want by name – though RAX still returns success or failure status to the caller.

L

our expert John Schwartzman is a long-time engineering consultant to business and government. He also teaches computer science at a local college.

ARRRGs!

The best way to follow along with this guide is to get the files for this tutorial from the DVD or from https:// github.com/ jschwartzman/ asm-tutorial.

Our next programs are cmdline.c (Figure 1, above right) and cmdline.asm (Figure 2, page 92). When a main function is invoked it has a few parameters that the user types on the command line. If you type ./cmdline alpha beta goldfish at the command prompt, Linux will execute the program cmdline . The program will receive as parameters, argc , which is the total number of string arguments (four in this case) followed by an array of pointers to the strings on the command line which are in an array of arrays called argv[] . In this case, cmdline will receive as strings ./cmdline , alpha , beta and goldfish . Cmdline.c and cmdline.asm read and print argc and the argv[] strings. Since this is Linux, you can guess how we receive these parameters. RDI will have the integer argc (the first argument), and RSI will have the vector of pointers, argv .

www.techradar.com/pro/linux

Figure 1: cmdline.c. A C program that prints all of the arguments it receives on the command line.

Cmdline.c should be easy to understand. The prototype for main is: int main(int argc, char* argv[]) . After printing argc , we use a for loop to print each parameter index, i , followed by the string parameter argv[i] . That’s it. Execute ./a.out alpha beta goldfish . Now do the same thing in assembly language. Execute ./cmdline alplha beta goldfish . At the beginning of cmdline.asm we define some constants. Some programmers are lazy and omit the constant declarations – they simply insert the appropriate numbers in the assembly code. The effect of this is to confuse the human readers of the program. These values look like ‘magic numbers’ when they’re just sprinkled into the code. We urge you to use LF , EOL , TAB and ARG_SIZE instead of 10, 0, 9 and 8. All programs should be self-documenting and a liberal use of constants improves the documentation. Highlevel languages are somewhat self-documenting, but assembly language needs a lot of documentation! Our main function calls printf , so main is a caller of printf – but main itself is called by the C startup code, so main is also a callee. Therefore, main must save and restore any callee-saved registers that it uses. Notice that we PUSH R12, R13 and RBX at the beginning of main and then POP them in reverse order at the finish label, before main returns. Before that, however, we have some boilerplate code. We PUSH

October 2019 LXF255     87


Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.