assemblymemory-addresspic

Indirect Addressing P16F877


Suppose i have two numbers n1 & n2, one in bank 0 and the other in bank 2, i want to do a sum operation on them using the indirect addressing approach, here is the code from my lecture which is wrong after tested in mplab 8.92, i need someone to correct it thanks:

nbr1 equ 0xA0
nbr2 equ 0x121
som12 equ 0x1A2
; les instructions ci-dessus utilisant la directive (equ) peuvent être remplacées par la directive cblock :
; cblock 0x20
; nbr1, nbr2, som12
; endc
w equ 0
f equ 1
; la macro suivante (bank0) est un sous-programme qui permet de configurer STATUS pour pointer sur bank#0

; ------------- Programme principal ---------------------------------------
org 0x00 ; définir la position initiale dans la mémoire programme
debut
    ;nbr1:
    CLRF STATUS;IRP = 0
    movlw 0xA0
    movwf FSR  ;FSR=0xA0
    movlw 0xFF ; transférer la valeur (0x0A) dans l'accumulateur w
    movwf INDF ; transférer le contenu de W à l'adresse du nbr1

    ;nbr2:
    bsf STATUS, IRP;IRP = 1
    movlw 0x21
    movwf FSR ;FSR=0x21
    movlw 0x01 ; transférer la valeur (0x0A) dans l'accumulateur w
    movwf INDF
    
    ;Charger addresse de nbr1 dans FSR
    bcf STATUS, IRP;IRP = 0
    movlw 0xA0
    movwf FSR ;FSR=0xA0
    addwf INDF,w ; additionner w à la variable nbr2
    
    bsf STATUS, IRP;IRP = 1
    movlw 0xA2
    movwf FSR ;FSR=0xA2
    movwf INDF ; stoker le résultat de la somme à partir de w sur la variable som12
    goto debut ; réexécuter la boucle depuis le label « début »
    end```


I tried to rewrite the code but unfortunately i dont know much, im used to the x86 assembly so using PICs are abit difficult

Solution

  • In PIC 16 are two addressing methods:

    1. Indirect addressing. Accessing bytes via INDF register use FSR register and IRB bit (it's in STATUS register) to compose 9 bit address.
    2. Direct addressing. Accessing bytes direct use 7 bit instruction low address bits and 2 high address bits RP0 and RP1 (there are in STATUS register) to compose 9 bit address.

    CODE EXAMPLE

            nbr1  equ  0xA0
            nbr2  equ  0x121
        //Set indirect address of nbr1 = 0X0A0
            bcf   STATUS, IRP    ;SET FSR high bit to 0
            movlw 0xA0
            movwf FSR            ;nbr1 indirect address is set to FSR register as 0X0A0
            movf  INDF,w         ;load byte data to WREG
        //Set direct address of nbr2 = 0x121
            bcf   STATUS, RP0    ;set the high two bits of direct address
            bsf   STATUS, RP1    ;RP1 = 1 
            addwf nbr2,w         ;nbr2 direct address is 0X121 
    

    Result addition of nbr1 and nbr2 is stored in WREG and CARRY flag.

    If you wont to use only indirect addressing then you must use another temporary register to store first value in to it.