User`s manual

Dynamic C Users Manual digi.com 171
11. USING ASSEMBLY LANGUAGE
This chapter gives the rules for mixing assembly language with Dynamic C code. A reference guide to the
Rabbit Instruction Set is available from the Help menu of Dynamic C.
11.1 Mixing Assembly and C
Dynamic C permits assembly language statements to be embedded in C functions and/or entire functions
to be written in assembly language. C statements may also be embedded in assembly code. C-language
variables may be accessed by the assembly code.
11.1.1 Embedded Assembly Syntax
Use the #asm and #endasm directives to place assembly code in Dynamic C programs. For example, the
following function will add two 64-bit numbers together. The same program could be written in C, but it
would be many times slower because C does not provide an add-with-carry operation (adc).
The keywords debug and nodebug can be placed on the same line as #asm. Assembly code blocks are
nodebug by default. This saves space and unnecessary calls to the debugger kernel.
All blocks of assembly code within a C function are assembled in nodebug mode. The only exception to
this is when a block of assembly code is explicitly marked with debug. Any blocks marked debug will
be assembled in debug mode even if the enclosing C function is marked nodebug.
void eightadd( char *ch1, char *ch2 ){
#asm
ld hl,(sp+@SP+ch2) ; get source pointer
ex de,hl ; save in register DE
ld hl,(sp+@SP+ch1) ; get destination pointer
ld b,8 ; number of bytes
xor a ; clear carry
loop:
ld a,(de) ; ch2 source byte
adc a,(hl) ; add ch1 byte
ld (hl),a ; store result to ch1 address
inc hl ; increment ch1 pointer
inc de ; increment ch2 pointer
djnz loop ; do 8 bytes
; ch1 now points to 64 bit result
#endasm
}