Question: Figure 3.21 C and assembly code for while version of factorial using guarded-do translation. The fact_while_gd_goto function illustrates the operation of the assembly-code version.

For C code having the general form long loop_while2 (long a, long b) { 1 2 3 4 5 6 long result while C 7 8

(a) C code long fact_while(long n) { } 1 2 WN (c) Corresponding assembly-language code long fact_while (1ong

Figure 3.21 

C and assembly code for while version of factorial using guarded-do translation. The fact_while_gd_goto function illustrates the operation of the assembly-code version.

9 10 11 12 13 jg rep; ret .L8: movq ret .L7 %rsi, %rax

We can see that the compiler used a guarded-do translation, using the jle instruction on line 3 to skip over the loop code when the initial test fails. Fill in the missing parts of the C code. Note that the control structure in the assembly code does not exactly match what would be obtained by a direct translation of the C code according to our translation rules. In particular, it has two different ret instructions (lines 10 and 13). However, you can fill out the missing portions of the C code in a way that it will have equivalent behavior to the assembly code.

For C code having the general form long loop_while2 (long a, long b) { 1 2 3 4 5 6 long result while C 7 8 result = b = } GCC, run with command-line option -01, produces the following code: a in %rdi, b in %rsi loop_while2: } return result; = testq %rsi, %rsi jle .L8 movq %rsi, %rax .L7: imulq subq testq %rdi, %rax %rdi, %rsi %rsi, %rsi

Step by Step Solution

3.31 Rating (148 Votes )

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock

While the generated code does not follow the exact pattern of the guardeddo translation ... View full answer

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 Computer Systems A Programmers Perspective Questions!