简体   繁体   English

数除以零是硬件异常

[英]number divide by zero is hardware exception

I have learnt during C++ exceptional handling that number divide by zero is a hardware exception.我在 C++ 异常处理期间了解到数字除以零是硬件异常。 Can anybody explain it why it is called hardware exception任何人都可以解释为什么它被称为硬件异常

Because it is not an exception in the C++ sense.因为它不是 C++ 意义上的例外。 Usually, in the C++ world, we use the word "hardware trap", to avoid any ambiguity, but "hardware exception" can also be used.通常,在 C++ 世界中,我们使用“硬件陷阱”一词来避免歧义,但也可以使用“硬件异常”。 Basically, the hardware triggers something which will cause you to land in the OS.基本上,硬件会触发一些会导致您登陆操作系统的事情。

And not all systems will generate a hardware trap for divide by 0. I've worked on one where you just got the largest possible value as a result, and kept on.并不是所有的系统都会产生除以 0 的硬件陷阱。我已经研究过一个结果,你只是得到了最大的可能值,然后继续下去。

The C++ Standard itself considers divide by zero to be Undefined Behaviour , but as usual an implementation can provide Implementation Defined Behaviour if it likes. C++ 标准本身认为除以零是Undefined Behavior ,但像往常一样,一个实现可以提供实现定义的行为,如果它喜欢的话。

C++20 stipulations: C++20规定:

7.1.4 If during the evaluation of an expression, the result is not mathematically defined or not in the range of representable values for its type, the behavior is undefined . 7.1.4 如果在对表达式求值期间,结果未在数学上定义或不在其类型的可表示值范围内,则行为未定义 [Note: Treatment of division by zero , forming a remainder using a zero divisor, and all floating-point exceptions varies among machines , and is sometimes adjustable by a library function.— end note [注意:被零除的处理,使用零除数形成余数,所有浮点异常因机器而异有时可以通过库函数进行调整。-尾注

Typically in practice, your CPU will check for divide by zero, and historically different CPU manufacturers have used different terminology for the CPU behaviour that results: some call it an "interrupt", others a "trap", or "signal", or "exception", or "fault", or "abort".通常在实践中,您的 CPU 将检查除以零,并且历史上不同的 CPU 制造商对导致的 CPU 行为使用了不同的术语:有些称之为“中断”,其他称之为“陷阱”或“信号”,或“异常”、“故障”或“中止”。 CPU designers don't tend to care about - or avoid clashes with - anything but their hardware and assembly language terminology....除了硬件和汇编语言术语之外,CPU 设计人员不倾向于关心或避免与之发生冲突。

Regardless, even if called a "hardware exception", it's nothing to do with C++ exceptions in the try / catch sense.无论如何,即使称为“硬件异常”,也与try / catch意义上的 C++ 异常无关。

On an Intel for example, a divide by zero will result in the CPU spontaneously saving a minimum of registers on the stack, then calling a function whose address must have been placed in a specific memory address beforehand.例如,在 Intel 上,除以零将导致 CPU 自发地在堆栈上保存最少的寄存器,然后调用其地址必须事先放置在特定内存地址中的函数。

It's up to the OS/executable to pick/override with some useful behaviour, and while some C++ compilers do specifically support interception of these events and generation of C++ Exceptions, it's not a feature mentioned by the C++ Standard, nor widely portable.由操作系统/可执行文件选择/覆盖一些有用的行为,虽然一些 C++ 编译器确实特别支持拦截这些事件和生成 C++ 异常,但它不是 C++ 标准提到的功能,也不是广泛可移植的。 The general expectation is that you'll either write a class that checks consistently, or perform ad-hoc checks before divisions that might fail.一般的期望是您要么编写一个始终检查的类,要么在可能失败的划分之前执行临时检查。

This is a hardware exception because it's detected by CPU.这是一个硬件异常,因为它被 CPU 检测到。

Your code in c/c++ or any other language is converted to CPU commands and then executed by CPU.您在 c/c++ 或任何其他语言中的代码被转换为 CPU 命令,然后由 CPU 执行。 So only CPU can find out you divided by zero所以只有 CPU 才能找出你除以零

It depends on your processor if you get an exception or not.如果您遇到异常,这取决于您的处理器。 Fixed point and floating point also are different or can be.定点和浮点也不同或可以。 The floating point spec, to be compliant, has both an exception and non-exception solution for devide by zero.为了符合要求,浮点规范具有除以零的异常和非异常解决方案。 If the fpu has that exception disabled then you would get the "properly signed infinity" otherwise you get an exception and the result is instead a nan or something like that I dont have the spec handy.如果 fpu 禁用了该异常,那么您将获得“正确签名的无穷大”,否则您将获得一个异常,结果是 nan 或类似的东西,我没有手头的规范。

The programmers reference manual for a particular processor should hopefully discuss fixed point divide by zero behavior if the processor has a divide at all.如果处理器有除法,则特定处理器的程序员参考手册应该有希望讨论定点除以零的行为。 If not then it is a soft divide and then it is up to the compiler library as to what it does (calls a divide by zero handler for example).如果不是,则它是软除法,然后由编译器库决定它的作用(例如调用除以零处理程序)。

It would be called a hardware exception in general because the hardware is detecting the problem, and the hardware does something as a result.它通常被称为硬件异常,因为硬件正在检测问题,因此硬件会执行某些操作。 Same thing when you have other problems like mmu access faults, data aborts, prefetch aborts, etc. hardware exception because it is an exception handled by hardware, generally...当您遇到其他问题(例如 mmu 访问错误、数据中止、预取中止等)时也是如此。硬件异常,因为它是由硬件处理的异常,通常......

Because, if it is checked, then it is checked and raised by the hardware.因为,如果它被选中,那么它就会被硬件选中并引发。 Specifically, the Arithmetic-Logic Unit (ALU) of your CPU will check for 0 as divider and generate an appropriate interrupt to signal the exception.具体来说,CPU 的算术逻辑单元 (ALU) 将检查 0 作为分频器并生成适当的中断以发出异常信号。

Otherwise, you would have to explicitely check for 0 in the assembler source code.否则,您必须在汇编源代码中明确检查 0。

Edit: Note that this apply to integer division only, since floating point division has specific states to signal a division by zero.编辑:请注意,这仅适用于整数除法,因为浮点除法具有特定状态来表示除以零。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM