Reputation: 1805
Does assembly code when used(linked) in a c project ignore const keyword declared before C datatypes and functions?? And can it modify the contents of the datatypes??
Upvotes: 3
Views: 1395
Reputation:
GCC places global variables marked as const in a separate section, called .rodata. The .rodata is also used for storing string constants.
Since contents of .rodata section will not be modified, they can be placed in Flash. The linker script has to modified to accomodate this.
#include <stdio.h>
const int a = 10 ;
int main ( void ) {
return a ;
}
.section .rodata
.align 4
a:
.long 10
gcc with 00 :
movl a(%rip), %eax // variabile constant
addq $32, %rsp
popq %rbp
gcc with O3 :
movl $10, %eax // numeric constant
addq $40, %rsp
ret
Upvotes: 0
Reputation: 320661
The question is not very precise. What does "ignores" mean?
Assembly language does not have the same concept of const
as C language does. So, assembly cannot ignore it. It simply has no idea about it.
Yet the assembly code generated by C compiler for a C program might easily be affected by the placement of const
keywords in your C program.
In other words, assembly code can be affected by const
keywords. But once that assembly code is built, the const
keyword is no longer necessary.
To say that assembler can modify something declared as const
is not exactly correct either. If you declare a variable as const
, in some cases the compiler might be smart enough to eliminate that variable entirely, replacing it with immediate value of that variable. This means that that const
variable might disappear from the final code entirely, leaving nothing for the assembly code to "modify".
Upvotes: 1
Reputation: 169211
And can it modify the contents of the datatypes??
Maybe, maybe not. If the original object was declared const
then the compiler might emit it into a read-only data segment, which would be loaded into a read-only memory page at runtime. Writing to that page, even from assembly, would trigger a runtime exception (access violation or segmentation fault).
You won't receive a compile-time error, but at runtime your program might crash or behave erratically.
Upvotes: 3
Reputation: 1335
Assembly uses the datatypes you declared in C to better optimize how it stores the information in memory. Everything is written in binary at the end of the day (int, long, char, etc), so there is no datatypes once you get to the low level code.
Upvotes: 1
Reputation: 80325
Does assembly code when used(linked) in a c project ignore const keyword declared before C datatypes and functions??
Yes, the const
keyword is utterly ignored by assembly code.
And can it modify the contents of the datatypes??
If the compiler was able to place a const
location in a read-only segment, then assembly code trying to modify it will cause a segmentation fault. Otherwise, unpredictable behavior will result, because the compiler may have optimized parts of the code, but not others, under the assumption that const
locations were not modified.
Upvotes: 9