为什么这个汇编程序以无限循环结束? [英] Why do this assembly program end with a infinite loop?

查看:42
本文介绍了为什么这个汇编程序以无限循环结束?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个程序是我教科书中的一个例子,这个程序的目的是计算 N+N-1+...+2+1 ,并将结果存储在 R1 中.为什么它以死循环块结束,它可以做什么?

This program is an example from my textbook,the purpose of this program is to calculate N+N-1+...+2+1 , and store the result in R1. Why it is end with the deadloop block, what can it do?

; asm4-1.s
N          EQU 10 
Stack_Size EQU 0x00000400 
           AREA MyStack, NOINIT, READWRITE, ALIGN=3 
Stack_Mem  SPACE Stack_Size 
__initial_sp
           AREA Reset, DATA, READONLY 
__Vectors DCD __initial_sp 
          DCD Reset_Handler 
          THUMB 
          PRESERVE8 
          AREA Init, CODE, READONLY 
          ENTRY

Reset_Handler
            
          LDR r0, =N 
          MOV r1, #0 

loop
          ADD r1, r0 
          SUBS r0, #1 
          BNE loop 
deadloop
          B deadloop ; Does this create an infinite loop? why is it here
          NOP
          END

推荐答案

这看起来像是 ARM 微控制器.

That looks like its for an ARM microcontroller.

在裸机上,没有什么可退出的,空的无限循环比运行更多指令使 CPU 进入睡眠状态更容易.

On bare metal, there's nothing to exit to, and an empty infinite loop is easier than running more instructions to put the CPU into a sleep state.

如果您让执行继续进行,它会尝试将内存中的下一个作为指令执行,可能会在您附加调试器并查看它们之前弄乱寄存器.或者触发连续重启,这将使得通过查看引导加载程序实际加载的外部信号引脚来判断变得更加困难.(例如,因为在您的指令错误之后的字节,以及错误处理程序也在其他地方运行垃圾代码,或者其他东西.)

If you let execution keep going, it would try to execute whatever is next in memory as instructions, possibly messing up registers before you can attach a debugger and look at them. Or trigger continuous reboots, which would make it harder to tell from looking at external signal pins that your bootloader actually loaded. (e.g. because the bytes right after your instruction fault, and the fault-handler also runs garbage code somewhere else, orsomething.)

或者在一个不太简单的引导加载程序中,最终可能会弄乱视频内存中的数据.

Or in a less totally trivial bootloader, could end up messing up data in video memory.

还有一种可能性是,在内存中运行随机垃圾作为指令可能会对硬件造成不利影响,具体取决于电路板和软件必须执行的操作才能损坏硬件.(例如,将 I/O 引脚配置为输出,即使它已经连接到电源或接地.)

There's also a possibility that running random garbage in memory as instructions could do something bad to the hardware, depending on the board and what software would have to do to damage the hardware. (e.g. configure an I/O pin as an output even though it's already connected to power or ground.)

这篇关于为什么这个汇编程序以无限循环结束?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆