Revision Questions
Revision Questions
Revision
1. (a) Answer the following questions by selecting the correct answer(s) from the choices
provided. Answer ‘None’ if none of them is correct.
(i) The execution of the ‘C’ instruction “i = i << 3;”, where i is a16-bit
number declared as “short i = 0xFABF;”, will result in:
a) i = 0xD5F8 c) i = 0xFF57
b) i = 0xD5FF d) i = 0x1F57
(ii) What would be the value of C after execution of the following statement if it
was declared as char and initialized to 0xAB?
(iv) Which is the correct way to call the function whose prototype is:
myfunction(int *x, int y); having: int i[5], j; k;
(v) What would be the content of a master device register in an SPI transmission
after 7 clock cycles, if its content was 0x0000 and the content of the slave
register was 0xABFB?
a) 0x0077 c) 0xE300
b) 0x00AB d) 0x00E3
2. (a) (i) What does the following ‘C’ code fragment do:
char C;
if (C & 0x40) C &= 0xBF;
else C |= 0x40;
(ii) What would be the value of C after execution of the code in (a)(i) if C
was initialized to 7910?
(b) What will be the values of i and j after executing the following code
fragment? Lines have been numbered to help clarify your answers.
1. int i, j = 2;
2. int a[3][3] = {{5, -4, 0}, {-2, 0, 10}, {8, -8, 0}};
3. int *aptr = &a[0][0];
4. aptr += 2;
5. i = *aptr;
6. aptr -= 1;
7. j -= *aptr;
3. (a) (i) What does the following ‘C’ code fragment do:
unsigned short A;
while ((A & 0x0110)== 0x0110) A &= 0xFEEF;
(ii) What would be the value of A in HEX after execution of the code in (a)(i)
if A was initialized to A35B16?
unsigned short X;
while ((B1 & 0x80) == 0);
X = (B1 & 0x1F) | ((B2 & 0xFC) << 3);
(i) Copy Fig. 1 to your answer sheet and use it to show the position of each bit
(a0 to a10 and s).
Byte 1
Byte 2
Figure 1
(ii) Re-write the code if we have 12 bits and they are stored as shown in Fig. 2
and the data is wrong when s=1.
Byte 1 a6 a5 a4 a3 a2 a1 a0 x
Byte 2 a11 a10 a9 a8 x x a7 s
Figure 2
4. (a) What will be the elements of matrix x after execution of the following code
fragment? Lines have been numbered to help justify your answers. Copy Fig. 3
to your answer book and use it to show the data memory content after
execution of the code.
Figure 3
(b) (i) What does the following ‘C’ code fragment do:
char PortA;
(ii) What would be the value of PortA in HEX after execution of the code in
(b)(i) if PortA was initialized to 1510?
5. (a) Write a C program to find and display the maximum element in an array and its
location in the array.
- The maximum size of the array is 20;
- The user should enter the size of the array and all elements of the
array;
(b) Re-write the C program in part (a) using a function to find and pass the
maximum element and its location to the calling main function.
6. (a) What will be the value of ‘i’ in decimal after execution of the following code
fragment?
1. char i, j = 19;
2. char *jptr = &j;
3. i = (*jptr << 2)| 0201;
(b) (i) The ‘C’ instruction “A &= 0x8000;”, where A is a 16-bit bus, can
be used to do which operation?
(ii) What will be the execution result of the ‘C’ instruction “i = i >> 7;”, where
i is a16-bit number declared as “short int i = 0xB0AF;”?
7. (a) (i) What does the following ‘C’ code fragment do:
unsigned char A;
if ((A & 0x11)== 0x11) A &= 0xEE;
(ii) What would be the value of A in HEX after execution of the code in (a)(i)
if A was initialized to 7716?
(b) Re-write your C program from question (a) using a function. The main function
should be used only to read the elements of the input matrix and vector and to
display the output vector. The matrix-vector multiplication should be performed
by the function. In the main function, just write the extra code that needs to be
added.
(c) Repeat (b) for matrix multiplication (use square matrices with a size of 5).
9. A diagonal matrix is a square matrix in which all the elements outside the main
diagonal are all zero and diagonal elements are non-zero.
A diagonal matrix is scalar if all of its diagonal elements are the same (see example
below).
2 0 0 0
0 2 0 0
𝐴𝐴 = � �
0 0 2 0
0 0 0 2
(b) One function to check if the matrix is scalar. Give two solutions using both
covered methods (i.e., using a pointer and an array as a formal parameter).
DIP
switch mbed #1 SPI mbed #2
Slave Master
(a) (b)
Figure 5
(b) Write the program for the master to continuously receive the status of the
switches from the slave microcontroller and display on a PC terminal how
many switches are OFF in decimal. Use the default frequency and the mode
where CPHA=0 and CPOL=1.
(c) Write the program for the slave to transmit the status of the switches to the
master.
11. The Sharp IR sensor is used with two mbed microcontrollers in a robotic system as
shown in Fig. 6(b). The simplest connection for the sensor, which can be used with
the mbed, is shown in Fig. 6(a).
4.5 to 5.5 V
Sharp
Sharp Vout IR
mbed #1 SPI mbed #2
IR sensor
sensor
Master Slave
(a) (b)
Figure 6
The mbed reads the input from the IR sensor as a 3.3V for 100mm away and 0V for 500mm.
(b) Write the program for the master to continuously read the measurement from
the sensor and send the distance in mm to the slave microcontroller using a
16-bit value. Use the default clock frequency and mode.
(c) Write the program for the slave to continuously receive the distance and
display it in mm.
12. The 4-position DIP switches in Fig. 7 are used with three mbed microcontrollers
connected through SPI. The DIP switches are connected to the two slave
microcontroller.
DIP 1
SPI mbed #2
mbed #1
Slave
Master DIP 2
mbed #3
Slave
Figure 7
(c) Write the program for the master to continuously receive the status of the
two 4-position DIP switches and display the status of the first two switches
and the last two switches from DIP1 and DIP2, respectively on the on-board
LEDs at the same time.