简体   繁体   中英

I'm writing AVR assembly in atmel, but “call” instruction doesn't work

I'm using Atmel Studio 6.2 to write some assembly code for Atmega328p.

However, the call instruction for subroutine doesn't work when I use simulator and step-by-step execute the program. It completely ignores the call instruction and goes on to the next line.

In order to test the call instruction I wrote a simple program, which looks as follows:

.include "m328pdef.inc"

ldi R16, 11
call hello_world
mov R1, R0
nop

hello_world:
    mov R0, R16
    ret

But even this doesn't work! It just goes on to execute mov R1, R0 . What might be the reason? It's so annoying when call doesn't work!

.include "m328pdef.inc"
ldi r16,11 
ldi r17,0
ldi r18,1
rcall hello_world
mov r16,r17
nop

hello world:
mov r16,r18
ret

I am using r16,r17, and r18 because its general use registers, only use it for debugging.
here is the step by step explanation:
1. load immediate value "11" to r16
2. load immediate value "0" to r17
3. load immediate value "1" to r18
4. call hello_world label. push address into stack
5. move value from r18 (1) to r16, so r16 value is 1
6. ret, pop the address, and goes back
7. move value from r17 (0) to r16, r16 value is 0

If you run the program at once instead of step-by-step, you wont know the difference.
Hope this help

You have to define stack http://www.avr-tutorials.com/assembly/writing-assembly-subroutines-avr-microcontroller

;Initialize the microcontroller stack pointer
                LDI    R16, low(RAMEND)
                OUT    SPL, R16
                LDI    R16, high(RAMEND)
                OUT    SPH, R16

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM