1

Let's say I have the following assembly program:

SYS_EXIT    = 60

.globl _start
_start:
    mov $SYS_EXIT,  %eax
    mov $4,         %edi
    syscall

Is it possible to set a register name as a variable/label, something like:

SYS_EXIT    = 60
SYS_CALL = '%eax'
FIRST_ARG = '%edi'

.globl _start
_start:
    mov $SYS_EXIT,  SYSCALL
    mov $4,         FIRST_ARG
    syscall

It just makes things a bit easier for me, especially with readability.

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
samuelbrody1249
  • 4,379
  • 1
  • 15
  • 58
  • 1
    You can do it in **NASM** using `%define` preprocessor directives. Those are textual keywords replaced by different keywords or numbers. Not sure about gas. – ecm Sep 19 '20 at 20:04
  • 2
    If you use preprocessed assembly, then you can `#define` it. Otherwise the x86 version of `gas` does not support that. The arm version does, but that doesn't help you :) – Jester Sep 19 '20 at 20:05
  • @Jester by preprocessed do you mean using `gcc` command instead of `as` ? – samuelbrody1249 Sep 19 '20 at 20:06
  • 3
    Yes, with `.S` extension `gcc` runs the preprocessor for you. Otherwise you can run it yourself too. – Jester Sep 19 '20 at 20:07
  • 1
    You might think this would help, but it very quickly introduces new problems. e.g. if you have to use EAX or EDX explicitly for a `div` instruction, it's easy to not notice that they're the same registers as SYS_CALL and THIRD_ARG when you have 2 different names for the same reg. If you use any register-name macros, you basically need to use macros for *every* name (at least for some subset of the registers that's easy to remember) or else you'll very quickly make your code harder to debug, except with disassembly view in a debugger which gets your macros out of the way. – Peter Cordes Sep 19 '20 at 20:19
  • 2
    It's not that hard to remember the x86-64 SysV ABI's calling conventions; it follows logically from a couple design ideas: [What's the best way to remember the x86-64 System V arg register order?](https://stackoverflow.com/a/63892271). I'd recommend doing that instead of macroing register names unless you're trying to write portable asm with macros to handle different calling conventions on different OSes. – Peter Cordes Sep 19 '20 at 20:22
  • @PeterCordes good question/answer on that. Yea I always find it difficult to remember the register orders -- esp %r10 before %r8 and %r9. – samuelbrody1249 Sep 19 '20 at 20:57

0 Answers0