Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The cycles are on the right of the code 5. Given the following three loop struct

ID: 3883714 • Letter: T

Question

The cycles are on the right of the code

5. Given the following three loop structures discussed in class METHOD 1: METHOD 2: METHOD 3: org$800 jsr bgmac(4) stop org$800 jsr bgmac(4) stop org $800 jsr bgmac(4) stop #XA #YA #0,MA #0,MA+2 bgmac lax #XA ;(2) | bgmac lax ; (2) | bgmac1dx #XA Idy movw movw clrb #YA #0,MA #0,MA+2 Idy movw movw ldab Idy movw movw ldab #YA #0,MA #0,MA+2 ; (5) ; (5) ; (5) ; (5) ; (5) ; (5) : (1) loop dbeg b,exit: (3/3) loop emacs MA (13) +1 loop cmpb #N :(13) beq exit (3/1) emacs MA emacs MA leax 2, x leay 2,y bra loo leax 2,x leay 2,y dbne b,loop; (3/3) (13) ; (3) exit rts incb bra ; (1) exit rts loop equ ? XA rmb*N YA rmb*N MA rmb 4 exit rts ; (5) N equ ? equ ? rmb 2*N XArmb 2*N YA rmb 2*N MArmb 4 XA MA rmb 4 Write a formula for the total execution time (in machine cycles) of each of the programs, above, as a function of N (for N2. Note that the cycle count for each instruction is given; for conditional branches, these are given as cycles for the branch taken/not taken. Then, using the showcycles command in the CW Command window, determine the number of cycles consumed by each method for N=255 (set a breakpoint at the stop instruction) Method 1 Method 2 Method 3

Explanation / Answer

Formula for total execution time is:

Method 1: 25*N+26

Method 2: 23*N+27

Method 3: 20*N+24

Number of cycles consumed when N=255:

Method 1: 25*255+26 = 6401

Method 2: 23*255+27 = 5892

Method 3: 20*255+24 = 5124

Explaination:

There are two types of instructions in each method: sequential(outside loop block) and looping(inside loop block). All the sequential instructions are taking same time in all the methods i.e. 4[jsr]+2[ldx]+2[ldy]+5[movw]+5[movw]+1[clrb/ldab]+5[exit] = 24 machine cycles.

Total Execution Time = (num_times_loop_executed * total_time_taken_looping_instr )+ time_consumed_exiting_loop + time_taken_seq_instr

Method1:

num_times_loop_executed = N (clrb initializes the register b with 0 and loop increments b by value 1 every time its executed. Loop exits when it's value equals N. Hence loop is executed from value 0,1,.....,N-1.)

total_time_taken_looping_instr = 1+3+13+2+2+1+3=25

time_consumed_exiting_loop = 1[cmpb] + 1[beq] = 2 (When value stored in regesiter is N then loop will exit and only these two instructiona will be executed)

Hence,

Total Execution Time = N * 25 + 2 + 24 = 25 * N +26

Method 2:

num_times_loop_executed = N (ldab initailizes register with N+1 and when dbeq is executed, it first decrements the value by 1 then checks branching condition. Thus loop is being executed for the values N+1,N.....,2. When this value becomes 1, dbeq will decrement it to 0 & loop will exit.)

total_time_taken_looping_instr = 3+13+2+2+3 = 23

time_consumed_exiting_loop = 3 (When value stored in registar reaches 0 then loop will exit and only dbeq instruction will be executed)

Hence,

Total Execution Time = N * 23 + 3 + 24 = 23 * N +27

Method 3:

num_times_loop_executed = N (ldab initailizes register with N , loop is being executed for the values N,N-1.....,1).

total_time_taken_looping_instr = 13 + 2 +2 +3 = 20

time_consumed_exiting_loop = 0 (When value stored in registar reaches 1, dbeq will decrement it to 0 and exit immediately, hence no further execution will take place

Hence,

Total Execution Time = N * 20 + 0 + 24 = 20 * N +24

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote