ELEC3710 Microprocessor Systems: Practice Problems: Solution
ELEC3710 Microprocessor Systems: Practice Problems: Solution
Problems
(a) 6 && 2
Solution:
non zero && non zero = T & T = 1
(b) 6 & -2
Solution:(0...0110)2 &(1...0010)2 = (0...0110)2 = 610
(c) 3 || 6
Solution:
non-zero || non-zero = T||T = T = 1
(d) 3|6
Solution:
(0..0011)2 —(0..0110)2 = (0..0111)2 = 710
(e) !(-3)
Solution:!(non-zero) = !(T) = F = 0
(f) ˜(-3)
Solution:
˜(11..101)2 = (00..010)2 = 210
2. Write an expression in C that is true if and only if bit 5 of the integer variable “x” is a 1.
Solution:
3. Assume that “n” and “x” are declared as int. Write a line of C code that regardless of the
current value of x, and without modifying any other bits of x will:
1
(a) Set bit n of x to 1
Solution:
x |= (1<<n);
x &= ˜(1<<n);
x ˆ= (1<<n)
4. Give the 8-bit binary value that remains in the unsigned char after executing the in-
dicated line of code, with the indicated initial value of x.
2
(f) x = (00001111)2 , x=!x
Solution:
(00000000)2
(g) x = (00000000)2 , x ||= 0x20
Solution:
(00000001)2
(h) x = (11111111)2 , x &&=0xF0
Solution:
(00000001)2
5. Given the macro defined below, show how each of the following usages would be ex-
panded.
(a) rem(5,2);
Solution:5 % 2
(b) rem(5+2,x);
Solution:5 + 2 % 2
(c) rem(5,x-2);
Solution:5 % x- 2
6. Define a macro called bit(x,n) that expands into an expression whose value is exactly
0 or 1 corresponding to the value of the n’th bit of x.
Solution:
7. Suppose a packed operand of 16 bits is to use bits 0-7 for a variable called age, bits 9-11
for a variable called hair color and bits 13-14 for gender. Implement this packed
operand using structure bit-fields.
Solution:
struct{
unsigned age : 8,
: 1,
hair_color : 3,
: 1,
gender : 2;
}
3
8. The first printf below prints 1C3F. What is printed by the second printf?
union{
short w;
char b[2];
}x;
x.w = 0x1C3F;
printf("%04x\n",x.w);
x.b[1] = 0xA6;
printf("%04x\n",x.w);
Solution:A63F
9. Write code to modify the 3rd byte (bits 16-23) of a 32-bit operand called ”x” using:
X.b[2] = new_value;
10. Write a C function that setts a specified bit within an array of bits that are packed eight per
byte into an array of bytes. The function must have exactly two parameters: the first is the
name of the array of bytes, the second is the index (starting at zero) of the bit to be set. The
function prototype should be
4
void SetBit(unsigned char bits[], int index);
Solution:
11. Write a sequence of C statements that swaps the contents of the two bytes stored within a
16-bit unsigned short int named ”pair”.
Solution:
12. What sequence of digits with the following code print when running on a CPU that uses
little endian byte numbering?
13. Use C’s bitwise and shift operators to write a sequence of C assignment statements that
increment the middle 6 bits of a 16 bit unsigned short int called ”x” without modifying any
of its other 10 bits (be careful of overflow).
Solution:
5
14. Define a macro called BYTE(a,n) where ”a” is to be replace by the name of a 32 bit
integer variable, and ”n” is a number between 0 and 3 that selects one of the four 8-bit
bytes within ”a” so that the macro can be used as follows.
long int x;
...
BYTE(x,2)=100;
...
if (BYTE(x,1) == 0) ...
Solution:
15. What changes when register EBP is used within the source or destination memory address
field of an instruction in the Intel processor?
Solution:
Answer = 15c
16. Suppose that register EDI and ESI already hold a single 64-bit quantity with the most
significant bits in EDI. Give a minimum length sequence of Intel instructions that would
be required to implement:
6
SHR EDI,1
RCR ESI,1
JNC L1
OR EDI,80000000h
L1:
17. Write a sequence of Intel protected mode instructions that will form the sum of the numbers
1 through 100.
Solution:
18. Write a sequence of Intel protected mode instructions that corresponds to the following C
code statements where x, y and z are labels on 32-bit memory locations whose contents
are unsigned integers.
7
MOV DWORD [_x],0
MOV DWORD [_y],0
L1: CMP DWORD [_y],1000
JNB L2
MOV EAX,[_y]
ADD [_x],EAX
SHL DWORD [_y],1
JMP L1
L2:
(d) if (x>10) if (x<20) y=1; else z=0;
Solution:
CMP DWORD [_x],10
JNA L2
CMP DWORD [_x],20
JNB L1
MOV DWORD [_y],1
JMP L2
L1: MOV DWORD [_z],0
L2:
19. Write a sequence of Intel protected mode instructions that corresponds to the following C
code statements where x, y and z are labels on 32-bit memory locations whose contents
are unsigned integers.
8
CMP DWORD [_x],+10
JL L1
L2:
(c) if (x<10 || x>20) y=0; else y=1;
Solution:
CMP DWORD [_x],10
JL L1
CMP DWORD [_x],20
JNG L2
L1: MOV DWORD [_y],0
JMP L3
L2: MOV DWORD [_y],1
L3:
(d) if ’a’ <= ch && ch=’z’) ch=ch-’a’+’A’
Solution:
CMP [_ch],’a’
JNAE L1
CMP [_ch],’z’
JNBE L1
SUB [_ch],’a’
ADD [_ch],’A’
L1:
(e) x=y/5;
Solution:
MOV EAX,[_y]
CDQ
MOV ECX,5
IDIV ECX
MOV [_x],EAX
20. Write a sequence of Intel protected mode instructions required to call the following func-
tion as indicated.
9
CALL _f1
ADD ESP,8
MOV [_x],EAX
(b) void f2(char); /* function prototype */
f2(c); /* function call */
Solution:
MOVZX EAX,BYTE [_c]
PUSH EAX
CALL _f2
ADD ESP,4
(c) void f3(char *); /* function prototype */
f3(&c); /* function call */
Solution:
MOV EAX,_c
PUSH EAX
CALL _f3
ADD ESP,4
21. Write a sequence of Intel protected mode instructions to implement the following function
in assembly. 1
Solution:
22. Write a sequence of Intel protected mode instructions required to implement the following
function in assembly language.
10
typedef unsigned long long int DWORD64
Solution:
Write a sequence of Intel protected mode instructions to implement this function so that it
counts and returns the number of bits that are 1 in its argument.
Solution:
24. Suppose that you have a 64-bit long long int named x. Give an appropriate sequence
of Intel assembly language instructions required to implement the simple C statement x++.
Solution:
11
unsigned long int x,y;
unsigned char int z;
Write a sequence of Intel protected mode instructions that corresponds to the following
statement:
z = (y*z)/15;
Solution:
26. Write a sequence of Intel protected mode instructions that correspond to the following
statements where x,y and z are labels on 32-bit memory locations whose contents are
signed integers.
12
MOV EAX,[_x]
CMP EAX,[_y]
JL then
CMP EAX,0
JNL endif
then: MOV DWORD [_z],2
endif:
27. Write a sequence of Intel protected mode instructions required to call the following func-
tion as indicated.
Solution:
PUSH DWORD x
CALL foo
ADD ESP,4
CALL bar
MOV [x],EAX
28. Write a sequence of Intel protected mode instructions to implement the following function
in assembly.
13
or (shorter)
MOVZX EAX[_k]
MOV EDX,[_a]
MOV AX,[EDX+4*EAX]
MOV [_x],AX
30. Suppose that you have a 64-bit fixed point real named x in 32.32 format. Give an appro-
priate sequence of Intel assembly language instructions required to add 0.5 to x.
Solution:
write a sequence of Intel protected mode instructions that correspond to the following
statement
x = (y*z)/15;
Solution:
14
32. Write a sequence of Intel protected mode instructions that corresponds to the following
statements, where x,y and z are labels on 32-bit memory locations whose contents are
unsigned long integers.
33. Write a sequence of Intel protected mode instructions required to call the following func-
tions
15
Solution:
PUSH DWORD x
CALL foo
ADD ESP,4
CALL bar
MOV [x],AX
Now assume that you have executed the first three instructions of function foo:
(a) What is the EBP-relative address expression used to reference the parameter x?
Solution:
[EBP+12]
(b) What is the EBP-relative address expression used to reference the parameter y?
Solution:
[EBP+8]
(c) How many bytes of temporary storage have been allocated?
Solution:
8
34. Translate the executable statement of each of the following C source code fragments into
Intel protected mode assembly:
(a) long int k, *a;
a=&k;
Solution:
MOV DWORD [a],k
(b) long int c[10], *a;
a=&c[1];
Solution:
MOV DWORD [a],c+4
(c) signed long int k, *a;
k=k/*a;
Solution:
16
MOV EAX,[_k]
CDQ
MOV ECX,[_a]
IDIV DWORD [ECX]
MOV [_k],EAX
(d) long long int big64;
big64 -= 5;
Solution:
SUB DWORD [_big64],5
SBB DWORD [_big64+4],0
(e)
char ch,b[10];
int k;
ch=b[k];
Solution:
MOV EAX,[_k]
MOV AL,[_b+EAX]
MOV [_ch],AL
Translate each of the following C statements into a sequence of Intel protected mode in-
structions.
17
(b) if (x>0 || y>0) z+=x; else z=0;
Solution:
CMP DWORD [_x],0
JG then
CMP DWORD [_y],0
JNG else
then: MOV EAX,[_x]
ADD [_z],EAX
JMP done
else: MOV DWORD [_z],0
done:
(c) z=0;
for (x=0; x<y,x++) z+= x;
Solution:
MOV DWORD [_z],0
MOV DWORD [_x],0
top: MOV EAX,[_x]
CMP EAX,[_y]
JNL done
MOV EAX,[_x]
ADD [_z],EAX
INC DWORD [_x]
JMP top
done:
36. Translate the following C function into a sequence of Intel protected mode instructions.
Assume that the functions disable interrupts and enable interrupts are li-
brary functions, your code merely needs to call them.
#define TRUE 1
#define FALSE 0
18
return current_value;
}
Solution:
37. Assuming one byte for the opcode itself, what is the minimum number of bytes required
to represent each of the following instructions?
19
38. Considering address alignment, what is the worst case time required to fetch a 24-byte data
object from memory with a 60 ns cycle time and a data bus that is 64 bits wide?
Solution:
Worst-case time required = 4 cycles × 60ns = 240 ns.
39. Write a sequence of Intel protected mode instructions (not a function) to exchange the
contents of two 32-bit memory locations named X and Y..
translate each of the following C source code statements into Intel protected mode assem-
bly
(a) a = &k;
Solution:
MOV DWORD [_a],_k
(b) a = &c[1];
Solution:
MOV DWORD [_a],_c+4
20
(c) a = &b[1];
Solution:
MOV EAX,[_b]
ADD EAX,4
MOV [_a],EAX
(d) a[1] = k;
Solution:
MOV EAX,[_k]
MOV EDX,[_a]
MOV [EDX+4],EAX
(e) ((char *) c)[k] = 0;
Solution:
MOV EAX,[_k]
MOV BYTE [_c+EAX],0
Translate each of the following C statements into a sequence of Intel protected mode in-
structions.
21
JNG DONE
PUSH DWORD [_y]
PUSH DWORD [_x]
CALL foo
ADD ESP,8
MOV [_z],EAX
DONE:
(c) y=0;
for (x=1;x>0;x<<=1) y++;
Solution:
MOV DWORD [_y],0
MOV DWORD [_x],1
TOP: CMP DWORD [_x],0
JNG DONE
INC DWORD [_y]
SHL DWORD [_x],1
JMP TOP
DONE:
42. Translate the following C function into a sequence of Intel protected mode instructions
long int Limit(long int min, long int x, long int max)
{
if (x<min) x = min;
if (x>max) x = max;
return x;
}
Solution:
22
43. Translate each of the following C assignment statements into an equivalent sequence of
Intel assembly language instructions:
x=y;
Solution:
MOV EAX,[_y]
CDQ
MOV [_x],EAX
MOV [_x+4],EDX
(b) unsigned short int x,y,z; /* 16 bits */
z = x/y;
Solution:
MOV AX,[_x]
SUB DX,DX
DIV WORD [_y]
MOV [_z],AX
(c) signed long int x,y,z; /* 32 bits */
z = x % y;
Solution:
MOV EAX,[_x]
CDQ
IDIV DWORD [_y]
MOV [_z],EDX
44. Translate the following C function into a sequence of Intel protected mode instructions.
Solution:
23
FillArray: PUSH EBP ;standard prologue
MOV EBP,ESP
MOV EAX,[EBP+8] ;get baseAdrs
MOV ECX,[EBP+12] ;get items
MOV EDX,[EBP+16] ;get value
L1: CMP ECX,0 ;items>0?
JNG L2 ;done if not
DEC ECX ;items--
MOV [EAX+ECX],EDX ;fill element
JMP L1 ;repeat loop
L2: POP EBP ;epilogue
RET
45. Which of the following must be true in order for the CPU in an 80x86 based system with
8259 interrupt controller to respond to an interrupt request? (list all that apply)
(a) The interrupting device must raise its corresponding IRQ line;
(b) The 8259 PIC mask register must have a 0 in the bit that corresponds to the device’s
IRQ line;
(c) The 8259 PIC in-service register must have 0’s in the bits that correspond to the IRQ
lines of all higher priority devices;
(d) Flag IF must be set to 1;
(e) The current instruction must be completed.
46. Which of the following are true statements? (list all that apply)
47. Which of the following are true statements? (list all that apply)
24
Solution:All of the above.
48. Which of the following 80x86 registers are pushed on the stack when the CPU responds to
an interrupt? (list all that apply)
(a) EAX
(b) EIP
(c) DS
(d) CS
(e) EFlags
49. Which of the following are re-enabled by executing an STI instruction immediately upon
entry to an ISR?
Solution:Option 49c.
50. Which of the following provide the index used to retrieve an address from the Interrupt
Descriptor Table (IDT) for a hardware interrupt?
Solution:Option 50a
51. Which of the following provide the index used to retrieve an address from the Interrupt
Descriptor Table (IDT) for a software interrupt?
Solution:Option 51b
52. Which of the following provide the index used to retrieve an address from the Interrupt
Descriptor Table (IDT) for an exception?
25
(a) The 8259 PIC;
(b) The second byte of an INT instruction;
(c) A hard-coded constant built into the CPU.
Solution:Option 52c.
53. Which of the following are true? (list all that apply)
(a) The starting address of the interrupt descriptor table (IDT) is always zero;
(b) The starting address of the IDT is stored in the IDTR register;
(c) The starting address of the IDT is different for hardware interrupts, software inter-
rupts and exceptions.
(d)
Solution:Option 53b.
54. Which of the following I/O methods provides the slowest possible data transfer rate?
Solution:Option 54b.
55. If interrupts are enabled and no interrupt routines are executing, the maximum delay before
an interrupt request is acknowledged by the CPU is...
(a) The time required for one memory read or write cycle;
(b) The maximum time required to fetch a single instruction from memory;
(c) The maximum time required to fetch and execute a single instruction.
Solution:Option 55c.
56. The maximum delay before a DMA request is acknowledged by the CPU is:
(a) The time required for one memory read or write cycle;
(b) The maximum time required to fetch a single instruction from memory;
(c) The maximum time required to fetch and execute a single instruction.
Solution:Option 56a.
26
57. In an Intel 80x86 microprocessor system, what device limits the ultimate effect of execut-
ing an STI (enable interrupts) instruction at the beginning of an interrupt service routine
so that only interrupts of higher priority are re-enabled?
Solution:Option 57c.
58. At the end of an interrupt service routine on an Intel 80x86 based microprocessor system,
we typically find a pair of instructions that send the value 2016 out to I/O port 2016 in
order to re-enable interrupts from lower priority devices. Executing these two instructions
changes the values held in
Solution:Option 58c.
59. Which of the following techniques should never be used to protect a critical section?
Solution:Option 59c.
60. Which of the following is only appropriate for protecting short critical sections? (list all
that apply)
27
(e) Semaphores.
Solution:Option 61c.
62. Which of the following can protect data that is shared between a thread and an interrupt
service routine? (list all that apply)
Solution:Option 62a.
63. Which of the following methods can prevent loss of data due to interrupt overrun?
(a) Setting a flag while the interrupt service routine (ISR) is busy, and testing on entry;
(b) Keeping interrupts enabled during the ISR
(c) Masking the device’s IRQ line in the PIC;
(d) Using a faster processor.
Solution:Option 63d - the key work here is loss. All other techniques prevent overrun but
lose data.
64. True or False : Each thread in a non-preemptive multi-threaded application must have its
own stack.
Solution:True.
65. True or False : Each thread in a preemptive multi-threaded application must have its own
stack.
Solution:True.
28
66. When a non-preemptive kernel is used, the context switch occurs:
Solution:Option 66a.
Solution:Option 67c.
68. The following function transmits a packet of information over a serial communications line
but may transmit corrupted packets when called by more than one thread in a multi-tasking
application. Describe what changes (if any) are required and why.
Solution:Preemptive Kernel: All of the code inside the SendPacket function comprises
a critical section and must be protected against an interrupt which may trigger a context
switch (and thus a possible reentry) by another thread. Immediately upon entry to the func-
tion we need a semaphore pend, and just before exit we need a semaphore post. Disabling
interrupts is not acceptable since the Send1Byte function may be interrupt driven.
Non-Preemptive Kernel: A context switch in a non-preemptive application would require
an explicit yield call to be coded by the application programmer. Although non appear
within the SendPacket function, it is possible (and likely) that one is coded within the
Send1Byte function while it waits for the serial device to finish sending the previous byte.
29
Disabling interrupts won’t provide protection since it won’t prevent the yield call. Again,
the only reliable solution is as before - protecting the critical section with a semaphore
pend-post.
69. Critical sections can be protected using any of the following techniques. Which three cause
the same detrimental effect on response time?
Solution:Option 69c.
Solution:Option 71b.
Solution:Option 72c.
30
73. Which threads compete for the same shared resource during an unbounded priority inver-
sion?
74. Suppose that threads T1 and T2 compete for shared resources R1 and R2. Which resource
acquisition sequences can cause deadlock.
75. Which solution to unbounded priority inversion has the least detrimental effect on system
response time?
Solution:Option 75b.
76. When both the main program and and interrupt service routine of a conventional (non-
multithreaded) application program access the same shared data, what method must be
used in the main program to prevent data corruption?
Solution:Option 76a.
77. A spin lock uses a flag stored in a Boolean variable to indicate whether or not a corre-
sponding shared resource is available. Which of all of the following statements are true?
(a) The flag can only be tested while the scheduler is running the thread that contains the
spin lock;
31
(b) The flag itself is a shared memory object;
(c) A spin lock’s test-and-set operation is a critical section;
(d) With spin locks, while one thread owns the shared resource, other threads waiting for
the same resource can be removed from the scheduler’s list of ready threads.
Solution:Option 78b.
79. Any kernel call that changes the ready status of a thread:
(a) Always invokes the scheduler, and thus perhaps a context switch;
(b) Only invokes the scheduler in a non-preemptive kernel;
(c) Only invokes the scheduler in a preemptive kernel.
Solution:Option 79a.
Solution:Option 80b.
81. Which thread owns the shared resource during an unbounded priority inversion?
Solution:Option 81c.
32
(a) The low priority thread is preempted by the medium priority thread;
(b) The low priority thread is preempted by the high priority thread;
(c) The medium priority thread is preempted by the high priority thread.
83. Consider an application with three threads T1, T2 and T3 and three shared resources R1,
R2 and R3. Fill in the blanks in a manner that corresponds to a three-way deadlock
Solution:
84. Which solution to unbounded priority inversion raises a thread’s priority for the least
amount of time?
Solution:Option 84b.
85. Which of the following memory allocation methods can be used in a shared function for
thread-specific local objects?
(a) Automatic;
(b) Static;
(c) Dynamic;
(d) Those generated by an alloca call.
86. Which of the following memory allocation methods can be used in a shared function for a
shared local object?
(a) Automatic;
(b) Static;
(c) Dynamic;
33
(d) Those generated by an alloca call.
87. Which of the following memory allocation methods are not possible for global objects?
(a) Automatic;
(b) Static;
(c) Dynamic;
(d) Those generated by an alloca call.
Solution:Option 87b.
(a) All shared functions are called by more than one thread;
(b) All shared functions are re-entrant;
(c) All shared functions contain shared objects;
(d) All shared functions are thread-safe;
Solution:Option 88a.
89. Use casts and subscripting to set the 2nd byte (bits 8-15) of a 32-bit int called x to zero
without modifying the other three bytes.
Solution:
((char *)&x)[1]=0;
90. What is the maximum number of bytes of memory that are allocated for data at any moment
during the execution of the following programs?
34
(e) int main(void)
{
int n=1000;
for (in j=0; j<2; j++)
{
char *p=alloc(n);
}
}
(f) int main(void)
{
int n=1000;
for (in j=0; j<2; j++)
{
char a[n];
}
}
Solution:Option 90a - 3000 bytes, Option 90b - 2000 bytes, 90c - 3000 bytes, Option
90d - 2000 bytes, Option 90e - 2000 bytes, Option 90f - 1000 bytes.
(g) int main(void)
{
int n=1000;
for (in j=0; j<2; j++)
{
char a[n];
}
}
91. Which of the following memory allocation methods support conservation of memory through
re-use?
(a) Automatic;
(b) Register;
(c) Static;
(d) Dynamic;
(e) Using alloca;
(f) Variable size arrays;
(g) None of the above;
92. Which of the following memory allocation methods are possible outside of a function?
35
(a) Automatic;
(b) Register;
(c) Static;
(d) Dynamic;
(e) Using alloca;
(f) Variable size arrays;
(g) None of the above;
93. Which of the following memory allocation methods cause the same object to be initialized
more than once during execution?
(a) Automatic;
(b) Register;
(c) Static;
(d) Dynamic;
(e) Using alloca;
(f) Variable size arrays;
(g) None of the above;
94. Which of the following memory allocation methods require calling a function to create an
object?
(a) Automatic;
(b) Register;
(c) Static;
(d) Dynamic;
(e) Using alloca;
(f) Variable size arrays;
(g) None of the above;
Solution:Option 94d.
95. Which of the following memory allocation methods destroys objects transparently during
program execution?
36
(a) Automatic;
(b) Register;
(c) Static;
(d) Dynamic;
(e) Using alloca;
(f) Variable size arrays;
(g) None of the above;
96. Which of the following memory allocation methods may be used to maintain a value in an
object from one invocation of a function to the next invocation of the same function?
(a) Automatic;
(b) Register;
(c) Static;
(d) Dynamic;
(e) Using alloca;
(f) Variable size arrays;
(g) None of the above;
97. Which of the following memory allocation methods will prevent application of the ‘address
of’ operator to an object?
(a) Automatic;
(b) Register;
(c) Static;
(d) Dynamic;
(e) Using alloca;
(f) Variable size arrays;
(g) None of the above;
Solution:Option 97b.
98. Which of the following memory allocation methods requires that the allocated object be
referenced indirectly through a pointer?
37
(a) Automatic;
(b) Register;
(c) Static;
(d) Dynamic;
(e) Using alloca;
(f) Variable size arrays;
(g) None of the above;
99. Which of the following memory allocation methods is not available inside a function?
(a) Automatic;
(b) Register;
(c) Static;
(d) Dynamic;
(e) Using alloca;
(f) Variable size arrays;
(g) None of the above;
Solution:Option 99g.
100. Which of the following memory allocation methods is used for function parameters?
(a) Automatic;
(b) Register;
(c) Static;
(d) Dynamic;
(e) Using alloca;
(f) Variable size arrays;
(g) None of the above;
Solution:Option 100a.
101. The code shown below compiles without fatal errors, but doesn’t execute as intended.
Provide a corrected version.
38
char *AddressOfCopy(int value)
{
int copy=value;
return ©
}
Solution:
102. Consider the program below, and then answer the following questions.
(a) What is the maximum number of instances of the function parameter n that co-exist
at any one time during the execution of the program?
(b) What is the maximum number of instances of the static integer s that co-exist at any
one time during the execution of the program?
(c) What is the maximum number of instances of automatic integer a that co-exists at
any one time during the execution of the program?
(d) Which two of the three objects n, s and a are allocated from the same region of
memory?
#include <stdio.h>
void go(int n)
{
static int s=0;
int a=++s;
if (n) go(n-1);
39
printf(" n %08X %d\n", &n, n);
printf(" a %08X %d\n", &a, a);
printf(" s %08X %d\n", &s, s);
printf("\n");
int main(void)
{
go(3);
return 0;
}
int x=0;
int main(void)
{
printf("%d",x);
x=4; one();
printf("%d",x);
x=5; two(x);
printf("%d",x);
x=6;
{
int x;
x=7; three();
printf("%d",x);
}
printf("%d",x);
x=8; four(&x);
printf("%d",x);
40
return 0;
}
\solstart
In order of {\tt printf} call: 0,4,5,7,2,3.
\fi
int foo(void)
{
int x=0;
x = x+1;
return x;
}
int bar(void)
{
static int x=0;
x=x+1;
return x;
}
int main(void)
{
printf("%d",foo());
printf("%d",foo());
printf("%d",foo());
printf("%d",foo());
return 0;
}
105. Rearrange the code shown below so that the scope of all variables is restricted as much as
possible without altering the intended operation of the code.
#include <stdio.h>
41
int old_value=0;
int average;
Solution:
#include <stdio.h>
106. Which lines of the following C program cause the compiler to produce an error message
(not just a warning message)?
int *f(int p)
{
register int r1=0;
register int r2=r1+1;
int a1=0;
int a2=a1+1;
42
static int s1=0;
static int s2=s1+1;
return &p;
}
Solution:
107. Suppose that variables x, y and z are all declared to be of type int which is a 16-bit
quantity. Suppose further that they are used to represent signed fixed point quantities as
follows
where x = a.b means that x has a bits for the whole number plus sign bit part and b
bits for the fractional part. Translate the following floating point statements into their fixed
point equivalents.
(a) z = x+y;
Solution:
z = (x>>4)+(y>>2);
(b) z = x*y;
Solution:
z = (x*y)>>10;
(c) z = x/y;
Solution:
z = (x>>2)/y;
(d) y = x*z;
Solution:
43
z = (x*z)>>6;
(e) z = 1 + x + xˆ2/2;
Solution:
z = (1<<4) + x>>4 + (x*x)>>11;
108. For each of the above computations, determine that maximum dynamic range, and the
maximum precision of the result.
44