Can somebody please explain this php code to me? : <pre><code>&lt;?php
$registers = array(
    "zero", "at", "v0", "v1", "a0", "a1", "a2", "a3", "t0", "t1", "t2", "t3", "t4", "t5", "t6", "t7", "s0",
    "s1", "s2", "s3", "s4", "s5", "s6", "s7", "t8", "t9", "k0", "k1", "gp", "sp", "fp", "ra"
);
$typeR = array(
    "add" =&gt; 32, "sub" =&gt; 34, "sll" =&gt; 0, "srl" =&gt; 2, "and" =&gt; 36, "or" =&gt; 37, "xor" =&gt; 38, "slt" =&gt; 42, "mul" =&gt; 24,
    "div" =&gt; 26, "jr" =&gt; 8
);
$typeI = array(
    "addi" =&gt; 8, "lui" =&gt; 15, "andi" =&gt; 12, "ori" =&gt; 13, "xori" =&gt; 14, "slti" =&gt; 10, "beq" =&gt; 4, "bne" =&gt; 5, "lw" =&gt; 35,
    "sw" =&gt; 43
);
$typeJ = array(
    "j" =&gt; 2, "jal" =&gt; 3
);

function typeR($address, $rs, $rt, $rd, $registers, $shamt, $typeR, $opfunc)
{
    if ((empty($rd) || !ctype_digit($shamt)) &amp;&amp; $opfunc != "jr") {
        return -1;
    }
    return array(
        "address" =&gt; $address,
        "bin" =&gt; "000000" . sprintf("%05d", decbin(array_search($rs, $registers)))
            . sprintf("%05d", decbin(array_search($rt, $registers)))
            . sprintf("%05d", decbin(array_search($rd, $registers)))
            . sprintf("%05d", decbin($shamt)) . sprintf("%06d", decbin($typeR[$opfunc]))
    );
}

function typeI($address, $rs, $rt, $imm, $registers, $typeI, $opfunc)
{
    $is_hex = strpos($imm, "x");
    $imm = $is_hex ? hexdec(substr($imm, $is_hex + 1)) : $imm;
    $imm = ($imm &lt; 0) ? substr(decbin($imm), -16) : sprintf("%016b", $imm);
    return array(
        "address" =&gt; $address,
        "bin" =&gt; sprintf("%06d", decbin($typeI[$opfunc]))
            . sprintf("%05d", decbin(array_search($rs, $registers)))
            . sprintf("%05d", decbin(array_search($rt, $registers))) . $imm
    );
}

function typeJ($address, $addr, $typeJ, $opfunc)
{
    return array(
        "address" =&gt; $address,
        "bin" =&gt; sprintf("%06d", decbin($typeJ[$opfunc])) . sprintf("%026d", decbin($addr))
    );
}

function blankLines($string)
{
    $string = str_replace(array("\0", "\t", "\r", " ", "\n"), "", $string);
    return (strlen($string) ? 1 : 0);
}

function findLabel($array, $arrayLen, $label)
{
    $label = $label . ":";
    for ($i = 0; $i &lt; $arrayLen; $i++) {
        $array[$i] = explode(" ", $array[$i]);
        $subarrayLen = count($array[$i]);
        for ($j = 0; $j &lt; $subarrayLen; $j++) {
            if (!strcmp($label, trim($array[$i][$j]))) {
                return ($i * 4);
            }
        }
    }
    return -1;
}

function comments($string)
{
    $commentI = ($string[0] == "#") ? -1 : strpos($string, "#");
    return (($commentI) ? substr($string, 0, $commentI - strlen($string)) : $string);
}

function format($array)
{
    $spaces = array("           ", "         ", "        ", "       ", "      ", "     ", "    ", "   ", "  ");
    $label = "";
    for ($i = 0; $i &lt; count($array); $i++) {
        $array[$i] = str_replace(",", " ", $array[$i]);
        $array[$i] = @strtolower(trim(str_replace($spaces, " ", comments($array[$i]))));
        $inc = blankLines($array[$i]);
        $labelF = strpos($array[$i], ":");
        $instr = $labelF ? substr($array[$i], $labelF + 1) : $array[$i];
        if (!$inc || (!strlen($instr) &amp;&amp; $i &lt; (count($array) - 2))) {
            if (!strlen($instr) &amp;&amp; $labelF) {
                $label = str_replace(array("\0", "\t", "\r", " ", "\n"), "", $array[$i]);
            }
            unset($array[$i]);
            $array = array_values($array);
            $i--;
        } elseif (isset($label)) {
            $array[$i] = ($label . " " . trim($array[$i]));
            unset($label);
            $array = array_values($array);
        }
    }
    return $array;
}

function fileWrite($file, $instEnd, $count)
{
    $arrayLen = count($instEnd);
    for ($i = 0; $i &lt; $arrayLen; $i++) {
        $instEnd[$i]["bin"] = substr($instEnd[$i]["bin"], -8, 8) . "+"
            . substr($instEnd[$i]["bin"], -16, 8) . "+"
            . substr($instEnd[$i]["bin"], -24, 8) . "+"
            . substr($instEnd[$i]["bin"], -32, 8);
        $instEnd[$i]["bin"] = explode("+", $instEnd[$i]["bin"]);
        $aux = 0;
        foreach ($instEnd[$i]["bin"] as $key =&gt; $value) {
            $instEnd[$i]["bin"][$key] = sprintf("%02X", bindec($value));
            $str = "0x" . sprintf("%03X", ($instEnd[$i]["address"] + $aux)) . " 0x" . $instEnd[$i]["bin"][$key];
            $str .= (((($count - 1) * 4) - 1) == (($i * 4) + $aux)) ? "\0" : "\n";
            fwrite($file, $str);
            $aux++;
        }
    }
    echo "\n\nArquivo de saída gerado!\n\n";
}

function assembler($arrayF, $typeR, $typeI, $typeJ, $registers, $argv)
{
    $fileOut = (!empty($argv[2])) ? $argv[2] : "memoria.mif";
    $file = fopen($fileOut, "w");
    if (!$file) {
        echo "\n# Falha ao tentar criar o arquivo quickpatricia-eu.mif!\n";
    }
    $instEnd = array();
    $arrayF = format($arrayF);
    $arrayLen = count($arrayF);
    $output = "";
    for ($i = 0; $i &lt; $arrayLen; $i++) {
        $sub = array(",", "$");
        $address = ($i * 4);
        $labelF = strrpos($arrayF[$i], ":");
        $instr = $labelF ? substr($arrayF[$i], $labelF + 1) : $arrayF[$i];
        $instr = explode(" ", trim($instr));
        if (array_key_exists($instr[0], $typeR)) {
            if ($instr[0] == "sll" || $instr[0] == "srl") {
                $rd = trim(str_replace($sub, "", $instr[1]));
                $rt = trim(str_replace($sub, "", $instr[2]));
                $shamt = $instr[3] ? trim(str_replace($sub, "", $instr[3])) : 0;
                $rs = "0";
            } elseif ($instr[0] == "jr") {
                $rs = trim(str_replace($sub, "", $instr[1]));
                $rd = "0";
                $rt = "0";
                $shamt = "0";
            } else {
                $rd = trim(str_replace($sub, "", $instr[1]));
                $rs = trim(str_replace($sub, "", $instr[2]));
                $rt = $instr[3] ? trim(str_replace($sub, "", $instr[3])) : 0;
                $shamt = "0";
            }
            $return = typeR($address, $rs, $rt, $rd, $registers, $shamt, $typeR, $instr[0]);
            if ($return == -1) {
                $output .= "\nA linha " . ($i + 1) . " possui algum erro.";
            } else {
                $instEnd[$i] = $return;
            }
        } elseif (array_key_exists($instr[0], $typeI)) {
            if (strpos($instr[2], "(")) {
                $begin = strpos($instr[2], "(");
                $end = strpos($instr[2], ")");
                $instr[3] = trim(substr($instr[2], $begin + 1, $end - strlen($instr[2])));
                $instr[2] = trim(substr($instr[2], 0, $begin));
                $imm = trim(str_replace($sub, "", $instr[2]));
                $rs = trim(str_replace($sub, "", $instr[3]));
                $rt = trim(str_replace($sub, "", $instr[1]));
            } elseif ($instr[0] == "lui") {
                $rs = "0";
                $rt = trim(str_replace($sub, "", $instr[1]));
                $imm = trim(str_replace($sub, "", $instr[2]));
            } elseif ($instr[0] == "bne" || $instr[0] == "beq") {
                $rs = trim(str_replace($sub, "", $instr[1]));
                $rt = trim(str_replace($sub, "", $instr[2]));
                $imm = trim(str_replace($sub, "", $instr[3]));
            } else {
                $rs = trim(str_replace($sub, "", $instr[2]));
                $imm = trim(str_replace($sub, "", $instr[3]));
                $rt = trim(str_replace($sub, "", $instr[1]));
            }
            $labelDef = (ctype_digit($imm) || $imm &lt; 0) ? -1 : findLabel($arrayF, $arrayLen, $imm);
            if ($labelDef == -1) {
                $instEnd[$i] = typeI($address, $rs, $rt, $imm, $registers, $typeI, $instr[0]);
            } else {
                $instEnd[$i] = typeI($address, $rs, $rt, $labelDef, $registers, $typeI, $instr[0]);
            }
        } elseif (array_key_exists($instr[0], $typeJ)) {
            $addr = trim(str_replace($sub, "", $instr[1]));
            $labelDef = findLabel($arrayF, $arrayLen, $addr);
            if ($labelDef == -1) {
                $instEnd[$i] = typeJ($address, $addr, $typeJ, $instr[0]);
            } else {
                $instEnd[$i] = typeJ($address, $labelDef, $typeJ, $instr[0]);
            }
        } elseif (!$labelF) {
            $output .= "\nA linha " . ($i + 1) . " não possui uma instrução válida!\n";
        }
    }
    if ($output) {
        echo "\n\n\nErros:" . $output . "\n";
    }else {
        fileWrite($file, $instEnd, $arrayLen);
    }
    fclose($file);
}
$arrayF = ($argv[1]) ? file($argv[1]) : 0;
if (!$arrayF) {
    echo "\n\n# Arquivo de dados não foi especificado pelo usuário!\n\n";
    exit(1);
}
assembler($arrayF, $typeR, $typeI, $typeJ, $registers, $argv);
</code></pre>

<p>This I found when I was looking for an assembly-to-mif "mounter". The problem is I know nothing of php (only a little of c and java)... can somebody please explain to me all tags and functions here, maybe do some analogies? I have to understand this code to make sure I see through every instruction conversion and can convert this code to java. Please, it would be a serious amount of help if some of you guys explained that.</p>
