zoukankan      html  css  js  c++  java
  • 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参与移位操作;

  • 相关阅读:
    移除DOM节点
    php 301重定向
    PHP 面向对象:方法重载
    JSON
    轮播图alt作为标题
    php 开发规范
    struts2文件上传 判断大小
    twitter api
    php 方法重写,参数不同,报错: Declaration of should be compatible with that
    Delphi中判断当前程序运行过程中长时间无鼠标与键盘操作
  • 原文地址:https://www.cnblogs.com/xihong2014/p/10100612.html
Copyright © 2011-2022 走看看