如何在32位计算机上完成64位数学运算? [英] How is 64-bit math accomplished on a 32-bit machine?

查看:760
本文介绍了如何在32位计算机上完成64位数学运算?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果一个32位处理器的长度确实只有32位,那么数学运算如何在64位数字上工作?例如:

If a 32-bit processor is, indeed, really only 32 bits in length, then how can math operations work on 64-bit numbers? For example:

long lngTemp1 = 123456789123;
long lngTemp2 = lngTemp1 * 123;

根据MSDN,C#中的长号是带符号的64位数字: http://msdn.microsoft.com/zh-CN/library/ctetwysk(VS.71).aspx

According to MSDN, a long in C# is a signed 64-bit number: http://msdn.microsoft.com/en-us/library/ctetwysk(VS.71).aspx

32位Intel微处理器如何执行上述代码而不会发生溢出?

How is it that a 32-bit Intel Microprocessor can execute code, like the above without getting an overflow?

推荐答案

他们使用进位进行加法和减法.汇编程序带进位加法"和带进位减法"(或借用")的操作程序可用于任意位长度扩展的精度加减法.

They use the carry bit for add and subtract. The assembler ops for "add with carry" and "subtract with carry" (or "borrow") can be used for arbitrary bit length extended precision addition and subtraction.

对于乘法,如果您只从乘法中得到32位结果,则可以将其分成16位值对并进行乘法,然后移位并加(带进位)以从32位获得完整的64位结果位乘.基本上,使用长手版本(在32位结果中可以匹配任意两个16位乘法),可以使用更有限的精度来生成任意位长乘法.

For multiply, if you only have a 32-bit result from multiply, you can break it into 16-bit value pairs and multiply and then shift and add (with carry) to get a full 64-bit result from 32-bit multiply. Basically, doing the long-hand version (any two 16-bit multiplies fit in a 32-bit result) can be used to generate arbitrary bit-length multiplies using a more limited precision.

FWIW,Intel 32位asm"mul"指令可以在EDX:EAX中放入64位结果,因此您实际上可以在32位块中进行乘法(要添加64位值),而不是16-位块(具有32位值以进行移位和相加).

FWIW, the Intel 32-bit asm "mul" instruction can put a 64-bit result in EDX:EAX so you can actually do multiplies in 32-bit chunks (with 64-bit values to add) rather than 16-bit chunks (with 32-bit values to shift and add).

这篇关于如何在32位计算机上完成64位数学运算?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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