Help Getting Started

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Help Getting Started

arzthaus
Hi, I'm not very experienced in programming (I took C++ in high school and then did a semester in college) so I am trying to think of some of the techniques used to "translate" from assembly to binary, particularly the c-instruction portion. The simplest way I can think of is just using a bunch of if-then statements to translate a line of code (e.g. "A+D") into binary, but this seems rather inefficient. However, writing some sort of translating algorithm seems unnecessary, as we are only required to process a fairly low number of instructions, so a number of if-thens doesn't seems like such a bad idea. I know we don't like to give out answers but any help on what the "best way" to go would be appreciated. Thanks.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Help Getting Started

cadet1620
Administrator
There are easier languages to get started with than C++.

If you are willing to consider a new language, I like Python as a first language.  Its advanced data types are much easier to use than C++'s standard template library.

Another advantage of Python is that it is an "interpreted" language so you can start it in an interactive mode that lets you execute program commands one at a time and see how they work.

For your assembler you need to have a Symbol Table that matches symbol names with their addresses.  This is very easy in Python using dictionaries.  Dictionaries are like arrays that have strings as indexes instead of numbers.
Python 3.4.3 (v3.4.3:9b73f1c3e601, Feb 24 2015, 22:43:06) [MSC v.1600 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> table = {}	# Create an empty dictionary
>>> table['Fred'] = 17
>>> table['Wilma'] = 23
>>> print(table['Fred'])
17
>>> 
You can also use dictionaries to do the command to binary translation.  For example, here's the start of my assembler's dictionary that translates the computation part of C-commands:
compDict = {'0': '0101010',
           '1': '0111111',
           '-1': '0111010',
           'D': '0001100',
           'A': '0110000',
           '!D': '0001101',
           '!A': '0110001',
           '-D': '0001111',
           '-A': '0110011',
           'D+1': '0011111',
           'A+1': '0110111',

C++ has a similar datatype called a "map", but it's harder to use because you need to declare what type of data its indexes and values are.  See this link for a C++ example:
    http://www.cprogramming.com/tutorial/stl/stlmap.html

--Mark
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Help Getting Started

arzthaus
Thank you for your information. I realize C languages aren't the best for this project, but I am most familiar with C++ and I am trying to become more proficient. For me, just creating the assembler program is not the only part of the project :) I am also learning more about C. I will look into the C++ STL maps, thanks.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Help Getting Started

cadet1620
Administrator
arzthaus wrote
Thank you for your information. I realize C languages aren't the best for this project, but I am most familiar with C++ and I am trying to become more proficient. For me, just creating the assembler program is not the only part of the project :) I am also learning more about C. I will look into the C++ STL maps, thanks.
If you want to delve deeper, you could try writing your own hash table based map.
Quick intro:
    http://www.sparknotes.com/cs/searching/hashtables/
--Mark
Loading...