ebmDevMag.com home
ebm Developer's Magazine
links

developer's mag
main page

article
part 1
part 2
part 3
part 4
part 5
part 6
part 7
part 8
part 9


2 - Anatomy of a Function

Key to writing ebm assembly code is adhering to the programming conventions regarding registers. The registers of a well-behaved function look like this:
  • r0-r6 contain the parameters as are passed, starting with r0.
  • r7 is a scratchpad, and needn't be saved or restored.
  • r8-r14 is the parent's workspace, and must be saved and restored if needed.
  • r15 is also a scratchpad, and needn't be saved or restored.
When you call a function from your code, expect the above rules to apply in reverse:
  • Pass your function parameters in r0 through r6.
  • Expect r0-r7 and r15 to be trashed by the subroutine.
  • Keep variables you need preserved in r8-r14.

Variable Needs

Although register data is convenient and fast, sometimes it isn't enough. Some code only needs a few registers, while other code can use the parameter values as workspace. Then there's code that needs local storage, and for that the stack is ideal:

  ; make room on stack for local variables
  ldc -OFFSET ; constant - must be multiple of 4
  add sp      ; add stack pointer value
  mov t,sp    ; move back & protect space
Note that OFFSET is negative because the stack grows downward.

Once allocated, we can access the data:

  ldc VAR_OFFSET ; offset to variable
  add sp
  mov (t),r0     ; or some other instruction...
Finally, remember to clean up:

  ; make room on stack for local variables
  ldc OFFSET ; now positive-we are returning the space
  add sp     ; add stack pointer value
  mov t,sp   ; move back & free up


Previous Section
Next Section

Copyright © 2001-2006 ebmDevMag.com - Legal Notice