How to multiply by 2^n in assembly?

拟墨画扇 提交于 2020-01-30 08:08:40

问题


Given 2 numbers, x and n, what's the method to multiply x by 2^n ? For instance x=3.7 and n=5. so 3.7*2^5 = 118.4. I need this done without using the FPU commands (math coprocessor).

So i figured that numbers in 32 bt processor are represented by 32 bits: the 1st is for the sign, next 8 (2-9) are for the exponent, and the following 23 are called the SIGNIFICAND.

The exponent field is the k in 2^k. So what i need to do is only change the exponent field and add n to it. exponent = exponent + n .

So how do i do this in assembly 8086 ?

Thank you


回答1:


Here's some quite ugly inline asm, VS style. Hopefully you'll get the idea:

float mul(float f, int p)
{
    __asm {
        mov eax, f
        mov ecx, p
        shl ecx, 23
        add eax, ecx
        mov f, eax
    }

    return f;
}

This obviously does no checking for overflow, etc.



来源:https://stackoverflow.com/questions/14956580/how-to-multiply-by-2n-in-assembly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!