【问题标题】:Determining how many clock cycles AVR assembly language code will take to execute确定执行 AVR 汇编语言代码需要多少时钟周期
【发布时间】:2019-07-02 02:46:10
【问题描述】:

如果给我如下汇编代码,我如何确定执行需要多少时钟周期?

      ldi r20, 250
loop: inc r20
      brne loop
      nop

在数据表中,所有指令占用 16 位(1 个指令字)。

自己尝试,我得到 14 作为答案。因为ldi r20, 250 被调用一次(1 个周期),所以在溢出到零之前循环被调用了6 次(6x2=12 个周期)。最后,nop 需要 1 个周期。总共是 14 个周期。

但是,答案显然是 19 个周期。谁能告诉我我做错了什么?

【问题讨论】:

    标签: assembly avr


    【解决方案1】:

    您已跳过在每个循环中包含 inc。所以每个循环都是brne (2) + inc (1)。因此,您的计算应该是(r20 括号中的值):

    1
    1 (251)
    2
    1 (252)
    2
    1 (253)
    2
    1 (254)
    2
    1 (255)
    2
    1 (0)
    1
    1
    

    brne是1个循环不取分支时。

    如果您展开该循环,它会更明显:

    ; (4 cycles)
    ldi r20, 250 ; 1
    inc r20      ; 1
    nop          ; these two represent the brne at 2 cycles because it branches
    nop
    
    ; (3 cycles)
    inc r20      ; 1
    nop          ; these two represent the brne at 2 cycles because it branches
    nop
    
    ; (3 cycles)
    inc r20      ; 1
    nop          ; these two represent the brne at 2 cycles because it branches
    nop
    
    ; (3 cycles)
    inc r20      ; 1
    nop          ; these two represent the brne at 2 cycles because it branches
    nop
    
    ; (3 cycles)
    inc r20      ; 1
    nop          ; these two represent the brne at 2 cycles because it branches
    nop
    
    ; (2 cycles)
    inc r20      ; 1
    nop          ; this represents the brne at 1 cycle, because its just overflowed and therefore ** will not branch **
    
    ; (1 cycle)
    nop
    

    【讨论】:

    • 谢谢!我实际上在想每个循环brne 只需要一个循环。现在我在指令集摘要的细则中看到它说如果条件为真则需要 2 个周期。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-27
    • 2010-10-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多