Loop Update
This commit is contained in:
@@ -89,7 +89,9 @@ std::string generateAssembly(const CompilerState& state) {
|
||||
instr.type == OpType::SUB ||
|
||||
instr.type == OpType::MUL ||
|
||||
instr.type == OpType::DIV ||
|
||||
instr.type == OpType::MOD);
|
||||
instr.type == OpType::MOD ||
|
||||
instr.type == OpType::LOGIC_AND ||
|
||||
instr.type == OpType::LOGIC_OR);
|
||||
|
||||
if (isWriteOp && stackMap.find(instr.arg1) == stackMap.end() && instr.arg1 != "RAX") {
|
||||
stackMap[instr.arg1] = currentStack;
|
||||
@@ -182,6 +184,11 @@ std::string generateAssembly(const CompilerState& state) {
|
||||
}
|
||||
break;
|
||||
}
|
||||
case OpType::JMP: {
|
||||
// Skok bezwarunkowy (u¿ywany na koñcu pêtli while)
|
||||
result += " jmp " + instr.arg1 + "\n";
|
||||
break;
|
||||
}
|
||||
case OpType::LABEL: {
|
||||
result += instr.arg1 + ":\n";
|
||||
break;
|
||||
@@ -318,6 +325,61 @@ std::string generateAssembly(const CompilerState& state) {
|
||||
result += " mov " + dst + ", edx\n"; // EDX to reszta z dzielenia!
|
||||
break;
|
||||
}
|
||||
case OpType::LOGIC_AND: {
|
||||
// a && b
|
||||
// Algorytm: result = (op1 != 0) * (op2 != 0)
|
||||
// Ale w ASM ³atwiej:
|
||||
// cmp op1, 0 -> setne al
|
||||
// cmp op2, 0 -> setne bl
|
||||
// and al, bl
|
||||
|
||||
std::string op1 = getVarLocation(instr.arg2, stackMap);
|
||||
std::string op2 = getVarLocation(instr.arg3, stackMap);
|
||||
std::string dst = getVarLocation(instr.arg1, stackMap);
|
||||
|
||||
// Sprawdzamy op1
|
||||
result += " mov eax, " + op1 + "\n";
|
||||
result += " cmp eax, 0\n";
|
||||
result += " setne al\n"; // al = 1 jeœli eax != 0
|
||||
|
||||
// Sprawdzamy op2 (u¿ywamy ECX jako pomocniczy)
|
||||
if (isdigit(op2[0])) result += " mov ecx, " + op2 + "\n";
|
||||
else result += " mov ecx, " + op2 + "\n";
|
||||
|
||||
result += " cmp ecx, 0\n";
|
||||
result += " setne cl\n"; // cl = 1 jeœli ecx != 0
|
||||
|
||||
// Logiczne AND na bajtach
|
||||
result += " and al, cl\n";
|
||||
|
||||
// Zapisujemy wynik (rozszerzamy bajt do dword)
|
||||
result += " movzx eax, al\n";
|
||||
result += " mov " + dst + ", eax\n";
|
||||
break;
|
||||
}
|
||||
case OpType::LOGIC_OR: {
|
||||
// a || b
|
||||
std::string op1 = getVarLocation(instr.arg2, stackMap);
|
||||
std::string op2 = getVarLocation(instr.arg3, stackMap);
|
||||
std::string dst = getVarLocation(instr.arg1, stackMap);
|
||||
|
||||
result += " mov eax, " + op1 + "\n";
|
||||
result += " cmp eax, 0\n";
|
||||
result += " setne al\n";
|
||||
|
||||
if (isdigit(op2[0])) result += " mov ecx, " + op2 + "\n";
|
||||
else result += " mov ecx, " + op2 + "\n";
|
||||
|
||||
result += " cmp ecx, 0\n";
|
||||
result += " setne cl\n";
|
||||
|
||||
// Logiczne OR
|
||||
result += " or al, cl\n";
|
||||
|
||||
result += " movzx eax, al\n";
|
||||
result += " mov " + dst + ", eax\n";
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user