C822 of 55
0 XP
  1. Hook
  2. Problems

C8 · Problem set 4

Problem set 4: C, pointers, bits and integer arithmetic

These problems use lessons 17 to 21: how C compiles to assembly, pointers and memory-mapped registers, the routines of the assembly lab, multiplication, division and fixed point, and floating point. An int is 4 bytes, a short 2 and a char 1, and bits are numbered from 0 at the right.

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, 214 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?

Have paper and a calculator ready. Work each problem out by hand, then enter the answer. Your first try on each one counts toward your score.

Practice

Keep practicing

16 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.