Question

For the C code 1 int loop while(int a, int b) int result 1; while (a < b) t result (atb); return result; Gcc generates the fo

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Answer A: The %edx register initialized on line 6 is used to stor the sum of the inputs a and b, and it is updated on line 11 to increment the sum by 1 as the value of a is incremented through the loop.

Answer B & Answer C in table below:

Register Table (input a's value at EBP + 8 and input b's value at EBP +12 )
Step no. Operation Operatnds Description of operation ECX EBX EAX EDX
1 movl 8(%ebp), %ecx /* Move value of 'a' into ECX. */ a
2 movl 12(%ebp), %ebx /* Move value of 'b' into EBX. */ a b
3 movl $1, %eax /* Move value 1 into EAX. */ a b 1
4 cmpl %ebx, %ecx /* Compare the values in registrs EBX and ECX i.e. b and a respectively (a >= b) */ check(a > = b)
5 jge .L11 /* If the contents of ECX are greater than or equal to the contents of EBX , jump to .L11. Else, continue next instruction. */ a b 1
6 leal (%ebx,%ecx), %edx /* Add contents of EBX and ECX and store in EDX */ a b 1 (a+b)
7 movl $1, %eax /* Move value 1 into EAX. */ a b 1 (a+b)
8 .L12: /* Label .L12 */ a b 1 (a+b)
9 imull %edx, %eax /* Multiply contents of EDX with contents of EAX and store result in EAX */ a b (a+b+(n-1))^n (a+b)
10 addl $1, %ecx /* Add 1 to contents of ECX and store in ECX */ a+n b (a+b+(n-1))^n (a+b)
11 addl $1, %edx /* Add 1 to contents of EDX and store in EDX */ a+n b (a+b+(n-1))^n (a+b)+n
12 cmpl %ecx, %ebx /* Compare the values in registrs ECX and EBX i.e. a and b respectively (b > a(incremented value)) */ check(b > a+n)
13 jg .L12 /* If the contents of EBX are greater than the contents of ECX, jump to .L12. Else, continue next instruction i.e actually exit. */ a+n b (a+b+(n-1))^n (a+b)+n
14 .L11: /* Label .L11 */

Answer D:


int loop_while_mod(int a, int b)
{
   int result = 1;
   int temp = 0;
   if(a >= b) goto L11;
   temp = a+b;
   L12:
    result = temp*result;
    a++;
    temp++;
   if(b > a) goto L12;
    L11:
    return result;
}

Add a comment
Know the answer?
Add Answer to:
For the C code 1 int loop while(int a, int b) int result 1; while (a < b) t result (atb); return ...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • WHAT is the value of A,B, and C Consider the following code fragment where the right-hand...

    WHAT is the value of A,B, and C Consider the following code fragment where the right-hand side of the assignments in func are to be completed by answering this question: typedef struct point { float *p; struct { float x; float y; }s; struct point* next; } point; void func(point *sp1, point* sp2) { sp2->p = A; sp2->S.X = B; sp2->s.y = c; int maino { point *init, *final; // assume init has been initialized to some value; func(init, final);...

  • #define SIZE 10 prob3: void prob3 (int mat [SIZE] [SIZE]) int r, c pushl movl tebp...

    #define SIZE 10 prob3: void prob3 (int mat [SIZE] [SIZE]) int r, c pushl movl tebp esp, ebp edi 2 pushl esi pushl ebx pushl $4, esp 8 (ebp), eax $1, (eax) subl movl mat [0] [0] 1; movl leal 40 (teax), Sebx movl $1, 40 (teax) 11 addl $80, teax 12 eax, -16 (tebp) movl 13 movl $1, tedi .L2 14 jmp 15 for (r 1; r<SIZE; r++) ( L5: 16 -16 (ebp), tebx movl 17 mat [r] [0]...

  • the answer is movl 8(%ebp), %edx or movl 4(%ebp), %edx? Consider the following C and assembly...

    the answer is movl 8(%ebp), %edx or movl 4(%ebp), %edx? Consider the following C and assembly code: int increment(int x){ return x+1; } increment: 1 push? 2 movl 3 movl 4 addl 5 6 popl ret %ebp %esp, %ebp 8(%ebp), %eax $1, %eax %ebp Suppose we want to move the return address into register %edx on line 5. Write 1 line of assembly code to perform this action. (Write your answer with exactly 2 spaces: one after the instruction, and...

  • A function, fun_a, has the following overall structure: int fun_a(unsigned x) I int val 0 while r...

    A function, fun_a, has the following overall structure: int fun_a(unsigned x) I int val 0 while retura Th e Gcc Ccompiler generates the following assembly code: %ebp+8 novi movl test1 x at 8 (%ebp), %eax 80, %eax %edx, %eax 2 L.7 5 .L10 %eax, %edx し10 xor1 %eax shr1 Shift right by1 ne 9.L7 10 andl $1, %eax Reverse engineer the operation of this code and then do the following: A. Use the assembly-code version to fill in the missing...

  • URGENT HELP NEEDED :( Convert the following C++ program into an x86 assembly language program. Comment...

    URGENT HELP NEEDED :( Convert the following C++ program into an x86 assembly language program. Comment the start of each "code block" that performs one of the listed mathematical calculations. Comments go to the right of the actual code, all starting on the same column. Post ONLY your ASM file here to Blackboard when complete. // Global variables char a = 5; short b = 7; int   c = 11; int   d = 13; // Code int main() {    ...

  • You are charged with maintaining a large C rogram and you come across the following code...

    You are charged with maintaining a large C rogram and you come across the following code typedef struct int left a-struct a [CNT] int right b-struct; void testlint i, b-struct *bp) int n bp- left bp-rights a-struct ap &bp- ali] ap- Cap- idx] n; The declaration of the compile time constant CNT and the structure a struct are in a file for which you don't have necessary access privilege. Fortunately you have a copy of the o' version of code,...

  • Exercise 3 [Conditionals] Consider the following assembly code for a function F3 with two integer arguments:...

    Exercise 3 [Conditionals] Consider the following assembly code for a function F3 with two integer arguments: F3: push EBP mov EBP, ESP mov EDX, DWORD PTR [ebp+12] mov EAX, DWORD PTR [ebp+8] cmp EAX, EDX # setup stack #if # goto .L1 #EAX- mov EAX, EDX # ignore for now mov DWORD PTR [EBP-4], EAX mov ESP, EBP pop EBE ret # cleanup stack To the right of each instruction, show the contents of the register whose value changes as...

  • Assembly Language NASM create a substring ASSIGNMENT INSTRUCTIONS: Create the Substring from the Given string, beginIndex...

    Assembly Language NASM create a substring ASSIGNMENT INSTRUCTIONS: Create the Substring from the Given string, beginIndex and endIndex The program should create a new string that is a substring of this string. The substring begins at the specified beginIndex and extends to the character at index endIndex – 1. Thus the length of the substring is endIndex-beginIndex. In other words you can say that beginIndex is inclusive and endIndex is exclusive while getting the substring. Initialize the following values in...

  • NOTE: explain the lines in comments for better understanding Write an assembly program (for x86 processors...

    NOTE: explain the lines in comments for better understanding Write an assembly program (for x86 processors - Irvine) that has two procedures, a main procedure and a procedure called Fib. The fib procedure is to uses a loop to calculate and printout the first N Fibonacci numbers. Fibonacci sequence is described by the following formula: Fib(1) = 1, Fib(2) = 1, Fib(n) = Fib(n – 1) + Fib(n – 2). The value of N is to be communicated to this...

  • 1) Assume the registers are initialized to the indicated values: Se al A c and code...

    1) Assume the registers are initialized to the indicated values: Se al A c and code frogments MUST use 32 ぁ ar movennes (anor be "nedfra-ne ibrary function or procedural calls ino- Pe Peue c for ll codr ond code frogments only othu ey document your code with Comments unless the code size is Drow Fow Chort for any flow of control involving branches and loops Use PoCedures as modules to organize most of the code and code fragments. In...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT