Skip to content

Instantly share code, notes, and snippets.

@afulsamet
Created October 4, 2022 22:10
Show Gist options
  • Save afulsamet/39b8ae12549506f69aff0a3008a1ba43 to your computer and use it in GitHub Desktop.
Save afulsamet/39b8ae12549506f69aff0a3008a1ba43 to your computer and use it in GitHub Desktop.
Inline string with using bit_cast (std:c++20)
#include <bit>
#include <array>
int main()
{
// Create raw array in stack: rsp+2C
volatile std::uint32_t stack[3];
// mov dword ptr ss:[rsp+2C], 6C6C6548
stack[0] = std::integral_constant<std::uint32_t, std::bit_cast<std::uint32_t>(std::array<char, sizeof(std::uint32_t)>{'H', 'e', 'l', 'l'})>::value;
// mov dword ptr ss:[rsp+30], 6F57206F
stack[1] = std::integral_constant<std::uint32_t, std::bit_cast<std::uint32_t>(std::array<char, sizeof(std::uint32_t)>{'o', ' ', 'W', 'o'})>::value;
// mov dword ptr ss:[rsp+34], 646C72
stack[2] = std::integral_constant<std::uint32_t, std::bit_cast<std::uint32_t>(std::array<char, sizeof(std::uint32_t)>{'r', 'l', 'd', '\0'})>::value;
// lea rdx, qword ptr ss:[rsp+2C]
std::printf("%s", (const char *)(&stack));
}
@afulsamet
Copy link
Author

afulsamet commented Oct 4, 2022

x64 disassembly

main():
    sub rsp,38
    mov dword ptr ss:[rsp+2C],6C6C6548
    mov dword ptr ss:[rsp+30],6F57206F
    mov dword ptr ss:[rsp+34],646C72
    lea rdx,qword ptr ss:[rsp+2C]
    lea rcx,qword ptr ds:[<"%s">]
    call <printf>
    xor eax,eax
    add rsp,38
    ret

Output

Hello World

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment