C8 Computer organization · Practice
Problem set 4: C, pointers, bits and integer arithmetic
Problems dealt one at a time, with new numbers every time. The kinds you miss come back more often, and you can keep going as long as you like.
0 solved
0 in a row
0 of 16 kinds solid
Check
An array starts at 2000, and the code reads element 3.
(a)
Where is a[3] if a is an int array?
(b)
Where is it if a is a short array?
Check
struct point { int id; int x; int y; } takes 12 bytes, with id at offset 0, x at 4 and y at 8. An array pts of these starts at 1000.
(a)
What address does pts[2].y read?
(b)
What offset does the lw use if a register already points at pts[2]?
Check
A device register holds 5B.
(a)
What does it hold after r |= (1 << 0)?
(b)
What does (r >> 0) & 1 give for the original value?
Check
A device register holds 5B.
(a)
What does it hold after r &= ~(1 << 1)?
(b)
What would it hold after r ^= (1 << 1) instead?
Check
A control register keeps a 3-bit field in bits 4 upward.
(a)
What mask selects the field?
(b)
What is the largest value the field can hold?
Check
Two numbers, 0.35 and 0.3, are stored in Q15.
(a)
What integer holds 0.35?
(b)
Their integers are multiplied, is added and the sum is shifted right by 15. What integer results?
Check
A 12-bit ADC with a 2500 mV reference reads 3358.
(a)
What does multiplying by 2500 and shifting right by 12 give?
Write units the way you normally would: 1.5 mV, 1500 µV and 0.0015 V are the same answer.
(b)
What is the largest product the multiply can produce?
Check
Signed division with div and rem, which round toward zero.
(a)
What does div give for 23 ÷ −2?
(b)
What does rem give for 23 and −2?
Check
The assembly lab's bit counters are run on 100.
(a)
How many passes does the x & (x - 1) loop make?
(b)
How many passes does a loop make that tests the low bit and shifts right until x is 0?
Check
A compiler computes x × 20 as (x << p) + (x << q) with p > q.
(a)
What is p?
(b)
What is q?
Check
int *p and char *q both hold 1000.
(a)
What address does p + 3 hold?
(b)
What address does q + 3 hold?
Check
A 16-bit Q8.8 number has 8 integer bits (sign included) and 8 fraction bits.
(a)
What integer stores 3.25?
(b)
What is the largest value Q8.8 can hold?
Check
A compiler multiplies x = 777 by 3 with a shift and an add: (x << p) + x.
(a)
What is p?
(b)
What does it compute?
Check
−2.25 and 0.5 are stored in Q8.8 and multiplied as integers.
(a)
What is the integer product?
(b)
What Q8.8 integer does shifting right by 8 give?
Check
for (i = 0; i < n; i++) a[i] = b[i] + c; runs with n = 200.
(a)
Indexed, each pass computes two addresses (slli, add, add), loads, adds, stores, increments i and branches: 8 instructions. How many in all?
(b)
With two pointers walking (load, add, store, two pointer addis, branch on an end pointer), how many?
Check
A filter on a microcontroller without an FPU works on floats near 300, doing 200 software multiply-adds per sample at 150 cycles each, at 200 MHz.
(a)
What's the spacing between floats near 300?
(b)
How many samples per second can it filter?
A kind of problem counts as solid once you've got it right on the first try twice in a row. Back to the lesson · Practice every topic in C8