0

Suppose I have a bit of _asm code that is like the following:

// Unimportant calculations that end in one of the following Labels
OPTIONA :  MOVE EAX, EDX
           JMP END
OPTIONB :  MOVE EBX, EDX
           JMP END
OPTIONC :  MOVE ECX, EDX
           JMP END
END:

Is there a way to exit the _asm part of the code without adding and "END" label?

If I was using C I would swap JMP END with return

JustJohn
  • 59
  • 1
  • 7
  • You can't "exit" without having someplace to go. You need to go to the end of the asm block (equivalently, the start of the compiler-generated code that follows it), and how do you jump somewhere without having a label? Why do you want to avoid having it? Also, what compiler/assembler are you using? – Nate Eldredge Jun 12 '21 at 22:17
  • I'm using the _asm in visual studio. It struck me as odd having to jump to the end of the assembly block, I thought there would be a better way to do it – JustJohn Jun 12 '21 at 22:29
  • 1
    No, I don't think so. The compiler expects to get control back at the first instruction following the asm, so that's where you need to jump. It doesn't push that on the stack as a return address or anything like that, so `ret` isn't an option. – Nate Eldredge Jun 12 '21 at 22:31
  • 1
    Incidentally, with many compilers, a `return;` statement in C compiles exactly to a `jmp` to the function epilogue - much like what you write here by hand. – Nate Eldredge Jun 12 '21 at 22:32
  • @NateEldredge: The Linux kernel uses GNU C [asm goto](https://gcc.gnu.org/onlinedocs/gcc/Extended-Asm.html#GotoLabels) to jump somewhere useful in the whole function, instead of just inside the asm statement. e.g. things like `asm goto("lock cmpxchg %2,%0 / jne %3" : "+m"(*ptr), "+a"(expected) : "r"(desired) : fail_jmp_target)`, with a C goto label `fail_goto_target:` somewhere else in the function. (Of course, in that cmpxchg example it's best to just use a GCC6 condition-code output operand to leave the condition in FLAGS, and let the compiler choose to jump or setcc or whatever.) – Peter Cordes Jun 13 '21 at 03:45
  • Of course if this is MSVC, then you have no efficient options, similar to the general inefficiency of MSVC inline asm's whole design. (No way to get data in or out in registers, except for getting it out by leaving it in EAX and falling off the end of a non-void function that ends with the asm statement. Apparently MSVC actually supports that while inlining, but clang -fasm-blocks doesn't.) – Peter Cordes Jun 13 '21 at 03:48
  • BTW, `move` isn't a valid mnemonic; probably you meant `mov`. And the last `jmp end` is redundant: you can just fall through. Also, with inline asm making branching worse, you might consider `cmov`. But it might still be better to branch instead of lengthening a dependency chain. – Peter Cordes Jun 13 '21 at 03:52

0 Answers0