⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 trap.c

📁 计算机系统结构的讲义,浓缩了一本一千多页的书.真的是好东西.
💻 C
📖 第 1 页 / 共 2 页
字号:
    if (!(firstAddr & 0x3)) {      word = (++wordPtr)->value;      bytes[3] = (word & 0xff);      bytes[2] = ((word >>= 8) & 0xff);      bytes[1] = ((word >>= 8) & 0xff);      bytes[0] = ((word >> 8) & 0xff);    }  }  if (first >= endMem) {    printf("Memory reference out of range\n");    return 1;  }  return 0;}/* *---------------------------------------------------------------------- * * Handle_Open -- * *	Handle the open system call. * * Results: *	The return value is 0 if no error occured, 1 if a problem *      arises. * * Side effects: *      The fd_map may be changed. * *---------------------------------------------------------------------- */static intHandle_Open(machPtr)DLX *machPtr;{  int where, path, result;  for (where = 0; where < FD_SIZE; where++)    if (machPtr->fd_map[where] == -1)      break;  if (where == FD_SIZE) {    /* no free file descriptors */    Set_Errno(machPtr, EMFILE);    RETURN_VALUE = -1;    return 0;  }  path = ARG(0);  if (String_To_Scratch(machPtr, path))    return 1;  result = open(SCRATCH_ADDR(path), ARG(1), ARG(2));  if (result == -1) {    Set_Errno(machPtr, errno);    RETURN_VALUE = -1;  } else {    machPtr->fd_map[where] = result;    RETURN_VALUE = where;  }  return 0;}/* *---------------------------------------------------------------------- * * Handle_Close -- * *	Handle the close system call. * * Results: *	The return value is 0 if no error occured, 1 if a problem *      arises. * * Side effects: *      The fd_map may be changed. * *---------------------------------------------------------------------- */static intHandle_Close(machPtr)DLX *machPtr;{  int result, fd;  fd = ARG(0);  if ((fd < 0) || (fd > FD_SIZE) || (machPtr->fd_map[fd] == -1)) {    /* illegal file descriptor */    Set_Errno(machPtr, EBADF);    RETURN_VALUE = -1;    return 0;  }  result = close(machPtr->fd_map[fd]);  if (result == -1) {    Set_Errno(machPtr, errno);    RETURN_VALUE = -1;  } else {    RETURN_VALUE = result;    machPtr->fd_map[fd] = -1;  }  return 0;}/* *---------------------------------------------------------------------- * * Handle_Read -- * *	Handle the read system call. * * Results: *	The return value is 0 if no error occured, 1 if a problem *      arises. * * Side effects: *      The simulator memory and scratch pad may be changed. * *---------------------------------------------------------------------- */static intHandle_Read(machPtr)DLX *machPtr;{  int result, fd, buf, nbytes;  fd = ARG(0);  buf = ARG(1);  nbytes = ARG(2);  if ((fd < 0) || (fd > FD_SIZE) || (machPtr->fd_map[fd] == -1)) {    /* illegal file descriptor */     Set_Errno(machPtr, EBADF);    RETURN_VALUE = -1;    return 0;  }  if ((buf < 0) || (buf >= machPtr->numChars)) {    printf("Memory reference out of range\n");    return 1;  }  if ((nbytes < 0) || (buf + nbytes - 1 >= machPtr->numChars)) {    printf("Memory reference out of range\n");    return 1;  }  result = read(machPtr->fd_map[fd], SCRATCH_ADDR(buf), nbytes);  if (result == -1) {    Set_Errno(machPtr, errno);    RETURN_VALUE = -1;  } else {    if (Block_From_Scratch(machPtr, buf, result))      return 1;    RETURN_VALUE = result;  }  return 0;}/* *---------------------------------------------------------------------- * * Handle_Write -- * *	Handle the write system call. * * Results: *	The return value is 0 if no error occured, 1 if a problem *      arises. * * Side effects: *      The scratch pad may be changed. * *---------------------------------------------------------------------- */int Handle_Write(machPtr)DLX *machPtr;{  int result, fd, buf, nbytes;  fd = ARG(0);  buf = ARG(1);  nbytes = ARG(2);  if ((fd < 0) || (fd > FD_SIZE) || (machPtr->fd_map[fd] == -1)) {    /* illegal file descriptor */    Set_Errno(machPtr, EBADF);    RETURN_VALUE = -1;    return 0;  }  if (Block_To_Scratch(machPtr, buf, nbytes))    return 1;  result = write(machPtr->fd_map[fd], SCRATCH_ADDR(buf), nbytes);  if (result == -1) {    Set_Errno(machPtr, errno);    RETURN_VALUE = -1;  } else {    RETURN_VALUE = result;  }  return 0;}/* *---------------------------------------------------------------------- * * Handle_Printf -- * *	Handle the printf system call.  This is done by scanning *      through the format string provided, and sending each % command *      and following plain text to the printf library function (with *      the appropriate argument). * * Results: *	The return value is 0 if no error occured, 1 if a problem *      arises. * * Side effects: *      The scratch pad may be changed. * *---------------------------------------------------------------------- */static intHandle_Printf(machPtr)DLX *machPtr;{  char *start, *end, *s, temp;  int fmt = ARG(0), argNumber = 1, ta[2], loc, done, result = 0;  double *pd = (double *)&(ta[0]);  /* convert the format string */  if (String_To_Scratch(machPtr, fmt))    return 1;  /* now break the format string into blocks of characters beginning with   * either the start of the format string, or a % command, and continuing   * through either the start of the next % command, or the end of the   * string.  */  for (start = SCRATCH_ADDR(fmt); *start; start = end) {    if (*start == '%') {      if (start[1] == '%')	end = start + 2;      else	end = start + 1;    } else      end = start;    for ( ; *end && (*end != '%'); end++)     /* find the end of this block */      ;    temp = *end;    *end = '\0';    if (*start != '%')      result += printf(start);    else {      /* find the type of argument this block takes (if any) */      for (done = 0, s = start + 1; *s && !done; s++)	switch (*s) {	case 'c' :	case 'd' :	case 'o' :	case 'u' :	case 'x' :	case 'X' :	  result += printf(start, ARG(argNumber++));	  done = 1;	  break;	case 'f' :	case 'e' :	case 'E' :	case 'g' :	case 'G' :	  ta[0] = ARG(argNumber++);	  ta[1] = ARG(argNumber++);	  result += printf(start, *pd);	  done = 1;	  break;	case 's' :	  loc = ARG(argNumber++);	  if (String_To_Scratch(machPtr, loc))	    return 1;	  result += printf(start, SCRATCH_ADDR(loc));	  done = 1;	  break;	case '%' :	  result += printf(start);	  done = 1;	  break;	}      if (!done)    /* no % command found */	result += printf(start);    }    *end = temp;  }  RETURN_VALUE = result;  return 0;}/* *---------------------------------------------------------------------- * * Set_Errno * *	Set the _error variable (if it exists) * * Results: *      None. * * Side effects: *      See the description above * *---------------------------------------------------------------------- */static voidSet_Errno(machPtr, errorNumber)DLX *machPtr;int errorNumber;{  char *p, *end;  int addr, index;  p = Tcl_GetVar(machPtr->interp, "_errno", TCL_LEAVE_ERR_MSG);  if (p != NULL) {    addr = strtoul(p, &end, 0);    if (*end != 0)      return;    index = ADDR_TO_INDEX(addr);    if (index < machPtr->numWords)      machPtr->memPtr[index].value = errorNumber;  }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -