Exercise set 4

Back to course page
Answers
  1. Let’s assume you have a stack-architecture CPU, with the following instructions:
    LOAD [address] Loads from memory into the register stack.
    LOAD imm Loads an immediate onto the register stack.
    STORE [address] Pops the stack top into memory.
    COPY Pushes a new copy of the value at the top of the stack.
    SWAP Swaps the top two values on the stack.
    ADD Pops the top two values from the stack, then pushes their sum.
    SUB Pops the top two values from the stack, then pushes their difference. The first value popped is the subtrahend and the second value is the minuend (difference=minuendsubtrahend).
    MUL Pops the top two values from the stack, then pushes their product.
    DIV Pops the top two values from the stack, then pushes their quotient. The first value popped is the divisor and the second value is the dividend (quotient=dividend÷divisor).
    If you need to implement a complex math library for this architecture, how would you implement the following? (Assume that you can load from a, b, c, and d by name, and that you’re storing the real and imaginary components to x and y, respectively.)
    1. Complex addition: (a+bi)+(c+di)=(a+c)+(b+d)i
    2. Complex multiplication: (a+bi)(c+di)=(acbd)+(bc+ad)i
    3. Complex division:
      a+bi = (ac+bd)+(bcad)i
      c+di c²+d²
  2. Implement these three fragments for an Intel 80387-class FPU. (If you’re feeling specific, assume that each component is double-precision.) Remember, you have FLD, FST/FSTP, FADD/FADDP, FSUB/FSUBP, FSUBR/FSUBRP, FMUL/FMULP, and FDIV/FDIVP at your disposal. (You’ve got plenty of other stuff too, but this is all you should need.)