C8 Computer organization · Practice
Problem set 2: loops, arrays, strings and structs
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
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?
Check
A nested loop: the outer runs 10 times with 3 instructions of its own; the inner runs 8 times per outer pass, each pass 4 instructions plus 2 of loop overhead.
(a)
How many instructions in all?
(b)
What share of them is the inner loop's overhead?
Check
A loop adds 3 copies of 9 × 10⁸ into a 32-bit signed register.
(a)
What does the register hold?
(b)
What true sum should it have held?
Check
A word copy loop runs lw, sw, two pointer addis, a count addi and a bnez, 6 instructions per word, after 3 to set up. It copies 100 words.
(a)
How many instructions does it run?
(b)
How many would a byte-by-byte copy of the same data take, with the same loop shape?
Check
A list of 1000 nodes holds 8 bytes of data and a 4-byte link per node, from an allocator with an 8-byte header that rounds blocks to multiples of 8.
(a)
How many bytes of heap does the list use?
(b)
How many times the size of a plain array of the same data is that?
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