Coldfire Assembly Language
Coldfire Assembly Language
Revision 1.0
January 24, 2007
Released
Table of Contents
Introduction
Basic Rules
Parameter Passing
Simple Examples
Increment Function
Subtraction Function
Inline Assembly
Introduction
So you want to write some assembly code for the NetBurner ColdFire platform. This is
not meant to be a complete guide to assembly language programming for the ColdFire,
but a minimal guide to get started. If you would like more information on assembly
language programming for the ColdFire, then you can find references to a couple of
books at the end of this document.
Basic Rules
Parameter Passing
When a function is called, any input parameters are passed on the stack. The return
address is then passed on the stack. The stack is stored in register A7.
Simple Examples
Increment Function
Suppose we want to write the following function:
int inc( int i )
{
return i + 1;
}
Subtraction Function
Suppose we have two parameters:
int sub( int a, int b )
{
return a - b;
}
a is the first parameter and it will be closest on the stack, followed by b being the next
closest, etc...
.global sub
sub:
move.l %a7@(4),%d0
move.l %a7@(8),%d1
sub.l %d1,%d0
rts
// Value of a
// Value of b
If the function being called takes parameters, then you must pass them clean up after
yourself when the function returns.
Take the following test function for example:
void ShowParams( int P1, int P2 )
{
iprintf( "P1 = %08x or %d
P2 = %08x or %d\r\n", P1, P1, P2, P2 );
}
#include
#include
#include
#include
#include
#include
#include
"predef.h"
<stdio.h>
<ctype.h>
<startnet.h>
<autoupdate.h>
<dhcpclient.h>
<bsp.h>
extern "C" {
void UserMain( void *pd );
}
const char *AppName = "ftest";
extern "C" {
int inc( int i );
int sub( int a, int b );
void asmstrcpy( char *dest, const char *source );
DWORD getsecs( void );
void ShowParams( int P1, int P2 );
void TestAsmCall();
}
void ShowParams( int P1, int P2 ) {
iprintf( "P1 = %08x or %d
P2 = %08x or %d\r\n", P1, P1, P2, P2 );
}
void Holderfunc() {
/* inc function */
asm(".global inc
asm("inc:
asm("
move.l %a7@(4),%d0
asm("
addq.l #1,%d0
asm("
rts
");
");
"); // Get the first parameter
");
");
/* sub function */
asm(".global sub
asm("sub:
asm("
move.l %a7@(4),%d0
asm("
move.l %a7@(8),%d1
asm("
sub.l %d1,%d0
asm("
rts
");
");
"); // Get the first parameter
"); // Get the second parameter
");
");
/* asmstrcpy function */
asm(".global asmstrcpy
asm("asmstrcpy:
asm("
move.l %a7@(4),%a0
asm("
move.l %a7@(8),%a1
asm("asm_loop:
asm("
move.b %a1@+,%d0
asm("
move.b %d0,%a0@+
asm("
bne asm_loop
");
");
");
");
");
");
");
");
asm("
rts
");
/* getsecs function */
asm(".global getsecs
asm(".extern Secs
asm("getsecs:
asm("
move.l Secs,%d0
asm("
rts
");
"); // Define global variable to access
");
");
");
/* TestAsmCall function */
asm(".global TestAsmCall
asm(".extern ShowParams
asm("TestAsmCall:
asm("
move.l #1,%d0
asm("
move.l %d0,%a7@asm("
move.l #2,%d0
asm("
move.l %d0,%a7@asm("
jsr ShowParams
asm("
addq.l #8,%a7
asm("
rts
");
");
");
");
"); // Push 2nd parameter on the stack
");
"); // Push 1st parameter on the stack
");
"); // Take parameters back off the stack
");
}
void UserMain( void *pd ) {
// Set up the TCP/IP stack buffers, etc...
InitializeStack();
// Get a DHCP address if needed. You may want to add a check for the
// return value from this function. See the function definition in
// \Nburn\include\dhcpclient.h.
GetDHCPAddressIfNecessary();
// Change our priority from highest to something in the middle.
OSChangePrio( MAIN_PRIO );
// Enable the ability to update code over the network.
EnableAutoUpdate();
iprintf( "Application started\n" );
char buffer[20];
while ( 1 )
{
OSTimeDly( 20 );
iprintf( "inc(25) = %ld\r\n", inc( 25 ) );
iprintf( "sub(5,4) = %ld\r\n", sub( 5, 4 ) );
asmstrcpy( buffer, "Test" );
iprintf( "buffer[%s]\r\n", buffer );
iprintf( "getsecs = %ld, Secs = %ld\r\n", getsecs(), Secs );
TestAsmCall();
}
/* asmstrcpy function */
.global asmstrcpy
asmstrcpy:
move.l %a7@(4),%a0
/*
move.l %a7@(8),%a1
/*
asm_loop:
move.b %a1@+,%d0
/*
move.b %d0,%a0@+
/*
bne asm_loop
rts
/* getsecs function */
.global getsecs
.extern Secs
getsecs:
move.l Secs,%d0
rts
/* TestAsmCall function */
.global TestAsmCall
.extern ShowParams
TestAsmCall:
move.l #1,%d0
move.l %d0,%a7@/* Push the second parameter on the stack */
move.l #2,%d0
move.l %d0,%a7@/* Push the first parameter on the stack */
jsr ShowParams
addq.l #8,%a7
rts