Debian Codegraph @

check/public-tests/segment_reg_t.c

raw
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 "segment_reg_t.h"
21
22 #include <string.h>
23
24 #include <fcml_assembler.h>
25 #include <fcml_common.h>
26 #include <fcml_common_utils.h>
27
28 #include "instructions_base_t.h"
29
30 fcml_bool fcml_tf_prefixes_segment_reg_suite_init(void) {
31 return FCML_TRUE;
32 }
33
34 fcml_bool fcml_tf_prefixes_segment_reg_suite_cleanup(void) {
35 return FCML_TRUE;
36 }
37
38 void fcml_fn_segment_reg(void) {
39
40 fcml_st_assembler_context context = {0};
41 context.assembler = assembler_intel;
42 context.entry_point.op_mode = FCML_OM_32_BIT;
43 context.entry_point.ip = 0x00401000;
44 context.configuration.enable_error_messages = FCML_TRUE;
45
46 fcml_st_instruction instruction = {0};
47 instruction.mnemonic = "cmps";
48 instruction.operands[0].type = FCML_OT_ADDRESS;
49 instruction.operands[0].address.segment_selector.segment_selector = fcml_reg_DS;
50 instruction.operands[0].address.address_form = FCML_AF_COMBINED;
51 instruction.operands[0].address.size_operator = FCML_DS_32;
52 instruction.operands[0].address.effective_address.base = fcml_reg_ESI;
53 instruction.operands[1].type = FCML_OT_ADDRESS;
54 instruction.operands[1].address.segment_selector.segment_selector = fcml_reg_ES;
55 instruction.operands[1].address.address_form = FCML_AF_COMBINED;
56 instruction.operands[1].address.size_operator = FCML_DS_32;
57 instruction.operands[1].address.effective_address.base = fcml_reg_EDI;
58
59 fcml_st_assembler_result result;
60
61 fcml_fn_assembler_result_prepare( &result );
62
63 if( !fcml_fn_assemble( &context, &instruction, &result ) ) {
64 STF_ASSERT_PTR_NOT_NULL( result.chosen_instruction );
65 if( result.chosen_instruction ) {
66 STF_ASSERT_EQUAL( 1, result.chosen_instruction->code_length );
67 if( result.chosen_instruction->code_length == 4 ) {
68 STF_ASSERT_EQUAL( 0xA7, result.chosen_instruction->code[0] );
69 }
70 }
71 } else {
72 STF_FAIL("Can not assemble instruction.");
73 }
74
75 fcml_fn_assembler_result_free( &result );
76
77 /* Now try to override ES.*/
78
79 instruction.operands[1].address.segment_selector.segment_selector = fcml_reg_CS;
80
81 if( fcml_fn_assemble( &context, &instruction, &result ) ) {
82
83 STF_ASSERT_PTR_NOT_NULL( result.errors.last_error );
84 if( result.errors.last_error ) {
85 STF_ASSERT_STRING_EQUAL( "Segment register can not be overridden.", result.errors.last_error->message );
86 }
87
88 } else {
89 STF_FAIL("Instruction should fail.");
90 }
91
92 fcml_fn_assembler_result_free( &result );
93
94 }
95
96 fcml_stf_test_case fcml_ti_segment_reg[] = {
97 { "fcml_fn_segment_reg", fcml_fn_segment_reg },
98 FCML_STF_NULL_TEST
99 };
100
101 fcml_stf_test_suite fcml_si_segment_reg = {
102 "suite-fcml-segment_reg", fcml_tf_prefixes_segment_reg_suite_init, fcml_tf_prefixes_segment_reg_suite_cleanup, fcml_ti_segment_reg
103 };
104