Skip to content

Commit

Permalink
Examples: add README encoder example to build
Browse files Browse the repository at this point in the history
  • Loading branch information
athre0z committed Jul 29, 2022
1 parent ceb74e9 commit 5e61af3
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 0 deletions.
8 changes: 8 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,14 @@ if (ZYDIS_BUILD_EXAMPLES AND NOT ZYAN_NO_LIBC)
endif ()

if (ZYDIS_FEATURE_ENCODER)
add_executable("EncodeMov" "examples/EncodeMov.c")
target_link_libraries("EncodeMov" "Zydis")
set_target_properties("EncodeMov" PROPERTIES FOLDER "Examples/Encoder")
target_compile_definitions("EncodeMov" PRIVATE "_CRT_SECURE_NO_WARNINGS")
zyan_set_common_flags("EncodeMov")
zyan_maybe_enable_wpo("EncodeMov")
_maybe_set_emscripten_cfg("EncodeMov")

add_executable("EncodeFromScratch" "examples/EncodeFromScratch.c")
target_link_libraries("EncodeFromScratch" "Zydis")
set_target_properties("EncodeFromScratch" PROPERTIES FOLDER "Examples/Encoder")
Expand Down
66 changes: 66 additions & 0 deletions examples/EncodeMov.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/***************************************************************************************************
Zyan Disassembler Library (Zydis)
Original Author : Mappa
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
***************************************************************************************************/

/**
* @file
* Demonstrates encoding a single instruction using the encoder.
*/

#include <stdio.h>
#include <string.h>

#include <Zydis/Zydis.h>

int main()
{
ZydisEncoderRequest req;
memset(&req, 0, sizeof(req));

req.mnemonic = ZYDIS_MNEMONIC_MOV;
req.machine_mode = ZYDIS_MACHINE_MODE_LONG_64;
req.operand_count = 2;
req.operands[0].type = ZYDIS_OPERAND_TYPE_REGISTER;
req.operands[0].reg.value = ZYDIS_REGISTER_RAX;
req.operands[1].type = ZYDIS_OPERAND_TYPE_IMMEDIATE;
req.operands[1].imm.u = 0x1337;

ZyanU8 encoded_instruction[ZYDIS_MAX_INSTRUCTION_LENGTH];
ZyanUSize encoded_length = sizeof(encoded_instruction);

if (ZYAN_FAILED(ZydisEncoderEncodeInstruction(&req, encoded_instruction, &encoded_length)))
{
puts("Failed to encode instruction");
return 1;
}

for (ZyanUSize i = 0; i < encoded_length; ++i)
{
printf("%02X ", encoded_instruction[i]);
}
puts("");

return 0;
}

0 comments on commit 5e61af3

Please sign in to comment.