But aside from these difficulties in C there is a more serious problem

But aside from these difficulties in c there is a

This preview shows page 6 - 9 out of 9 pages.

Exercise 8.16 (a) ent f lod x ldc 1 adi ret ent g lod x lod y adi ret mst mst ldc 3 cup f ldc 4 ldc 5 adi cup g (b) The text did not describe how to return a value in three-address code, since the call instruction was described as a one-address instruction. We extend the call to a two-address instruction to indicate a name for the return value. Thus t1 = call f indicates that the return value of f should be stored in t1 . With this convention, three-address code for the given program is: entry f t1 = x + 1 return t1 entry g t2 = x + y return t2 begin_args begin_args arg 3 t3 = call f
Image of page 6
Compiler Construction: Principles and Practice Chapter 8 Exercise Answers, Page 7 arg t3 t4 = 4 + 5 arg t4 call g Exercise 8.19 0: IN 0,0,0 input u 1: IN 1,0,0 input v 2: JEQ 1,6(7) if v == 0 jump to output 3: LDA 2,0(1) move v to reg 2 4: DIV 1,0,1 divide u by v, place in reg 1 5: MUL 1,1,2 multiply by v 6: SUB 1,0,1 subtract from u 7: LDA 0,0(2) move reg2 to u 8: LDA 7,-7(7) jump to location 2 9: OUT 0,0,0 print u 10: HALT 0,0,0 actually unnecessary Exercise 8.20 (a) As in the answer to the previous exercise, if we want to move a value from register r to register s, we would write: LDA s,0(r) (b) How to imitate a call is described in item 8 on page 457 of the text. To imitate a return, we must load the return address into the pc. If the return address is in register s, this is done by LDA 7,0(s) If the return value is at some offset a with base address in register s, then we must use LD 7,a(s) Exercise 8.24 The Borland compiler code works as follows. It first loads x into register ax (the accumulator) and compares it to the value of y in memory. Then, if x is greater than or equal to y , the code performs a jump to the code xor ax,ax
Image of page 7
Compiler Construction: Principles and Practice Chapter 8 Exercise Answers, Page 8
Image of page 8
Image of page 9

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture