Question
Consider the following assembly code: abs(int): movl %edi, %eax movl %edi, %edx sarl $31, %edx xorl %edx, %eax subl %edx, %eax ret which is what
Consider the following assembly code:
abs(int): movl %edi, %eax movl %edi, %edx sarl $31, %edx xorl %edx, %eax subl %edx, %eax ret
which is what an older version of our gcc compiler produced, using the optimization level 1 (-O1), when it compiled the following abs(int x) function:
int abs(int x) { if ( x < 0 ) x = -x; return x; }
Notice how gcc assembles abs(int x) without branching, i.e., without affecting the execution flow (without using the jump instruction). We shall see in our next Unit that branching is rather unpredictable and may cause problem in the execution pipeline of the microprocessor.
Let's hand trace the above assembly code using the Test Case 1: x = 7 and expected result = 7:
movl %edi, %eax copies the content of %edi (x = 7) into %eax.
Once executed, here is the content of our registers:
%edi <- 00000000000000000000000000000111 <- 29 0s ->
%eax <- 00000000000000000000000000000111 <- 29 0s ->
movl %edi, %edx copies the content of %edi (x = 7) into %edx.
Once executed, here is the content of our registers:
%edi <- 00000000000000000000000000000111 <- 29 0s ->
%edx <- 00000000000000000000000000000111 <- 29 0s ->
sarl $31, %edx shifts the content of %edx right (arithmetic fill in with sign bit->0) 31 times.
Once executed, here is the content of our register:
%edx <- 00000000000000000000000000000000 <- 32 0s ->
xorl %edx, %eax
%eax <- 00000000000000000000000000000111 ^ %edx <- 00000000000000000000000000000000 %eax <- 00000000000000000000000000000111
subl %edx, %eax
%eax <- 00000000000000000000000000000111 - %edx <- 00000000000000000000000000000000 %eax <- 00000000000000000000000000000111
ret
And the return value is %eax = 00000000000000000000000000000111 = 7
Your task in this question is to hand trace the above assembly code using the Test Case 2: x = -7 and expected result = 7.
Use the above hand tracing as a model for your answer, i.e., follow its format when showing the result of executing each instruction. Make sure you show the full content of both registers for the mov* instructions and make sure you show the full content of the modified register for the other instructions. Finally, show the value that is returned to the caller (or calling) function.
Remember that x (-y) = x + y.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started