I am using same code snippet in C and C++.
#include <stdio.h>
int main() {
goto myLabel;
printf("skippedn");
myLabel:
printf("after myLabeln");
return 0;
}
Using Visual Studio 2022 IDE and Compiler.
Assembly Code for C++
0000000140001000 sub rsp,28h
0000000140001004 jmp 0000000140001014
0000000140001006 jmp 0000000140001014
0000000140001008 lea rcx,[0000000140004230h]
000000014000100F call 0000000140001090
0000000140001014 lea rcx,[0000000140004240h]
000000014000101B call 0000000140001090
0000000140001020 xor eax,eax
0000000140001022 add rsp,28h
0000000140001026 ret
Assembly Code for C
0000000140001000 sub rsp,28h
0000000140001004 jmp 0000000140001012
0000000140001006 lea rcx,[0000000140006000h]
000000014000100D call 0000000140001090
0000000140001012 lea rcx,[0000000140006010h]
0000000140001019 call 0000000140001090
000000014000101E xor eax,eax
0000000140001020 add rsp,28h
0000000140001024 ret
Question is why C++ assembly code uses 2 jmp instructions when C is using 1.
2
Answers
C and C++ are two completely different programming languages.
Different compilers are used to compile them. The actual compiler might be a single, monolithic program, but functionally there are two logically distinct compilers and algorithms, that have nothing to do with each other.
It is not entirely unexpected that different compilers will generate different compiled code from syntactically identical source. The differences in the resulting compiled code result from the different algorithms that are employed by the different compilers, when translating the source code. The differences carry no special, inherent meaning.
It is like this by design in debug builds (msvc bug database) see :
S2019 (debug, x86) generates two identical JMP instructions for one goto statement