User Tools

Site Tools


Inline Assembly

SharpBASIC supports inline assembly code written in NASM (Netwide Assembler), which is also used by the compiler as back-end. To include assembly code, use the asm-statement:

asm do
  ' ...

SharpBASIC identifiers can be used inside assembly code blocks. Hexadecimal numbers can be used as well, either in SharpBASIC notation, e.g. #hFF, or in traditional notation, e.g. FFh or 0xFF.

The following inline assembly example is from the SharpBASIC 32-bits system library and copies a block of memory. Note that inside assembly code blocks, comments start with a semicolon.

' ----------------------------------------------------------------------------
' ----------------------------------------------------------------------------
' copy a memory block
' saddr: source address
' daddr: destination address
' size: length in bytes to be copied
' ----------------------------------------------------------------------------
sub cpmem(saddr: ptr, daddr: ptr, size: uint)
  asm do
    push     esi
    push     edi
    push     eax
    push     ecx

    mov      ecx, [size]           ; length
    cmp      ecx, #h0              ; larger than 0 ?
    jle     .out

    mov      esi, [saddr]          ; source
    mov      edi, [daddr]          ; destination

    mov      al, [esi]             ; from source
    mov      [edi], al             ; to destination
    inc      esi                   ; increment pointers
    inc      edi
    dec      ecx                   ; decrement counter

    cmp      ecx, #h0              ; done?
    jne      .copy

    pop      ecx
    pop      eax
    pop      edi
    pop      esi

Keep in mind that no registers are preserved when setting up an inline assembly code block.

inline-assembly.txt · Last modified: 2023/07/01 10:41 by admin