10.6 Forcing inline assembly operands into specific registers

Sometimes specifying the exact register that is used for an operand is preferable to letting the compiler allocate a register automatically.

For example, the inline assembly block may contain a call to a function or system call that expects an argument or return value in a particular register.

To specify the register to use, the operand of the inline assembly statement must be a local register variable, which you declare as follows:

register int foo __asm("r2");
register float bar __asm("s4") = 3.141;

A local register variable is guaranteed to be held in the specified register in an inline assembly statement where it is used as an operand. Elsewhere it is treated as a normal variable, and can be stored in any register or in memory. Therefore a function can contain multiple local register variables that use the same register if only one local register variable is in any single inline assembly statement.

Example

// This function uses named register variables to make a Linux 'read' system call.
// The three arguments to the system call are held in r0-r2, and the system
// call number is placed in r7.
int syscall_read(register int fd, void *buf, unsigned count) {
	register unsigned r0 __asm("r0") = fd;
	register unsigned r1 __asm("r1") = buf;
	register unsigned r2 __asm("r2") = count;
	register unsigned r7 __asm("r7") = 0x900003;
	__asm("svc #0"
		: "+r" (r0)
		: "r"  (r1), "r" (r2), "r" (r7));
	return r0;
}
Non-ConfidentialPDF file icon PDF versionDUI0774J
Copyright © 2014–2017, 2019 Arm Limited or its affiliates. All rights reserved.