Spec-Zone.ru › C++

Ключевое слово C++: asm

Использование

  • Объявление блока встроенного ассемблера

Пример

Обратите внимание: хотя этот пример хорошо работает с GCC/Clang на платформе x86_64 под Linux, его работу в других случаях нельзя гарантировать, поскольку объявление asm является условно поддерживаемым и(начиная с C++11) определяемым реализацией.

#include <cstring>

int main() noexcept
{
    const char* const c_string = "Hello, world!\n";
    asm
    (R"(
        movq $1, %%rax                 # syscall number for sys_write
        movq $1, %%rdi                 # file descriptor 1 (stdout)
        movq %0, %%rsi                 # pointer to the c‐string
        movq %1, %%rdx                 # length of the c‐string
        syscall                        # invokes an OS system-call handler
    )"
    :                                  // no output operands
    :   "r"(c_string),                 // input: pointer to the c‐string
        "r"(std::strlen(c_string))     // input: size of the c‐string
    :   "%rax", "%rdi", "%rsi", "%rdx" // clobbered registers
    );
}

Вывод:

Hello, world!

<references>

© cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
https://en.cppreference.com/cpp/keyword/asm

Spec-Zone.ru

Настройки Оффлайн Что нового Помощь О нас
Spec-Zone .ru
спецификации, руководства, описания, API