include_once "parser.php";
include_once "toMachineLanguage.php";
?>
MIPS Online Assembler
readBox($c);
?>
/*precondition: $code contains the mips code
entered into the text box by the user
including any spacing and comments they type in
postcondition:
if the code is valid
the binary version of their code is ouput to
the screen
else
a message saying the code is not valid is output to the screen
*/
function readBox($code)
{
$code = explode("\n",$code);
if(!is_array($code))
{
echo "data is not an array
";
return -1;
}
/* $data is a two D array that will hold the data after parsing
the fields of $data mirror the fields of the database we will be using
*/
// values in the $data arrary will contain "" if data is invalid
$data = array(
array(
"mips" =>"", // used to store unparsed mips instr
"label" =>"", // used to store the label preceding instr
"instr" =>"", // used to store instr type
"args" =>"", // stores the args to instr in a comma delemited list
"comment"=>"", // stores comment of function
"binary" =>"", // stores bianry code
"goto" =>"", // stores label atatched to branch instr's
"instrNum" =>""// stores the inst number on lines that have mips instructions
)
);
/* loops through the array of $code and populates the
$data array
for branch instruction the $binary field wil be ""
*/
$instrNum =0;
$err = 0;
for($i=0; $i".
$err = -1;
}
else
{
$offset = $destInstrNum - $srcInstrNum -1;
// echo "offset = $offset ";
$oldBinary= $data[$i]["binary"];
$data[$i]["binary"] =$oldBinary.toBinary($offset,16);
}
}
}
}
if($err == 0)
{
// outputs machine code
echo "Machine Language code
";
for($i=0; $i< sizeof($code); $i++)
{
if($data[$i]["binary"] != "")
echo $data[$i]["binary"]."
";
}
}
}
?>