Help with subroutines

Go To Last Post
8 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi! I am trying to use subroutines, but I can't find the mistake, the code runs but doesn't change if the bit 0 from port d is 0 or 1.

Could you help me please!

These are the instructions: read bit 0 of port B. If the bit is active, you must execute the procedure that triples r16, If the bit is inactive, you must execute the procedure that duplicates and increments r16.

 

.NOLIST
.INCLUDE "8515def.inc"
.LIST

    rjmp Main

 

Main:
    ldi r16,HIGH(RAMEND)
    out SPH,r16
    ldi r16,LOW(RAMEND)
    out SPL,r16

    ser r16        
    out DDRB,r16
    sbi PORTB,0
       
    sbic PINB, 0  
    rjmp triple_r16
    rcall double_r16_add1

triple_r16:
    ldi r16,50
    ldi r17,50
    add r16,r17
    add r16,r17
    mov r0,r16

 

double_r16_add1:
     clr r16
    ldi r16,50
    push r17
    lsl r16
    ldi r17,1
    add r16,r17
    mov r1,r16

Azul Garcia

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Your subroutines need a ret instruction at the end of each one.

 

Jim

 

Until Black Lives Matter, we do not have "All Lives Matter"!

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

azugr@outlook.com wrote:

double_r16_add1:
    clr r16
    ldi r16,50
    push r17
    lsl r16
    ldi r17,1
    add r16,r17
    mov r1,r16

    ret

Be careful, the program will crush on this unmatched push.

What will happen then after triple, any loop back, the code is missing. Or, do specify just:

    loop: rjmp loop

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

   clr r16
    ldi r16,50

 

makes no sense to do clr r16

 

  push r17 ...never do a push without ensuring something will do a pop (usually a pop or rarely a ret to do 2 pops)

 

  ldi r17,1
    add r16,r17    ...why not just inc r16???

 

 

Also, your tripler of r16 is a fail, since the first thing you do with r16 is erase its value with a 50.

When in the dark remember-the future looks brighter than ever.   I look forward to being able to predict the future!

Last Edited: Sun. Jun 13, 2021 - 06:41 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Maybe start with a tutorial, like this.   There is a section on subroutines.

 

Also, your main needs to loop forever.

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I am so grateful for your warning. I was definitely lost!

Azul Garcia

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thanks so much for your help! I will take your advice

Azul Garcia

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

thanks so much! This tutorial is better than my course's material

Azul Garcia