데이터 전송 명령 문서의 검증 코드다. if __name__ == "__main__": 아래가 자체 테스트다.
"""「데이터 전송 명령」: MOV·XCHG의 규칙과 오류, 틀린 MOV 다섯 줄, 두 변수 바꾸기 추적, 카드 C1·C2
실행: python3 16_data-transfer-instructions_verify.py (표준 라이브러리만 쓴다)
MASM으로 돌린 결과가 아니다. 아래의 작은 x86 흉내기(8·16·32비트 레지스터, ZF·SF·CF·OF,
리틀 엔디언 메모리)로 문서에 적힌 값을 확인한다.
주소와 배열 번호는 0부터 센다.
"""
# ---- x86 흉내기 (이 파일 안에서만 쓴다. 다른 파일을 import하지 않는다) ----
MASK = {1: 0xFF, 2: 0xFFFF, 4: 0xFFFFFFFF}
R32 = ["eax", "ebx", "ecx", "edx", "esi", "edi", "ebp", "esp"]
PART = {}
for _r in "abcd":
PART[_r + "l"] = ("e" + _r + "x", 0, 1)
PART[_r + "h"] = ("e" + _r + "x", 8, 1)
PART[_r + "x"] = ("e" + _r + "x", 0, 2)
for _r in ("si", "di", "bp", "sp"):
PART[_r] = ("e" + _r, 0, 2)
for _r in R32:
PART[_r] = (_r, 0, 4)
SREG = {"cs", "ds", "ss", "es", "fs", "gs"}
class AsmError(Exception):
"""어셈블러가 받아 주지 않는 문장."""
def signed(v, size):
"""size바이트 값 v를 부호 있는 수로 읽는다."""
bits = 8 * size
v &= MASK[size]
return v - (1 << bits) if v >> (bits - 1) else v
class CPU:
def __init__(self):
self.r = dict.fromkeys(R32, 0)
self.mem = {}
self.ZF = self.SF = self.CF = self.OF = 0
# --- 메모리: 리틀 엔디언 ---
def rd(self, addr, size):
return sum(self.mem.get(addr + i, 0) << (8 * i) for i in range(size))
def wr(self, addr, size, v):
for i in range(size):
self.mem[addr + i] = (v >> (8 * i)) & 0xFF
def dump(self, addr, n):
return [self.mem.get(addr + i, 0) for i in range(n)]
# --- 피연산자: "al" 같은 레지스터 이름, 정수(즉시값), ("m", 주소, 크기) ---
def size(self, op):
if isinstance(op, str):
if op in SREG or op in ("ip",):
return 2
if op == "eip":
return 4
return PART[op][2]
if isinstance(op, tuple):
return op[2]
return None
def get(self, op):
if isinstance(op, int):
return op
if isinstance(op, tuple):
return self.rd(op[1], op[2])
full, shift, size = PART[op]
return (self.r[full] >> shift) & MASK[size]
def set(self, op, v):
if isinstance(op, tuple):
self.wr(op[1], op[2], v & MASK[op[2]])
return
full, shift, size = PART[op]
keep = self.r[full] & ~(MASK[size] << shift) & 0xFFFFFFFF
self.r[full] = keep | ((v & MASK[size]) << shift)
def _check(self, d, s):
if isinstance(d, int):
raise AsmError("immediate value cannot be destination")
if d in ("eip", "ip", "cs"):
raise AsmError("EIP/IP/CS cannot be the destination")
if isinstance(d, tuple) and isinstance(s, tuple):
raise AsmError("memory-to-memory not permitted")
if isinstance(d, str) and d in SREG and isinstance(s, int):
raise AsmError("immediate move to segment register not permitted")
ds, ss = self.size(d), self.size(s)
if ds is None:
raise AsmError("ambiguous operand size")
if ss is not None and ds != ss:
raise AsmError("size mismatch")
if ss is None and not -(1 << (8 * ds - 1)) <= s <= MASK[ds]:
raise AsmError("immediate too large")
return ds
def flags(self):
return (self.ZF, self.SF, self.CF, self.OF)
def _zs(self, r, n):
self.ZF = int(r == 0)
self.SF = (r >> (8 * n - 1)) & 1
# --- 명령 ---
def mov(self, d, s):
self._check(d, s)
if isinstance(d, str) and d in SREG:
return
self.set(d, self.get(s)) # 플래그는 건드리지 않는다
def xchg(self, a, b):
if isinstance(a, int) or isinstance(b, int):
raise AsmError("no immediate operands")
self._check(a, b)
va, vb = self.get(a), self.get(b)
self.set(a, vb)
self.set(b, va)
def movzx(self, d, s):
if not isinstance(d, str):
raise AsmError("destination must be a register")
if self.size(s) >= self.size(d):
raise AsmError("source must be smaller")
self.set(d, self.get(s))
def movsx(self, d, s):
if not isinstance(d, str):
raise AsmError("destination must be a register")
if self.size(s) >= self.size(d):
raise AsmError("source must be smaller")
self.set(d, signed(self.get(s), self.size(s)))
def _add(self, a, b, n, keep_cf=False):
m, top = MASK[n], 8 * n - 1
full = a + b
r = full & m
if not keep_cf:
self.CF = (full >> (top + 1)) & 1
self.OF = (((a ^ r) & (b ^ r)) >> top) & 1
self._zs(r, n)
return r
def _sub(self, a, b, n, keep_cf=False):
m, top = MASK[n], 8 * n - 1
r = (a - b) & m
if not keep_cf:
self.CF = int(a < b)
self.OF = (((a ^ b) & (a ^ r)) >> top) & 1
self._zs(r, n)
return r
def add(self, d, s):
n = self._check(d, s)
self.set(d, self._add(self.get(d), self.get(s) & MASK[n], n))
def sub(self, d, s):
n = self._check(d, s)
self.set(d, self._sub(self.get(d), self.get(s) & MASK[n], n))
def inc(self, d):
n = self.size(d)
if n is None:
raise AsmError("ambiguous operand size")
self.set(d, self._add(self.get(d), 1, n, keep_cf=True)) # CF는 그대로
def dec(self, d):
n = self.size(d)
if n is None:
raise AsmError("ambiguous operand size")
self.set(d, self._sub(self.get(d), 1, n, keep_cf=True)) # CF는 그대로
def neg(self, d):
n = self.size(d)
self.set(d, self._sub(0, self.get(d), n)) # 0 - 값
def dup(count, *items):
return list(items) * count
class Data:
"""데이터 세그먼트. 정의한 순서대로 바이트를 채우고 이름마다 주소·TYPE·LENGTHOF·SIZEOF를 적어 둔다."""
def __init__(self, cpu, base=0x00404000):
self.cpu, self.base, self.loc, self.sym = cpu, base, base, {}
def _flat(self, items):
for it in items:
if isinstance(it, list):
yield from self._flat(it)
else:
yield it
def define(self, name, tsize, *items):
start, count = self.loc, 0
for it in self._flat(items):
if isinstance(it, str) and tsize == 1:
for ch in it:
self.cpu.wr(self.loc, 1, ord(ch))
self.loc += 1
count += 1
else:
self.cpu.wr(self.loc, tsize, (it or 0) & MASK.get(tsize, (1 << 8 * tsize) - 1))
self.loc += tsize
count += 1
if name:
self.sym[name] = {"addr": start, "type": tsize, "length": count, "size": count * tsize}
def byte(self, name, *items):
self.define(name, 1, *items)
def word(self, name, *items):
self.define(name, 2, *items)
def dword(self, name, *items):
self.define(name, 4, *items)
def label(self, name, tsize):
self.sym[name] = {"addr": self.loc, "type": tsize, "length": 1, "size": tsize}
def offset(self, name):
return self.sym[name]["addr"]
def m(self, name, off=0, size=None):
"""이름+오프셋 메모리 피연산자. size를 주면 PTR로 크기를 바꾼 것이다."""
return ("m", self.sym[name]["addr"] + off, size or self.sym[name]["type"])
def bytes_of(self, name):
return self.cpu.dump(self.sym[name]["addr"], self.sym[name]["size"])
def must_fail(fn, *args):
try:
fn(*args)
except AsmError as e:
return str(e)
raise AssertionError("오류가 나야 하는데 통과했다")
# ---- 흉내기 끝 ----
if __name__ == "__main__":
c = CPU(); d = Data(c)
# MOV의 규칙과 오류 세 줄
d.byte("count", 100); d.word("wVal", 2)
c.mov("bl", d.m("count")); assert c.get("bl") == 100
c.mov("ax", d.m("wVal")); assert c.get("ax") == 2
c.mov(d.m("count"), "al"); assert c.get(d.m("count")) == 2
assert must_fail(c.mov, "al", d.m("wVal")) == "size mismatch"
assert must_fail(c.mov, "ax", d.m("count")) == "size mismatch"
assert must_fail(c.mov, "eax", d.m("count")) == "size mismatch"
# 틀린 MOV 다섯 줄
c = CPU(); d = Data(c)
d.byte("bVal", 100); d.byte("bVal2", None); d.word("wVal", 2); d.dword("dVal", 5)
assert "segment" in must_fail(c.mov, "ds", 45)
assert must_fail(c.mov, "esi", d.m("wVal")) == "size mismatch"
assert "destination" in must_fail(c.mov, "eip", d.m("dVal"))
assert "immediate value cannot be destination" == must_fail(c.mov, 25, d.m("bVal"))
assert "memory-to-memory" in must_fail(c.mov, d.m("bVal2"), d.m("bVal"))
# 메모리에서 메모리로: 레지스터를 거친다
c.mov("al", d.m("bVal")); c.mov(d.m("bVal2"), "al"); assert c.get(d.m("bVal2")) == 100
# MOV는 플래그를 건드리지 않는다
c.mov("al", 0xFF); c.add("al", 1); f = c.flags(); c.mov("al", 0x55); c.mov("ebx", 0); assert c.flags() == f
# XCHG
c = CPU(); d = Data(c)
d.word("var1", 0x1000); d.word("var2", 0x2000)
c.mov("ax", 0x1111); c.mov("bx", 0x2222); c.xchg("ax", "bx"); assert (c.get("ax"), c.get("bx")) == (0x2222, 0x1111)
c.mov("ax", 0x12AB); c.xchg("ah", "al"); assert c.get("ax") == 0xAB12
c.mov("bx", 0x5555); c.xchg(d.m("var1"), "bx"); assert (c.get(d.m("var1")), c.get("bx")) == (0x5555, 0x1000)
c.mov("eax", 1); c.mov("ebx", 2); c.xchg("eax", "ebx"); assert (c.get("eax"), c.get("ebx")) == (2, 1)
assert "memory-to-memory" in must_fail(c.xchg, d.m("var1"), d.m("var2"))
assert must_fail(c.xchg, "ax", 5) == "no immediate operands"
# 두 변수 바꾸기 추적 (문서의 표)
c = CPU(); d = Data(c)
d.word("var1", 0x1000); d.word("var2", 0x2000)
trace = []
c.mov("ax", d.m("var1")); trace.append((c.get("ax"), c.get(d.m("var1")), c.get(d.m("var2"))))
c.xchg("ax", d.m("var2")); trace.append((c.get("ax"), c.get(d.m("var1")), c.get(d.m("var2"))))
c.mov(d.m("var1"), "ax"); trace.append((c.get("ax"), c.get(d.m("var1")), c.get(d.m("var2"))))
assert trace == [(0x1000, 0x1000, 0x2000), (0x2000, 0x1000, 0x1000), (0x2000, 0x2000, 0x1000)]
# 카드 C2 추적
c = CPU(); d = Data(c)
d.byte("a", 0x11); d.byte("b", 0x22)
c.mov("eax", 0)
c.mov("al", d.m("a")); c.mov("ah", d.m("b")); assert c.get("ax") == 0x2211
c.xchg("ah", "al"); assert c.get("ax") == 0x1122
c.mov(d.m("a"), "al"); c.mov(d.m("b"), "ah")
assert (c.get(d.m("a")), c.get(d.m("b"))) == (0x22, 0x11)
# 카드 C1 판별
d.word("w", 7); d.dword("dw", 9)
assert must_fail(c.mov, "ax", d.m("dw")) == "size mismatch"
assert "memory-to-memory" in must_fail(c.mov, d.m("w"), d.m("w"))
c.mov(d.m("w"), 300); assert c.get(d.m("w")) == 300
assert must_fail(c.mov, "bl", 300) == "immediate too large"
assert "destination" in must_fail(c.mov, "cs", "ax")
print("ALL CHECKS PASSED")