Coding Challenge (C amd64 Linux)

What does this output and how does it work?
#include <stdio.h>

#define TAUNT 1

void taunt1(void)
{
printf("skill\n");
}

void taunt2(void)
{
printf("issue\n");
}

void dispatch(void)
{
asm volatile goto(
"mov %0, %%rcx\n"
"test %%rcx, %%rcx\n"
"jz %l[taunt1]\n"
"jmp %l[taunt2]\n"
: : "i"(TAUNT) : "rcx" : taunt2, taunt1
);

taunt1:
taunt1();
return;

taunt2:
taunt2();
return;
}

int main(void)
{
dispatch();

return 0;
}
#include <stdio.h>

#define TAUNT 1

void taunt1(void)
{
printf("skill\n");
}

void taunt2(void)
{
printf("issue\n");
}

void dispatch(void)
{
asm volatile goto(
"mov %0, %%rcx\n"
"test %%rcx, %%rcx\n"
"jz %l[taunt1]\n"
"jmp %l[taunt2]\n"
: : "i"(TAUNT) : "rcx" : taunt2, taunt1
);

taunt1:
taunt1();
return;

taunt2:
taunt2();
return;
}

int main(void)
{
dispatch();

return 0;
}
1 Reply
Saßì
Saßì13mo ago
This C code demonstrates the use of inline assembly and the 'asm' goto construct to conditionally jump to different functions based on the value of the 'TAUNT ' macro. Explanation: The 'dispatch' function contains inline assembly that checks the value of the 'TAUNT' macro. If 'TAUNT' is 1, it jumps to the label 'taunt1'; otherwise, it jumps to 'taunt2'. The labels 'taunt1' and 'taunt2' are defined as functions that print different messages ("skill" and "issue" respectively). The 'main' function calls 'dispatch', which, based on the value of 'TAUNT', executes either 'taunt1' or' taunt2'. Note: The use of inline assembly and 'asm goto ' is a specialized and low-level technique. It's typically not recommended unless there's a specific need for such low-level control.
Want results from more Discord servers?
Add your server