C813 of 55
0 XP
  1. Hook
  2. Problems

C8 · Problem set 2

Problem set 2: loops, arrays, strings and structs

These problems use lessons 9 to 12: branches and loops, arrays and strings, how structs and unions are laid out, and the heap and linked structures. Words are 4 bytes, memory is little-endian, and instructions run one per cycle unless a problem says otherwise.

Check

A loop runs 50 times with a 3-instruction body.

(a)

With the test at the top (a branch out, the body, a jump back), how many instructions run, counting the final failed test?

(b)

With the test at the bottom (the body, then a branch back while not done)?

Check

A branch at address 1024 goes back 3 instructions to the top of a loop.

(a)

What byte offset does it encode?

(b)

What is the target address?

Check

short m[5][25] starts at 8192, stored row by row.

(a)

Where is m[3][1]?

(b)

How many bytes apart are m[i][j] and m[i + 1][j]?

Check

strcmp compares two strings byte by byte. They first differ at position 8 (counting from 0).

(a)

How many passes does its loop make?

(b)

How many byte loads does it make?

Check

On RV32, struct { char a; T b; char c; } where T is a 4-byte type aligned to 4.

(a)

How big is the struct?

(b)

And with b first?

Check

struct rec { int id; short x; short y; char tag[8]; } (20 bytes) is stored in an array from 4096.

(a)

What address does recs[2].y read?

(b)

And recs[2].tag[3]?

Check

The word DEADBEEF is stored at 0x1000.

(a)

What does lbu read from 0x1001?

(b)

What does lhu read from 0x1000?

Check

A big-endian 32-bit field arrives as the bytes 18, 2, 86, 120.

(a)

What value does it hold?

(b)

What does lw read on RV32?

Check

A linear search through 50 words runs 5 instructions a pass, plus 2 to set up. The value sits at index 9.

(a)

How many instructions does it run to find it?

(b)

And for a value that isn't there?

Check

int *p and short *q both hold 4096.

(a)

What does p + 3 hold?

(b)

And q + 3?

Check

A status word 12345678 holds a 5-bit field starting at bit 4.

(a)

What is the field's value?

(b)

What mask selects it in place?

Check

A switch on x with cases 0 to 10 − 1 compiles to a jump table of word addresses at 8192.

(a)

Which address holds the target for x = 2?

(b)

How many bytes is the table?

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.