# [ CPU Emulator ] by Python


 어셈블리어를 공부하다가 어셈블리어를 외부로부터 읽어서 txt파일로 출력하는 프로그램이다.

# 구현소스

import sys
readFile = open('homework_3.s', 'r')
writeFile = open('output.txt', 'w')
orig_stdout = sys.stdout
sys.stdout = writeFile

regi = {'%eax':0, '%ebx':0, '%ecx':0, '%edx':0, '%esi':0, '%edi':0,'%esp':0, '%ebp':0}
stack = []

def argToValue(arg):
    if (arg[0] == '%'):
        for k, v in regi.items():
            if (k == arg):
                return v
    if (arg[0:3] == "$0x"):
        return int(arg[1:],16)
    return int(arg[1:],10)

def movFunc(arg):
    regi[arg[1]] = argToValue(arg[0])
def subFunc(arg):
    regi[arg[1]] -= argToValue(arg[0])
def addFunc(arg):
    regi[arg[1]] += argToValue(arg[0])
def xorFunc(arg):
    regi[arg[1]] = argToValue(arg[0]) ^ argToValue(arg[1])
def pushFunc(arg):
    stack.append({argToValue('%esp'):argToValue(arg[0])})
    regi['%esp'] -= 4
def popFunc(arg):
    regi[arg[0]] = stack.pop().values()[0]
    regi['%esp'] += 4
def nopFunc(arg):
    pass
def retFunc(arg):
    pass

operator = dict( mov = movFunc, sub = subFunc, add = addFunc, xor = xorFunc, push = pushFunc, pop = popFunc, nop = nopFunc, ret = retFunc)

while 1:
    line = readFile.readline().replace(",","").split()
    if not line:
        break
    print "=========input========="
    print line
    operator[line[0]](line[1:])
    print "-------stack info------"
    for x in stack[::-1]:
        print x
    print "-----register info-----"
    for key, value in regi.items():
        print key, value

readFile.close()
writeFile.close()


sys.stdout = orig_stdout




# 실행결과


-INPUT


	mov	$1000, %esp
	push	%ebp
	mov	%esp, %ebp
	mov	$0, %eax
	mov	$0x0, %ebx
	xor	%esi, %esi
	xor	%edi, %edi
	xor	%ecx, %ecx
	mov	%ecx, %edx
        mov     $0x100, %eax
        mov     %eax, %ebx
        sub     $0x10, %ebx
        sub     $10, %ebx
	mov	%ebx, %edx
	push	%edx
	add	%edx, %edx
	push	%edx
	pop	%ebx
	xor	$0x10, %ebx
	mov	$0x200, %edx
	sub	$0x20, %edx
	add	%edx, %edx
	mov	$10, %eax
	sub	%eax, %edx		
	push	%edx
	pop	%ecx
        nop
	pop	%ebp
	ret

-OUTPUT


=========input=========
['mov', '$1000', '%esp']
-------stack info------
-----register info-----
%ebp 0
%eax 0
%edx 0
%edi 0
%esp 1000
%ebx 0
%esi 0
%ecx 0
=========input=========
['push', '%ebp']
-------stack info------
{1000: 0}
-----register info-----
%ebp 0
%eax 0
%edx 0
%edi 0
%esp 996
%ebx 0
%esi 0
%ecx 0
=========input=========
['mov', '%esp', '%ebp']
-------stack info------
{1000: 0}
-----register info-----
%ebp 996
%eax 0
%edx 0
%edi 0
%esp 996
%ebx 0
%esi 0
%ecx 0
=========input=========
['mov', '$0', '%eax']
-------stack info------
{1000: 0}
-----register info-----
%ebp 996
%eax 0
%edx 0
%edi 0
%esp 996
%ebx 0
%esi 0
%ecx 0
=========input=========
['mov', '$0x0', '%ebx']
-------stack info------
{1000: 0}
-----register info-----
%ebp 996
%eax 0
%edx 0
%edi 0
%esp 996
%ebx 0
%esi 0
%ecx 0
=========input=========
['xor', '%esi', '%esi']
-------stack info------
{1000: 0}
-----register info-----
%ebp 996
%eax 0
%edx 0
%edi 0
%esp 996
%ebx 0
%esi 0
%ecx 0
=========input=========
['xor', '%edi', '%edi']
-------stack info------
{1000: 0}
-----register info-----
%ebp 996
%eax 0
%edx 0
%edi 0
%esp 996
%ebx 0
%esi 0
%ecx 0
.........(생략)

cpu emulator.pyhomework_3.soutput.txt

WRITTEN BY
Who1sth1s

,