Question

In: Computer Science

. Finally the penny drops with your boss. He realises this all has some impact on...

. Finally the penny drops with your boss. He realises this all has some impact on how he writes his programs. He says he doesn’t understand how the simple instructions you described can be used to implement his complex programs. He knows the compiler must achieve this somehow, but has no clue what it might do. You decide to show him using the iterative solution to a most frequently recursive problem – determining fibonacci numbers. You focus on the body of the function rather than the function call elements of the code.
In doing so you recall learning in Programming Languages that in C we are guaranteed that logic expressions will be “short cut”, that is only as many terms of the expression will be evaluated as is necessary to determine the logic value. In the case of ||, as soon as one of the expressions evaluates to true, then no more of the expressions will be evaluated.

int fibonacci(int number) {
int f, d, t, i;
if (number == 0 || number == 1) {
f = number;
} else {
}
}
return f; }
f = 1;
d = 1;
for (i = 0; i < number - 2; i++) {
t = f;
f = f + d;
d = t;
i. Translate the above C source code into ARM assembly language, showing each stage in the process. (You don’t need to translate the function call and return parts).
ii. Explain why we might get unexpected results when using bitwise and (&) instead of logical and (&&)
iii. Explain how shortcutting simplifies the evaluation of the || expression in the code you have just translated.

Solutions

Expert Solution

The given above code must be written as,

int fibonacci(int number) {

int f, d, t, i;

if (number == 0 || number == 1) {

f = number;

}

else

{

f = 1;

d = 1;

for (i = 0; i < number - 2; i++) {

t = f;

f = f + d;

d = t;

}

}

return f; }

i. The Fibonacci sequence Translation

Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be:

1, 2, 3, 5, 8, 13, 21, 34, 55, 89 and so on...

By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms.

fibonacci(int):

        push    rbp

        mov     rbp, rsp

        mov     DWORD PTR [rbp-20], edi

        cmp     DWORD PTR [rbp-20], 0

        je      .L2

        cmp     DWORD PTR [rbp-20], 1

        jne     .L3

.L2:

        mov     eax, DWORD PTR [rbp-20]

        mov     DWORD PTR [rbp-4], eax

        jmp     .L4

.L3:

        mov     DWORD PTR [rbp-4], 1

        mov     DWORD PTR [rbp-8], 1

        mov     DWORD PTR [rbp-12], 0

.L5:

        mov     eax, DWORD PTR [rbp-20]

        sub     eax, 2

        cmp     DWORD PTR [rbp-12], eax

        jge     .L4

        mov     eax, DWORD PTR [rbp-4]

        mov     DWORD PTR [rbp-16], eax

        mov     eax, DWORD PTR [rbp-8]

        add     DWORD PTR [rbp-4], eax

        mov     eax, DWORD PTR [rbp-16]

        mov     DWORD PTR [rbp-8], eax

        add     DWORD PTR [rbp-12], 1

        jmp     .L5

.L4:

        mov     eax, DWORD PTR [rbp-4]

        pop     rbp

        ret

ii. Explanation

The logical and operator ‘&&’ expects its operands to be boolean expressions (either 1 or 0) and returns a boolean value.
The bitwise and operator ‘&’ works on Integral (short, int, unsigned, char, bool, unsigned char, long) values and return Integral value.

iii. Shortcutting the code

In c programming

int fibonacci(int number) {

int f, d, t, i;

f = number==0?1:number;

d = 1;

for (i = 0; i < number - 2; i++) {

t = f;

f = f + d;

d = t;

}

return f; }

Translation for above code in assembly language

fibonacci(int):

        push    rbp

        mov     rbp, rsp

        mov     DWORD PTR [rbp-20], edi

        cmp     DWORD PTR [rbp-20], 0

        je      .L2

        mov     eax, DWORD PTR [rbp-20]

        jmp     .L3

.L2:

        mov     eax, 1

.L3:

        mov     DWORD PTR [rbp-4], eax

        mov     DWORD PTR [rbp-8], 1

        mov     DWORD PTR [rbp-12], 0

.L5:

        mov     eax, DWORD PTR [rbp-20]

        sub     eax, 2

        cmp     DWORD PTR [rbp-12], eax

        jge     .L4

        mov     eax, DWORD PTR [rbp-4]

        mov     DWORD PTR [rbp-16], eax

        mov     eax, DWORD PTR [rbp-8]

        add     DWORD PTR [rbp-4], eax

        mov     eax, DWORD PTR [rbp-16]

        mov     DWORD PTR [rbp-8], eax

        add     DWORD PTR [rbp-12], 1

        jmp     .L5

.L4:

        mov     eax, DWORD PTR [rbp-4]

        pop     rbp

        ret

Explanation

The ternary operator is used to simplify your if-else statements that are used to assign values to variables and could reduce the lines of compilation in your code.


Related Solutions

Jack had been working on his gas furnace all afternoon. He finally thought that he fixed...
Jack had been working on his gas furnace all afternoon. He finally thought that he fixed the old, dilapidated furnace. It was going to get really cold that evening so he wanted to make sure that the furnace would keep them warm all night. Jack went to bed at 11:00 p.m. as usual but he noticed that he had a slight headache before going to bed. He just thought that he needed to sleep. His wife and kids also had...
As a stock analyst, your boss, Jerry, has asked you to compile some information on stock...
As a stock analyst, your boss, Jerry, has asked you to compile some information on stock of Southern Infrastructure Corporation including a 95% confidence interval for the mean daily return that he needs to include in a report to senior management. He says that he is also not sure exactly what a 95% confidence interval means and would like you to add an explanation. You have been following the share price of Southern Infrastructure Corporation and have recorded the daily...
Your boss has given you some data on a project (Project C). The project will require...
Your boss has given you some data on a project (Project C). The project will require an initial investment of $450,000 which will result in cash flows of $120,000 in the first three years, and $130,000 in the next three years. It is expected that $15,000 of the initial investment will be recoverable at the end of the project. Assuming a cost of capital of 12% calculate the following (you must provide the units): Assuming a cost of capital of...
Your boss is so impressed with your answers to the previous questions, he assigns you an...
Your boss is so impressed with your answers to the previous questions, he assigns you an additional task of calculating the selling price for a new product. The company expects that a $25,000 profit goal is reasonable for this product if they are able to sell 18,000 units. What is the appropriate selling price for this new product? Remember that you can calculate the contribution by adding fixed costs to the profit goal and then dividing by the total quantity...
6. Your boss has asked you to evaluate a portfolio strategy that purports to capture all...
6. Your boss has asked you to evaluate a portfolio strategy that purports to capture all of the upside and none of the downside of the market. The key is a complex rule for buying and selling shares. You decide to simulate the strategy and discover that it lags the market on the upside and loses a small amount on average when the market declines. What’s more, when you test the strategy with historical data it suffers sharp losses in...
Your grandfather gives your $10,000 to invest and you must invest every penny. He wants you...
Your grandfather gives your $10,000 to invest and you must invest every penny. He wants you to have an expected return of 12.40% for your portfolio. You can chose between stock J with an expected return of 13% and stock L with an expected return of 8%. How much should you invest in stock J to achieve these results?
You just got off the phone with your boss, and boy was he mad. The final...
You just got off the phone with your boss, and boy was he mad. The final numbers just came in for the year, and you went over your budget by over 6%. As Director of Operations, you oversee most of the production departments at the Whispering Phone Book. Your budget covers production, distribution, and operations. Your boss wouldn’t listen when you tried to explain that the overage was not all your fault, and in fact, was not all bad. You...
All analysis should be done in MS Excel, and a memo to your boss explaining the...
All analysis should be done in MS Excel, and a memo to your boss explaining the situation, your analysis, and conclusion should be prepared in MS Word. You must upload both files to be graded. You will only get full credit for the assignment if your analysis is correct, all calculations and/or formulas use cell references, and your memo has the right content, flows well, and is properly formatted, and grammatically correct. An example of a good memo, and an...
Your favorite client, Mr. T, tells you that in 2019 he finally turned his longstanding meteorite...
Your favorite client, Mr. T, tells you that in 2019 he finally turned his longstanding meteorite hunting hobby (something he’s loved since he was a kid) into a “real business.” You ask a few due diligence questions and find out the following: Over the past few years Mr. T has found around 50 actual meteorites. He has sold only four of those, at prices ranging from $8,000 to $15,000; his other “finds” he’s kept even though there is a ready...
in your opinion , Is the EU a success for all nations? Some nations? Has it...
in your opinion , Is the EU a success for all nations? Some nations? Has it been harmful to some nations? Explain. minimum 400 words
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT