Motorola Axiom CMD11E1 Guía de usuario Pagina 33

  • Descarga
  • Añadir a mis manuales
  • Imprimir
  • Pagina
    / 101
  • Tabla de contenidos
  • MARCADORES
  • Valorado. / 5. Basado en revisión del cliente
Vista de pagina 32
3.4. WRITING YOUR FIRST FUNCTION 33
next and then the program section, though it is easier to follow the code if the
program section precedes the data section as in the previous example.
The program section should start with the main code, i.e. the code you want
to execute. The main code should be followed by various functions. The order is
not important.
What is a function? A function, also known as a subroutine is a self contained
code that implements a well defined functionality. What do I mean by self con-
tained? You should b e able to draw a line above and below your code for the
function, and make sure that
1. Only way to branch out of the two lines is with JSR or BSR or RTS instruc-
tions. If you find any other branching instruction such as BRA, BEQ etc. then
your code is most likely incorrect
2. Only way to branch into an instruction between the two lines from an
instruction outside the two lines is with a JSR or BSR instruction. If you
find any other branching instruction such as BRA, BEQ etc. then your code
is most likely incorrect.
The function should terminate with a return from subroutine RTS instruction. It
is a good programming practice not to have more than one RTS statement in any
function.
Once you have written your function, it is there for you to use as many times
as you need. To use the function, you should know where its first instruction is
located in memory (technically known as the entry point). If you use the assembler
to create the function, you can place a label before the very first instruction. The
assembler will automatically EQUate the label with the first instruction. If the
function needs any additional information, they will have to be supplied by the
user prior to using the function (technically known as binding). The function you
will be writing will use one of the registers for binding. Also, many functions will
return some useful value to the caller. In this case, it is a good idea to return the
value in one of the registers.
Unless you write functions that do absolutely nothing (technically known as
stubs), the function will use and modify one or more registers. If the caller hap-
pens to keep valuable data in one of these registers, then you have a potential
problem. There are two possible solutions: The caller could save the values in
the registers before calling your function, and then restore it after your function
returns. Alternatively, your function can save the values in the registers it uses
and then restore the values before it returns. The first approach more efficient
but the second approach will result in fewer bugs. I strongly recommend that you
get into the habit of writing functions that clean up after themselves and restore
Vista de pagina 32
1 2 ... 28 29 30 31 32 33 34 35 36 37 38 ... 100 101

Comentarios a estos manuales

Sin comentarios