Return to course page

Pre-quiz on third week's material.

Question 1: The instruction leaq 7(%rax, %rbx, 4), %rdx does which of the following?

  1. rdx = 7 + rax + rbx*4

  2. *(7 + rax + rbx*4) = rdx

  3. rdx = *(7 + rax + rbx*4)

  4. rdx = 7 + (rax + rbx)*4

  5. *(7 + (rax + rbx)*4) = rdx

  6. rdx = *(7 + (rax + rbx)*4)

The textbook presents the following to implementations of factorial (in figures 3.19 and 3.20):

fact_do:                    fact_while:
    movl $1, %eax               movl $1, %eax
L2:                             jmp L5
    imulq %rdi, %rax        L6:
    subq $1, %rdi               imulq %rdi, %rax
    cmpq $1, %rdi               subq $1, %rdi
    jg  L2                  L5:
    rep                         cmpq $1, %rdi
    ret                         jg L6
                                rep
                                ret

Question 2: (see above) Consider the mathematical instructions imulq, subq, and cmpq. Let d be the number of these instructions executed by fact_do and w be the number of these instructions executed by fact_while. Assume both functions are invoked with the argument 3 (which is passed into the functions in %rdi). What is the value of d - w?

Answer:

Question 3: (see above) Compare the speed of fact_do and fact_while

  1. they are equally fast

  2. fact_do is faster by a constant number of cycles (an additive constant)

  3. fact_do is faster by a constant factor (a multiplicative constant)

  4. fact_do is asymptotically faster

  5. fact_while is faster by a constant number of cycles (an additive constant)

  6. fact_while is faster by a constant factor (a multiplicative constant)

  7. fact_while is asymptotically faster

Question 4: Figure 3.28 explains how six arguments are passed to a function in six registers. A function with seven arguments

  1. cannot be implemented using x86-64 calling conventions

  2. would pass the arguments in seven registers

  3. would pass the seventh argument on the stack

  4. would pass the first argument on the stack

  5. would pass all seven arguments on the stack

  6. none of the above

Consider the Y86-64 instruction encodings in Figure 4.2. Assume that memory is little-endian.

Question 5: (see above) Why not encode ret, halt, and nop in just 4 bits instead of 8?

  1. it is impossible to have an instruction shorter than a byte

  2. it possible but inefficient at runtime to have an instruction shorter than a byte

  3. a real system probably would use 4-bit instructions, but the book wanted to simplify the encoding to make the student's life easier

Question 6: (see above) Suppose address 0x10 contains instruction rrmovq %rsp, %r8. If we read the value in address 0x10 as a short value, it will be

  1. 0x10

  2. 0x2048

  3. 0x4820

  4. 0x284

  5. 0x8402

  6. none of the above

Return to main page
Return to course page