1

I just wanted to write some ASM code and include it in a C/C++ code, not through inline-mode, but creating a different ASM module. I've found out that a CodeBlocks project allows ASM sources, so I've followed these steps, actually with some slight changes, like the building command:

gcc -c myasmfile.s -o ./obj/Debug/myasmfile.o

And everything seems to work, but labels.

Here is an example:

.text
.intel_syntax noprefix
.globl mytest
mytest:
    push    rbp
    mov     rbp, rsp

    mov     rcx, 0

    .mylabel:
    inc     rcx
    cmp     rcx, 10
    jne      .mylabel

    mov     rax, rcx

    leave
    ret

When I try to run and debug the function, this is what I get:

Debug screenshot

It's like it recognizes mylabel as a new function, cutting all the code after it. I can't explain myself why it's showing the same code twice, but it happens even without labels, so I don't think it's related to them (anyway, why does it happen?).

Then I've read this discussion, and I've tried all possible solutions listed there, but no success. Here is what I've tried:

  • using suffix f and b for forward and backward respectively.

  • using %= for local labels

  • tried even without . in the label declaration

Why do labels work in inline-assembly, while they do not in my case?

EDIT Thanks to @fuz, now my code is working. By the way, inside the debug window the function is copied twice, despite the memory addresses are the same.

Debug screenshot (working)

Marco Luzzara
  • 5,540
  • 3
  • 16
  • 42
  • You should add `-g` switch to generate debug info. "tried even without . in the label declaration" <- that's a good idea, do not use leading dots because those are directives. – Jester Sep 04 '17 at 12:38
  • 2
    Lcaol labels must be prefixed `.L`, not just `.`. You can also use numbered labels. If you like, I can write this up in an answer. – fuz Sep 04 '17 at 12:40
  • Note that the discussion you cited is about inline assembly. You don't write inline assembly so it doesn't apply to you. – fuz Sep 04 '17 at 12:40
  • @fuz I know but I hoped to find an answer. Anyway I changed `.mylabel` into `.Lmylabel:` and it does work. Thank you. Is there a reason why is the same function displayed twice in my debug console? – Marco Luzzara Sep 04 '17 at 12:45
  • @Jester I tried removing the `.` and using the `L` before the label, but no success. It seems the `.` is necessary. – Marco Luzzara Sep 04 '17 at 12:49
  • @MarcoLuzzara It shouldn't be displayed twice. Could you show me what the debug console looks like after you changed `.mylabel` to `.Lmylabel`? – fuz Sep 04 '17 at 12:49
  • 6
    Learning a language from obscure internet resources like youtube videos is a bad idea. And read the documentation of the tools you use. The binutils documentation is well comprehendible. – too honest for this site Sep 04 '17 at 12:50
  • @Jester Actually that's not true. Please read the GAS manual. GAS strictly detects labels by checking if the word ends in a colon. It doesn't matter at all if the label begins with a period or not. However, only labels beginning with `.L` or numeric labels are not entered into the symbol table. – fuz Sep 04 '17 at 12:51
  • @fuz technically you may be able to use leading dot, the point is that you should not because those are expected to be directives by the readers of your program. – Jester Sep 04 '17 at 12:53
  • @fuz: gcc is a frontend only, it is no way related to the syntax of the assembly files. As there is no tag for gas, the generic gnu tag plus assembly seem to be the best fit. – too honest for this site Sep 04 '17 at 12:54
  • @Jester: Not exactly. The colon **after** the label signals it is not a directive. The dot alone does not make it a directive. And the `L` after the dot signals it is a local label. Just read the manual. – too honest for this site Sep 04 '17 at 12:56
  • @Jester You should most definitely use `.L` to prefix local symbols that are not supposed to end up in the symbol tables. That's the entire point of these labels. – fuz Sep 04 '17 at 12:56
  • @Olad The colon after the label signals that's it's a label. Directive and mnemonics are handled the same way in the parser. You can even write macros whose names begin with a dot. There is really nothing special about these identifiers, it's all just convention. – fuz Sep 04 '17 at 12:57
  • @fuz: 1) My Nick is "Ola**f**". 2) That's pretty much the same what I wrote! Give me a more clear hint if you just intend to disagree with me in principle, even if I support your position. The `.L` is special, though, because that's what makes the assembler differentiate a local from a global label. – too honest for this site Sep 04 '17 at 12:59
  • @fuz With the `.L` before the lable, in the debug window the code is displayed twice too, but maybe it's a bug since the memory addresses are the same. – Marco Luzzara Sep 04 '17 at 13:00
  • @Olaf That's not entirely correct: `.L` distinguishes labels that do not end up in the symbol tables. Global labels are distinguished with the `.globl` directive. I'm sorry, I got a bit confused with a ll the comments. – fuz Sep 04 '17 at 13:17
  • @MarcoLuzzara Could you show me how your debug window looks? – fuz Sep 04 '17 at 13:17
  • @fuz: My point was that the colon is what makes a label a label. The `.L` only signals how the label is treated. Other compilers use e.g. `$` as prefix or suffix or only allow numbers for local labels, some don't need a colon. That's what the documentation is for … (The proverb "real engineers don't read manuals" is an "invention" of bad engineers faking to be good ones) – too honest for this site Sep 04 '17 at 13:27
  • @fuz The debug window is the same as the one in the image I put in the question, except that the function is now have been fully read and between the two copies there are some nop and meaningless instruction. But as I said, the memory addresses are the same. – Marco Luzzara Sep 04 '17 at 13:39
  • @MarcoLuzzara Can you please still post a screenshot so I can understand what exactly you see? – fuz Sep 04 '17 at 13:41
  • @fuz Sure, edited. Thank you for the help anyway. – Marco Luzzara Sep 04 '17 at 13:52
  • @MarcoLuzzara This is very weird. I know that assembling with `-g` to generate debug symbols can do this kind of thing, but that seems unlikely in your case. Perhaps a bug? – fuz Sep 04 '17 at 14:00
  • @fuz Maybe you're right about the `-g` option. In my project I'm using debugging symbol, otherwise I could not debug ASM code I've written. – Marco Luzzara Sep 04 '17 at 14:15
  • @MarcoLuzzara Are you sure? Single stepping assembly should work without `-g` (though, the proper line won't be highlighted). – fuz Sep 04 '17 at 14:23
  • @fuz You're right, I forgot that. However I've tried without debugging symbol but the two copies remain. That's probably a bug. – Marco Luzzara Sep 04 '17 at 14:30

0 Answers0