从3个嵌套循环中计算延迟

从3个嵌套循环中计算延迟

本文介绍了从3个嵌套循环中计算延迟的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的锻炼:•在1 MHz时钟频率下使用三个环路计算可能的最大延迟. (回答49.94 s)

My exercise: •Calculate the maximum delay possible using three loops @ 1 MHz clock frequency. (Answer 49.94 s)

delay:   ldi    r23,$FF          ;Initialise 3rd loop counter
loop3:   ldi    r24,$FF          ;Initialise 2nd loop counter
loop2:   ldi    r25,$FF          ;Initialise 1st loop counter
loop1:   dec         r25              ;Decrement the 1st loop counter
          brne   loop1            ;and continue to decrement until 1st loop counter = 0
          dec    r24               ;Decrement the 2nd loop counter
          brne   loop2            ;If the 2nd loop counter is not equal to zero repeat the 1st loop, else continue
          dec    r23
          brne   loop3
      ret                   ;Return

我正在尝试使用这3个循环来计算最大延迟,答案显然是49.94 s,我真的在挣扎,用2个嵌套循环要简单得多.

I'm trying to calculate the maximum delay using those 3 loops the answer apparently is 49.94 s and I'm really struggling it was much simpler with 2 nested loops.

这是我尝试过的方法,但是答案还很遥远.

Here is what I've tried, but the answer is way off.

33*((255*3)-1) + 17*((33*3)-1) + 11*3

ldi-1个时钟周期,brne 1或2个时钟周期

ldi - 1 clock cycle, brne 1 or 2 clock cycles

架构:ATmega8535

Architecture: ATmega8535

推荐答案

对于初学者来说,最长的循环会将0而不是FF加载到计数器,但是让我们坚持使用FF,这样我们可以得到预期的答案.使用FF,循环运行254次,并在255号退出.

For starters, the longest loop would load 0 not FF to the counter, but let's stick with FF so we get the expected answer. With FF the loop runs 254 times and exits on the 255th.

对于ldi,通用公式为1,对于完整迭代为(n-1) * (body + 3)(对于dec1,对于brne2),对于最后一个是(body + 2)(对于dec1对于未使用brne). body表示循环主体中的任何内容,因为最里面的循环0为空.

The general formula is 1 for ldi, (n-1) * (body + 3) for the full iterations (1 for dec and 2 for brne) and (body + 2) for the final one (1 fordec and 1 for not taken brne). body means whatever is in the loop body, for the innermost loop that's 0 as it's empty.

因此,对于最内层的循环:1 + 254 * (0 + 3) + (0 + 2) = 765.对于中间循环,body是最内层循环中的765,因此我们具有:1 + 254 * (765 + 3) + (765 + 2) = 195840.对于最外面的循环,body是中间循环的195840,因此我们有:1 + 254 * (195840 + 3) + (195840 + 2) = 49939965这是预期的答案.

Thus, for the innermost loop: 1 + 254 * (0 + 3) + (0 + 2) = 765.For the middle loop, the body is the 765 from the innermost loop, thus we have: 1 + 254 * (765 + 3) + (765 + 2) = 195840.For the outermost loop the body is the 195840 from the middle loop, thuswe have: 1 + 254 * (195840 + 3) + (195840 + 2) = 49939965 which is the expected answer.

这篇关于从3个嵌套循环中计算延迟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 12:40