0

I want to know in the following simple code, at which point the variable c is being calculated. Does it get calculated before main label? Or when execution in main reaches the ldr r0, [r3] instruction, it will call the _GLOBAL__sub_I_a function/label?

Basically how to read the assembly code in this case (it is ARM none). The code can be found here: https://godbolt.org/z/jcYahM1zW

c++:

#include <cstdio>

int a = 2;
int b = 5;
int c = a * b;

int main() {    
    return c;
}

Assembly:

main:
  ldr r3, .L3
  ldr r0, [r3]
  bx lr
.L3:
  .word .LANCHOR0
_GLOBAL__sub_I_a:
  ldr r2, .L6
  ldr r3, [r2]
  ldr r2, [r2, #4]
  mul r3, r2, r3
  ldr r1, .L6+4
  str r3, [r1]
  bx lr
.L6:
  .word .LANCHOR1
  .word .LANCHOR0
a:
  .word 2
b:
  .word 5
c:
Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
DEKKER
  • 877
  • 6
  • 19
  • Search the internet for "ARM ARM". Or "Arm assembly reference manual". – Thomas Matthews Aug 11 '22 at 21:26
  • 1
    `C++` requires that the global variables are initialized before `main` runs. So I would think that the runtime system calls the code at `_GLOBAL__sub_I_a` before invoking `main`. – Galik Aug 11 '22 at 21:26
  • @ThomasMatthews ARM ARM is the "architecture reference manual" not "assembly reference manual" ;-) – Tom V Aug 11 '22 at 21:41
  • @DEKKER: nothing in this code calls `_GLOBAL__sub_I_a`. How the global variable gets initialized will vary from system to system, depending on the runtime library and compiler implementation. – Tom V Aug 11 '22 at 21:45
  • 1
    @TomV: Godbolt.org filters directives by default, including ones that add the address of `_GLOBAL__sub_I_a` to a list of "init" functions to be called by CRT code before calling `main`. Look at the full directives. – Peter Cordes Aug 12 '22 at 00:39
  • I think the compiler could do that before the code is generated. No need to calculate `c` at runtime. Just compute the value in the compiler and set thing that holds that value (memory or register). – Martin York Aug 12 '22 at 01:58
  • @Galik: there is also [Deferred_dynamic_initialization](https://en.cppreference.com/w/cpp/language/initialization#Deferred_dynamic_initialization) – Jarod42 Aug 12 '22 at 09:54
  • @PeterCordes: I said nothing in *this* code calls it. That is correct. The fact that it must get called is somehow is obvious, but that call isn't shown in this code :-) – Tom V Aug 12 '22 at 13:02
  • @MartinYork: You'd think so, but these are non-const *global* variables. Some other compilation unit may have static constructors that modify `a` or `b`, or perhaps there's some other reason GCC gives up on doing constant-propagation. The godbolt link in the question has optimization enabled. – Peter Cordes Aug 12 '22 at 18:53
  • @TomV: I think when I commented, I hadn't read the question carefully, and hadn't noticed it was wondering if `ldr r0, [r3]` called some other code (e.g. from a page-fault handler? Or by literal magic.) But still, part of the whole *compiler output* from that C source does arrange for that function to be called, just not the part that made it past the filters. The filters are designed for people that either just want to look at actual code for normal functions, or those who know what directives must be there (e.g. sections). And are occasionally imperfect, removing a `.set` to make a synonym – Peter Cordes Aug 12 '22 at 18:58

0 Answers0