3

我明天要考试,其中一门课是汇编中的例外。我有一个它们的列表,但是我无法找到一个简单的解释来解释什么是异常以及每个异常发生的时间。

例外情况是:

  1. 区域违规
  2. 除以0(这个很明显)
  3. 无效指令
  4. 无效地址
  5. 失败

任何信息都会有很大帮助,我只需要大致了解每个信息是什么。

4

4 回答 4

3

Normally the code flows as the instructions goes. So when you read the program, you can see where each instruction follows one after the other. The CPU follows this list. Now an exception, as the name says, interrupts this flow of execution. You already have given some examples. An execpetion occurs whenever something happens that the normal flow of instructions can not continue and it has to be dealt with. So exceptions are basically a way of notifiying the OS or the programmer that something out of the way happened. This can either be a bug in the code or it can be intentional.

For example, a division by zero is usually a bug. Accessing an invalid pointer can also be a bug, but it can also be a trigger for the OS to swap a memory page in from disk. The program gets halted, the OS makes sure that the page gets loaded and then resets the code to continue as if nothing bad happened.

Exceptions are rather similar to interrupts, because they break the regular flow of the program and have to be serviced and often you don't know when they will exactly happen.

于 2013-05-30T07:41:25.170 回答
3

严格来说,您所指的异常不是汇编语言,而是CPU的操作行为。

从概念上讲,CPU 有一组状态寄存器,使用指令流,并根据这些指令操作状态。现在,该流包含操纵状态的指令(例如,寄存器 EAX 加 1)和改变未来指令流的指令(例如 JMP topOfLoop)。

异常就像是隐式执行的跳转。例如,如果当前指令是 DIV 并且除数为零,则 CPU 可以中止指令并转而跳转到不同的指令流,这称为异常处理程序。这很有用,因为异常处理程序可以做一些事情来从坏情况中恢复。此外,每条指令都可能触发许多不同类型的异常(内存错误、保护错误、操作数错误、模式错误、对齐错误等),显式编写指令来检查所有这些条件会很麻烦。

于 2013-05-30T05:05:54.803 回答
0

当处理器无法继续正常执行程序时,基本上是程序运行能力出现异常。因此,理想情况下,它会转到异常处理程序,即一些其他代码,与中断处理程序非常相似,用于处理异常。就像中断处理程序一样,必须有人编写代码以及编写应用程序。除以零是显而易见的,无效指令也是如此,当找到的位模式与处理器支持的指令(机器代码)不匹配时,处理器将无法继续。内存故障,当内存系统由于某种原因(例如 ECC 多位错误或无效地址)而无法读取或写入内存位置时。

并非所有处理器都有异常,有些只是有一个中断,那就是“程序正常执行的异常”......

于 2013-05-30T13:50:26.410 回答
0

通常,您也可以查找中断。列表中的这些例外只是软件中断的示例。

于 2013-05-30T05:12:31.153 回答