Solaris X86 64-bit Assembly Programming
This is a simple example on writing, compiling, and debugging Solaris 64-bit x86 assembly language with a C program.
This is also referred to as “AMD64” assembly. The term “AMD64” is used in an inclusive sense to refer to all X86 64-bit processors, whether AMD Opteron family or Intel 64 processor family. Both run Solaris x86.
I’m keeping this example simple mainly to illustrate how everything comes together—compiler, assembler, linker, and debugger when using assembly language.
The example I’m using here is a C program that calls an assembly language program passing a C string. The assembly language program takes the C string and calls printf() with it to print the string.
AMD64 Register Usage
But first let’s review the use of AMD64 registers.
AMD64 has several 64-bit registers, some special purpose (such as the stack pointer) and others general purpose. By convention, Solaris follows the AMD64 ABI in register usage, which is the same used by Linux, but different from Microsoft Windows in usage (such as which registers are used to pass parameters). This blog will only discuss conventions for Linux and Solaris.
The following chart shows how AMD64 registers are used.
The first six parameters to a function are passed through registers.
If there’s more than six parameters, parameter 7 and above are pushed on the stack before calling the function. The stack is also used to save temporary “stack” variables for use by a function.
64-bit Register | Usage |
|---|---|
%rip |
Instruction Pointer points to the current instruction |
%rsp |
Stack Pointer |
%rbp |
Frame Pointer (saved stack pointer pointing to parameters on stack) |
%rdi |
Function Parameter 1 |
%rsi |
Function Parameter 2 |
%rdx |
Function Parameter 3 |
%rcx |
Function Parameter 4 |
%r8 |
Function Parameter 5 |
%r9 |
Function Parameter 6 |
%rax |
Function return value |
%r10, %r11 |
Temporary registers (need not be saved before used) |
%rbx, %r12, %r13, %r14, %r15 |
Temporary registers, but must be saved before use and restored before returning from the current function (usually with the push and pop instructions). |
32-, 16-, and 8-bit registers
To access the lower 32-, 16-, or 8-bits of a 64-bit register use the following:
64-bit register | Least significant 32-bits | Least significant 16-bits | Least significant 8-bits |
|---|---|---|---|
| %rax | %eax | %ax | %al |
| %rbx | %ebx | %bx | %bl |
| %rcx | %ecx | %cx | %cl |
| %rdx | %edx | %dx | %dl |
| %rsi | %esi | %si | %sil |
| %rdi | %edi | %di | %dil |
| %rbp | %ebp | %bp | %bpl |
| %rsp | %esp | %sp | %spl |
| %r9 | %r9d | %r9w | %r9b |
| %r10 | %r10d | %r10w | %r10b |
| %r11 | %r11d | %r11w | %r11b |
| %r12 | %r12d | %r12w | %r12b |
| %r13 | %r13d | %r13w | %r13b |
| %r14 | %r14d | %r14w | %r14b |
| %r15 | %r15d | %r15w | %r15b |
| %r16 | %r16d | %r16w | %r16b |
There’s other registers present, such as the 64-bit %mm registers, 128-bit %xmm registers, 256-bit %ymm registers, and 512-bit %zmm registers. Except for %mm registers, these registers may not present on older AMD64 processors.
Assembly Source
The following is the source for a C program, helloas1.c, that calls an assembly function, hello_asm().
$ cat helloas1.c |
The assembly function called above, hello_asm(), is defined below.
$ cat helloas2.s |
In the assembly source above, the C skeleton code under “#if defined(lint)” is optionally used for lint to check the interfaces with your C program–very useful to catch nasty interface bugs. The “asm_linkage.h” file includes some handy macros useful for assembly, such as ENTRY_NP(), used to define a program entry point, and SET_SIZE(), used to set the function size in the symbol table.
The function hello_asm calls C function printf() by passing two parameters, Parameter 1 (P1) is a printf format string, and P2 is a string variable. The function begins by moving %rdi, which contains Parameter 1 (P1) passed hello_asm, to printf()’s P2, %rsi. Then it sets printf’s P1, the format string, by loading the address of the format string in %rdi, P1. Finally it calls printf.
After returning from printf, the hello_asm function returns itself.
Instructions starting with dot “.” are pseudo instructions. Pseudo instructions don’t generate code but control things such as alignment (.align), externals (.extern). string data (.ascii), and byte data (.byte). Labels are external symbols unless they begin with a dot “.” (such as “.printf_string” in this example).
Larger, more complex assembly functions usually do more setup than the example above. If a function is returning a value, it would set %rax to the return value. Also, it’s typical for a function to save the %rbp and %rsp registers of the calling function and to restore these registers before returning. %rsp contains the stack pointer and %rbp contains the frame pointer. Here is the typical function setup and return sequence for a function:
ENTRY_NP(sample_assembly_function) |
Compiling and Running Assembly
Use the Solaris cc command to compile both C and assembly source, and to pre-process assembly source. You can also use GNU gcc instead of cc to compile, if you prefer. The “-m64” option tells the compiler to compile in 64-bit address mode (instead of 32-bit).
$ cc -m64 -o helloas2-cpp.s -D_ASM -E helloas2.s |
Debugging Assembly with MDB
MDB is the Solaris system debugger. It can also be used to debug user programs, including assembly and C. The following example runs the above program, hello-asm, under control of the debugger.
In the example below I load the program, set a breakpoint at the assembly function hello_asm, display the registers and the first parameter, step through the assembly function, and continue execution.
$ mdb hello-asm# Start the debugger |
In the example above, at the start of function hello_asm(), I display the stack contents with “$C”, display the registers contents with “$r”, then disassemble the current function with “::dis”.
The first function parameter, which is a C string, is passed by reference with the string address in %rdi (see the register usage chart above).
The address is 0x400cf8, so I print the value of the string with the “/S” MDB command: “0x0000000000400cf8/S”.
I can also print the contents at an address in several other formats.
Here’s a few popular formats. For more, see the mdb(1) man page for details.
- address/S C string
- address/C ASCII character (1 byte)
- address/E unsigned decimal (8 bytes)
- address/U unsigned decimal (4 bytes)
- address/D signed decimal (4 bytes)
- address/J hexadecimal (8 bytes)
- address/X hexadecimal (4 bytes)
- address/B hexadecimal (1 bytes)
- address/K pointer in hexadecimal (4 or 8 bytes)
- address/I disassembled instruction
Finally, I step through each machine instruction with the “[” command, which steps over functions. If I wanted to enter a function, I would use the “]” command. Then I continue program execution with “:c”, which continues until the program terminates.
MDB Basic Cheat Sheet
Here’s a brief cheat sheet of some of the more common MDB commands useful for assembly debugging.
There’s an entire set of macros and more powerful commands, especially some for debugging the Solaris kernel, but that’s beyond the scope of this example.
$CDisplay function stack with pointers
$cDisplay function stack
$eDisplay external function names
$vDisplay non-zero variables and registers
$rDisplay registers
::fpregsDisplay floating point (or “media” registers). Includes %st, %xmm, and %ymm registers.
::statusDisplay program status
::runRun the program (followed by optional command line parameters)
$qQuit the debugger
address:bSet a breakpoint
address:dDelete a breakpoint
$bDisplay breakpoints
:cContinue program execution after a breakpoint
[Step 1 instruction, but step over function calls
]Step 1 instruction
address::disDisassemble instructions at an address
::eventsDisplay events
Assembly Language Formats
X86 assembly language comes in two formats—one used by Intel and Microsoft DOS and Windows, and the other used by ATT UNIX and UNIX-like systems (including Solaris and Linux). Here’s a chart illustrating the differences:
| Intel/Microsoft syntax | ATT/UNIX/Solaris/Linux syntax |
|---|---|
| movrax,(4*20h) | mov$[4*0x20],%rax |
| movrax,[ebx+20h] | mov0x20(%ebx),%rax |
| learax,[ebx+ecx] | lea(%ebx,%ecx),%rax |
| subrax,[ebx+ecx*4-20h] | sub-0x20(%ebx,%ecx,4),%rax |
| leaeax,[rcx+rax*8-0x30] | lea-0x30(%rcx,%rax,8),%eax |
As you can see the main difference is the operands are reverse. Intel uses “destination, source” and ATT uses “source, destination”. Other differences is ATT prefixes literals with “$” and registers with “%”. Intel uses a “h” suffix to designate hexadecimal and ATT uses “0x” prefix. Intel uses “[register+offset]”, addressing and ATT uses “offset(register)”. A simple way to help translate from Intel to ATT syntax is to assemble the source into an object file and use Solaris dis(1) to disassemble the object.
Further Information
“Assembly Language Techniques for Oracle Solaris on x86 Platforms” by Paul Lowik (2004).
Good tutorial on Solaris x86 optimization with assembly.
The Solaris Operating System on x86 Platforms
An excellent, detailed tutorial on X86 architecture, with Solaris specifics.
By an ex-Sun employee, Frank Hofmann (2005).
“AMD64 ABI Features”, Solaris 64-bit Developer’s Guide
contains rules on data types and register usage for Intel 64/AMD64-class processors.
(available at docs.oracle.com)
Solaris X86 Assembly Language Reference Manual
(available at docs.oracle.com)
SPARC Assembly Language Reference Manual
(available at docs.oracle.com)
System V Application Binary Interface (2003) defines the AMD64 ABI for UNIX-class operating systems, including Solaris, Linux, and BSD.
Google for it—the original website is gone.- cc(1), gcc(1), mdb(1), dis(1) man pages.