Solved

Basic Assembler Simulator help

Posted on 2003-11-03
15
1,017 Views
Last Modified: 2011-09-20
Just wondering if anyone could point me to some resources, or information on a problem I have. I have the Mano Computer System Architecture book, but im not very familiar with Assembly at all, ive scoured google and most sites i find either go way too into detail that I get lost or direct me to a pay site where i have to buy a book. Anyways the problem at hand:

Im trying to write a very basic Assembler Simulator in C, that takes in input from a text file containing Assembly and produces output in the form of binary object file containing the corresponding machine code image of the program.

This program must be able to handle the directives: ORG, END, DEC
If you need more information I can provide some.

Im not asking to be handed a completed answer :) Though I wouldnt complain, just looking for some help and a point in the right direction as I do want to learn how to do this.

Thanks, I will award more points depending on the responces I get



0
Comment
Question by:Afterlife
  • 7
  • 4
  • 2
15 Comments
 
LVL 5

Expert Comment

by:mtmike
Comment Utility
Writing an assembler is not too tricky, just a lot of work. Assembly consists of mnemonic instructions that have a corresponding binary representation.

A single mnemonic (eg DEC) usually has multiple different binary forms depending on the size and arguments of the instruction. For example:

DEC register
DEC [register]
DEC [register + displacement]

Start out by using the lex/yacc combination to generate a parser that accepts the different mnemonics and their various formats (size, addressing mode). You will need an instruction set reference to find out the combinations that are allowed for each instruction (and of course the list of instructions that your assembler should support).
0
 

Author Comment

by:Afterlife
Comment Utility
Do you know where I could find an example program that does what Im trying to do so I can use it for referance and to learn off. As it stands im failry clueless in assembly.

All i know is i need to be able to convert from say an expression such as A = B + 9, change this into Assembly such and the Load, and then store all this stuff, and then change to hex.

This is a project some students across the U.S have to do, and im clueless as to how to begin, i will severly up the points im offering for some serious help!

Thanks in advance.
0
 
LVL 5

Expert Comment

by:mtmike
Comment Utility
Start by reading the lex/yacc documentation. These tools are invaluable for compiler construction in general.

The 'lex' (or newer 'flex') tool constructs lexical analyzers for mapping characters to tokens. The 'yacc' (or newer 'bison') tool can be used to specify a grammar using productions. These programs generate C code.

Forget about the code generation part for now. First get the input file into a datastructure you can work with.

And, I imagine your professor/teacher has given you a grammar, instruction set reference and maybe some example test programs to start with.
0
 

Author Comment

by:Afterlife
Comment Utility
grammar I think yeah, but no test programs or anything, the prof has been failry unhelpful with regards to this assignment.

Program must satisfy the following specific goals:
Support the directives: ORG, END, DEC.

Support symbolic address reference labels (must start with alphabetic character, length 1-3 alphanumeric characters, terminating in comma – ‘,’)

Support operation mnemonic syntax defined by Mano (Table 6.1, page 175) with hexadecimal object codes defined.

Support assembly instruction grammar, with or without symbolic address



I have no idea what this lex/yakk thing is? This assignment is "supposed" to be simple, tho im a complete newb where assembler is concerned and the materials we have to work with focus mor eon computer architecture and logic/circuit design not assembler...

I think all im supposed to do is read in a file say like A=B+3 and then convert this to Assembler and then from that to Hex, i dont have to convert it to binary just yet thats like the 3rd part or something.

Await your responce.
0
 

Author Comment

by:Afterlife
Comment Utility
oh and its not a compiler its a Simulator, im supposed to be faking an assembler in C.
0
 
LVL 5

Expert Comment

by:mtmike
Comment Utility
Well, you do have to convert from a text input file to machine code (whether or not you print out hex values or dump them to a binary). Don't know what the "simulation" part is then, since this is what a real assembler does. Perhaps the Mano book (never seen it) defines some "demonstration" assembly language.

Anyway, I still think using lex/yacc is the quickest way to get the job done. Depends on what you're allowed to use of course.

The 'lex' thing is just a tool to recognize patterns, so you don't have to dissect your input file using hand-crafted C code (think <ctype.h>).

The flex manual:
http://www.gnu.org/software/flex/manual/html_chapter/flex_toc.html

The 'yacc' thing can recognize grammars.

The bison manual:
http://www.gnu.org/manual/bison-1.35/html_node/index.html

The 'flex' and 'bison' tools are simply improved version of 'lex' and 'yacc'. They work the same and are present on any unix system. There are windows version too.

I am willing to help, but I am not sure what other advice to give you, since I have no clue what the intended assembly looks like, what the input file format (grammar) is and how sophisticated the whole simulator needs to be.
0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 

Author Comment

by:Afterlife
Comment Utility
Would you like me to send you the webpage with the instructions and stuff? I asked if were allowed to use yacc or flex and apprantly thats way too advanced for this assignment.

Do you have an e-mail or MSN/ICQ name/number maybe we can converse in real time or something.

Thanks
0
 
LVL 5

Expert Comment

by:mtmike
Comment Utility
You can post a link to the webpage (or upload it somewhere) so everyone can take a look at it.
0
 
LVL 1

Expert Comment

by:vbs03
Comment Utility
Hi Afterlife,
    I think I know what u r after. Uneed to simulate an assembler in C which bassically will handle certain opcodes like LOAD, ADD, STORE etc and will also handle directives like ORG, DEC etc.
For this u need to understand a few data structures required for manipulating the Assembler instructions.
1) Symbol Table - Hold information relating to variables used in Assembly program.

There are multiple type of Assemblers that we can simulate  based on requirements:
1) One Pass assembler
2) Two Pass assembler (most frequently used)
3) Macro processor (Additional to implement macro functionality)


If this is what u r looking for I'll be glad to guide u. Please reply to this post saying the same. I'll provide more details based on ur queries.

Regards ,
VBS

0
 

Author Comment

by:Afterlife
Comment Utility
VBS yeah thats basically it...

The objective of this project:
To design and write an Assembler program that accepts input in the form of text files containing Assembly language program source codes (expressed for the given Instruction set architecture) and produces output in the form of a binary object file containing the corresponding machine code image of the program.  This text file will contain an assembly language program that conforms to the system and grammar of the assembly language introduced by Mano in Chapter 6.  

Description:
Your program must satisfy the following specific goals:
· Support the directives: ORG, END, DEC.
· Support symbolic address reference labels (must start with alphabetic character, length 1-3 alphanumeric characters, terminating in comma – ‘,’)
· Support operation mnemonic syntax defined by Mano (Table 6.1, page 175) with hexadecimal object codes defined.
· Support assembly instruction grammar, with or without symbolic address

The Assembler must produce a listing of the assembly language program and the corresponding allocation addresses and object codes.
It should also produce a listing of the Symbol Table used during the Assembly process. Both the listings should be printable (i.e. text)      

You must include a binary object file.  
 

NOTES:
Using the principle of required prior declaration, wherein a symbolic reference (such as an address label or variable name label) can be resolved immediately if it has already been defined, a single-pass assembler strategy will be adequate to translate the program into machine code.

One approach to parsing and resolving programming constructs is to fully expand the construct in Assembly language format, generating labels as required on the first pass through the code, then performing the second pass in order to fully resolve all address (branch point) labels.

In order to demonstrate and test your model, you must produce at least one non-trivial example assembly language program that your Assembler will correctly assemble into a target binary object code file. This object code file contains a loadable, binary executable image of your program, suitable for immediate loading and execution.

Program must implement all instructions to permit your assembler to fully support the machine model.




I honestly have no idea where to even begin, if you could supply at least some code for me to work off, or if you have fully functional code doing what is asked, then I can greatly bump up the points! I wouldnt just use your code tho, I would use it to learn and implement my own way, but seeing as im strapped for time at the momment I dont have time to sit down and learn this as slowly as I would like.
0
 
LVL 1

Accepted Solution

by:
vbs03 earned 125 total points
Comment Utility
OK u need to start of defining an structure of opcodes
i.e. ur Assembler statements - LOAD, STORE etc. This structure could be defined as:
struct opcodes{
     char opname[15];
     int opval;
} = {"LOAD",0, "STORE", 1, ......};

In this way declare and initialise the opcode table using ur Mano table.

Now for the symbol table:
struct symtab{
     char id;
     int address;
};

Step 1: Then in the main function start ur program by opening the source file. Parse this source file line by line,
Step 2: Perform syntax analysis by ensuring that the instructions on each and every line follow the folllowing syntax

label: opcode <operand1> <operand2> <operand3> ....

To perform the syantax analysis for the same u need to create a new function which will take opcode as parameter and verify the number of operands. In this function perform the following validation also that if opcode does not exist then print error.

Step 3: After syntax checking, make use of operands to enter into the symtab structure, first the name of operand and next the location of operand i.e. the line number.

Step 4: In Step 1 along with source file we will be opening the target file also. This will be a new file we will open in o/p mode which will contain the translated code. Afetr Step 3 we will enter the tarnslated object code for the scanned line into the target file.

Step 5: At the end of source program file, have the object code file ready to execute and the symbol table in memory as required. Both cann be printed any time.

This is the entire flow of one-pass assembler, of course in short. If u have followed what I have said please start implementing it. I will guide u further as ur development begins. In case ur finding gr8 difficulty then I will provide u with code for some of the important functions used in the one-pass assembler.

Regards,
VBS
0
 

Author Comment

by:Afterlife
Comment Utility
Time is against me!

Ill try doing some of this tommorow, tho I have 5 classes and 5 finals which start in under 2 weeks, so getting this done in a week and studying for 5 finals, is pressure!!!



If you could provide maybe some more detailed instructions, examples, and maybe those functions would be great. I know this sounds greedy, but im really running out of time and this is only the first part of the project!

If you want I can tell you the second part of the project, im not sure if part 1 is needed to implement it or not, I can also give you the Mano table, i have a scan of it, so if you need it I can post it.

My regards - Afterlife
0
 

Author Comment

by:Afterlife
Comment Utility
Thanks for the help, but I have not had the time to try any of the suggested from VB.

I dont know wether I am to assign points  no matter what, or if a mod can just close this topic.

Thanks
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
IT Help Desk Summit 4 239
Array in beta 10 265
DOS Dongle bypassing 10 1,520
Learning ARM system and assembly language with a Raspberry pi 2 70
Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
Restoring deleted objects in Active Directory has been a standard feature in Active Directory for many years, yet some admins may not know what is available.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now