程序在调用后卡住,未从子例程返回

时间:2019-04-14 18:00:06

标签: assembly io avr atmega16

我是汇编编程的新手,我正在编写此代码,以使连接到portD的led闪烁。  在这里,我将堆栈指针的值设置为ramend,但是在调用delay子例程后,调试模式将停止,并且所有时间闪烁!我不明白为什么! 谢谢!

.include "m16def.inc"
 .ORG 0

 start:
	ldi r16 , high(ramend)
	out sph , r16
	ldi r16 , low(ramend)
	out spl , r16
	ldi r16 , 0b00110000 ; direction
	ldi r19 , 0b01001001    
	out DDRD , r16
	out PORTD , r19
	rjmp loop

loop:
	in r17 , PIND
	sbis PIND , 6          // part b
	rjmp BLINK
	rjmp loop

BLINK:
	ldi r24 , 2       
	ldi r18 , 0b00010000
	out PORTD , r18
	call delay
	ldi r18 , 0b00000000
	out PORTD , r18
	call delay
	dec r24
	brne BLINK   // if z = 0
	rjmp loop

delay:                 // For CLK(CPU) = 1 MHz
    LDI r20 , 16      // One clock cycle
delay1:
    LDI r21 , 125     // One clock cycle
delay2:
    LDI r22 , 250     // One clock cycle
delay3:
    DEC r22           // One clock cycle
    NOP                // One clock cycle
    BRNE delay3        // Two clock cycles when jumping to Delay3, 1 clock when continuing to DEC

    DEC r21            // One clock cycle
    BRNE delay2         // Two clock cycles when jumping to Delay2, 1 clock when continuing to DEC

    DEC r20             // One clock Cycle
    BRNE delay1         // Two clock cycles when jumping to Delay1, 1 clock when continuing to RET
RET

0 个答案:

没有答案