In this lab you are expected to compare the source code and
In this lab, you are expected to compare the source code and assembly code for the following C code. (you can get the assembly code with usually –s flag). Can you see any optimizations that can be done on the assembly code?
int a=1, b=2, c=3;
int proc1 (int a, int b, int c)
{ If (a>0) return b – c;
else return b + c; }
int main()
{ int w, x, y; w = proc1 (a, b, c);
switch (w)
{ case 0: x=3; break;
case 1: x=4; break;
case 2: x=7; break;
default: x=9; break; }
y= w + x; return 0; }
what this program does and how assembly code can be optimized.
Solution
step 1: starting with main
step 2: will call function proc1(1,2,3)
step 3: in proc1 condition check a=1,a>1yes so b-c=(2-3) = -1
step 4: w having value -1 case value{0,1,2} so default case will run
step 5: x=9,and w=-1 so y=w+x (9-1) answerrrrrr: y=8
