当前位置: 动力学知识库 > 问答 > 编程问答 >

assembly language, strange loop

问题描述:

I'm struggling to understand what this code does

ldi r20, 80

loop: asr r20

brsh loop

nop

What is this code doing and how many clock cycles does this code take to execute?

网友答案:

This seems to be Atmel AVR assembly code. In a loop that continues until the carry bit of the status register is set, it shifts hexadecimal 80 arithmetically right by one position so that r20 takes the successive values of 80, then c0, then e0, then f0, f8, fc, fe, ff. Until now, the low bit has been 0, and this value has been shifted into the carry bit of the status register. But on the next shift, the low bit of ff is shifted, which is a 1. This causes the loop to exit.

See http://www.atmel.com/webdoc/avrassembler/avrassembler.wb_ASR.html on the ASR instruction.

The net effect is to cause a small delay.

网友答案:

For clock cycles:

  • ldi = 1,
  • asr = 1,
  • brsh = Documented at 1/2 I think 1 for false condition and 2 for true.
  • nop = 1

Gene seems to be spot on in his answer so if my count is correct a total of 17 (don't bet any money on it).

source for the clock cycles was: http://www.avr-asm-tutorial.net/avr_en/beginner/COMMANDS.html#Functions

分享给朋友:
您可能感兴趣的文章:
随机阅读: