Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Dec 2, 2010, 06:11 PM   #1
MKang25
macrumors regular
 
Join Date: Jun 2010
Assembly, MIPS Question

I have to build a simple assembler, where I read in a instruction in assembly and then convert it to its machine code equivalent. I need some help getting started. I know how to find the machine code equivalents, looking up opcodes, registers codes etc.. I am just stuck on how to actually go about converting it in the program. In Mips you can only alter bytes, but what I need to do is combine or alter bits.
MKang25 is offline   0 Reply With Quote
Old Dec 2, 2010, 07:04 PM   #2
chown33
macrumors 603
 
Join Date: Aug 2009
Quote:
Originally Posted by MKang25 View Post
... In Mips you can only alter bytes, but what I need to do is combine or alter bits.
Everything made sense until that last sentence. The MIPS instruction-set has opcodes that load & store bytes, half-words, words, and double-words. I'm not sure how this is relevant, though, unless you're writing a MIPS assembler in MIPS assembler.

In any case, you combine and alter bits by ANDing and ORing them into place in longer operands (bytes, words, etc.). You may also have to apply some shifting, depending on where the bits are located in the longer operand. This is extremely common in high-level languages, and also common in assembly languages that have no direct bit-manipulation op-codes.
chown33 is offline   0 Reply With Quote
Old Dec 2, 2010, 07:31 PM   #3
lee1210
macrumors 68040
 
lee1210's Avatar
 
Join Date: Jan 2005
Location: Dallas, TX
What language are you writing your assembler in? Like chown33, that was the part that confused me as you failed to say and your last sentence was confusing.

Ultimately in MIPS-32 you'll have (if I remember correctly) a 6-bit opcode then a series of operands that are either constants or 5-bit register numbers. (I looked and in one format there's also a 6-bit function code) My approach would be to have constants for:
Each opcode 0-31
Each register 0-32 (named R#)
the number of bits to shift an opcode left (27)
The numbers to shift over operands

Then it gets pretty boring. Maybe a switch or other lookup that maps the ASCII for each instruction to your constant opcode value, the ASCII for R0-31 to your constant with the same value, etc. Each instruction has a fixed format (I think there's 4-5 instruction formats... I looked, only 3, lucky you) so you could write a subroutine for each instruction format that shifts it's arguments into the proper positions in the resulting 32-bit instruction.

-Lee

Last edited by lee1210; Dec 2, 2010 at 07:42 PM.
lee1210 is offline   0 Reply With Quote
Old Dec 3, 2010, 01:40 PM   #4
MKang25
Thread Starter
macrumors regular
 
Join Date: Jun 2010
Sorry for not being clear. I am writing a simple MIPS assembler using MIPs. I figured out how to modify the bits. A question that I now have is I have to read in a mips instruction to convert to machine code. So I would read in something like " add $t0, $t1, $t2 " . In the data section I would store the string. Also in the data section I would have a list of all the registers and opcodes I am using and their respective hex values. How would I go about looking up the hex values for the registers and opcodes. For the instruction above, I would parse the string and I get to the register how would i "Search" the data section for the memory location of $t0 in the data section so that I could load the hex value of it into a register.
MKang25 is offline   0 Reply With Quote
Old Dec 3, 2010, 01:48 PM   #5
subsonix
macrumors 68030
 
Join Date: Feb 2008
Create a hash table for the mnemonics, where each one resolves to an index position in a array. At this array position you store the opcode. If there are several different ones, depending on addressing mode and so on, add an array of opcodes at that index position.

The values to the right of the mnemonic I would parse using a switch/case state machine. So each row would be read from right to left.

Are you going to support labels and constants as well?
subsonix is offline   0 Reply With Quote
Old Dec 3, 2010, 01:55 PM   #6
MKang25
Thread Starter
macrumors regular
 
Join Date: Jun 2010
I haven't learned how to use hash tables. I also have to create a label/address table for the little segment of code that I input.
MKang25 is offline   0 Reply With Quote
Old Dec 3, 2010, 02:02 PM   #7
subsonix
macrumors 68030
 
Join Date: Feb 2008
Quote:
Originally Posted by MKang25 View Post
I haven't learned how to use hash tables. I also have to create a label/address table for the little segment of code that I input.
Ok, but you could simply leave out the hash function then, and search an array of mnemonics linearly for a match. The array could for example contain structs with the mnemonic and the opcode, or something like that.
subsonix is offline   0 Reply With Quote
Old Dec 3, 2010, 02:03 PM   #8
chown33
macrumors 603
 
Join Date: Aug 2009
It sounds like you need to do some studying and some research. Asking for answers here is neither.

If you're expected to write an assembler in assembler, then you've presumably been given enough information in class to do this, or pointers to where you can study the information. If not, then you should approach your in-class information provider, i.e. your teacher or teaching aid and describe what information you lack.

There are any number of representations for mnemonic tables and such. Even a brute force search of an array of pointers to char-strings would work, and you wouldn't need to know what a hashtable is.

If you're in a class, you're expected to come up with an answer yourself, even a sub-optimal one. Having multiple people supply you with answers isn't the same thing at all.
chown33 is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
nMP Assembly Process CH12671 Mac Pro 5 Mar 8, 2014 03:06 PM
Assembly Questions (HW) ArtOfWarfare Mac Programming 3 Feb 7, 2013 11:56 AM
Assembly Language Troubles :( Cresselia Mac Programming 4 Oct 5, 2012 08:06 PM
Display Assembly Replacement TowleC Buying Tips and Advice 0 Aug 6, 2012 05:12 PM
Assembly Compiler/IDE SuperMiguel Mac Programming 7 Jun 23, 2012 05:36 PM

Forum Jump

All times are GMT -5. The time now is 11:20 AM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC