Embedded interview encountered a few pen questions

Embedded interview encountered a few pen questions

 

Embedded programming problem:

1, find the error the following program (an interrupt service routine ISR)

    interrupt double compute_area(double radius)

    {

        double area = PI *radius * radius;

        printf("\nArea=%f",area);

        return area;

    }

    answer:

        a, ISR can not return a value.

        b, ISR can not pass parameters.

        c, in many processors compilers, floating-point operations are not rushed in. Some processors compilers need to stack additional registers, some processor compiler will not do floating point in an ISR. In addition ISR should be short and efficient. Doing floating-point operations in the ISR is unwise.

        D, is often printf into the problems and performance, it is generally not used printf function.

2, the meaning and use of the volatile keyword

    Syntax const and volatile syntax is the same, but volatile means "outside the scope of the compiler know that this data can be changed." Environmental change data under the conditions of unknown cause, therefore, volatile Do not attempt to make any assumptions about the data to tell the compiler, during this optimization is particularly important.

    In layman's terms, a volatile defined as: defined as volatile variable is said that this variable might be different unexpected change, so the compiler would not have to assume that the value of this variable. Precisely, the optimizer must always carefully re-read the value of this variable when this variable is used, instead of using the value in the register backup.

    Here are a few examples of the use of volatile variables:

    a, parallel device hardware registers, such as the STATUS register, the processor is constantly changing, so the value of the register are constantly varies with the state of the processor.

    b, an interrupt service routine will get access to non-automatic variable

    c, multi-threaded application variables are shared by several tasks. Each thread has may modify the value of a variable.

    

    Here's a volatile issue:

    A parameter can be either const is volatile? A pointer can cause volatile?

    The first question: is the best example is a read-only status register. He is volatile, because it may be an unexpected change; he is const, because the program should not attempt to modify it.

    The second question: Yes. Although this is not common, an example is that when the interrupt service routine modification of a pointer to a buffer.    

 

    Look at an example:

    The following function What is wrong?

    int square(volatile int *ptr)

    {

        return * ptr * (* ptr);

    }

    Analysis: for the purpose of the program is to return * ptr pointer points to the square value, since it is * ptr parameter points to a volatile, the compiler will generate the code similar to the following:

    int square(volatile int *ptr)

    {

        int a,b;

        a = *ptr;

        b = *ptr;

        return a*b;

    }

    Since the value of * ptr may change unexpectedly, so a and b may not be the same. The results are predictable.

    The correct code is as follows:

    long square(volatile int *ptr)

    {

        int a;

        a = *ptr;

        return a*a;

    }

 

3, to access a particular memory location features

    Required to set an absolute address of an integer variable 0x67a9 value 0xaa66. The compiler is a pure ANSI compiler.

    Typical answer:

        int *ptr;

        ptr = (int *)0x67a9;

        * Ptr = 0xaa66;

    One kind of irrational methods:

        *(int *const)(0x67a9) = 0xaa66;

4, two code write, a bit 3 of a set, a bit 3 of a clear

    Best answer:

    #define BIT3 (0x1 << 3)

    static int a;

    void set_bit3(void)

    {

        a |= BIT3;

    }

    void clear_bit3(void)

    {

        a &= BIT3;

    }

 

Guess you like

Origin blog.csdn.net/ll148305879/article/details/92796903