198k views
1 vote
int decode2(int x, int y, int z); is compiled into 32bit x86 assembly code. The body of the code is as follows: NOTE: x at %ebp+8, y at %ebp+12, z at %ebp+16 movl 12(%ebp), %edx subl 16(%ebp), %edx movl %edx, %eax sall $31, %eax sarl $31, %eax imull 8(%ebp), %edx xorl %edx, %eax Parameters x, y, and z are stored at memory locations with offsets 8, 12, and 16 relative to the address in register %ebp. The code stores the return value in register %eax. The shl or sal instruction is used to shift the bits of the operand destination to the left, by the number of bits specified in the count operand Write C code for decode2 that will have an effect equivalent to our assembly Code. int decode2(int x, int y, int z) { }

1 Answer

2 votes

Answer: provided in the explanation segment

Step-by-step explanation:

This looks a bit confusing but you can follow through using the provided code.

/*

Line 1. x at %ebp+8, y at %ebp+12, z at %ebp+16

Line 2. movl 12(%ebp), %edx

Line 3. subl 16(%ebp), %edx

Line 4. movl %edx, %eax

Line 5. sall $31, %eax

Line 6. sarl $31, %eax

Line 7. imull 8(%ebp), %edx

Line 8. xorl %edx, %eax

*/

#include<stdio.h>

// function decode above program

int decode2(int x,int y,int z){

// In Line 2 y is stored in variable

int var1 = y;

// In Line 3 subtract z from var1 and store in var1

var1 = var1 - z;

// In Line 4 move var1 value to another value count

int count = var1;

// In Line 5 left shift by 31 value in count

count<<31;

// In Line 6 right shift by 31 value in count

count>>31;

// In Line 7 multiply x with value in var1

var1 = var1*x;

// In Line 8 xor the var1 and count and store in count

count = var1 ^ count;

// return count;

return count;

}

// main function for testing

int main(){

printf("Enter the value of x ");

int x;

scanf("%d",&x);

printf("Enter the value of y ");

int y;

scanf("%d",&y);

printf("Enter the value of z ");

int z;

scanf("%d",&z);

printf("value of decode2(%d,%d,%d) is %d\\",x,y,z,decode2(x,y,z));

return 0;

}

cheers i hope this helps

User Ryanna
by
5.0k points