Assembly Code Help Needed

Discussion in 'Homework Help' started by mobiusmorte, Aug 22, 2014.

  1. mobiusmorte

    Thread Starter New Member

    Aug 22, 2014
    2
    0
    Hello!
    I am brand new to this forum so please forgive any ignorance I display.

    My instructor has given us a sample of PIC Assembly Code to evaluate and explain. I have never done any coding before and he encouraged us to use whatever resources we can to get the job done.

    So, if anyone could provide any insight you would be helping this new student out tremendously.

    Thanks in advance! :D

    Code ( (Unknown Language)):
    1.  
    2. ;Start here
    3. ;First, set the processor type
    4.     LIST P=16F84
    5.  
    6. ;Second, set the configuration bits according to the datasheet
    7. ;Code protect off, power-up timer on, watchdog off, RC oscillator
    8.     __CONFIG    H'3FFB'
    9.  
    10. ;Next, declare some variables (copied)
    11. PORTB       EQU 6       ;
    12. TRISB       EQU 86H     ;
    13. OPTREG      EQU 81H     ;
    14. STATUS      EQU 3       ;
    15. CARRY       EQU 0       ;
    16. RP0     EQU 5       ;
    17. MSB     EQU 3       ;
    18.  
    19. ;Declare some new variables...according to the datasheet, user RAM starts at address 0x0C
    20. W_TEMP      EQU 0x0C        ;
    21. STATUS_TEMP EQU 0x0D        ;
    22.  
    23. TEMP1       EQU 0x0E        ;
    24. TEMP2       EQU 0x0F        ;
    25. SECH        EQU 0x10        ;
    26. SECL        EQU 0x11        ;
    27. T0OVFLCNT   EQU 0x12        ;
    28.  
    29. TMR0        EQU 0x01        ;
    30. INTCON      EQU 0x0B        ;
    31. T0IF        EQU 2       ;
    32. T0IE        EQU 5       ;
    33. GIE     EQU 7       ;
    34.  
    35. ;Here, a macro will be declared so that in the code, the same instructions won't need to be typed over
    36. ;and over to complete
    37. ;this function. Instead, wherever SUB1616 is located in the code, when the code is compiled, all these instructions
    38. ;will be inserted in that spot of the code.
    39.  
    40. ;This macro does A1:A2 - B1:B2, only to check whether or not A1:A2 is >= B1:B2
    41. SUB1616     macro   A1,A2,B1,B2
    42.  
    43.         MOVF    B2,0        ;
    44.         SUBWF   A2,0        ;
    45.         MOVF    B1,0        ;
    46.         BTFSS   STATUS,CARRY    ;
    47.         INCFSZ  B1,0        ;
    48.         GOTO    $ + 3       ;
    49.         BCF STATUS,CARRY    ;
    50.         GOTO    $ + 2       ;
    51.         SUBWF   A1,0        ;
    52.  
    53.         endm
    54.  
    55. ;An interrupt will be used, so the main code address and the interrupt code address must be declared
    56. ;According to the datasheet, main code goes at 0x0000, and interrupt code goes at 0x0004
    57.  
    58.         ORG     0x0000
    59.         GOTO    MAIN
    60.  
    61.         ORG 0x0004
    62.         MOVWF   W_TEMP      ;
    63.         MOVF    STATUS,0
    64.         MOVWF   STATUS_TEMP ;
    65.  
    66. INT_TMR0    BCF INTCON,T0IF ;
    67.         INCF    T0OVFLCNT,1 ;
    68.         BTFSS   T0OVFLCNT,4 ;
    69.         GOTO    INT_TMR0END ;
    70.         CLRF    T0OVFLCNT   ;
    71.         INCFSZ  SECL,1      ;
    72.         GOTO    INT_TMR0END ;
    73.         INCF    SECH,1      ;
    74.         GOTO    INT_TMR0END ;
    75.  
    76. INT_TMR0END MOVF    STATUS_TEMP,0   ;
    77.         MOVWF   STATUS      ;
    78.         SWAPF   W_TEMP,1
    79.         SWAPF   W_TEMP,0    ;
    80.         RETFIE
    81.  
    82.  
    83. MAIN        CLRF    PORTB           ;
    84.         CLRF    STATUS      ;
    85.         CLRF    W_TEMP
    86.         CLRF    STATUS_TEMP
    87.         CLRF    TEMP1
    88.         CLRF    TEMP2
    89.         CLRF    SECH
    90.         CLRF    SECL
    91.         CLRF    T0OVFLCNT
    92.         CLRF    TMR0
    93.         CLRF    INTCON
    94.  
    95.             BSF     STATUS,RP0      ;  
    96.             CLRF    TRISB           ;
    97.         MOVLW   0x07
    98.         MOVWF   OPTREG      ;
    99.         BCF STATUS,RP0  ;
    100.         CLRF    TMR0        ;
    101.         BCF INTCON,T0IF ;
    102.         BSF INTCON,T0IE ;
    103.         BSF INTCON,GIE  ;
    104.         BSF PORTB,0     ;
    105.         BCF STATUS,CARRY    ;
    106. A       MOVLW   0x00        ;
    107.         MOVWF   TEMP1       ;
    108.         MOVLW   0x02
    109.         MOVWF   TEMP2       ;
    110.         BCF INTCON,GIE  ;
    111.         SUB1616 SECH,SECL,TEMP1,TEMP2   ;
    112.         BSF INTCON,GIE  ;
    113.         BTFSS   STATUS,CARRY    ;
    114.         GOTO    A       ;
    115.         BCF PORTB,0     ;
    116. B       MOVLW   0x00        ;
    117.         MOVWF   TEMP1       ;
    118.         MOVLW   0x3E
    119.         MOVWF   TEMP2       ;6
    120.         BCF INTCON,GIE  ;
    121.         SUB1616 SECH,SECL,TEMP1,TEMP2   ;
    122.         BSF INTCON,GIE  ;
    123.         BTFSS   STATUS,CARRY    ;
    124.         GOTO    B       ;
    125.         BSF PORTB,1     ;
    126. C       MOVLW   0x00        ;
    127.         MOVWF   TEMP1       ;
    128.         MOVLW   0x7A
    129.         MOVWF   TEMP2       ;
    130.         BCF INTCON,GIE  ;
    131.         SUB1616 SECH,SECL,TEMP1,TEMP2   ;
    132.         BSF INTCON,GIE  ;
    133.         BTFSS   STATUS,CARRY    ;
    134.         GOTO    C       ;
    135.         BSF PORTB,2     ;
    136. D       MOVLW   0x00        ;
    137.         MOVWF   TEMP1       ;
    138.         MOVLW   0xB6
    139.         MOVWF   TEMP2       ;
    140.         BCF INTCON,GIE  ;
    141.         SUB1616 SECH,SECL,TEMP1,TEMP2   ;
    142.         BSF INTCON,GIE  ;
    143.         BTFSS   STATUS,CARRY    ;
    144.         GOTO    D       ;
    145.         BSF PORTB,3     ;
    146. E       MOVLW   0x01        ;
    147.         MOVWF   TEMP1       ;
    148.         MOVLW   0x2E
    149.         MOVWF   TEMP2
    150.         BCF INTCON,GIE  ;
    151.         SUB1616 SECH,SECL,TEMP1,TEMP2   ;
    152.         BSF INTCON,GIE  ;
    153.         BTFSS   STATUS,CARRY    ;
    154.         GOTO    E       ;
    155.         BSF PORTB,4     ;
    156.  
    157.         BCF INTCON,GIE  ;
    158.         BCF INTCON,T0IE ;
    159.         CLRF    TMR0        ;
    160.         BCF INTCON,T0IF ;
    161.         GOTO    E       ;
    162.  
    163.  
    164.         END
    165.  
    166.  
     
  2. jpanhalt

    AAC Fanatic!

    Jan 18, 2008
    5,699
    905
    Suggestions for solving the homework:

    1) Add meaningful comments. You may need to start with each line and say what it does. For something like "CLRF PORTB" , that alone may not be very meaningful in the context of the whole program; however, after the instruction set becomes second nature to you, you will be able to look at several lines of code (e.g., think of it as a paragraph) and write a comment to describe what those lines do.

    Once you get the comments, the flow of the program should be more apparent.

    2) I have found flow charting a complex program to be helpful and use Lucid Chart (free version): https://www.lucidchart.com
    Some people don't find flow charting helpful -- it's a personal choice. In my case, I had been using flow charting for many years before I even knew what a "PIC" was, so using it for writing code just came naturally.

    John
     
    mobiusmorte likes this.
  3. MaxHeadRoom

    Expert

    Jul 18, 2013
    10,553
    2,375
    The first thing to do is go to the Picmicro site and download the 16F84 manual and look up details of the particular functions and also refer to the instruction set.
    Also for laying out the code I prefer to place a Colon at routine positions it makes it easier when searching.
    As so.

    Code ( (Unknown Language)):
    1.  
    2. INT_TMR0:
    3.         bcf INTCON,T0IF ;
    4.         incf    T0OVFLCNT,1 ;
    5.         btfss   T0OVFLCNT,4 ;
    6.         goto    INT_TMR0END ;
    7.         clrf    T0OVFLCNT   ;
    8.         incfsz  SECL,1      ;
    9.         goto    INT_TMR0END ;
    10.         incf    SECH,1      ;
    11.         goto    INT_TMR0END ;
    12.  
    13. INT_TMR0END:
    14.         movf    STATUS_TEMP,0
    15.  
    You also may learn something from this http://www.winpicprog.co.uk/pic_tutorial.htm
    Max.
     
    mobiusmorte likes this.
  4. joeyd999

    AAC Fanatic!

    Jun 6, 2011
    2,689
    2,750
    My condolences, mobiusmorte.

    This is an embarrassingly awful snippet of code. Now I know where bad programmers come from...

    In any case, you'll need the data sheet for the 16F84. Go line by line, and work out what each line does. It will be helpful to use the simulator in MPLAB -- use the watch window to see how each instruction operates.

    Hopefully, you'll get a feel for what's going on in the '84. But, IMHO, you are not learning good coding practices...
     
    mobiusmorte likes this.
  5. mobiusmorte

    Thread Starter New Member

    Aug 22, 2014
    2
    0
    Thank you to all the suggestions, I have a plan of action now. Hopefully I will be able to help others in the future in return.
     
Loading...