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
The higher the astm grain size number, the finer the gran is. a)-true b)-false
Answers: 2
question
Engineering, 04.07.2019 18:20
Inspection for bearing condition will include: (clo4) a)-color b)-smell c)-size d)-none of the above
Answers: 1
question
Engineering, 04.07.2019 19:10
Agas contained within a piston-cylinder assembly e end nation about same energy states, 1 and 2, where pi 10 bar, v undergoes two processes, a and b, between the sam 0.1 m3, ui-400 kj and p2 1 bar, v2 1.0 m2, u2 200 kj: process a: process from 1 to 2 during which the pressure- volume relation is pv constant process b: constant-volume process from state 1 to a pressure of 2 bar, followed by a linear pressure-volume process to +20 0 state 2 kinetic and potential energy effects can be ignored. for each of the processes a and b, (a) sketch the process on p-v coordinates, (b) evaluate the work, in kj, and (c) evaluate process the heat transfer, in kj
Answers: 2
question
Engineering, 06.07.2019 02:30
Precipitation hardening can be achieved in many light alloys by a three-step heat treatment. what is carried out and what is the purpose at each step? why is such a treatment necessary based on the nucleation and growth theory of phase transformation. compare this treatment with tempering martensite for carbon steels (similarities and differences).
Answers: 1
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, 06.02.2021 17:30
question
World Languages, 06.02.2021 17:30
question
Physics, 06.02.2021 17:30
question
Health, 06.02.2021 17:30
Questions on the website: 13722363