Basics & Sample Programs
Here are some sample programs. System calls are made using the
syscall instruction on an x86-64 version of GNU/Linux as opposed
to using int 0x80 on an x86 version of GNU/Linux. All the programs are in long mode. The list of system calls can be found in /usr/include/asm-x86_64/unistd.h. Calling Convention
- System Calls
- The kernel or system call interface uses registers
RDI, RSI, RDX, R10, R8, R9for passing arguments in that order. A maximum of 6 parameters can be passed. - The kernel destroys
registers
RCXandR11. - The number of the system call is passed in the
register
RAX. - No argument is passed on the stack.
- The return value is placed in
RAX.A value in the range -1 to -4095 (0xFFFFFFFFto0xFFFF0000). - In 32-bit mode, GNU/Linux supports 6 arguments in
the system call and they are passed in the
registers
EBX, ECX, EDX, ESI, EDIandEBPwith the system call number inEAX.
- The kernel or system call interface uses registers
- Function Calls
- For a complete list of the registers that should be used for passing parameters, and for return values, refer the x86-64 ABI .
- The integer and pointer arguments are passed in
the registers
RDI, RSI, RDX, RCX, R8, R9in that order. - The registers
XMM0-XMM7are used to pass the single and double precision floating point arguments. Rest of the arguments might have to be passed on the stack. - The
RAXregister should hold the number of SSE registers (XMM0-XMM7) that are used in the passing of arguments. - The registers
RBX, RSP, RBP, R12-R15are callee-saved registers and are preserved across function calls. RBXis the optional base pointer andRBPis the optional frame pointer.R11is the temporary register used by the Procedure Linkage Table andR10is used to pass a function's static chain pointer.- Integer or pointer type return values are returned in
RAXandRDX.Floating point return values are returned inXMM0andXMM1.Long double precision values are returned inST(0)andST(1).
List of Sample Programs
- Get the CPU name using CPUID instruction
- A "Hello World!" sample program The next five programs are samples from Dr. Paul Carter's assembly tutorial . They have been converted from 32-bit to 64-bit long mode.
- Sample input output functions
- A sample program for multiplication and division operations on an integer
- A sample program to find prime
numbers demonstrating use of
CMPandJMPinstructions in programs - A sample program to demonstrate bit shifts
- A sample program to print greater of two numbers without using jump or branch instructions
- A sample program to demonstrate recursion by calculate factorial of a number
- A sample program to calculate the square of a Hilbert's matrix
- A sample program to demonstrate working of a doubly linked-list

