1

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!

reflective_mind
  • 1,475
  • 3
  • 15
  • 28
sosostris
  • 73
  • 8
  • In the debugger, do you use the command “step” or the command “next?” – fuz Oct 07 '16 at 18:56
  • 1
    I'm asking because typically debuggers have both a command that goes to the next instruction executed by the processor and a command that executes until the next line of code in your program (e.g. after `hello_world` completed execution). – fuz Oct 07 '16 at 18:57
  • 2
    ooohh!!! I'm just stupid!! yes i'm stupid!!! when it's call, i should click 'step into' !!!!! it works now T.T i have to bump my head into wall for the stupidity!!!! thanks a lot :) :) :) – sosostris Oct 07 '16 at 19:01
  • So in that "stupid simple program" you could have did `ldi R16,5` in `hello_world:` and see if the `R16` did or didn't change after `call`. – Ped7g Oct 07 '16 at 19:08

2 Answers2

0
.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

duck
  • 369
  • 3
  • 17
0

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