From 805d58c6aeeeba3e4989c0eed6781b3861e8fae0 Mon Sep 17 00:00:00 2001 From: "Kartik K. Agaram" Date: Thu, 25 Jan 2018 22:39:31 -0800 Subject: 4199 --- html/subx/010core.cc.html | 179 +++++++++++++++++++++++++--------------------- 1 file changed, 96 insertions(+), 83 deletions(-) (limited to 'html/subx/010core.cc.html') diff --git a/html/subx/010core.cc.html b/html/subx/010core.cc.html index f1dbbeeb..4488688c 100644 --- a/html/subx/010core.cc.html +++ b/html/subx/010core.cc.html @@ -15,18 +15,17 @@ body { font-size: 12pt; font-family: monospace; color: #aaaaaa; background-color a { color:#eeeeee; text-decoration: none; } a:hover { text-decoration: underline; } * { font-size: 12pt; font-size: 1em; } -.Conceal { color: #4e4e4e; } -.Constant { color: #00a0a0; } -.LineNr { color: #444444; } -.Identifier { color: #c0a020; } +.cSpecial { color: #008000; } +.traceContains { color: #008000; } .PreProc { color: #800080; } +.LineNr { color: #444444; } +.Constant { color: #00a0a0; } .Delimiter { color: #800080; } +.Identifier { color: #c0a020; } .Normal { color: #aaaaaa; background-color: #080808; padding-bottom: 1px; } .Comment { color: #9090ff; } .Comment a { color:#0000ee; text-decoration:underline; } .SalientComment { color: #00ffff; } -.traceContains { color: #008000; } -.cSpecial { color: #008000; } --> @@ -107,7 +106,7 @@ if ('onhashchange' in window) { 44 /* arg1 and arg2 must be signed */ \ 45 int64_t tmp = arg1 op arg2; \ 46 arg1 = arg1 op arg2; \ - 47 trace(2, "run") << "storing 0x" << HEXWORD << arg1 << end(); \ + 47 trace(2, "run") << "storing 0x" << HEXWORD << arg1 << end(); \ 48 SF = (arg1 < 0); \ 49 ZF = (arg1 == 0); \ 50 OF = (arg1 != tmp); \ @@ -118,7 +117,7 @@ if ('onhashchange' in window) { 55 #define BINARY_BITWISE_OP(op, arg1, arg2) { \ 56 /* arg1 and arg2 must be unsigned */ \ 57 arg1 = arg1 op arg2; \ - 58 trace(2, "run") << "storing 0x" << HEXWORD << arg1 << end(); \ + 58 trace(2, "run") << "storing 0x" << HEXWORD << arg1 << end(); \ 59 SF = (arg1 >> 31); \ 60 ZF = (arg1 == 0); \ 61 OF = false; \ @@ -148,7 +147,7 @@ if ('onhashchange' in window) { 85 # opcode ModR/M SIB displacement immediate 86 # instruction mod, reg, Reg/Mem bits scale, index, base 87 # 1-3 bytes 0/1 byte 0/1 byte 0/1/2/4 bytes 0/1/2/4 bytes - 88 ¦ 05 0a 0b 0c 0d # add 0x0d0c0b0a to EAX + 88 05 0a 0b 0c 0d # add 0x0d0c0b0a to EAX 89 # All hex bytes must be exactly 2 characters each. No '0x' prefixes. 90 +load: 1 -> 05 91 +load: 2 -> 0a @@ -165,52 +164,52 @@ if ('onhashchange' in window) { 102 load_program(text_bytes); 103 EIP = 1; // preserve null pointer 104 while (EIP < End_of_program) -105 ¦ run_one_instruction(); +105 run_one_instruction(); 106 } 107 108 // skeleton of how x86 instructions are decoded 109 void run_one_instruction() { 110 uint8_t op=0, op2=0, op3=0; -111 trace(2, "run") << "inst: 0x" << HEXWORD << EIP << end(); +111 trace(2, "run") << "inst: 0x" << HEXWORD << EIP << end(); 112 switch (op = next()) { 113 case 0xf4: // hlt -114 ¦ EIP = End_of_program; -115 ¦ break; +114 EIP = End_of_program; +115 break; 116 // our first opcode 117 case 0x05: { // add imm32 to EAX -118 ¦ int32_t arg2 = imm32(); -119 ¦ trace(2, "run") << "add imm32 0x" << HEXWORD << arg2 << " to reg EAX" << end(); -120 ¦ BINARY_ARITHMETIC_OP(+, Reg[EAX].i, arg2); -121 ¦ break; +118 int32_t arg2 = imm32(); +119 trace(2, "run") << "add imm32 0x" << HEXWORD << arg2 << " to reg EAX" << end(); +120 BINARY_ARITHMETIC_OP(+, Reg[EAX].i, arg2); +121 break; 122 } 123 // End Single-Byte Opcodes 124 case 0x0f: -125 ¦ switch(op2 = next()) { -126 ¦ // End Two-Byte Opcodes Starting With 0f -127 ¦ default: -128 ¦ ¦ cerr << "unrecognized second opcode after 0f: " << HEXBYTE << NUM(op2) << '\n'; -129 ¦ ¦ exit(1); -130 ¦ } -131 ¦ break; +125 switch(op2 = next()) { +126 // End Two-Byte Opcodes Starting With 0f +127 default: +128 cerr << "unrecognized second opcode after 0f: " << HEXBYTE << NUM(op2) << '\n'; +129 exit(1); +130 } +131 break; 132 case 0xf3: -133 ¦ switch(op2 = next()) { -134 ¦ // End Two-Byte Opcodes Starting With f3 -135 ¦ case 0x0f: -136 ¦ ¦ switch(op3 = next()) { -137 ¦ ¦ // End Three-Byte Opcodes Starting With f3 0f -138 ¦ ¦ default: -139 ¦ ¦ ¦ cerr << "unrecognized third opcode after f3 0f: " << HEXBYTE << NUM(op3) << '\n'; -140 ¦ ¦ ¦ exit(1); -141 ¦ ¦ } -142 ¦ ¦ break; -143 ¦ default: -144 ¦ ¦ cerr << "unrecognized second opcode after f3: " << HEXBYTE << NUM(op2) << '\n'; -145 ¦ ¦ exit(1); -146 ¦ } -147 ¦ break; +133 switch(op2 = next()) { +134 // End Two-Byte Opcodes Starting With f3 +135 case 0x0f: +136 switch(op3 = next()) { +137 // End Three-Byte Opcodes Starting With f3 0f +138 default: +139 cerr << "unrecognized third opcode after f3 0f: " << HEXBYTE << NUM(op3) << '\n'; +140 exit(1); +141 } +142 break; +143 default: +144 cerr << "unrecognized second opcode after f3: " << HEXBYTE << NUM(op2) << '\n'; +145 exit(1); +146 } +147 break; 148 default: -149 ¦ cerr << "unrecognized opcode: " << HEXBYTE << NUM(op) << '\n'; -150 ¦ exit(1); +149 cerr << "unrecognized opcode: " << HEXBYTE << NUM(op) << '\n'; +150 exit(1); 151 } 152 } 153 @@ -219,47 +218,47 @@ if ('onhashchange' in window) { 156 istringstream in(text_bytes); 157 in >> std::noskipws; 158 while (has_data(in)) { -159 ¦ char c1 = next_hex_byte(in); -160 ¦ if (c1 == '\0') break; -161 ¦ if (!has_data(in)) { -162 ¦ ¦ raise << "input program truncated mid-byte\n" << end(); -163 ¦ ¦ return; -164 ¦ } -165 ¦ char c2 = next_hex_byte(in); -166 ¦ if (c2 == '\0') { -167 ¦ ¦ raise << "input program truncated mid-byte\n" << end(); -168 ¦ ¦ return; -169 ¦ } -170 ¦ Mem.at(addr) = to_byte(c1, c2); -171 ¦ trace(99, "load") << addr << " -> " << HEXBYTE << NUM(Mem.at(addr)) << end(); -172 ¦ addr++; +159 char c1 = next_hex_byte(in); +160 if (c1 == '\0') break; +161 if (!has_data(in)) { +162 raise << "input program truncated mid-byte\n" << end(); +163 return; +164 } +165 char c2 = next_hex_byte(in); +166 if (c2 == '\0') { +167 raise << "input program truncated mid-byte\n" << end(); +168 return; +169 } +170 Mem.at(addr) = to_byte(c1, c2); +171 trace(99, "load") << addr << " -> " << HEXBYTE << NUM(Mem.at(addr)) << end(); +172 addr++; 173 } 174 End_of_program = addr; 175 } 176 177 char next_hex_byte(istream& in) { 178 while (has_data(in)) { -179 ¦ char c = '\0'; -180 ¦ in >> c; -181 ¦ if (c == ' ' || c == '\n') continue; -182 ¦ while (c == '#') { -183 ¦ ¦ while (has_data(in)) { -184 ¦ ¦ ¦ in >> c; -185 ¦ ¦ ¦ if (c == '\n') { -186 ¦ ¦ ¦ ¦ in >> c; -187 ¦ ¦ ¦ ¦ break; -188 ¦ ¦ ¦ } -189 ¦ ¦ } -190 ¦ } -191 ¦ if (c == '\0') return c; -192 ¦ if (c >= '0' && c <= '9') return c; -193 ¦ if (c >= 'a' && c <= 'f') return c; -194 ¦ if (c >= 'A' && c <= 'F') return tolower(c); -195 ¦ // disallow any non-hex characters, including a '0x' prefix -196 ¦ if (!isspace(c)) { -197 ¦ ¦ raise << "invalid non-hex character " << NUM(c) << "\n" << end(); -198 ¦ ¦ break; -199 ¦ } +179 char c = '\0'; +180 in >> c; +181 if (c == ' ' || c == '\n') continue; +182 while (c == '#') { +183 while (has_data(in)) { +184 in >> c; +185 if (c == '\n') { +186 in >> c; +187 break; +188 } +189 } +190 } +191 if (c == '\0') return c; +192 if (c >= '0' && c <= '9') return c; +193 if (c >= 'a' && c <= 'f') return c; +194 if (c >= 'A' && c <= 'F') return tolower(c); +195 // disallow any non-hex characters, including a '0x' prefix +196 if (!isspace(c)) { +197 raise << "invalid non-hex character " << NUM(c) << "\n" << end(); +198 break; +199 } 200 } 201 return '\0'; 202 } @@ -287,13 +286,27 @@ if ('onhashchange' in window) { 224 return result; 225 } 226 -227 :(before "End Includes") -228 #include <iomanip> -229 #define HEXBYTE std::hex << std::setw(2) << std::setfill('0') -230 #define HEXWORD std::hex << std::setw(8) << std::setfill('0') -231 // ugly that iostream doesn't print uint8_t as an integer -232 #define NUM(X) static_cast<int>(X) -233 #include <stdint.h> +227 string rname(uint8_t r) { +228 switch (r) { +229 case 0: return "EAX"; +230 case 1: return "ECX"; +231 case 2: return "EDX"; +232 case 3: return "EBX"; +233 case 4: return "ESP"; +234 case 5: return "EBP"; +235 case 6: return "ESI"; +236 case 7: return "EDI"; +237 default: raise << "invalid register " << r << '\n' << end(); return ""; +238 } +239 } +240 +241 :(before "End Includes") +242 #include <iomanip> +243 #define HEXBYTE std::hex << std::setw(2) << std::setfill('0') +244 #define HEXWORD std::hex << std::setw(8) << std::setfill('0') +245 // ugly that iostream doesn't print uint8_t as an integer +246 #define NUM(X) static_cast<int>(X) +247 #include <stdint.h> -- cgit 1.4.1-2-gfad0