A small program to determine whether the arm immediate value is legal

Publisher:水云间梦Latest update time:2017-01-04 Source: eefocusKeywords:arm Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

      Today the teacher assigned homework, one of which is to judge the immediate number. As we all know, not all immediate numbers in ARM are legal.

 

      Description: If the immediate value is denoted as , the 8-bit constant is denoted as immed_8, and the 4-bit rotate right value is denoted as rotate_imm, the effective immediate value is obtained by rotating an 8-bit immediate value left by an even number of bits. Then the effective immediate value can be expressed as: 
= immed_8 rotate right (2 * rotate_imm) 

Example: Valid immediate numbers 0x0000F200, 0x00110000, 0x00012800 
      Invalid immediate numbers 0x1010, 0x00102, 0xFF1000 
Obviously, all 8-bit immediate numbers are valid.

 

      In fact, judging whether a number is an immediate number is also very simple from the definition:

 

1. Check if all bits that are 1 are in one byte

2. If condition 1 is met, see if it can be obtained by shifting left by an even number of bits

 

Anything that meets these two conditions is a legal immediate number, but I still wrote a small program to make the judgment. It seems that I am still so lazy, hehe.


#include  

  

/****************************************************** *************  

Description: Circular left shift function  

Input: val, the immediate value to be judged  

    n, the number of bits to shift left (0--15)  

Return: the immediate value after circular left shift n bits  

*************************************************** **********/   

unsigned int left(unsigned int val, int n)   

{   

    if (n < 0 || n > 15)     

    {   

        return 0xffffffff;   

    }   

      

    unsigned int rtn;   

      

    rtn = val << (2 * n);   

    rtn |= (val & (0xffffffff << (32 - 2 * n))) >> (32 - 2 * n);   

      

    return rtn;   

}   

  

/****************************************************** *************  

Description: Determine whether the immediate value is valid  

Input: val, the immediate value to be judged  

Return: true, the immediate value is valid  

            false, the immediate value is invalid  

*************************************************** **********/   

int Judge(unsigned int val)   

{     

    int i = 0;  

      

    for (i = 0; i < 16; i++)     

    {   

        if(left(val, i) <= 0x000000ff )   

            return 1;   

    }   

      

    return 0;   

}  

  

int main(void)  

{  

    int num = 0;      

      

      

    while(printf("Please input:"), 1 == scanf("%x", &num))  

    {  

        if(Judge(num))  

        {     

            printf("It is a legal immediate number!/n");  

        }  

        else  

        {  

            printf("Illegal immediate value!/n");  

        }  

    }  

      

    return 0;  


Keywords:arm Reference address:A small program to determine whether the arm immediate value is legal

Previous article:STM32 system structure, clock tree
Next article:The difference between arm 2440 and 6410

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号