Where are the literals stored in instructions like this if (var == 'x')?

Say in a statement like this

char var;

if( var == 'x');

Do we share the memory for "x" in the first place?

If so, is that (stack / data)?

Thank!

+5
source share
4 answers

The value "x" may be stored directly in the code segment as part of the comparison instruction, or it may be stored in the code segment for direct loading into the register or in the data segment for indirect loading or compared. It depends on the compiler.

+24
source

Almost every compiler oriented to a suitable architecture (16 bits and higher) will put a constant for "x" in the CPU instruction for comparison:

cmp  r0, #'x'

, , , .

+7

, , GCC. -S, . -Wa,-al, , , . ,

int foo(char var) {
    if (var == 'x')
        return 42;
    return 17;
}

gcc -c -Wa,-al Windows

GAS LISTING C:\DOCUME~1\Ross\LOCALS~1\Temp/ccyDNLLe.s                   page 1


   1                            .file   "q3715034.c"
   2                            .text
   3                            .p2align 4,,15
   4                    .globl _foo
   5                            .def    _foo;   .scl    2;      .type   32;
.endef
   6                    _foo:
   7 0000 55                    pushl   %ebp
   8 0001 31C0                  xorl    %eax, %eax
   9 0003 89E5                  movl    %esp, %ebp
  10 0005 807D0878              cmpb    $120, 8(%ebp)
  11 0009 5D                    popl    %ebp
  12 000a 0F94C0                sete    %al
  13 000d 48                    decl    %eax
  14 000e 83E0E7                andl    $-25, %eax
  15 0011 83C02A                addl    $42, %eax
  16 0014 C3909090              ret
  16      90909090
  16      90909090

7 9 - , , 11 16 - . 8 0. 10 - var == 'x', $120 cmpb $120, 8(%ebp); ASCII 'x' 120 0x78 . 0x78 .text 8, CMPB. , SETE, AL 1, , 0 . DECL, ANDL ADDL 17, 42 AL EAX, .

+4
. . ++ rvalue, , lvalue, . ++ , . , - (int, double, MyClass,...), . , char double, . . 'x' 3.14.

How and where the compiler stores these values ​​is beyond the scope of the standard. He does not even have to store them directly. For example, a type expression x * 8can be translated into assembly code that shifts the value of x three bits to the left.

+1
source

All Articles