当我除以零会怎样? [英] What happens when I divide by zero?

查看:92
本文介绍了当我除以零会怎样?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在我不是在问除以零的数学答案,而是想知道计算机尝试除以零时的处理方式吗?



我可以根据我们所处的水平看到不同的答案?
从较高的角度看,我希望语言规范中可能只说嘿,你不能这样做会引发错误。



在汇编程序级别看,当我们尝试除以0时,CPU会尝试调用除法指令吗?



如果这样做,将带我们进入机器代码级别。现在会发生什么?



现在,如果那没有发生并且我们强迫发生,那么结果将是什么?

解决方案


看一下汇编级别,当我们尝试除以0时,CPU是否会尝试调用除法指令?


Yep指令被获取并解码。那会发生什么呢?发现除数为零,并且该过程停止执行其操作。它将引发某种异常,流水线(如果有的话)将被刷新,并且很有可能将控制跳转到一些预定义的错误处理代码-操作系统通常会控制称为中断向量的机器级跳转表(或者可以将其与中断向量表)。



但是有许多体系结构,诸如错误处理之类的东西也有所不同。英特尔x86至少遵循上述步骤。现在会发生什么?


我不知道那是什么意思。从CPU的角度来看,这是所有机器代码级别。


Now I'm not asking about the mathematical answer of dividing by zero, I want to what my computer does when it tries to divide by zero?

I can see different answers depending on what level we look at? Looking at a high-level I expect the language specification may just say "hey you can't do that throw an error"

Looking at an assembly level, will the CPU try to call the divide instruction when we try to divide by 0?

If it does that'll take us to the machine code level. What happens now?

Now if that doesn't happen and we force it to happen, what would the result be?

解决方案

Looking at an assembly level, will the CPU try to call the divide instruction when we try to divide by 0?

Yep instruction gets fetched and decoded. What happens then? The divisor is found to be zero and the process stops doing what it's doing. It throws some kind of an exception, pipeline (if there is one) is flushed and most likely control jumps to some predefined error handling code - often the OS controls a machine level jump table called an interrupt vector (or this could be a separate to the interrupt vector table).

There are many architectures however, and things like error handling vary. Intel x86 follows above procedure at least.

If it does that'll take us to the machine code level. What happens now?

I have no idea what that means. From the CPU's perspective, it is all machine code level.

这篇关于当我除以零会怎样?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆