1958 views|0 replies

3836

Posts

19

Resources
The OP
 

Share the written test questions for embedded software engineers [Copy link]

1. Reverse a string
2. Reverse a linked list
3. Count how many bits are set to 1 in a byte
4. Search for a given byte
5. Find the longest possible substring in a string
6. Convert a string to an integer
7. Convert an integer to a string
1. What are the differences between asynchronous and synchronous serial communication of the C51 microcontroller? Talk about their advantages and disadvantages.

2. How are the data bus and address bus of the C51 microcontroller multiplexed? Explain the principle.

3. What is the absolute addressing range of the C51 microcontroller in k?

4. Explain the following addressing method

(1)MOV A , #30H (2)MOV A,@R0

(3)ADD A , R4 (4)MOV A+@DPTR

5. What are the following pointers?

(1)int *a[10]; (2)int (*a)[10];

(3)int (*P)(int); (4)int (*a[10])(int);

6.

void swap(int a, int b)

{

int temp;

temp = a;

a=b;

b=temp;

}

main(void)

{

int x="3",y=4;

swap(x,y);

}

After the above program is executed, x=?, y=?

7. typedef struct test{

int i;

short s;

char c;

union{

int a;

short b;

}

}example;

int y ;

y = sizeof(example);

In the TC environment, what is y=?

8. Program to reverse an array. For example, “hello, world!” becomes “!dlrow, olleh” after reversal.

9. What parts do you think an embedded operating system includes? What are the characteristics of a real-time operating system?

10. What parts does a von Neumann-structured computer consist of?

11. Talk about the differences between programs, processes, and threads.

12. Tell us how you understand

Program = Data + Structure + Algorithm

13. According to the function prototype, implement an array and sort it by bubble from large to small.

void tibbule(int a[],int n); //a[] is an array, n is the length of the array

14. How many layers does OSI have? How many layers does TCP/IP have?

15. Program to insert and delete nodes in a doubly linked list.

5. Write a data type: an array of 10 pointers to a function that takes an integer parameter and returns an integer.

Integer number.

6. Embedded systems always require users to perform bit operations on variables or registers. Given an integer variable a, write two pieces of code. The first one sets a

The first one clears bit 3 of a, and the second one clears bit 3 of a. In the above two operations, other bits must remain unchanged.

7. Embedded systems often require programmers to access a specific memory location. In a project, it is required to set an absolute address

The integer variable at 0x67a9 has the value 0xaa66. The compiler is a pure ANSI compiler. Write code to accomplish this task.

8. Reverse the order of the string.

9. float a,b,c Find the values of the following two:
(a+b)+c == (b+a)+c
(a+b)+c == (a+c)+b

10. What is the function of the keyword static?
What is the meaning of the keyword const?

11. What does the keyword volatile mean? Give three different examples.
1) Can a parameter be both const and volatile? Explain why.
2) Can a pointer be volatile? Explain why.
3) What is wrong with the following function:
int square(volatile int *ptr)
{
return *ptr * *ptr;
}

12. Embedded systems often require programmers to access a specific memory location. In a project, it is required to set an absolute address

The integer variable at 0x67a9 has the value 0xaa66. The compiler is a pure ANSI compiler. Write code to accomplish this task.

13. Dynamic Memory Allocation
What is the output of the following code snippet and why?
char *ptr;
if ((ptr = (char *)malloc(0)) == NULL)
puts("Got a null pointer");
else
puts("Got a valid pointer");

14. Typedef is frequently used in C to declare a synonym for an existing data type. You can also use the preprocessor to do something similar.

For example, consider the following:
#define dPS struct s *
typedef struct s * tPS;
In both cases, the intention is to define dPS and tPS as pointers to structure s. Which approach is better? (If any)

Why?

15. Interrupts are an important part of embedded systems, which has led many compiler developers to provide an extension to allow standard C to support interrupts.

The fact is that a new keyword __interrupt is created. The following code uses the __interrupt keyword to define an interrupt

Service Subroutine (ISR), please comment on this code.

__interrupt double compute_area (double radius)
{
double area = PI * radius * radius;
printf("\nArea = %f", area);
return area;
}


16. What is the output of the following code and why?
void foo(void)
{
unsigned int a = 6;
int b = -20;
(a+b > 6) ? puts("> 6") : puts("<= 6");
}

17. Evaluate the following code snippet:
unsigned int zero = 0;
unsigned int compzero = 0xFFFF;
/*1's complement of zero */

18. C allows some shocking structures. Is the following structure legal? If so, what does it do?
int a = 5, b = 7, c;
c = a+++b;

19. Write a reentrant function.

20. What should I pay attention to when writing ISR?

21. How to design a memory copy function, considering the principles of efficiency and robustness.

22. How do you understand the meaning of "address alignment", "memory allocation", and "processor byte order"? Are there any points that need to be paid attention to in application? For example,
how to test the byte order of a processor?

23. Give at least two examples to illustrate the process of CPU responding to interrupts from the perspective of hardware and microcode execution.

24. How to implement task scheduling?

25. How to avoid priority inversion?

26. What is the difference between task lock and terminal lock? How should we choose between them in application?

27. What are the means of inter-process communication and synchronization? Give examples.

4 Embedded system related questions
a) For the integer variable A=0x12345678, please draw how it is stored in memory in little endian and big endian.
b) In the ARM system, when calling a function, how are the parameters passed?
c) What is the difference between an interrupt (such as a keyboard interrupt) and an exception (such as a division by zero exception)?

Definition:
1) In the following test questions, it is assumed that all necessary header files have been correctly included.
2) Data types
char 1 byte
int 2 bytes (16-bit system, assume that integer is 2 bytes)
long int 4 bytes
float 4 bytes
double 8 bytes
long double 10 bytes
pointer 2 bytes (note, 16-bit system, the address bus is only 16 bits)

Question 1: Test your understanding of the volatile keyword
#include
static jmp_buf buf;
main()
{
volatile int b;
b =3;

if(setjmp(buf)!=0)
{
printf("%d ", b);
exit(0);
}
b=5;
longjmp(buf, 1);
}

Please tell me, the output of this program is
(a) 3
(b) 5
(c) 0
(d) none of the above

Question 2: Test type conversion
main()
{
struct node
{
int a;
int b;
int c;
};
struct node s= { 3, 5,6 };
struct node *pt = &s;
printf("%d" , *(int*)pt);
}

The output of this program is:
(a) 3
(b) 5
(c) 6
(d) 7

Question 3: Check the recursive call of
int foo ( int x , int n) 
{
int val;
val =1;

if (n>0)
{
if (n%2 == 1) val = val *x;
val = val * foo(x*x , n/2);
}
return val;
}

What function (operation) does this code perform on x and n?
(a) x^n (x raised to the power of n)
(b) x*n (the product of x and n)
(c) n^x (n raised to the power of x)
(d) None of the above

Question 4: Tests pointers. This question is only suitable for those who are particularly careful and have a deep understanding of pointers and arrays.
main()
{
int a[5] = {1,2,3,4,5};
int *ptr = (int*)(&a+1);

printf("%d %d" , *(a+1), *(ptr-1));
}

The output of this program is:
(a) 2 2
(b) 2 1
(c) 2 5
(d) None of the above

Question 5: Examining multidimensional arrays and pointers
void foo(int [][3]);
main()
{
int a [3][3]= { { 1,2,3} , { 4,5,6},{7,8,9}};
foo(a);
printf("%d" , a[2][1]);
}
void foo(int b[][3])
{
++ b;
b[1][1] =9;
}

The output of this program is:
(a) 8
(b) 9
(c) 7
(d) None of the above

Question 6: Check the comma expression
main()
{
int a, b,c, d;
a=3;
b=5;
c=a,b;
d=(a,b);

printf("c=%d" ,c);
printf("d=%d" ,d);
}

The output of this program is:
(a) c=3 d=3
(b) c=5 d=3
(c) c=3 d=5
(d) c=5 d=5

Question 7: Examining pointer arrays
main()
{
int a[][3] = { 1,2,3 ,4,5,6};
int (*ptr)[3] = a;

printf("%d %d ", (*ptr)[1], (*ptr)[2]);
++ptr;
printf("%d %d" , (*ptr)[1], (*ptr )[2]);
}

The output of this program is:
(a) 2 3 5 6
(b) 2 3 4 5
(c) 4 5 0 0
(d) None of the above

Question 8: Examining function pointers
int *f1(void)
{
int x =10;
return(&x);
}
int *f2(void)
{
int*ptr;
*ptr =10;
return ptr;
}
int *f3(void)
{
int *ptr;
ptr=(int*) malloc(sizeof(int));
return ptr;
}

Which of the three functions above is most likely to cause pointer problems?
(a) only f3
(b) only f1 and f3
(c) only f1 and f2
(d) f1, f2, and f3

Question 9: Check the self-increment operation (++)
main()
{
int i=3;
int j;

j = sizeof(++i+ ++i);

printf("i=%dj=%d", i ,j);
}

The output of this program is:
(a) i=4 j=2
(b) i=3 j=2
(c) i=3 j=4
(d) i=3 j=6

Question 10: Check formal parameters, actual parameters, pointers and arrays
void f1(int *, int);
void f2(int *, int);
void(*p[2]) (int *, int);
main()
{
int a;
int b;

p[0] = f1;
p[1] = f2;
a=3;
b=5;

p[0](&a, b);
printf("%d\t %d\t", a, b);
p[1](&a, b);
printf("%d\t %d\t" , a, b);
}
void f1(int* p , int q)
{
int tmp;
tmp =*p;
*p = q;
q= tmp;
}
void f2(int* p , int q)
{
int tmp;
tmp =*p;
*p = q;
q= tmp;
}

The output of this program is:
(a) 5 5 5 5
(b) 3 5 3 5
(c) 5 3 5 3
(d) 3 3 3 3

Question 11: Check the decrement operation (--)
void e(int );
main()
{
int a;
a=3;
e(a);
}
void e(int n)
{
if(n>0)
{
e(--n);
printf("%d", n);
e(--n);
}
}

The output of this program is:
(a) 0 1 2 0
(b) 0 1 2 1
(c) 1 2 0 1
(d) 0 2 1 1

Question 12: Check the typedef type definition, function pointer
typedef int (*test) ( float * , float*)
test tmp;
The type of tmp is
(a) pointer to function, which takes two pointers to floating point numbers (float) as arguments (pointer to function)
Pointer to function of having two arguments that is pointer to float
(b) integer
(c) pointer
to function having two argument that is pointer to float and return int
(d) none of the above

Question 13: The difference and connection between arrays and pointers
main()
{
char p;
char buf[10] ={ 1,2,3,4,5,6,9,8};
p = (buf+1)[5];
printf("%d" , p);
}

The output of this program is:
(a) 5
(b) 6
(c) 9
(d) None of the above

Question 14: Examine the pointer of pointer array
void f(char**);
main()
{
char * argv[] = { "ab" ,"cd" , "ef" ,"gh", "ij" ,"kl" };
f( argv );
}
void f( char **p )
{
char* t;

t= (p+= sizeof(int))[-1];

printf( "%s" , t);
}

The output of this program is:
(a) ab
(b) cd
(c) ef
(d) gh

Question 15: This question tests the variable length parameters of C, just like printf() in the standard function library. This topic is generally not taught in domestic university classes, and it is understandable that it is not taught.
#include
int ripple ( int , );
main()
{
int num;
num = ripple ( 3, 5,7);
printf( " %d" , num);
}
int ripple (int n, )
{
int i , j;
int k;
va_list p;

k = 0;
j = 1;
va_start(p, n);

for (; j{
i = va_arg(p , int);
for (; i; i &=i-1 )
++k;
}
return k;
}

The output of this program is:
(a) 7
(b) 6
(c) 5
(d) 3

Question 16: Test your knowledge of static variables
int counter (int i)
{
static int count = 0;
count = count + i;
return (count );
}
main()
{
int i , j;

for (i=0; i <=5; i++)
j = counter(i);
}

At the end of this program, the value of j is:
(a) 10
(b) 15
(c) 6
(d) 7

Detailed reference answer
Question 1: (b)
Volatile literally means "easy to volatilize". When this keyword is used to describe a variable, it means that after assigning a value to the variable (writing it), if it is read immediately, the written value may be different from the read value, so it is said to be "easy to volatilize".
This is because the variable may be a register directly connected to an external device. After you write to it, the register may also be changed by the write operation of the external device; or, the variable may be
changed by an interrupt program or another process.
Volatile will not be affected by compiler optimization. After longjmp, its value is the variable value assumed later. The final value of b is 5, so 5 is printed out.
setjmp: Set non-local jump/* setjmp.h*/
Stores context information such as register values so that the lomgjmp function can return control to the statement following the one calling setjmp.Returns 0 when it is initially called.
Lonjjmp: Execute a non-local jump/* setjmp.h*/
Transfers control to the statement where the call to setjmp (which initialized buf) was made. Execution continues at this point as if longjmp cannot return the value 0.A nonvolatile automatic variable might be changed by a call to longjmp.When you use setjmp and longjmp, the only automatic variables guaranteed to remain valid are those declared volatile.
Note: Test program without volatile qualifier (result may be very)
For more details, please refer to setjmp and longjmp in C language

Question 2: (a)
The addresses of the members of a structure in memory increase in the order in which they are defined. If a pointer to a structure is considered to be a pointer to its first member, then the pointer does point to the first member.

Question 3: (a)
This question is more difficult.
The non-recursive version of this program
int what ( int x , int n)
{
int val;
int product;
product = 1;
val = x;

while(n>0)
{
if (n%2 == 1)
product = product*val; /*If it is an odd power, x(val) must be multiplied once first;
for an even power, it will be multiplied by 1 here at the end of the return*/
val = val* val;
n = n/2;
}
return product;
}

/*Use binary complex multiplication strategy*/
Algorithm description
(while n>0)
{
if next most significant binary digit of n(power) is one
then multiply accumulated product by current val,
reduce n(power) sequence by a factor of two using integer division.
get next val by multiply current value of itself
}

Question 4: (c)
The type of a is an integer array, which has 5 members. The type of &a is a pointer to an integer array, so &a + 1 points to a[6], so *(a+1) is equivalent to a[1]
ptr is equivalent to a[6], and ptr-1 is equivalent to a[5].

Question 5: (b)
The question itself gives enough clues:
b[0][0] = 4
b[1][0] = 7

Question 6: (c)
Check the comma expression. The priority of the comma expression is very low, lower than the priority of the assignment (=). The value of the comma expression is the value of the last element. The
comma expression also has the function of splitting the parameter list of the function..
E1, E2, ..., En
The left and right sides of the expression above are E1, E2, ... En. The values are calculated separately, and the calculated structure of En is assigned to the entire comma expression
c=a,b; / *yields c=a* /
d=(a,b); /* d =b */

Question 7: (a)
ptr is a pointer to an array, which has 3 int members

Question 8: (c)
There is obviously a problem with f1. It returns a pointer to a local variable. Local variables are stored in the stack. After exiting the function, the local variable is destroyed. It is meaningless to keep its pointer because the stack space it points to may be overwritten by other variables.
There is also a problem with f2. ptr is a local variable and is not initialized. Its value is unknown. We don’t know where *ptr points to. Directly assigning a value to *ptr may overwrite important system variables. This is what is commonly called a wild pointer.

Question 9: (b)
The sizeof operator gives the size of the space required by its operand. It is determined at compile time, so even if its operand is an expression, it does not need to be calculated at run time. ( ++i + ++ i ) will not be executed, so the value of i is still 3.

Question 10: (a)
Obviously, choose a.
f1 swaps the values of *p and q. After f1 is executed, the values of *p and q are indeed swapped, but the change of q will not affect the change of b. *p is actually a.
So after executing f1, a=b=5.
This question tests a wide range of knowledge, including typedef custom types, function pointers, and pointer arrays. void(*p[ 2 ]) ( int *, int);
defines an array of function pointers p. p has two pointer elements. The elements are function pointers. The function pointed to by the function pointer is a function with two parameters and returns void. The two parameters are pointers to integers and integers.
p[ 0 ] = f1; p[ 1 ] = f2 contain address of function. function name without parenthesis represent address of function Value and address of variable is passed to function only argument that is effected is a (address is passed). Because of call by value f1, f2 can not effect b

Question 11: (a)

Examination--operation and recursive call, just analyze it carefully

Question 12: (c)
If you don’t know how to do it, I suggest you read C Expert Programming.
Start from the left, stop when you see a bracket, and treat the contents of the first bracket as a whole.

Question 13: (c)
Examine when an array is a pointer. For some type T, if an expression is T[] (array of T), the value of the expression is actually a pointer to the first element of the array. So (buf+1)[5] is actually *(buf+6) or buf[6]

Question 14: (b)

The value of sizeof(int) is 2, so p+=sizeof(int) points to argv[2]. I guess everyone has no doubt about this.
(p+=sizeof(int))[-1] points to argv[1]. Do you understand? Because (p+=sizeof(int))[-1] is equivalent to (p+=2)[-1], which is (p+2-1).

Question 15: (c)
C compilers usually provide a series of macros for handling variable parameters to mask the differences caused by different hardware platforms and increase the portability of programs. These macros include va_start, va_arg, and va_end.
When using the ANSI standard form, the prototype declaration of a function with a variable number of parameters is: type funcname (type para1, type para2, ...)
This form requires at least one common formal parameter. The ellipsis after it does not mean omission, but is part of the function prototype. type is the type of the function return value and formal parameters.
Different compilers have different implementations of this variable length parameter. It is a built-in function in gcc4.x.
For more information about variable length parameters, please refer to
/2004-11/26.html
/2004-11/24.html
Program Analysis
va_list p; /*Define a variable to save the pointer to the function parameter list*/
va_start( p , n); /*Use the va_start macro to initialize the variable p. The second parameter n of the va_start macro is a fixed parameter that must be defined by ourselves.
The last parameter pushed onto the stack of the variable length function is the first parameter in the parameter list when calling it*/
for (; j{
i = va_arg( p , int); /*va_arg takes out the current parameter and considers the parameter taken out to be an integer (int) */
for (; i; i &=i-1 ) /*Judge whether the taken out i is 0*/
++k; /*If i is not 0, k is incremented, and i and i-1 are ANDed until i is 0.
This is a trick. Its function will be discussed below*/
}

When we call the ripple function, the value of the first parameter n in the parameter list passed to the ripple function is 3. va_start initializes p to point to the first unnamed parameter (n is a named parameter), which is 5 (the first one). Each call to va_arg will return a parameter and set p to point to the next parameter. va_arg uses a type name to determine the type of the returned parameter and the internal implementation of var_arg to determine how far to move to reach the next parameter
(; i; i&=i-1) k++ /* Calculate how many bits of i are set to 1 */
5 is represented in binary as (101) 2
7 is represented in binary as (111) 3
So k returns 5 (2+3), which means that the answer to this question should be c. Let's take
an example, and it will be easy to understand.
Let i= 9 = 1001
i-1 = 1000
(i-1) +1 = i
1000
+1
1001
because i and i-1 are the rightmost bit (the lowest bit) They are definitely different. If i1, i-1 is definitely 0, and vice versa. The operation i & i-1 will eliminate the rightmost 1 bit in the two-complement digital system.

Question 16: (b)
The answer is (b)
. It is said that Gauss could solve this type of geometric progression problem when he was in the first grade of elementary school. This question tests the knowledge of static variables. After each function call, the value of the static variable will not be lost, which is obviously different from the temporary local variables in the stack.
Therefore, after the first call to counter(0), count = 0.
After the second call to counter(1), count = 0+1;
the third call to counter(2) count = 1+2; /* count = count +i */
The fourth call to counter(3) count = 3+3;
the fifth call to counter(4) count = 6+4;
the sixth call to counter(5) count = 10+5;


1. Write swap (x, y) using macro definition
2. Array a[N] stores numbers from 1 to N-1, one of which is repeated once. Write a function to find the repeated numbers. The time complexity must be o(N) Function prototype:
int do_dup(int a[], int N)
3 One statement to determine whether x is a power of 2
4. unsigned int intvert(unsigned int x, int p, int n) to convert x, p is the starting conversion position, n is the length to be converted, assuming the starting point is on the right. For example, x=0b0001 0001, p=4, n=3, after conversion x=0b0110 0001.

Ros:
1. There are indeed two ways

#define SWAP(x,y) x+=y; y=xy; x=xy

#define SWAP(x,y) x^=y; y^=x; x^=y

The first method is more general and can be used for the same type. The second method uses bitwise operators, but bitwise operations of float and double types cannot pass the compiler. But I always think that no matter what data type is used, it is ultimately binary in memory, so the second method should be logically feasible, but it cannot pass the compiler.

2,
int do_dup(int a[], int N){
char *p_count,i;
p_count = (*char)malloc(N);//Time complexity is o(N) but occupies a little more space
for(i=0;i<N;i++){
*(p_count+a[i])++;
if(*(p_count+a[i]) == 2){
return a[i];
}
}

free(p_count);
}
Of course, some people use hash tables, but I have never used hashing. But the idea is not much different.

3,Key: The nth power of 2 has a commonality in binary, that is, only one bit is 1, and the rest are all 0;
(x&(x-1))?0:1 //1 for true, 0 for false

4. Conversion is actually the result of XORing each bit with 1.

unsigned int intvert(unsigned int x,int p,int n){

return x^=(((1<<n)-1)<<p);//(1<<n)-1 to get n 1 is so smart.

}

This post is from DSP and ARM Processors
 

Guess Your Favourite
Just looking around
Find a datasheet?

EEWorld Datasheet Technical Support

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号
快速回复 返回顶部 Return list