int main () { int i1 = 20, i2 = 4, i3; float f1 = 1.5,f2 = 0.3,f3; i3 = _add(i1, i2); printf("add(%d, %d)=%d\n", i1, i2, i3); i3 = _sub(i1, i2); printf("sub(%d, %d)=%d\n", i1, i2, i3); i3 = _mul(i1, i2); printf("mul(%d, %d)=%d\n", i1, i2, i3); i3 = _div(i1, i2); printf("div(%d, %d)=%d\n", i1, i2, i3); f3 = _fadd(f1, f2); printf("fadd(%f, %f)=%f\n", f1, f2, f3); f3 = _fsub(f1, f2); printf("fsub(%f, %f)=%f\n", f1, f2, f3); f3 = _fmul(f1, f2); printf("fmul(%f, %f)=%f\n", f1, f2, f3); f3 = _fdiv(f1, f2); printf("fdiv(%f, %f)=%f\n", f1, f2, f3); return 0; }
//----------------------------------------------------------------------------- // // 命令コマンドを解釈してバイナリに変換 // //----------------------------------------------------------------------------- bool encode(char* instName, char* buffer, map<uint32_t, string>& labelNames, uint32_t currentLine, uint32_t& code, bool& useLabel) { uint32_t rs = 0; uint32_t rt = 0; uint32_t rd = 0; uint32_t imm = 0; double d = 0; char label[MAX_LINE_SIZE]; char dummy[MAX_LINE_SIZE]; if (eq(instName, "add")) { int n = sscanf(buffer, formRRR, dummy, &rd, &rs, &rt); if (n == 4) { code = _add(rs, rt, rd); return true; } } if (eq(instName, "sub")) { int n = sscanf(buffer, formRRR, dummy, &rd, &rs, &rt); if (n == 4) { code = _sub(rs, rt, rd); return true; } } if (eq(instName, "mul")) { int n = sscanf(buffer, formRRR, dummy, &rd, &rs, &rt); if (n == 4) { code = _mul(rs, rt, rd); return true; } } if (eq(instName, "and")) { int n = sscanf(buffer, formRRR, dummy, &rd, &rs, &rt); if (n == 4) { code = _and(rs, rt, rd); return true; } } if (eq(instName, "or")) { int n = sscanf(buffer, formRRR, dummy, &rd, &rs, &rt); if (n == 4) { code = _or(rs, rt, rd); return true; } } if (eq(instName, "nor")) { int n = sscanf(buffer, formRRR, dummy, &rd, &rs, &rt); if (n == 4) { code = _nor(rs, rt, rd); return true; } } if (eq(instName, "xor")) { int n = sscanf(buffer, formRRR, dummy, &rd, &rs, &rt); if (n == 4) { code = _xor(rs, rt, rd); return true; } } if (eq(instName, "addi")) { int n = sscanf(buffer, formRRI, dummy, &rt, &rs, &imm); if (n == 4) { code = _addi(rs, rt, imm); return true; } } if (eq(instName, "subi")) { int n = sscanf(buffer, formRRI, dummy, &rt, &rs, &imm); if (n == 4) { code = _subi(rs, rt, imm); return true; } } if (eq(instName, "muli")) { int n = sscanf(buffer, formRRI, dummy, &rt, &rs, &imm); if (n == 4) { code = _muli(rs, rt, imm); return true; } } if (eq(instName, "slli")) { int n = sscanf(buffer, formRRI, dummy, &rt, &rs, &imm); if (n == 4) { code = _slli(rs, rt, imm); return true; } } if (eq(instName, "srai")) { int n = sscanf(buffer, formRRI, dummy, &rt, &rs, &imm); if (n == 4) { code = _srai(rs, rt, imm); return true; } } if (eq(instName, "andi")) { int n = sscanf(buffer, formRRI, dummy, &rt, &rs, &imm); if (n == 4) { code = _andi(rs, rt, imm); return true; } } if (eq(instName, "ori")) { int n = sscanf(buffer, formRRI, dummy, &rt, &rs, &imm); if (n == 4) { code = _ori(rs, rt, imm); return true; } } if (eq(instName, "nori")) { int n = sscanf(buffer, formRRI, dummy, &rt, &rs, &imm); if (n == 4) { code = _nori(rs, rt, imm); return true; } } if (eq(instName, "xori")) { int n = sscanf(buffer, formRRI, dummy, &rt, &rs, &imm); if (n == 4) { code = _xori(rs, rt, imm); return true; } } if (eq(instName, "fadd")) { int n = sscanf(buffer, formFFF, dummy, &rd, &rs, &rt); if (n == 4) { code = _fadd(rs, rt, rd); return true; } } if (eq(instName, "fsub")) { int n = sscanf(buffer, formFFF, dummy, &rd, &rs, &rt); if (n == 4) { code = _fsub(rs, rt, rd); return true; } } if (eq(instName, "fmul")) { int n = sscanf(buffer, formFFF, dummy, &rd, &rs, &rt); if (n == 4) { code = _fmul(rs, rt, rd); return true; } } if (eq(instName, "fmuln")) { int n = sscanf(buffer, formFFF, dummy, &rd, &rs, &rt); if (n == 4) { code = _fmuln(rs, rt, rd); return true; } } if (eq(instName, "finv")) { int n = sscanf(buffer, formFF, dummy, &rd, &rs); if (n == 3) { code = _finv(rs, rt, rd); return true; } } if (eq(instName, "fsqrt")) { int n = sscanf(buffer, formFF, dummy, &rd, &rs); if (n == 3) { code = _fsqrt(rs, rt, rd); return true; } } if (eq(instName, "fmov")) { int n = sscanf(buffer, formFF, dummy, &rd, &rs); if (n == 3) { code = _fmov(rs, rt, rd); return true; } } if (eq(instName, "fneg")) { int n = sscanf(buffer, formFF, dummy, &rd, &rs); if (n == 3) { code = _fneg(rs, rt, rd); return true; } } if (eq(instName, "imovf")) { int n = sscanf(buffer, formFR, dummy, &rt, &rs); if (n == 3) { code = _imovf(rs, rt, rd); return true; } } if (eq(instName, "fmovi")) { int n = sscanf(buffer, formRF, dummy, &rt, &rs); if (n == 3) { code = _fmovi(rs, rt, rd); return true; } } // to use ALU if (eq(instName, "mvlo")) { int n = sscanf(buffer, formRI, dummy, &rt, &imm); if (n == 3) { code = _mvlo(rt, rt, imm); return true; } } if (eq(instName, "mvhi")) { int n = sscanf(buffer, formRI, dummy, &rt, &imm); if (n == 3) { code = _mvhi(rt, rt, imm); return true; } } if (eq(instName, "fmvlo")) { int n = sscanf(buffer, formFI, dummy, &rt, &imm); if (n == 3) { code = _fmvlo(rt, rt, imm); return true; } } if (eq(instName, "fmvhi")) { int n = sscanf(buffer, formFI, dummy, &rt, &imm); if (n == 3) { code = _fmvhi(rt, rt, imm); return true; } } if (eq(instName, "j")) { int n = sscanf(buffer, formL, dummy, label); if (n == 2) { labelNames[currentLine] = string(label); // cerr << "assigned (" << currentLine << ", " << string(label) << ") in labelNames" << endl; useLabel = true; code = _j(0); return true; } } if (eq(instName, "beq")) { int n = sscanf(buffer, formRRL, dummy, &rs, &rt, label); if (n == 4) { labelNames[currentLine] = string(label); // cerr << "assigned (" << currentLine << ", " << string(label) << ") in labelNames" << endl; useLabel = true; code = _beq(rs, rt, imm); return true; } } if (eq(instName, "blt")) { int n = sscanf(buffer, formRRL, dummy, &rs, &rt, label); if (n == 4) { labelNames[currentLine] = string(label); // cerr << "assigned (" << currentLine << ", " << string(label) << ") in labelNames" << endl; useLabel = true; code = _blt(rs, rt, imm); return true; } } if (eq(instName, "ble")) { int n = sscanf(buffer, formRRL, dummy, &rs, &rt, label); if (n == 4) { labelNames[currentLine] = string(label); // cerr << "assigned (" << currentLine << ", " << string(label) << ") in labelNames" << endl; useLabel = true; code = _ble(rs, rt, imm); return true; } } if (eq(instName, "fbeq")) { int n = sscanf(buffer, formFFL, dummy, &rs, &rt, label); if (n == 4) { labelNames[currentLine] = string(label); // cerr << "assigned (" << currentLine << ", " << string(label) << ") in labelNames" << endl; useLabel = true; code = _fbeq(rs, rt, imm); return true; } } if (eq(instName, "fblt")) { int n = sscanf(buffer, formFFL, dummy, &rs, &rt, label); if (n == 4) { labelNames[currentLine] = string(label); // cerr << "assigned (" << currentLine << ", " << string(label) << ") in labelNames" << endl; useLabel = true; code = _fblt(rs, rt, imm); return true; } } if (eq(instName, "fble")) { int n = sscanf(buffer, formFFL, dummy, &rs, &rt, label); if (n == 4) { labelNames[currentLine] = string(label); // cerr << "assigned (" << currentLine << ", " << string(label) << ") in labelNames" << endl; useLabel = true; code = _fble(rs, rt, imm); return true; } } if (eq(instName, "jr")) { int n = sscanf(buffer, formR, dummy, &rs); if (n == 2) { code = _jr(rs, rt, rd); return true; } } if (eq(instName, "call")) { int n = sscanf(buffer, formL, dummy, label); if (n == 2) { labelNames[currentLine] = string(label); // cerr << "assigned (" << currentLine << ", " << string(label) << ") in labelNames" << endl; useLabel = true; code = _call(0); return true; } } if (eq(instName, "callr")) { int n = sscanf(buffer, formR, dummy, &rs); if (n == 2) { code = _callr(rs, rt, rd); return true; } } if (eq(instName, "return")) { int n = sscanf(buffer, form, dummy); if (n == 1) { code = _return(rs, rt, rd); return true; } } if (eq(instName, "ldr")) { int n = sscanf(buffer, formRRR, dummy, &rd, &rs, &rt); if (n == 4) { code = _ldr(rs, rt, rd); return true; } } if (eq(instName, "fldr")) { int n = sscanf(buffer, formFRR, dummy, &rd, &rs, &rt); if (n == 4) { code = _fldr(rs, rt, rd); return true; } } if (eq(instName, "sti")) { int n = sscanf(buffer, formRRI, dummy, &rt, &rs, &imm); if (n == 4) { code = _sti(rs, rt, imm); return true; } } if (eq(instName, "ldi")) { int n = sscanf(buffer, formRRI, dummy, &rt, &rs, &imm); if (n == 4) { code = _ldi(rs, rt, imm); return true; } } if (eq(instName, "fsti")) { int n = sscanf(buffer, formFRI, dummy, &rt, &rs, &imm); if (n == 4) { code = _fsti(rs, rt, imm); return true; } } if (eq(instName, "fldi")) { int n = sscanf(buffer, formFRI, dummy, &rt, &rs, &imm); if (n == 4) { code = _fldi(rs, rt, imm); return true; } } if (eq(instName, "inputb")) { int n = sscanf(buffer, formR, dummy, &rt); if (n == 2) { code = _inputb(rs, rt, rd); return true; } } if (eq(instName, "outputb")) { int n = sscanf(buffer, formR, dummy, &rt); if (n == 2) { code = _outputb(rs, rt, rd); return true; } } if (eq(instName, "halt")) { int n = sscanf(buffer, form, dummy); if (n == 1) { code = _halt(rs, rt, rd); return true; } } if (eq(instName, "dump")) { int n = sscanf(buffer, form, dummy); if (n == 1) { code = _dump(rs, rt, rd); return true; } } return false; }