diff options
author | Kartik K. Agaram <vc@akkartik.com> | 2021-03-03 22:09:50 -0800 |
---|---|---|
committer | Kartik K. Agaram <vc@akkartik.com> | 2021-03-03 22:21:03 -0800 |
commit | 71e4f3812982dba2efb471283d310224e8db363e (patch) | |
tree | ea111a1acb8b8845dbda39c0e1b4bac1d198143b /linux/bootstrap/017jump_disp8.cc | |
parent | c6b928be29ac8cdb4e4d6e1eaa20420ff03e5a4c (diff) | |
download | mu-71e4f3812982dba2efb471283d310224e8db363e.tar.gz |
7842 - new directory organization
Baremetal is now the default build target and therefore has its sources at the top-level. Baremetal programs build using the phase-2 Mu toolchain that requires a Linux kernel. This phase-2 codebase which used to be at the top-level is now under the linux/ directory. Finally, the phase-2 toolchain, while self-hosting, has a way to bootstrap from a C implementation, which is now stored in linux/bootstrap. The bootstrap C implementation uses some literate programming tools that are now in linux/bootstrap/tools. So the whole thing has gotten inverted. Each directory should build one artifact and include the main sources (along with standard library). Tools used for building it are relegated to sub-directories, even though those tools are often useful in their own right, and have had lots of interesting programs written using them. A couple of things have gotten dropped in this process: - I had old ways to run on just a Linux kernel, or with a Soso kernel. No more. - I had some old tooling for running a single test at the cursor. I haven't used that lately. Maybe I'll bring it back one day. The reorg isn't done yet. Still to do: - redo documentation everywhere. All the README files, all other markdown, particularly vocabulary.md. - clean up how-to-run comments at the start of programs everywhere - rethink what to do with the html/ directory. Do we even want to keep supporting it? In spite of these shortcomings, all the scripts at the top-level, linux/ and linux/bootstrap are working. The names of the scripts also feel reasonable. This is a good milestone to take stock at.
Diffstat (limited to 'linux/bootstrap/017jump_disp8.cc')
-rw-r--r-- | linux/bootstrap/017jump_disp8.cc | 407 |
1 files changed, 407 insertions, 0 deletions
diff --git a/linux/bootstrap/017jump_disp8.cc b/linux/bootstrap/017jump_disp8.cc new file mode 100644 index 00000000..30e60a74 --- /dev/null +++ b/linux/bootstrap/017jump_disp8.cc @@ -0,0 +1,407 @@ +//: jump to 8-bit offset + +//:: jump + +:(before "End Initialize Op Names") +put_new(Name, "eb", "jump disp8 bytes away (jmp)"); + +:(code) +void test_jump_disp8() { + run( + "== code 0x1\n" + // op ModR/M SIB displacement immediate + " eb 05 \n" // skip 1 instruction + " 05 00 00 00 01 \n" + " 05 00 00 00 02 \n" + ); + CHECK_TRACE_CONTENTS( + "run: 0x00000001 opcode: eb\n" + "run: jump 5\n" + "run: 0x00000008 opcode: 05\n" + ); + CHECK_TRACE_DOESNT_CONTAIN("run: 0x00000003 opcode: 05"); +} + +:(before "End Single-Byte Opcodes") +case 0xeb: { // jump disp8 + int8_t offset = static_cast<int>(next()); + trace(Callstack_depth+1, "run") << "jump " << NUM(offset) << end(); + EIP += offset; + break; +} + +//:: jump if equal/zero + +:(before "End Initialize Op Names") +put_new(Name, "74", "jump disp8 bytes away if equal, if ZF is set (jcc/jz/je)"); + +:(code) +void test_je_disp8_success() { + ZF = true; + run( + "== code 0x1\n" + // op ModR/M SIB displacement immediate + " 74 05 \n" // skip 1 instruction + " 05 00 00 00 01 \n" + " 05 00 00 00 02 \n" + ); + CHECK_TRACE_CONTENTS( + "run: 0x00000001 opcode: 74\n" + "run: jump 5\n" + "run: 0x00000008 opcode: 05\n" + ); + CHECK_TRACE_DOESNT_CONTAIN("run: 0x00000003 opcode: 05"); +} + +:(before "End Single-Byte Opcodes") +case 0x74: { // jump disp8 if ZF + const int8_t offset = static_cast<int>(next()); + if (ZF) { + trace(Callstack_depth+1, "run") << "jump " << NUM(offset) << end(); + EIP += offset; + } + break; +} + +:(code) +void test_je_disp8_fail() { + ZF = false; + run( + "== code 0x1\n" + // op ModR/M SIB displacement immediate + " 74 05 \n" // skip 1 instruction + " 05 00 00 00 01 \n" + " 05 00 00 00 02 \n" + ); + CHECK_TRACE_CONTENTS( + "run: 0x00000001 opcode: 74\n" + "run: 0x00000003 opcode: 05\n" + "run: 0x00000008 opcode: 05\n" + ); + CHECK_TRACE_DOESNT_CONTAIN("run: jump 5"); +} + +//:: jump if not equal/not zero + +:(before "End Initialize Op Names") +put_new(Name, "75", "jump disp8 bytes away if not equal, if ZF is not set (jcc/jnz/jne)"); + +:(code) +void test_jne_disp8_success() { + ZF = false; + run( + "== code 0x1\n" + // op ModR/M SIB displacement immediate + " 75 05 \n" // skip 1 instruction + " 05 00 00 00 01 \n" + " 05 00 00 00 02 \n" + ); + CHECK_TRACE_CONTENTS( + "run: 0x00000001 opcode: 75\n" + "run: jump 5\n" + "run: 0x00000008 opcode: 05\n" + ); + CHECK_TRACE_DOESNT_CONTAIN("run: 0x00000003 opcode: 05"); +} + +:(before "End Single-Byte Opcodes") +case 0x75: { // jump disp8 if !ZF + const int8_t offset = static_cast<int>(next()); + if (!ZF) { + trace(Callstack_depth+1, "run") << "jump " << NUM(offset) << end(); + EIP += offset; + } + break; +} + +:(code) +void test_jne_disp8_fail() { + ZF = true; + run( + "== code 0x1\n" + // op ModR/M SIB displacement immediate + " 75 05 \n" // skip 1 instruction + " 05 00 00 00 01 \n" + " 05 00 00 00 02 \n" + ); + CHECK_TRACE_CONTENTS( + "run: 0x00000001 opcode: 75\n" + "run: 0x00000003 opcode: 05\n" + "run: 0x00000008 opcode: 05\n" + ); + CHECK_TRACE_DOESNT_CONTAIN("run: jump 5"); +} + +//:: jump if greater + +:(before "End Initialize Op Names") +put_new(Name, "7f", "jump disp8 bytes away if greater, if ZF is unset and SF == OF (jcc/jg/jnle)"); +put_new(Name, "77", "jump disp8 bytes away if greater (addr, float), if ZF is unset and CF is unset (jcc/ja/jnbe)"); + +:(code) +void test_jg_disp8_success() { + ZF = false; + SF = false; + OF = false; + run( + "== code 0x1\n" + // op ModR/M SIB displacement immediate + " 7f 05 \n" // skip 1 instruction + " 05 00 00 00 01 \n" + " 05 00 00 00 02 \n" + ); + CHECK_TRACE_CONTENTS( + "run: 0x00000001 opcode: 7f\n" + "run: jump 5\n" + "run: 0x00000008 opcode: 05\n" + ); + CHECK_TRACE_DOESNT_CONTAIN("run: 0x00000003 opcode: 05"); +} + +:(before "End Single-Byte Opcodes") +case 0x7f: { // jump disp8 if SF == OF and !ZF + const int8_t offset = static_cast<int>(next()); + if (SF == OF && !ZF) { + trace(Callstack_depth+1, "run") << "jump " << NUM(offset) << end(); + EIP += offset; + } + break; +} +case 0x77: { // jump disp8 if !CF and !ZF + const int8_t offset = static_cast<int>(next()); + if (!CF && !ZF) { + trace(Callstack_depth+1, "run") << "jump " << NUM(offset) << end(); + EIP += offset; + } + break; +} + +:(code) +void test_jg_disp8_fail() { + ZF = false; + SF = true; + OF = false; + run( + "== code 0x1\n" + // op ModR/M SIB displacement immediate + " 7f 05 \n" // skip 1 instruction + " 05 00 00 00 01 \n" + " 05 00 00 00 02 \n" + ); + CHECK_TRACE_CONTENTS( + "run: 0x00000001 opcode: 7f\n" + "run: 0x00000003 opcode: 05\n" + "run: 0x00000008 opcode: 05\n" + ); + CHECK_TRACE_DOESNT_CONTAIN("run: jump 5"); +} + +//:: jump if greater or equal + +:(before "End Initialize Op Names") +put_new(Name, "7d", "jump disp8 bytes away if greater or equal, if SF == OF (jcc/jge/jnl)"); +put_new(Name, "73", "jump disp8 bytes away if greater or equal (addr, float), if CF is unset (jcc/jae/jnb)"); + +:(code) +void test_jge_disp8_success() { + SF = false; + OF = false; + run( + "== code 0x1\n" + // op ModR/M SIB displacement immediate + " 7d 05 \n" // skip 1 instruction + " 05 00 00 00 01 \n" + " 05 00 00 00 02 \n" + ); + CHECK_TRACE_CONTENTS( + "run: 0x00000001 opcode: 7d\n" + "run: jump 5\n" + "run: 0x00000008 opcode: 05\n" + ); + CHECK_TRACE_DOESNT_CONTAIN("run: 0x00000003 opcode: 05"); +} + +:(before "End Single-Byte Opcodes") +case 0x7d: { // jump disp8 if SF == OF + const int8_t offset = static_cast<int>(next()); + if (SF == OF) { + trace(Callstack_depth+1, "run") << "jump " << NUM(offset) << end(); + EIP += offset; + } + break; +} +case 0x73: { // jump disp8 if !CF + const int8_t offset = static_cast<int>(next()); + if (!CF) { + trace(Callstack_depth+1, "run") << "jump " << NUM(offset) << end(); + EIP += offset; + } + break; +} + +:(code) +void test_jge_disp8_fail() { + SF = true; + OF = false; + run( + "== code 0x1\n" + // op ModR/M SIB displacement immediate + " 7d 05 \n" // skip 1 instruction + " 05 00 00 00 01 \n" + " 05 00 00 00 02 \n" + ); + CHECK_TRACE_CONTENTS( + "run: 0x00000001 opcode: 7d\n" + "run: 0x00000003 opcode: 05\n" + "run: 0x00000008 opcode: 05\n" + ); + CHECK_TRACE_DOESNT_CONTAIN("run: jump 5"); +} + +//:: jump if lesser + +:(before "End Initialize Op Names") +put_new(Name, "7c", "jump disp8 bytes away if lesser, if SF != OF (jcc/jl/jnge)"); +put_new(Name, "72", "jump disp8 bytes away if lesser (addr, float), if CF is set (jcc/jb/jnae)"); + +:(code) +void test_jl_disp8_success() { + ZF = false; + SF = true; + OF = false; + run( + "== code 0x1\n" + // op ModR/M SIB displacement immediate + " 7c 05 \n" // skip 1 instruction + " 05 00 00 00 01 \n" + " 05 00 00 00 02 \n" + ); + CHECK_TRACE_CONTENTS( + "run: 0x00000001 opcode: 7c\n" + "run: jump 5\n" + "run: 0x00000008 opcode: 05\n" + ); + CHECK_TRACE_DOESNT_CONTAIN("run: 0x00000003 opcode: 05"); +} + +:(before "End Single-Byte Opcodes") +case 0x7c: { // jump disp8 if SF != OF + const int8_t offset = static_cast<int>(next()); + if (SF != OF) { + trace(Callstack_depth+1, "run") << "jump " << NUM(offset) << end(); + EIP += offset; + } + break; +} +case 0x72: { // jump disp8 if CF + const int8_t offset = static_cast<int>(next()); + if (CF) { + trace(Callstack_depth+1, "run") << "jump " << NUM(offset) << end(); + EIP += offset; + } + break; +} + +:(code) +void test_jl_disp8_fail() { + ZF = false; + SF = false; + OF = false; + run( + "== code 0x1\n" + // op ModR/M SIB displacement immediate + " 7c 05 \n" // skip 1 instruction + " 05 00 00 00 01 \n" + " 05 00 00 00 02 \n" + ); + CHECK_TRACE_CONTENTS( + "run: 0x00000001 opcode: 7c\n" + "run: 0x00000003 opcode: 05\n" + "run: 0x00000008 opcode: 05\n" + ); + CHECK_TRACE_DOESNT_CONTAIN("run: jump 5"); +} + +//:: jump if lesser or equal + +:(before "End Initialize Op Names") +put_new(Name, "7e", "jump disp8 bytes away if lesser or equal, if ZF is set or SF != OF (jcc/jle/jng)"); +put_new(Name, "76", "jump disp8 bytes away if lesser or equal (addr, float), if ZF is set or CF is set (jcc/jbe/jna)"); + +:(code) +void test_jle_disp8_equal() { + ZF = true; + SF = false; + OF = false; + run( + "== code 0x1\n" + // op ModR/M SIB displacement immediate + " 7e 05 \n" // skip 1 instruction + " 05 00 00 00 01 \n" + " 05 00 00 00 02 \n" + ); + CHECK_TRACE_CONTENTS( + "run: 0x00000001 opcode: 7e\n" + "run: jump 5\n" + "run: 0x00000008 opcode: 05\n" + ); + CHECK_TRACE_DOESNT_CONTAIN("run: 0x00000003 opcode: 05"); +} + +:(code) +void test_jle_disp8_lesser() { + ZF = false; + SF = true; + OF = false; + run( + "== code 0x1\n" + // op ModR/M SIB displacement immediate + " 7e 05 \n" // skip 1 instruction + " 05 00 00 00 01 \n" + " 05 00 00 00 02 \n" + ); + CHECK_TRACE_CONTENTS( + "run: 0x00000001 opcode: 7e\n" + "run: jump 5\n" + "run: 0x00000008 opcode: 05\n" + ); + CHECK_TRACE_DOESNT_CONTAIN("run: 0x00000003 opcode: 05"); +} + +:(before "End Single-Byte Opcodes") +case 0x7e: { // jump disp8 if ZF or SF != OF + const int8_t offset = static_cast<int>(next()); + if (ZF || SF != OF) { + trace(Callstack_depth+1, "run") << "jump " << NUM(offset) << end(); + EIP += offset; + } + break; +} +case 0x76: { // jump disp8 if ZF or CF + const int8_t offset = static_cast<int>(next()); + if (ZF || CF) { + trace(Callstack_depth+1, "run") << "jump " << NUM(offset) << end(); + EIP += offset; + } + break; +} + +:(code) +void test_jle_disp8_greater() { + ZF = false; + SF = false; + OF = false; + run( + "== code 0x1\n" + // op ModR/M SIB displacement immediate + " 7e 05 \n" // skip 1 instruction + " 05 00 00 00 01 \n" + " 05 00 00 00 02 \n" + ); + CHECK_TRACE_CONTENTS( + "run: 0x00000001 opcode: 7e\n" + "run: 0x00000003 opcode: 05\n" + "run: 0x00000008 opcode: 05\n" + ); + CHECK_TRACE_DOESNT_CONTAIN("run: jump 5"); +} |