From 69f25028231e91e11fda98a5e18d814b8f7b32c8 Mon Sep 17 00:00:00 2001 From: ChenYen-Yen Date: Tue, 22 Oct 2024 16:27:49 +0800 Subject: [PATCH] update PrintInt and change PutString to PutInt --- code/build.linux/Makefile | 1 - code/machine/console.cc | 6 ++++-- code/machine/console.h | 2 +- code/machine/interrupt.cc | 5 +++++ code/threads/kernel.cc | 5 ++++- code/threads/kernel.h | 4 +++- code/userprog/ksyscall.h | 28 +--------------------------- code/userprog/synchconsole.cc | 4 ++-- code/userprog/synchconsole.h | 2 +- 9 files changed, 21 insertions(+), 36 deletions(-) diff --git a/code/build.linux/Makefile b/code/build.linux/Makefile index c253080..34161e5 100755 --- a/code/build.linux/Makefile +++ b/code/build.linux/Makefile @@ -332,7 +332,6 @@ S_OFILES = switch.o OFILES = $(C_OFILES) $(S_OFILES) $(PROGRAM): $(OFILES) - cat ../test/*.sh ../test/Makefile ../test/*.c $(LD) $(OFILES) $(LDFLAGS) -o $(PROGRAM) $(C_OFILES): %.o: diff --git a/code/machine/console.cc b/code/machine/console.cc index f122beb..a95973a 100755 --- a/code/machine/console.cc +++ b/code/machine/console.cc @@ -173,10 +173,12 @@ ConsoleOutput::PutChar(char ch) } void -ConsoleOutput::PutString(char *str) +ConsoleOutput::PutInt(int value) { ASSERT(putBusy == FALSE); - WriteFile(writeFileNo, str, strlen(str)); + char *printStr = (char*)malloc(sizeof(char)*15); + sprintf(printStr, "%d\n", value); + WriteFile(writeFileNo, printStr, strlen(printStr)*sizeof(char)); putBusy = TRUE; kernel->interrupt->Schedule(this, ConsoleTime, ConsoleWriteInt); } \ No newline at end of file diff --git a/code/machine/console.h b/code/machine/console.h index 03226e9..2047594 100755 --- a/code/machine/console.h +++ b/code/machine/console.h @@ -76,7 +76,7 @@ class ConsoleOutput : public CallBackObj { void PutChar(char ch); // Write "ch" to the console display, // and return immediately. "callWhenDone" // will called when the I/O completes. - void PutString(char *str); + void PutInt(int n); void CallBack(); // Invoked when next character can be put // out to the display. diff --git a/code/machine/interrupt.cc b/code/machine/interrupt.cc index 7c5027f..96ff68b 100755 --- a/code/machine/interrupt.cc +++ b/code/machine/interrupt.cc @@ -359,3 +359,8 @@ Interrupt::DumpState() cout << "\nEnd of pending interrupts\n"; } +void +Interrupt::PrintInt(int value) +{ + return kernel->PrintInt(value); +} \ No newline at end of file diff --git a/code/threads/kernel.cc b/code/threads/kernel.cc index 7a2bd34..e5749ce 100755 --- a/code/threads/kernel.cc +++ b/code/threads/kernel.cc @@ -308,4 +308,7 @@ int Kernel::CreateFile(char *filename) return fileSystem->Create(filename); } - +void Kernel::PrintInt(int value) +{ + return synchConsoleOut->PutInt(value); +} diff --git a/code/threads/kernel.h b/code/threads/kernel.h index 0f13c5a..9a42aad 100755 --- a/code/threads/kernel.h +++ b/code/threads/kernel.h @@ -42,7 +42,9 @@ class Kernel { void ConsoleTest(); // interactive console self test void NetworkTest(); // interactive 2-machine network test - Thread* getThread(int threadID){return t[threadID];} + Thread* getThread(int threadID){return t[threadID];} + + void PrintInt(int n); int CreateFile(char* filename); // fileSystem call diff --git a/code/userprog/ksyscall.h b/code/userprog/ksyscall.h index 27fc524..6b315f7 100755 --- a/code/userprog/ksyscall.h +++ b/code/userprog/ksyscall.h @@ -37,33 +37,7 @@ int SysCreate(char *filename) } void SysPrintInt(int value) { - static char zero[3] = "0\n"; - - if (value == 0) { - kernel->synchConsoleOut->PutString(zero); - return; - } - - char outputBuf[INT_BUF_LENGTH]; - bool isNeg = false; - int curPos = INT_BUF_LENGTH; - outputBuf[--curPos] = '\0'; - outputBuf[--curPos] = '\n'; - - if (value < 0) { - isNeg = true; - value = -value; - } - - while (value > 0) { - outputBuf[--curPos] = '0' + (value % 10); - value /= 10; - } - - if (isNeg) - outputBuf[--curPos] = '-'; - - kernel->synchConsoleOut->PutString(&outputBuf[curPos]); + kernel->interrupt->PrintInt(value); } OpenFileId SysOpen(char *name) { diff --git a/code/userprog/synchconsole.cc b/code/userprog/synchconsole.cc index b786d7f..aa0e5a6 100755 --- a/code/userprog/synchconsole.cc +++ b/code/userprog/synchconsole.cc @@ -107,10 +107,10 @@ SynchConsoleOutput::PutChar(char ch) } void -SynchConsoleOutput::PutString(char *str) +SynchConsoleOutput::PutInt(int value) { lock->Acquire(); - consoleOutput->PutString(str); + consoleOutput->PutInt(value); waitFor->P(); lock->Release(); } diff --git a/code/userprog/synchconsole.h b/code/userprog/synchconsole.h index e412ff2..9c2ff47 100755 --- a/code/userprog/synchconsole.h +++ b/code/userprog/synchconsole.h @@ -41,7 +41,7 @@ class SynchConsoleOutput : public CallBackObj { ~SynchConsoleOutput(); void PutChar(char ch); // Write a character, waiting if necessary - void PutString(char *ch); + void PutInt(int value); private: ConsoleOutput *consoleOutput;// the hardware display