Question: Assignment 2 through 5 dealt with coding for F 1 5 translator. In contrast this assignment is a problem workout. Consider the FORTRAN program in

Assignment 2 through 5 dealt with coding for F15 translator. In contrast this assignment is a problem
workout.
Consider the FORTRAN program in Table 1 to test 495 as the 3-digits Kaprekars Constant:
1. Translate the above program to three address codes:
(a) Generate the array of TAC instructions starting at index 100.[5+2+10+8=25]
i. For function isValidInput.
ii. For function swap.
iii. For function getDescendingAndAscending.
iv. For program KaprekarConstant3Digit.
String constants need handling as constants in static area.
Note: You may extend the set of 3-address instructions with read n, print n, and stop.
Note: mod is a built-in system that behaves the same as %
(b) Show the Symbol Table for program KaprekarConstant3Digit (works as the Global Symbol
Table in this context) with the symbol name, data type, category, and size. Mark appropriate
parent / child symbol table pointers to build the tree of symbol tables. [5]
(c) Show the Symbol Tables for functions isValidInput, swap and getDescendingAndAscending
showing the symbol name, data type, category, size, and offset. Mark appropriate parent / child
pointers to build the tree of symbol tables. [3+2+5=10]
2. Peephole optimize the code of program KaprekarConstant3Digit, and functions isValidInput and
getDescendingAndAscending with multiple iterations as needed: [10*3=30]
Renumber the peephole-optimized quads from 100.
3. For program KaprekarConstant3Digit, and functions isValidInput & getDescendingAndAscending,
perform the following. [2*3+6*3+2*3=30]
(a) Identify the leader quads, construct the basic blocks and build the CFG. If the control from a
basic block falls through to a goto, link directly to the target of the goto.
(b) For every block, optimize for local common sub-expression, copy propagation and dead-code
elimination within the block using value-numbering and further peephole-optimization.
(c) Regenerate the array of TAC instructions and redraw the CFG after local optimization for for pro-
gram KaprekarConstant3Digit, and functions isValidInput & getDescendingAndAscending.
PROGRAM KaprekarConstant3Digit
IMPLICIT NONE
INTEGER :: num, iterations, descending, ascending
LOGICAL :: isValidInput ! Declare the function type explicitly
PRINT *, "Enter a three-digit number (digits not all same):"
READ *, num
IF (.NOT. isValidInput(num)) THEN
PRINT *, "Invalid! Input must be a three-digit integer with at least two different digits."
STOP
END IF
iterations =0
DO
CALL getDescendingAndAscending(num, descending, ascending)
num = descending - ascending
iterations = iterations +1
PRINT *, "Iteration ", iterations, ": ", num
IF (num ==495) EXIT
END DO
PRINT *, "Kaprekar's constant reached in ", iterations, " iterations."
END PROGRAM KaprekarConstant3Digit
SUBROUTINE getDescendingAndAscending(n, descending, ascending)
IMPLICIT NONE
INTEGER, INTENT(IN) :: n
INTEGER, INTENT(OUT) :: descending, ascending
INTEGER :: d1, d2, d3
d1= n /100! Extract digits
d2= MOD(n /10,10)
d3= MOD(n,10)
IF (d1< d2) CALL swap(d1, d2)! Sort digits for descending order
IF (d1< d3) CALL swap(d1, d3)
IF (d2< d3) CALL swap(d2, d3)
descending = d1*100+ d2*10+ d3
IF (d1> d2) CALL swap(d1, d2)! Sort digits for ascending order
IF (d1> d3) CALL swap(d1, d3)
IF (d2> d3) CALL swap(d2, d3)
ascending = d1*100+ d2*10+ d3
END SUBROUTINE getDescendingAndAscending
SUBROUTINE swap(a, b)
IMPLICIT NONE
INTEGER, INTENT(INOUT) :: a, b
INTEGER :: temp
temp = a
a = b
b = temp
END SUBROUTINE swap
LOGICAL FUNCTION isValidInput(n)
IMPLICIT NONE
INTEGER, INTENT(IN) :: n
INTEGER :: d1, d2, d3
IF (n <100.OR. n >999) THEN
isValidInput =.FALSE.
RETURN
END IF
d1= n /100
d2= MOD(n /10,10)
d3= MOD(n,10)
isValidInput =(d1/= d2).OR.(d1/= d3).OR.(d2/= d3)! Check if all digits are the same
END FUNCTION isValidInput
Table 1: Program to compute 3-digits Kaprekars Constant

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!