added Random Int Generator

This commit is contained in:
mmichlol
2026-02-07 15:53:25 +01:00
parent 99b9ae450e
commit 5edf6ed382
4 changed files with 103 additions and 12 deletions

View File

@@ -41,19 +41,18 @@ std::string generateAssembly(const CompilerState& state) {
result += "global main\n";
result += "extern printf\n";
result += "extern getchar\n";
result += "extern _getch\n"; // ZMIANA: Dodajemy _getch (zamiast lub obok getchar)
result += "extern _getch\n";
result += "extern rand\n";
result += "extern srand\n";
result += "extern time\n";
result += "section .data\n";
result += " fmt db '%d', 10, 0\n";
result += "section .data\n";
result += " fmt_int db '%d', 10, 0\n"; // Format dla liczb
result += " fmt_str db '%s', 10, 0\n"; // NOWOŒÆ: Format dla stringów
result += " fmt_str db '%s', 10, 0\n"; // Format dla stringów
// --- WYPISYWANIE STRINGÓW ---
for (const auto& pair : state.stringLiterals) {
// Nazwa etykiety: db 'Tresc', 0
// Uwaga: ASM nie lubi pewnych znaków, ale zak³adamy proste litery
result += " " + pair.second + " db '" + pair.first + "', 0\n";
}
for (const auto& pair : state.stringLiterals) { result += " " + pair.second + " db '" + pair.first + "', 0\n"; }
result += "section .text\n\n";
for (const auto& pair : state.functions) {
@@ -88,7 +87,9 @@ std::string generateAssembly(const CompilerState& state) {
instr.type == OpType::ADD ||
instr.type == OpType::EQ ||
instr.type == OpType::SUB ||
instr.type == OpType::MUL);
instr.type == OpType::MUL ||
instr.type == OpType::DIV ||
instr.type == OpType::MOD);
if (isWriteOp && stackMap.find(instr.arg1) == stackMap.end() && instr.arg1 != "RAX") {
stackMap[instr.arg1] = currentStack;
@@ -264,9 +265,59 @@ std::string generateAssembly(const CompilerState& state) {
}
}
// ... wewn¹trz case OpType::CALL ...
if (instr.arg1 == "sys_seed") {
result += " mov rcx, 0\n";
result += " call time\n"; // Pobierz czas
result += " mov rcx, rax\n"; // Czas jako argument dla srand
result += " call srand\n"; // Inicjuj generator
break;
}
if (instr.arg1 == "sys_rand") {
result += " call rand\n"; // Wynik w EAX
break;
}
result += " call " + instr.arg1 + "\n";
break;
}
case OpType::DIV: {
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 += " cdq\n"; // Rozszerza EAX na EDX:EAX (konieczne przed dzieleniem)
// IDIV nie przyjmuje sta³ej (np. 10), musi byæ rejestr
if (isdigit(op2[0]) || op2[0] == '-') {
result += " mov ecx, " + op2 + "\n";
result += " idiv ecx\n";
}
else {
result += " idiv dword " + op2 + "\n";
}
result += " mov " + dst + ", eax\n"; // Wynik dzielenia
break;
}
case OpType::MOD: {
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 += " cdq\n"; // Rozszerza EAX
if (isdigit(op2[0]) || op2[0] == '-') {
result += " mov ecx, " + op2 + "\n";
result += " idiv ecx\n";
}
else {
result += " idiv dword " + op2 + "\n";
}
result += " mov " + dst + ", edx\n"; // EDX to reszta z dzielenia!
break;
}
}
}
@@ -277,4 +328,4 @@ std::string generateAssembly(const CompilerState& state) {
}
return result;
}
}