컴공 일기229
게시글 주소: https://dev.orbi.kr/00062045045
요새는 운동을 하러 가는 것 이외엔, 공학과 수학에 집중하고 있습니다. 나도 모르게, 몰입이 되고 있다는 느낌을 많이 받고 있어요. 다만, 결과는 과정의 수려함들을 집어삼켜 먹는 습성이 있는 걸 잘 알아서, 큰 욕심은 내지 않고 있습니다.
과정에 주목하는 삶을 살고 있달까요. 뭐, 무엇인가를 크게 이뤄나가는 일상은 아닙니다만, 개인적으론 참 만족스럽습니다.
저번 일기에서 그려보았듯, Assembler를 제작하고 있어요. 재미있는 여정입니다.
Parser 모듈 : 입력 코드에 대한 접근을 캡슐화(Capsulation)합니다. 어셈블리 명령을 읽어 들여 구문을 분석하고, 명령 세부요소(필드/기호)에 편리하게 접근하도록 합니다. 추가적으로 모든 공백과 주석문을 제거합니다.
Parser.h
#pragma once
#ifndef HACK_ASSEMBLER_PARSER_H
#define HACK_ASSEMBLER_PARSER_H
#include <iostream>
#include <fstream>
#include <string>
#include <algorithm>
#include <map>
using namespace std;
class Parser
{
public:
Parser(string& filename);
//File is closed implicitly.
bool hasMoreCommands();
//Returns true if the file contains commands that still need to be parsed,
//Return flase otherwise.
void advance(unsigned long& lineNr);
char commandType(unsigned long& lineNr);
string symbol();
//Returns the symbol or decimal value of the current command.
//Should only be called if commandType() returns 'A' or 'L'.
string destM();
// Returns the destination mnemonic of the current command.
string compM();
string jumpM();
private:
ifstream fin;
string currentCommand;
map <char, char> commandTable;
};
#endif
-Parser.cpp
#include "Parser.h"
using namespace std;
Parser::Parser(string& fileName)
{
fin.open(fileName);
if (fin.fail())
{
cout << fileName << "not found." << endl;
exit(1);
}
//Populate the command map table.
commandTable['@'] = 'A';
commandTable['A'] = 'C';
commandTable['D'] = 'C';
commandTable['M'] = 'C';
commandTable['0'] = 'C';
commandTable['1'] = 'C';
commandTable['-'] = 'C';
commandTable['!'] = 'C';
commandTable['('] = 'L';
}
bool Parser::hasMoreCommands()
{
return !fin.eof();
}
void Parser::advance(unsigned long& lineNr)
{
string currentLine;
unsigned long commentPos;
bool commandFound;
commandFound = false;
while (!commandFound && getline(fin, currentLine))
{
lineNr++;
//Remove whitespace.
currentLine.erase(remove_if(currentLine.begin(), currentLine.end(), ::isspace), currentLine.end());
//Remove comments.
commentPos = currentLine.find("//");
//if comments were found
if (commentPos != string::npos)
{
currentLine.erase(commentPos, currentLine.length() - commentPos);
}
commandFound = !currentLine.empty();
}
currentCommand = currentLine;
}
char Parser::commandType(unsigned long& lineNr)
{
if (commandTable.find(currentCommand[0]) != commandTable.end())
{
return commandTable[currentCommand[0]];
}
cout << "Invalid syntax at line: " << lineNr << endl;
}
string Parser::symbol()
{
unsigned long openBracketPos, closeBracketPos;
openBracketPos = currentCommand.find('(');
closeBracketPos = currentCommand.find(')');
//A-instruction: return everything after the '@' character
if (currentCommand[0] == '@')
{
return currentCommand.substr(1, currentCommand.length() - 1);
}
else if (openBracketPos != string::npos && closeBracketPos != string::npos) {
return currentCommand.substr(openBracketPos + 1, closeBracketPos - openBracketPos - 1);
}
// If the function was called in error, return a blank string.
return "";
}
string Parser::destM()
{
unsigned long equalSignPos;
//Return everything before the '=' character.
if (equalSignPos != string::npos)
{
return currentCommand.substr(0, equalSignPos);
}
//If no destination was specified, return a blank string.
return "";
}
string Parser::compM()
{
unsigned long equalSignPos, semiColonPos;
equalSignPos = currentCommand.find('=');
semiColonPos = currentCommand.find(';');
// Return the computation mnemonic based on three cases.
if (equalSignPos != string::npos)
{
//Case 1: dest = comp ; jump
if (semiColonPos != string::npos)
{
return currentCommand.substr(equalSignPos + 1, semiColonPos - equalSignPos - 1);
}
//Case 2: dest = comp
return currentCommand.substr(equalSignPos + 1, currentCommand.length() - equalSignPos - 1);
}
else if (semiColonPos != string::npos)
{
//Case 3: comp ; jump
return currentCommand.substr(0, semiColonPos);
}
return "";
}
string Parser::jumpM()
{
unsigned long semiColonPos;
//Return everything after the ';' character.
if (semiColonPos != string::npos)
{
return currentCommand.substr(semiColonPos + 1, currentCommand.length() - semiColonPos - 1);
}
//If a jump was not specified, return a blank string.
}
CodeTranslator 모듈 : 어셈블리 언어의 연상기호를 2진 코드로 변환합니다.
-CodeTranslator.h
#pragma once
#ifndef HACK_ASSEMBLER_CODE_H
#define HACK_ASSEMBLER_CODE_H
#include <iostream>
#include <map>
using namespace std;
class CodeTranslator
{
public:
CodeTranslator();
//Populates the code translation map tables
// with the language specification.
string dest(string destMenmonic, unsigned long& lineNr);
//Returns the binary code of the destination mnemonic
//as a string containing 3 bits.
//Line number is passed for use in an error message.
string comp(string compMnemonic, unsigned long& lineNr);
//Returns the binary code of the computation mnemonic
//as a string containing 3 bits.
//Line number is passed for use in an error message.
string jump(string jumpMnemonic, unsigned long& lineNr);
//Returns the binary code of the jump mnemonic
//as a string containing 3 bits.
//Line number is passed for use in an error meassage.
private:
map<string, string> destTable;
map<string, string> compTable;
map<string, string> jumpTable;
};
#endif
-CodeTranslator.cpp
| #include "CodeTranslator.h" | |
| using namespace std; | |
| CodeTranslator::CodeTranslator() { | |
| // Populate the translation map tables. | |
| destTable[""] = "000"; | |
| destTable["M"] = "001"; | |
| destTable["D"] = "010"; | |
| destTable["MD"] = "011"; | |
| destTable["A"] = "100"; | |
| destTable["AM"] = "101"; | |
| destTable["AD"] = "110"; | |
| destTable["AMD"] = "111"; | |
| compTable["0"] = "0101010"; | |
| compTable["1"] = "0111111"; | |
| compTable["-1"] = "0111010"; | |
| compTable["D"] = "0001100"; | |
| compTable["A"] = "0110000"; | |
| compTable["!D"] = "0001101"; | |
| compTable["!A"] = "0110001"; | |
| compTable["-D"] = "0001111"; | |
| compTable["-A"] = "0110011"; | |
| compTable["D+1"] = "0011111"; | |
| compTable["A+1"] = "0110111"; | |
| compTable["D-1"] = "0001110"; | |
| compTable["A-1"] = "0110010"; | |
| compTable["D+A"] = "0000010"; | |
| compTable["D-A"] = "0010011"; | |
| compTable["A-D"] = "0000111"; | |
| compTable["D&A"] = "0000000"; | |
| compTable["D|A"] = "0010101"; | |
| compTable["M"] = "1110000"; | |
| compTable["!M"] = "1110001"; | |
| compTable["-M"] = "1110011"; | |
| compTable["M+1"] = "1110111"; | |
| compTable["M-1"] = "1110010"; | |
| compTable["D+M"] = "1000010"; | |
| compTable["D-M"] = "1010011"; | |
| compTable["M-D"] = "1000111"; | |
| compTable["D&M"] = "1000000"; | |
| compTable["D|M"] = "1010101"; | |
| jumpTable[""] = "000"; | |
| jumpTable["JGT"] = "001"; | |
| jumpTable["JEQ"] = "010"; | |
| jumpTable["JGE"] = "011"; | |
| jumpTable["JLT"] = "100"; | |
| jumpTable["JNE"] = "101"; | |
| jumpTable["JLE"] = "110"; | |
| jumpTable["JMP"] = "111"; | |
| } | |
| string CodeTranslator::dest(string destMnemonic, unsigned long& lineNr) { | |
| if (destTable.find(destMnemonic) != destTable.end()) { | |
| return destTable[destMnemonic]; | |
| } | |
| // If none of the mnemonics are found, output an error message, | |
| // and provide the line number in the original source where the error occurred. | |
| cout << "Invalid syntax in destination statement at line: " << lineNr << endl; | |
| exit(1); | |
| } | |
| string CodeTranslator::comp(string compMnemonic, unsigned long& lineNr) { | |
| if (compTable.find(compMnemonic) != compTable.end()) { | |
| return compTable[compMnemonic]; | |
| } | |
| // If none of the mnemonics are found, output an error message, | |
| // and provide the line number in the original source where the error occurred. | |
| cout << "Invalid syntax in computation statement at line: " << lineNr << endl; | |
| exit(1); | |
| } | |
| string CodeTranslator::jump(string jumpMnemonic, unsigned long& lineNr) { | |
| if (jumpTable.find(jumpMnemonic) != jumpTable.end()) { | |
| return jumpTable[jumpMnemonic]; | |
| } | |
| // If none of the mnemonics are found, output an error message, | |
| // and provide the line number in the original source where the error occurred. | |
| cout << "Invalid syntax in jump statement at line: " << lineNr << endl; | |
| exit(1); | |
| } |
0 XDK (+0)
유익한 글을 읽었다면 작성자에게 XDK를 선물하세요.
-
오르비문학 1화 0 0
오르비문학 1화
-
Test 0 0
Tetsteyey
-
수학 4등급만 받으면 2 0
쫀득하게 인서울 할 수 있는데
-
엘든링 왜 자꾸 멈추지 1 0
컴퓨터 좋은건데 씨발
-
목 졸라줘 5 1
켁켁켁 숨막혀 ㅜㅜ
-
시험지에 따라서 난이도가 가장 극단적으로 달라지는 번호같음....
-
개쉽게 풀리는데 이거 맞나
-
정시로 갑시다 8 0
내신반영을 노려서 내신 깡패 정시러
-
나왔어 12 0
다시감 근데 저게 왜 이륙햇냐
-
갑자기생각난썰 1 1
고1 2학기 학급회장선거때 후보가 2명이엇는데 그 친구들 둘이 합의하고 한명이...
-
그만하고 잘까 1 0
흐름이 끊겨버렷네
-
세기말 수능 1 1
2000학년도 대학수학능력시험
-
강은양t 0 0
현역 고3이고 작년까지 모고 3~4등급 나왔는데 지금부터 강은양t 들으려고 합니다....
-
2시열차 1 0
출발
-
지금 강민철 현강 다니고 있는데 저랑 너무 안맞는 느낌이 심하게 들어서...
-
뭘 해야하나요 0 0
이번에 고등학교 2학년 된 이공계 지망하는 지방 일반고학생입니다. 생기부를 제대로...
-
이게 오르비를 재밌게 오래하려면 10 4
수험생활을 지속해야 함
-
에ㅔㅔㅔㅔㅔㅔㄴ들리스레인ㄴㄴ 0 1
폴온마이헐트 코코로노 키즈니ㅣㅣㅣ
-
내 이상형 중단발에 속눈썹 1 0
-
우와 보추야동 많이떴다 2 2
보다자야지
-
심심한데 무물보 5 0
응애 나 아가학생
-
본인 물1 점수 꼬라지 0 1
3모 48점 (99) 5더프 47점인가였는데 시험이 어려웠어서 전국석차 30등쯤...
-
오후8시부터자다가깼더니 1 0
다시잠이안오네.. 비상..!!
-
생각나는구나
-
ㅇㄴ근데 0학점 패논패과목을 오ㅑㄹ케 빡세게시켜 0 0
그냥 좀 봐주면 안되나
-
시발점 한 다음 스블 0 0
고2이고대수 개념원리, 쎈, 고쟁이 했습니다개정 시발점 사놓은 게 있어서...
-
러셀 외부생 더프 성적표 0 0
문자로 발송되나요?? 아님 직접 찾으러 가야햐나요??
-
원래 사람은 별을 쫓아 달려갈 때 가장 빛나는 법이여설령 닿지 못할지라도적어도 내...
-
저걸 어케 함 진짜 와.. 원과목 중 생1만 수능공부로 안해봤는데 안하길잘한듯
-
시발 나 개폐급임 2 1
조별과제 하는족족 내것만 교수님 피드백 나오고 술처먹다 팀원들한테 자료 제출 개늦게하고 자퇴마렵다
-
딱 한 마디만 하고 자러감 9 3
미쿠 ㅈㄴ 예뻐어~~~~~~~~~~~~
-
중앙대 가기 59일차 3 1
안녕하세요 중앙대29학번 부산사나이 이동현입니다 음 오늘이 벌써 59일차군요...
-
이제 좀 자보실까 11 1
음음
-
리젠존나느리네 1 0
오르비망함?
-
너무멍청해짐 1 0
ㅜㅜㅜㅜㅜ
-
생윤 진짜 1도 모르는 쌩노베인데 누구 듣는 게 좋을가여
-
15살과 엄마 그 사이는 2 0
뭐라함 급함
-
대신 연세대 가겠다 선언
-
작년 10모 20번 0 0
이렇게 푸는거 맞나..?
-
위키하우 도움 ㅈㄴ 안되네 6 0
ㅗㅗㅗㅗㅗㅗ
-
새르비 할수록 4 0
헛소리가 늘어가는듯
-
아니 난 신라면 쳐돌이라 5 0
신라면만 먹는데….
-
내가사실은생명과학을좋아함 1 0
수능말고 그냥생명과학
-
. 11 1
-
님들 최애 과목 말해보셈 7 0
난 국어
-
님들 최애 라면 말해보셈 10 0
난 신라면
-
라면이랑 과자 안먹은지 6일차 2 0
후후

저 컴공인데 엄청 어려워 보이네요...
어.. 어셈블러를 직접 구현하는 과정이 컴공에 없기는 하죠 ㅠㅠ 개인적으로 그게 아쉬워서 방학을 기회로 해보고 있답니다!
와...컴공 가고 싶었는데 뭔가 엄청 어려워보인다
원리를 알면 생각보다 간단한 코드예요. 번역을 하려면, 문장을 단어별로 세세히 쪼갤 필요가 있고, 그 단어의 의미를 하나하나 파악하잖아요?
그런 원리예요 ㅎㅎ 그걸 코드로 표현한다면 복잡해보이지만, 어느정도 공부를 하고 나면 한 눈에 보이는 정도입니다.
ㅠㅠ 컴공 지망생이긴 한데 혹시 가서 고등학고 때 배웠던 과목 중 쓰이는 과목이 있나요..?
수능 국어요! 코딩도 결국 글쓰기예요. 그러니까, '글'을 읽는 원리라든가, 쓰는 원리에 익숙하면 굉장히 유리한 점을 지니고 있습니다. 수학도 많이 쓰이고요. 근데, 고등학교 수학에서 배웠던 것들이 '기반'이 되기는 하는데, 그것이 '중심'이 되는 것은 물론 아닙니다.