Update putint and getint

And rapport for TD1
This commit is contained in:
Yorick Barbanneau 2021-10-19 08:19:21 +02:00
parent e2df8860bd
commit 26f9a4226f
4 changed files with 84 additions and 41 deletions

View file

@ -68,17 +68,23 @@ int copyStringFromMachine(int from, char *to, unsigned size)
unsigned i = 0;
int res;
// Need to read size-1 to put final /0 if needed
while((i < size) && (machine->ReadMem(from+i,1,&res))){
// Need to read `size` value into memory to put final /0 if needed
for(i=0; i < size; i++){
// What should we do if a problem occurs when accessing
// memory?
if (machine->ReadMem(from+i,1,&res) == false){
DEBUG('s', "Error When reading memory in copyStringFromMachine\n");
return -1;
}
DEBUG('s', "\n ->copyFromMachine keycode:%d", res);
*(to+i) = (char)res;
if ((char)res == '\0'){
return i;
}
i++;
*(to+i) = (char)res;
if ((char)res == '\0'){
return i;
}
}
*(to+i)='\0';
return size;
*(to+size)='\0';
return i;
}
int copyStringToMachine(int to, char* from, unsigned size)
@ -89,7 +95,10 @@ int copyStringToMachine(int to, char* from, unsigned size)
if (from[i] == '\0') {
break;
}
machine->WriteMem(to+i,1,(int)from[i]);
if ( machine->WriteMem(to+i,1,(int)from[i]) == false) {
DEBUG('s', "Error when writing memory in copyStringFromMachine\n");
return -1;
}
}
// Write the last /0
@ -148,7 +157,7 @@ ExceptionHandler (ExceptionType which)
case SC_GetChar:
{
DEBUG ('s', "GetChar.\n");
// Get the char input an write it to the register n.2
// Get the char input and write it to the register n.2
int c = consoledriver->GetChar();
machine->WriteRegister(2, c);
break;
@ -165,9 +174,11 @@ ExceptionHandler (ExceptionType which)
// 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)
// It seems to be easier to check than in userspace mode
int n_char = ( size < MAX_STRING_SIZE) ? size : MAX_STRING_SIZE;
// allocate on byte more than buffer to put our \0 if needed
// allocate on byte more than buffer to put our \0 at the end,
// if needed
char *write = new char[n_char + 1];
consoledriver->GetString(write, n_char);
writesize = copyStringToMachine(addr, write, n_char);