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, 03.07.2019 14:10
Amass of 1.5 kg of air at 120 kpa and 24°c is contained in a gas-tight, frictionless piston-cylinder device. the air is now compressed to a final pressure of 720 kpa. during the process, heat is transferred from the air such that the temperature inside the cylinder remains constant. calculate the boundary work input during this process.
Answers: 2
question
Engineering, 04.07.2019 18:10
Assuming compressible flow of air and that the measurements are done at flagstaff a pitot static tube that gives the difference of total and static pressure measures 0.35 m of mercury. what is the velocity of air? assume the temperature to be 300k. (submit your excel or matlab calculation sheet)
Answers: 1
question
Engineering, 04.07.2019 18:20
Derive the correction factor formula for conical nozzle i=-(1+ cosa) and calculate the nozzle angle correction factor for a nozzle whose divergence hal-fangle is 13 (hint: assume that all the mass flow originates at the apex of the cone.
Answers: 3
question
Engineering, 04.07.2019 19:10
Afoot bridge is made as a simple deck, 4 m long, with a cross section 2 m (wide) and 20 cm thick, and made of wood. the deck is supported at the two ends. the maximum load allowable on the bridge is 10 tons, provided it is uniformly distributed on the deck. to sense this load, a strain gauge is placed at the center of the bridge and its resistance is monitored. if the sensor has a nominal resistance of 350 s2 and a gauge factor of 3.6, what is the reading of the strain gauge at maximum load? the modulus of elasticity for the wood used in the construction is 10 gpa.
Answers: 2
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, 24.01.2020 08:31
question
Biology, 24.01.2020 08:31
question
Computers and Technology, 24.01.2020 08:31
Questions on the website: 13722361