Engineering
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.

answer
Answers: 3

Other questions on the subject: Engineering

image
Engineering, 04.07.2019 18:10, Talos02
Burgers vector is generally parallel to the dislocation line. a)-true b)-false
Answers: 2
image
Engineering, 04.07.2019 18:20, myahlit84
Inadequate stores control is not an obstacle to effective work order system. (clo4) a)-true b)-false
Answers: 3
image
Engineering, 04.07.2019 18:20, rjone8429
Asimple rankine cycle uses water as the working fluid. the water enters the turbine at 10 mpa and 480c while the condenser operates at 6 kpa. if the turbine has an isentropic efficiency of 80 percent while the pump has an isentropic efficiency of 70 percent determine the thermal efficiency
Answers: 1
image
Engineering, 04.07.2019 19:10, nayi2002
The short distance from the objective lens to the object causes problems at high magnification. which of the following is the most serious? a. cleaning the object surface b. positioning the object c. reflection from the object surface. d. illumination of the object
Answers: 1
Do you know the correct answer?
Ben bitdiddle is trying to compute the function f(a, b) = 2a+3b for nonnegative b. he goes overboard...

Questions in other subjects:

Konu
Chemistry, 19.09.2019 20:00