Use procedure scope to avoid local labels?

2019-09-08 11:25发布

问题:

I ported some MASM code to NASM. The port was fairly painless, except for the need for local labels. The local labels were needed because the MASM code had multiple procedures, and some labels were common to the procedure. For example

;; MASM code
_FOO PROC
  ...
Exit_Failure:
  ...
Exit_Success:
  ...
  ret
_FOO ENDP

_BAR PROC
  ...
Exit_Failure:
  ...
Exit_Success:
  ...
  ret
_BAR ENDP

Under NASM, I have to use local labels. For example:

;; NASM code
  global _FOO
  section .text

_FOO:
  ...
.Exit_Failure:
  ...
.Exit_Success:
  ...
  ret

When the code is assembled with debugging information, it produces labels that are eyesores (to me). The code will produce labels _FOO.Exit_Failure, _FOO.Exit_Success and so on. In addition to being an eyesore, they complicate porting because I have to add a dot to every label.

I can't seem to find NASM's notion of a "named" section of code so the label can be scoped.

How do I scope procedures to avoid the need for local labels?

回答1:

NASM provides the dot-prefixed local label functionality in part so that you can make procedure-local labels. There is no MASM-equivalent setting for making labels always be local to procedures because NASM itself doesn't really recognize the existence of procedures. It just keeps records of labels, which we sometimes use as procedure entry points without NASM really being able to tell the difference between those or any other non-local labels.

You can make macro-local labels prefixed with %% instead of a period, allowing you to use the same macro multiple times inside the same function, but that's it.



标签: nasm