New to Assembly , need help with call tables

Discussion in 'Embedded Systems and Microcontrollers' started by integralx2, Feb 12, 2010.

  1. integralx2

    Thread Starter New Member

    Sep 27, 2008
    8
    0
    Ok , so I have this project here, that was already finished, but I am trying t o figure out how the calltable works. The thing is just suppose to blink leds in order, and it does do this. My question is with following code below :

    Blink
    decfsz BLNKCNT,F ;Decrement loop counter and return if not zero
    goto BlinkEnd
    movlw HalfSec ;Reinitialize BLNKCNT
    movwf BLNKCNT
    call BlinkTable ;Toggle the LED
    movwf PORTC
    BlinkEnd
    return

    NOW when we do a call BlinkTable we get

    BlinkTable
    1 movf PORTC,W ; copy the present state of the LEDs into W
    2 andlw B'00000111' ; keep only the 3 LSBs
    3 addwf PCL, F ; change PC
    4 retlw B'00000001' ; 000 -> 001
    5 retlw B'00000010' ; 001 -> 010
    6 retlw B'00000100' ; 010 -> 100
    7 nop ; 011
    8 retlw B'00000001' ; 100 -> 001
    (note that i put the numbers running down the screen to explain my problem).
    Ok. What I dont get is how the table works. THe thing is suppose to blink every 5 seconds. So does it execute lines 1-3 then goes onto line 4 ,and does retlw, and loads the w register with following binary, then returns from subroutine ? Then when it goes around the program ( blinks the led on for 5 seconds) then comes back to turn on the 2nd led, how does it know that it should go to line 5? Does it skip or something the previous lines, cause the count was incremented by 3, and we already have one , so then go to line 4 ? I am really confused on this.

    Thanks for the help
    t
     
  2. Markd77

    Senior Member

    Sep 7, 2009
    2,803
    594
    PCL is just the position in the program, so at your blinktable line 3 it could be anything.
    Suppose it was 50, and the value in W is 0 (portc = b'00000000'), PCL changes to 51, your line 4. This is because PCL is autoincremented every cycle unless there is a goto.
    If W is 2, PCL gets autoincremented and then 2 added, so it jumps to 53, your line 6.
    The maximum possible in your program is 7 (portc= b'00000111') which would jump to 58 (after the end of your table).
    When it gets to the retlw it loads W with your value and returns straight to the line after 'call blinktable'.
    Hope this makes things clear.
    There is more information in AN556 which you can search for on the Microchip- website.
     
Loading...