C166 -MDH

Writing a C logic for moving MDH register contents after MUL instruction   http://www.keil.com/forum/5231/

unnecessary code generation    http://www.keil.com/forum/3528/

Hi Heinz,

I made a similar observation. Consider this example:

int a, b, c, d;

void main(void)
{
    a = c / d;
    b = c % d;
}

0000 F2F70000 R    MOV       R7,d
0004 F2F60200 R    MOV       R6,c
0008 F6F60EFE      MOV       MDL,R6
000C 4B77          DIV       R7
000E F6070600 R    MOV       a,MDL
0012 F6F60EFE      MOV       MDL,R6
0016 4B77          DIV       R7
0018 F2F40CFE      MOV       R4,MDH
001C F2F50EFE      MOV       R5,MDL
0020 F6F40400 R    MOV       b,R4
0024 CB00          RET

Whereas the more optimal code would be

MOV     R6,d
MOV     MDL,c
DIV     R6
MOV     a,MDL
MOV     b,MDH
RET

Quite a difference, isn't it? Oh well, maybe we are spoilt by modern compilers like gcc or msvc?

RE: Writing a C logic for moving MDH register contents after MUL instruction

Are you trying to do a 32-bit multiplication? You may have forgotten to use a type cast to long. Remember that in C the product of two ints is int. Compile this and see the difference:

int a, b, c, d;
c = ( a * b ) >> 16;
d = ( (long)a * (long)b ) >> 16;

u16 u_16;
u32 u_32;

void main ()
{

int a,b,c;
short d,e;
long f;

f = (long)a*b ;
u_16 = ((u16)(f >> 16)<<1) | ((u16)f>>15);

上述代码可以实现 32位整体移位15并赋值到 16位变量;中间代码会有 MDH,MDL参与移位操作;

原文地址:https://www.cnblogs.com/xihong2014/p/10100612.html