| 1 |
/* |
| 2 |
* FCML - Free Code Manipulation Library. |
| 3 |
* Copyright (C) 2010-2020 Slawomir Wojtasiak |
| 4 |
* |
| 5 |
* This library is free software; you can redistribute it and/or |
| 6 |
* modify it under the terms of the GNU Lesser General Public |
| 7 |
* License as published by the Free Software Foundation; either |
| 8 |
* version 2.1 of the License, or (at your option) any later version. |
| 9 |
* |
| 10 |
* This library is distributed in the hope that it will be useful, |
| 11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 |
* Lesser General Public License for more details. |
| 14 |
* |
| 15 |
* You should have received a copy of the GNU Lesser General Public |
| 16 |
* License along with this library; if not, write to the Free Software |
| 17 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 18 |
*/ |
| 19 |
|
| 20 |
#include "prefixes_t.h" |
| 21 |
|
| 22 |
#include <fcml_assembler.h> |
| 23 |
#include <fcml_choosers.h> |
| 24 |
#include <fcml_intel_dialect.h> |
| 25 |
#include <fcml_common_utils.h> |
| 26 |
|
| 27 |
#include "instructions_base_t.h" |
| 28 |
|
| 29 |
fcml_bool fcml_tf_prefixes_suite_init(void) { |
| 30 |
return FCML_TRUE; |
| 31 |
} |
| 32 |
|
| 33 |
fcml_bool fcml_tf_prefixes_suite_cleanup(void) { |
| 34 |
return FCML_TRUE; |
| 35 |
} |
| 36 |
|
| 37 |
void fcml_fn_prefixes_66(void) { |
| 38 |
|
| 39 |
/* Prefixes aren't needed.*/ |
| 40 |
|
| 41 |
fcml_st_assembler_context context = {0}; |
| 42 |
context.assembler = assembler_intel; |
| 43 |
context.entry_point.op_mode = FCML_OM_16_BIT; |
| 44 |
context.entry_point.ip = 0x00401000; |
| 45 |
|
| 46 |
fcml_st_instruction instruction = {0}; |
| 47 |
instruction.mnemonic = "adc"; |
| 48 |
instruction.operands[0] = FCML_REG( fcml_reg_BP ); |
| 49 |
instruction.operands[1] = FCML_IMM16( 0x0201 ); |
| 50 |
|
| 51 |
fcml_st_assembler_result result; |
| 52 |
|
| 53 |
fcml_fn_assembler_result_prepare( &result ); |
| 54 |
|
| 55 |
if( !fcml_fn_assemble( &context, &instruction, &result ) ) { |
| 56 |
STF_ASSERT_PTR_NOT_NULL( result.chosen_instruction ); |
| 57 |
if( result.chosen_instruction ) { |
| 58 |
STF_ASSERT_EQUAL( 4, result.chosen_instruction->code_length ); |
| 59 |
if( result.chosen_instruction->code_length == 4 ) { |
| 60 |
STF_ASSERT_EQUAL( 0x81, result.chosen_instruction->code[0] ); |
| 61 |
STF_ASSERT_EQUAL( 0xD5, result.chosen_instruction->code[1] ); |
| 62 |
STF_ASSERT_EQUAL( 0x01, result.chosen_instruction->code[2] ); |
| 63 |
STF_ASSERT_EQUAL( 0x02, result.chosen_instruction->code[3] ); |
| 64 |
} |
| 65 |
} |
| 66 |
fcml_fn_assembler_result_free( &result ); |
| 67 |
} else { |
| 68 |
STF_FAIL("Can not assemble instruction."); |
| 69 |
} |
| 70 |
|
| 71 |
/* Prefixes 0x66 is needed to change default operand size attribute back to 16 bits.*/ |
| 72 |
|
| 73 |
context.entry_point.operand_size_attribute = FCML_DS_32; |
| 74 |
context.entry_point.op_mode = FCML_OM_16_BIT; |
| 75 |
context.entry_point.ip = 0x00401000; |
| 76 |
|
| 77 |
instruction.mnemonic = "adc"; |
| 78 |
instruction.operands[0] = FCML_REG( fcml_reg_BP ); |
| 79 |
instruction.operands[1] = FCML_IMM16( 0x0201 ); |
| 80 |
|
| 81 |
if( !fcml_fn_assemble( &context, &instruction, &result ) ) { |
| 82 |
STF_ASSERT_PTR_NOT_NULL( result.chosen_instruction ); |
| 83 |
if( result.chosen_instruction ) { |
| 84 |
STF_ASSERT_EQUAL( 5, result.chosen_instruction->code_length ); |
| 85 |
if( result.chosen_instruction->code_length == 5 ) { |
| 86 |
STF_ASSERT_EQUAL( 0x66, result.chosen_instruction->code[0] ); |
| 87 |
STF_ASSERT_EQUAL( 0x81, result.chosen_instruction->code[1] ); |
| 88 |
STF_ASSERT_EQUAL( 0xD5, result.chosen_instruction->code[2] ); |
| 89 |
STF_ASSERT_EQUAL( 0x01, result.chosen_instruction->code[3] ); |
| 90 |
STF_ASSERT_EQUAL( 0x02, result.chosen_instruction->code[4] ); |
| 91 |
} |
| 92 |
} |
| 93 |
fcml_fn_assembler_result_free( &result ); |
| 94 |
} else { |
| 95 |
STF_FAIL("Can not assemble instruction."); |
| 96 |
} |
| 97 |
|
| 98 |
/* Prefixes 0x66 is needed to change default operand size attribute to 32 bits.*/ |
| 99 |
|
| 100 |
context.entry_point.operand_size_attribute = FCML_DS_16; |
| 101 |
context.entry_point.op_mode = FCML_OM_16_BIT; |
| 102 |
context.entry_point.ip = 0x00401000; |
| 103 |
|
| 104 |
instruction.mnemonic = "adc"; |
| 105 |
instruction.operands[0] = FCML_REG( fcml_reg_EBP ); |
| 106 |
instruction.operands[1] = FCML_IMM32( 0x02010000 ); |
| 107 |
|
| 108 |
if( !fcml_fn_assemble( &context, &instruction, &result ) ) { |
| 109 |
STF_ASSERT_PTR_NOT_NULL( result.chosen_instruction ); |
| 110 |
if( result.chosen_instruction ) { |
| 111 |
STF_ASSERT_EQUAL( 7, result.chosen_instruction->code_length ); |
| 112 |
if( result.chosen_instruction->code_length == 5 ) { |
| 113 |
STF_ASSERT_EQUAL( 0x66, result.chosen_instruction->code[0] ); |
| 114 |
STF_ASSERT_EQUAL( 0x81, result.chosen_instruction->code[1] ); |
| 115 |
STF_ASSERT_EQUAL( 0xD5, result.chosen_instruction->code[2] ); |
| 116 |
STF_ASSERT_EQUAL( 0x00, result.chosen_instruction->code[3] ); |
| 117 |
STF_ASSERT_EQUAL( 0x00, result.chosen_instruction->code[4] ); |
| 118 |
STF_ASSERT_EQUAL( 0x01, result.chosen_instruction->code[3] ); |
| 119 |
STF_ASSERT_EQUAL( 0x02, result.chosen_instruction->code[4] ); |
| 120 |
} |
| 121 |
} |
| 122 |
fcml_fn_assembler_result_free( &result ); |
| 123 |
} else { |
| 124 |
STF_FAIL("Can not assemble instruction."); |
| 125 |
} |
| 126 |
|
| 127 |
/* Prefixes 0x66 is needed to change default operand size attribute back to 32 bits.*/ |
| 128 |
|
| 129 |
context.entry_point.operand_size_attribute = FCML_DS_16; |
| 130 |
context.entry_point.op_mode = FCML_OM_32_BIT; |
| 131 |
context.entry_point.ip = 0x00401000; |
| 132 |
|
| 133 |
instruction.mnemonic = "adc"; |
| 134 |
instruction.operands[0] = FCML_REG( fcml_reg_EBP ); |
| 135 |
instruction.operands[1] = FCML_IMM32( 0x02010000 ); |
| 136 |
|
| 137 |
if( !fcml_fn_assemble( &context, &instruction, &result ) ) { |
| 138 |
STF_ASSERT_PTR_NOT_NULL( result.chosen_instruction ); |
| 139 |
if( result.chosen_instruction ) { |
| 140 |
STF_ASSERT_EQUAL( 7, result.chosen_instruction->code_length ); |
| 141 |
if( result.chosen_instruction->code_length == 5 ) { |
| 142 |
STF_ASSERT_EQUAL( 0x66, result.chosen_instruction->code[0] ); |
| 143 |
STF_ASSERT_EQUAL( 0x81, result.chosen_instruction->code[1] ); |
| 144 |
STF_ASSERT_EQUAL( 0xD5, result.chosen_instruction->code[2] ); |
| 145 |
STF_ASSERT_EQUAL( 0x00, result.chosen_instruction->code[3] ); |
| 146 |
STF_ASSERT_EQUAL( 0x00, result.chosen_instruction->code[4] ); |
| 147 |
STF_ASSERT_EQUAL( 0x01, result.chosen_instruction->code[3] ); |
| 148 |
STF_ASSERT_EQUAL( 0x02, result.chosen_instruction->code[4] ); |
| 149 |
} |
| 150 |
} |
| 151 |
fcml_fn_assembler_result_free( &result ); |
| 152 |
} else { |
| 153 |
STF_FAIL("Can not assemble instruction."); |
| 154 |
} |
| 155 |
|
| 156 |
} |
| 157 |
|
| 158 |
fcml_stf_test_case fcml_ti_prefixes[] = { |
| 159 |
{ "fcml_fn_prefixes_66", fcml_fn_prefixes_66 }, |
| 160 |
FCML_STF_NULL_TEST |
| 161 |
}; |
| 162 |
|
| 163 |
fcml_stf_test_suite fcml_si_prefixes = { |
| 164 |
"suite-fcml-prefixes", fcml_tf_prefixes_suite_init, fcml_tf_prefixes_suite_cleanup, fcml_ti_prefixes |
| 165 |
}; |