subject
Engineering, 22.01.2020 07:31 mistydsargentp5wrh4

Ben bitdiddle is trying to compute the function f(a, b) = 2a+3b for nonnegative b. he goes overboard in the use of function calls and recursion and produces the following high-level code for functions f and g.

// high-level code for functions f and g

int f(int a, int b) {

int j;

j = a;

return j + a + g(b); }

int g(int x) {

int k;

k = 3;

if (x == 0) return 0;

else return k + g(x − l);

}

ben then translates the two functions into assembly language as follows. he also writes a function, test, that calls the function f(5, 3).

; arm assembly code ;

f: r0= a, r1 = b, r4 = j;

; g: r0= x, r4 = k

0x8000 test mov r0, #5 ; a = 5

0x8004 mov r1, #3 ; b = 3

0x8008 bl f ; call f(5, 3)

0x800c loop b loop ; and loop forever

0x8010 f push {r1,r0,lr, r4} ; save registers on stack

0x8014 mov r4, r0 ; j = a

0x8018 mov r0, r1 ; place b as argument for g

0x801c bl g ; call g(b)

0x8020 mov r2, r0 ; place return value in r2

0x8024 pop {r1,r0} ; restore a and b after call

0x8028 add r0, r2, r0 ; r0 = g(b) + a

0x802c add r0, r0, r4 ; r0 = (g(b) + a) + j

0x8030 pop {r4,lr} ; restore r4, lr

0x8034 mov pc, lr ; return

0x8038 g push {r4,lr} ; save registers on stack

0x803c mov r4, #3 ; k = 3

0x8040 cmp r0, #0 ; x == 0?

0x8044 bne else ; branch when not equal

0x8048 mov r0, #0 ; if equal, return value = 0

0x804c b done ; and clean up

0x8050 else sub r0, r0, #1 ; x = x-1

0x8054 bl g ; call g(x - 1)

0x8058 add r0, r0, r4 ; r0 = g(x - 1) + k

0x805c done pop {r4,lr} ; restore r0,r4,lrfrom stack

0x8060 mov pc, lr ; return

you will probably find it useful to make drawings of the stack similar to the one in figure 6.14 to you answer the following questions.

(a) if the code runs starting at test, what value is in r0 when the program gets to loop? does his program correctly compute 2a+3b?

(b) suppose ben changes the instructions at addresses 0x8010 and 0x8030 to push {r1,r0,r4} and pop {r4}, respectively. will the program

(1) enter an infinite loop but not crash;

(2) crash (cause the stack to grow beyond the dynamic data segment or the pc to jump to a location outside the program);

(3) produce an incorrect value in r0 when the program returns to loop (if so, what or

(4) run correctly despite the deleted lines?

(c) repeat part (b) when the following instructions are changed. note that labels aren’t changed, only instructions.

(i) instructions at 0x8010 and 0x8024 change to push {r1,lr, r4} and pop {r1}, respectively. (ii) instructions at 0x8010 and 0x8024 change to push {r0,lr, r4} and pop {r0}, respectively. (iii) instructions at 0x8010 and 0x8030 change to push {r1,r0, lr} and pop {lr}, respectively. (iv) instructions at 0x8010, 0x8024, and 0x8030 are deleted.

(v) instructions at 0x8038 and 0x805c change to push {r4} and pop {r4}, respectively.

(vi) instructions at 0x8038 and 0x805c change to push {lr} and pop {lr}, respectively.

(vii) instructions at 0x8038 and 0x805c are deleted.

ansver
Answers: 3

Another question on Engineering

question
Engineering, 04.07.2019 18:10
Acompressor receives the shaft work to decrease the pressure of the fluid. a)- true b)- false
Answers: 3
question
Engineering, 04.07.2019 18:10
Calculate the bore of a cylinder that has a stroke of 18 inches and an extension time of 6 seconds at a flow rate of 4 gal/min.
Answers: 3
question
Engineering, 04.07.2019 18:10
Machinery that is a key part of the process and without which the plant or process cannot function is classifed as: (clo4) a)-critical machinery b)-essential machinery c)-general purpose machinery d)-none of the specified options.
Answers: 1
question
Engineering, 04.07.2019 18:20
Inadequate stores control is not an obstacle to effective work order system. (clo4) a)-true b)-false
Answers: 3
You know the right answer?
Ben bitdiddle is trying to compute the function f(a, b) = 2a+3b for nonnegative b. he goes overboard...
Questions
question
Mathematics, 02.12.2020 03:50
question
Mathematics, 02.12.2020 03:50
question
Mathematics, 02.12.2020 03:50
question
Medicine, 02.12.2020 03:50
Questions on the website: 13722361