Using the online GCC/GAS Assembler, write an assembly program which computes 3x3 - 2x2 + x - 1 using just the general purpose 32-bit registers, EAX, EBX, ECX, and EDX. You can save and restore registers on the stack if necessary. Your code must work for both positive and negative integers. As an example, if x is 4, the result will be 163, while the result for -4 will be -229.
Your program will end by moving the computed result to %eax
and
returning. However, the return value will typically be too large to
display. If you put the result in %eax
and set a breakpoint for
the ret
instruction, stepping one step from that will show as
the function's result just after executing the return instruction (so you
will need to step one step after getting to the breakpoint). For example,
executing the code
mov $9876, %rbx mov $12345, %raxup to the
ret
statement, then clicking once
on step over gives this result where
the red circle on the right shows the returned value is 12345. As an aside, if you run
this program to completion, it will say "Program finished with exit code
57". The 57 is because the C runtime does some processing on return results
(for example, masking off various bits). So the printed return value is
often different from the value you have placed into %rax
or %eax
. In OnlineGDB, The only way to see the actual value
returned (in %rax
) is to take one step after
reaching ret
.
%ecx
and leave the value in that
register. This simulates passing the value as a parameter to a
function. In particular, one should be able to change the value
of x by changing the value of %ecx
in one place
in your code.