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 through dealt with coding for F translator. In contrast this assignment is a problem
workout.
Consider the FORTRAN program in Table to test as the digits Kaprekars Constant:
Translate the above program to three address codes:
a Generate the array of TAC instructions starting at index
i For function isValidInput.
ii For function swap.
iii. For function getDescendingAndAscending.
iv For program KaprekarConstantDigit.
String constants need handling as constants in static area.
Note: You may extend the set of address instructions with read n print n and stop.
Note: mod is a builtin system that behaves the same as
b Show the Symbol Table for program KaprekarConstantDigit 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.
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.
Peephole optimize the code of program KaprekarConstantDigit, and functions isValidInput and
getDescendingAndAscending with multiple iterations as needed:
Renumber the peepholeoptimized quads from
For program KaprekarConstantDigit, and functions isValidInput & getDescendingAndAscending,
perform the following.
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 subexpression, copy propagation and deadcode
elimination within the block using valuenumbering and further peepholeoptimization.
c Regenerate the array of TAC instructions and redraw the CFG after local optimization for for pro
gram KaprekarConstantDigit, and functions isValidInput & getDescendingAndAscending.
PROGRAM KaprekarConstantDigit
IMPLICIT NONE
INTEGER :: num, iterations, descending, ascending
LOGICAL :: isValidInput Declare the function type explicitly
PRINT "Enter a threedigit number digits not all same:
READ num
IF NOT. isValidInputnum THEN
PRINT "Invalid! Input must be a threedigit integer with at least two different digits."
STOP
END IF
iterations
DO
CALL getDescendingAndAscendingnum descending, ascending
num descending ascending
iterations iterations
PRINT "Iteration iterations, : num
IF num EXIT
END DO
PRINT "Kaprekar's constant reached in iterations, iterations."
END PROGRAM KaprekarConstantDigit
SUBROUTINE getDescendingAndAscendingn descending, ascending
IMPLICIT NONE
INTEGER, INTENTIN :: n
INTEGER, INTENTOUT :: descending, ascending
INTEGER :: d d d
d n Extract digits
d MODn
d MODn
IF d d CALL swapd d Sort digits for descending order
IF d d CALL swapd d
IF d d CALL swapd d
descending d d d
IF d d CALL swapd d Sort digits for ascending order
IF d d CALL swapd d
IF d d CALL swapd d
ascending d d d
END SUBROUTINE getDescendingAndAscending
SUBROUTINE swapa b
IMPLICIT NONE
INTEGER, INTENTINOUT :: a b
INTEGER :: temp
temp a
a b
b temp
END SUBROUTINE swap
LOGICAL FUNCTION isValidInputn
IMPLICIT NONE
INTEGER, INTENTIN :: n
INTEGER :: d d d
IF n OR n THEN
isValidInput FALSE.
RETURN
END IF
d n
d MODn
d MODn
isValidInput d dORd dORd d Check if all digits are the same
END FUNCTION isValidInput
Table : Program to compute digits Kaprekars Constant
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
