Update all syscall

And add PutInt() and GetInt()
This commit is contained in:
Yorick Barbanneau 2021-10-15 02:04:09 +02:00
parent 2b7c7ee780
commit 2a3198c949
7 changed files with 95 additions and 31 deletions

View file

@ -41,8 +41,8 @@ CFLAGS += -fsanitize=undefined
LDFLAGS += -fsanitize=undefined
# décommenté TD1 - partie V
#CFLAGS += -fsanitize=address
#LDFLAGS += -fsanitize=address -lpthread
CFLAGS += -fsanitize=address
LDFLAGS += -fsanitize=address -lpthread
endif
ifeq ($(NACHOS_SYS),MAC_OS_SYS)

View file

@ -181,6 +181,24 @@ GetString:
syscall
j $31
.end GetString
.globl PutInt
.ent PutInt
PutInt:
addiu $2,$0,SC_PutInt
syscall
j $31
.end PutInt
.globl GetInt
.ent GetInt
GetInt:
addiu $2,$0,SC_GetInt
syscall
j $31
.end GetInt
#endif
/* dummy function to keep gcc happy */

View file

@ -11,7 +11,6 @@
#include "copyright.h"
#include "system.h"
//----------------------------------------------------------------------
// SimpleThread
// Loop 10 times, yielding the CPU to another ready thread

View file

@ -37,22 +37,22 @@ int ConsoleDriver::GetChar()
return ch;
}
void ConsoleDriver::PutString(const char s[MAX_STRING_SIZE])
void ConsoleDriver::PutString(const char s[])
{
int i;
for (i = 0; i < MAX_STRING_SIZE; i++){
if (*(s+i) =='\0'){
return;
}
int i = 0;
while (*(s+i) != '\0')
{
PutChar(*(s+i));
i++;
}
}
void ConsoleDriver::GetString(char * s, int n)
{
int i;
for ( i = 0; i < n - 1; i++){
for ( i = 0; i < n; i++){
char c = GetChar();
DEBUG('s', "consoledriver->getstring keycode:%d, position:%d\n", (int)c, i);
if ( c == '\0' || c == '\n' || c == EOF) {
break;
}
@ -61,4 +61,22 @@ void ConsoleDriver::GetString(char * s, int n)
*(s+i) = '\0';
}
void ConsoleDriver::PutInt(int n)
{
char *buffer = new char[MAX_STRING_SIZE];
snprintf(buffer,MAX_STRING_SIZE,"%d",n);
DEBUG('s', "consoledriver->PutInt buffer:%s\n", buffer);
PutString(buffer);
delete [] buffer;
}
int ConsoleDriver::GetInt()
{
int n;
char *buffer = new char[MAX_STRING_SIZE];
GetString(buffer,MAX_STRING_SIZE);
sscanf(buffer, "%d", &n);
delete [] buffer;
return n;
}
#endif // CHANGED

View file

@ -18,6 +18,8 @@ class ConsoleDriver:dontcopythis {
void PutString(const char *s); // Behaves like fputs(3S)
void GetString(char *s, int n); // Behaves like fgets(3S)
void PutInt(int n);
int GetInt();
private:
Console *console;
};

View file

@ -69,7 +69,8 @@ int copyStringFromMachine(int from, char *to, unsigned size)
int res;
// Need to read size-1 to put final /0 if needed
while((i<size-1) && (machine->ReadMem(from+i,1,&res))){
while((i < size) && (machine->ReadMem(from+i,1,&res))){
DEBUG('s', "\n ->copyFromMachine keycode:%d", res);
*(to+i) = (char)res;
if ((char)res == '\0'){
return i;
@ -83,7 +84,8 @@ int copyStringFromMachine(int from, char *to, unsigned size)
int copyStringToMachine(int to, char* from, unsigned size)
{
unsigned i;
for (i=0; i < size - 1; i++) {
for (i=0; i < size ; i++) {
DEBUG('s', "\n ->copyToMachine keycode:%d", (int)from[i]);
if (from[i] == '\0') {
break;
}
@ -134,14 +136,11 @@ ExceptionHandler (ExceptionType which)
int readsize;
int addr_start = machine->ReadRegister(4);
do {
char* read = new char[MAX_STRING_SIZE];
char *read = new char[MAX_STRING_SIZE + 1];
readsize = copyStringFromMachine( addr_start, read, MAX_STRING_SIZE);
DEBUG('s', "\nRead buffer:%s, size:%d\n", read, readsize);
consoledriver->PutString(read);
// beware, the last element of our buffer was added by
// copyStringFromMachine(), we need to remove one memory case
// or the first char of our next read will be
// forgotten
addr_start += readsize - 1;
addr_start += readsize ;
delete [] read;
} while(readsize == MAX_STRING_SIZE);
break;
@ -151,32 +150,55 @@ ExceptionHandler (ExceptionType which)
DEBUG ('s', "GetChar.\n");
// Get the char input an write it to the register n.2
int c = consoledriver->GetChar();
machine->WriteRegister((2), c);
machine->WriteRegister(2, c);
break;
}
case SC_GetString:
{
DEBUG ('s', "GetString.\n");
DEBUG('s', "Begin GetString.\n");
int writesize = 0;
int addr = machine->ReadRegister(4);
int size = machine->ReadRegister(5);
DEBUG ('s', "size:%d\n", size);
DEBUG('s', "size:%d\n", size);
do {
char* write = new char[MAX_STRING_SIZE];
consoledriver->GetString(write, MAX_STRING_SIZE);
DEBUG('s', "write:%s:\n", write);
writesize = copyStringToMachine(addr, write, MAX_STRING_SIZE);
// Do not process char before size
// but for now we don't have any method to determine if
// string passed to our function est bigger than expected (size)
int n_char = ( size < MAX_STRING_SIZE) ? size : MAX_STRING_SIZE;
// allocate on byte more than buffer to put our \0 if needed
char *write = new char[n_char + 1];
consoledriver->GetString(write, n_char);
writesize = copyStringToMachine(addr, write, n_char);
addr += writesize;
DEBUG('s', "Writed:%d\n", writesize);
size = size - writesize;
DEBUG('s', "writed:%s, size:%d, remain:%d\n", write, writesize, size);
delete [] write;
} while( writesize == MAX_STRING_SIZE - 1 );
} while( size > 0 && writesize == MAX_STRING_SIZE );
DEBUG('s', "End GetString\n");
break;
}
case SC_PutInt:
{
DEBUG('s', "PutInt.\n");
int n = machine->ReadRegister(4);
DEBUG('s', "PutInt parameter: %d\n", n);
consoledriver->PutInt(n);
}
break;
case SC_GetInt:
{
DEBUG('s', "GetInt\n");
int n = consoledriver->GetInt();
DEBUG('s', "Number entered: %d\n", n);
machine->WriteRegister(2,n);
break;
}
#endif
default:
{
printf("Unimplemented system call %d\n", type);
printf("Unimplemented system call %d\n", type);
ASSERT(FALSE);
}
}

View file

@ -36,6 +36,8 @@
#define SC_PutString 12
#define SC_GetChar 13
#define SC_GetString 14
#define SC_PutInt 15
#define SC_GetInt 16
#endif
#ifdef IN_USER_MODE
@ -137,9 +139,12 @@ void Yield ();
#ifdef CHANGED
void PutChar (char c);
void PutString (char * s);
void GetChar(char c);
void GetString(char * s, int n );
void PutString (char *s);
char GetChar();
void GetString(char *s, int n );
void PutInt(int n);
int GetInt();
#endif // CHANGED
#endif // IN_USER_MODE