summaryrefslogtreecommitdiffhomepage
path: root/asm/intel64/codes.cpp
diff options
context:
space:
mode:
authorRoland Reichwein <mail@reichwein.it>2020-11-09 09:50:58 +0100
committerRoland Reichwein <mail@reichwein.it>2020-11-09 09:50:58 +0100
commit1ac8ab06e9aad3b6d22685255459d71cb49e1f28 (patch)
tree95e4ca7de492180aef9d459ee40663b1bf134b66 /asm/intel64/codes.cpp
parentdb0654fa48ddc07e6bcaaaeddfa301a32806dadc (diff)
First program: Can add 2 integers and return result via exit code
Diffstat (limited to 'asm/intel64/codes.cpp')
-rw-r--r--asm/intel64/codes.cpp46
1 files changed, 33 insertions, 13 deletions
diff --git a/asm/intel64/codes.cpp b/asm/intel64/codes.cpp
index 66a08dd..5d93a57 100644
--- a/asm/intel64/codes.cpp
+++ b/asm/intel64/codes.cpp
@@ -29,14 +29,14 @@ namespace {
{"dl", 2}, {"dh", 6},
{"ax", 0}, {"sp", 4},
- {"bx", 3}, {"bp", 7},
- {"cx", 1}, {"si", 5},
- {"dx", 2}, {"di", 6},
+ {"bx", 3}, {"bp", 5},
+ {"cx", 1}, {"si", 6},
+ {"dx", 2}, {"di", 7},
{"eax", 0}, {"esp", 4},
- {"ebx", 3}, {"ebp", 7},
- {"ecx", 1}, {"esi", 5},
- {"edx", 2}, {"edi", 6},
+ {"ebx", 3}, {"ebp", 5},
+ {"ecx", 1}, {"esi", 6},
+ {"edx", 2}, {"edi", 7},
};
}
@@ -44,15 +44,26 @@ namespace {
// Manual, page 530
// Reg + Reg/Memory
uint8_t ModRM(const std::string& reg, const std::string& rm) {
- // TODO: extend
uint8_t result{0b11000000};
- auto index1{ IndexOfRegister.find(reg) };
- if (index1 == IndexOfRegister.end())
- throw std::runtime_error("Unknown register for arg1: "s + reg);
-
- result |= (index1->second << 3);
-
+ size_t val_reg{};
+ // reg
+ if (reg.size() > 0 && reg[0] == '/') { // "/digit"
+ try {
+ val_reg = stoull(reg.substr(1));
+ } catch (const std::exception& ex) {
+ throw std::runtime_error("ModRM: Bad digit in arg1: "s + reg);
+ }
+ } else { // reg
+ auto index1{ IndexOfRegister.find(reg) };
+ if (index1 == IndexOfRegister.end())
+ throw std::runtime_error("ModRM: Unknown register for arg1: "s + reg);
+ val_reg = index1->second;
+ }
+
+ result |= (val_reg << 3);
+
+ // rm
auto index2{ IndexOfRegister.find(rm) };
if (index2 == IndexOfRegister.end())
throw std::runtime_error("Unknown register for arg2: "s + rm);
@@ -62,6 +73,15 @@ uint8_t ModRM(const std::string& reg, const std::string& rm) {
return result;
}
+uint8_t RegNo(const std::string& reg)
+{
+ auto index{ IndexOfRegister.find(reg) };
+ if (index == IndexOfRegister.end())
+ throw std::runtime_error("Reg: Unknown register for arg: "s + reg);
+
+ return index->second;
+}
+
#if 0
prefixes{
"lock", 0xf0,