1

I have an issue where when i run this program:

LIST P=16F877A
        #INCLUDE<P16F877A.INC>
ORG 0 ;PROGRAM MEMORY ADDRESS =0x0000
        GOTO MAIN

N1      EQU 70      ;Counter
RL      EQU 71      ;Lower byte of result 
RH      EQU 72      ;Higher byte of result 



        ORG 30
MAIN:   

        MOVLW .6    ;W=6
        MOVWF N1    ;N1=W=6
        MOVLW .10   ;W=10
        CLRF RL
        CLRF RH
    
UP:     ADDWF RL ;RL=W+RL       ;MULTIPLICATION LOOP
        BTFSC STATUS,C ; CHECK C FLAG IN STATUS
        INCF RH         ;IF(C==1 THEN RH=RH+1
        DECFSZ N1       ;N1=N1-1
        GOTO UP         ;IF(N1!=0) THEN GOTO UP LABEL
        
ADD:
        MOVLW .23
        ADDWF RL
        CLRW
        MOVLW .101
        SUBWF RL
        

HERE:   GOTO HERE
        END 

The result should be 83-101 and i should get -18 in the RL variable but i get 238. Can someone help ? The program is for a pic microcontroller

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
mmdqn
  • 11
  • 2
  • 1
    `238` is `-18`. View/print it as signed. Note that `RH` should be 255 if you want a 16 bit result. – Jester Jan 08 '21 at 13:54
  • Sorry im a complete noob can you please explain how ? – mmdqn Jan 08 '21 at 15:40
  • You did not say how you view the value. The bit pattern `11101110` in unsigned is `238` in signed it is `-18`. See [two's complement on wikipedia](https://en.wikipedia.org/wiki/Two%27s_complement). – Jester Jan 08 '21 at 15:52
  • im using MPLAB IDE it has a Watch feature where you can see the variables change with each step of the program that is how i got 238 in decimal and 11101110 in binary – mmdqn Jan 08 '21 at 15:56
  • Check if it has an option to view as signed. – Jester Jan 08 '21 at 15:58
  • 1
    found it thank you – mmdqn Jan 08 '21 at 16:07

0 Answers0