Yuval Froman
Yuval Froman

Reputation: 91

IAR Embedded workbench illegal opcode error

I write this code, the purpose is to return 𝑠𝑢𝑚 = ∑A∙B, when A,B are 2 arrays of numbers in length of 8.

When the program gets to the RET command, there is an error: "Illegal opcode found on address 0X0". I can't figure out why does it happen.

    #include <msp430xG46x.h>     ;define controlled include file

         ORG 1100h
Arr1     DW   2,0,4,2,6,5,1,1  
Arr2     DW   3,0,8,5,2,9,3,7  
Size     DW   8         ; Arr length

var1     DS32  1

         RSEG   CODE                  ; ORG   0x3100 - place program in 'CODE' segment in to Flash memory
         RSEG   CSTACK

Main    
         MOV   #Arr1,R5
         MOV   #Arr2,R6
         MOV   Size,R7
         CLR   R8
Loop      
          call #Func
          DEC  R7
          JNZ  Loop
L1        JMP  L1

Func      
          MOV  @R5,R10            ;Adding @R6, @R5 times to R8 
          CLR  R4
          ADD  @R6,R4
L2       
          DEC  R10
          JZ   Result
          ADD  @R6,R4
          ADC  R9
          JMP  L2
Result    
          MOV  R4,var1
          MOV  #2,R11
          MOV  R9,var1(R11)
          ADD  var1,R8
          INCD R5
          INCD R6      
          RET

;-------------------------------------------------------------------------------

          COMMON  INTVEC                  ; Interrupt Vectors
;-------------------------------------------------------------------------------

         ORG     RESET_VECTOR            ; POR, ext. Reset

         DW      Main

         END

Upvotes: 0

Views: 987

Answers (1)

Damiano
Damiano

Reputation: 716

1) SP register is not initialized so you'r pushing memory to 0x0000 that depending on your MCU could be anything. This make a mess with calls and rets too.

2) Your code is ending in stack segment as RSEG CSTACK come after RSEG CODE and before your code.

Change

    RSEG   CODE                  ; ORG   0x3100 - place program in 'CODE' segment in to Flash memory
    RSEG   CSTACK

Main

to

     RSEG   CSTACK
     RSEG   CODE                  ; ORG   0x3100 - place program in 'CODE' segment in to Flash memory

Main     MOV     #SFE(CSTACK), SP

Upvotes: 1

Related Questions