Analyzing Project Files

We will analyze the default main.asm file that was generated when the project was created with the New Bareboard Project wizard. The following listing shows the assembler source code for the Fibonacci program.

Listing: main.asm file

;*******************************************************************
;* This stationery serves as the framework for a              *

;* user application. For a more comprehensive program that    *

;* demonstrates the more advanced functionality of this       *

;* processor, please see the demonstration applications       *

;* located in the examples subdirectory of the                *

;* Freescale CodeWarrior for the S12Z Program directory       *

;*******************************************************************

; Include derivative-specific definitions

            INCLUDE 'derivative.inc'

            

; export symbols

            XDEF Entry, _Startup, main

            ; we use export 'Entry' as symbol. This allows us to

            ; reference 'Entry' either in the linker .prm file

            ; or from C/C++ later on

            XREF __SEG_END_SSTACK      ; symbol defined by the linker 
for the end of the stack

; variable/data section

MY_EXTENDED_RAM: SECTION

; Insert here your data definition.

Counter     ds.w 1

FiboRes     ds.w 1

; code section

MyCode:     SECTION

main:

_Startup:

Entry:

         LD S, #__SEG_END_SSTACK - 1   ; initialize the stack pointer

         CLI         ; enable interrupts

EndlessLoop:

            LD D2, #1                 ; D2 contains counter

CouterLoop:

            ST D2, Counter            ; update global.

            BSR CalcFibo

            ST D4, FiboRes            ; store result

            LD D2, Counter

            INC D2

            CMP D2, #24                ; larger values cause overflow.

            BNE   CouterLoop

            BRA   EndlessLoop         ; restart.

; Function to calculate fibonacci numbers. Argument is in D2.

CalcFibo:

           LD D3, #$00                ; second last

           LD D4, #$01                ; last

           DBEQ D2,FiboDone           ; loop once more (if D2 was 1, 
were done already)

FiboLoop:

           ADD D3, D4                 ; overwrite second last with new 
value

           EXG D3, D4                 ; exchange them -> order is  
correct again

           DBNE D2,FiboLoop

FiboDone:

           RTS                        ; result in D4

When writing your assembly source code, pay special attention to the following: