Calculator Community > ASM

SPASM Macros

Pages: (1/2) > >>

Galandros:

Macros are like a language that can produce assembly code and ease some tasks (like math tables and other data). See wikipedia topic for better explanation.
SPASM macros can be quite fun (recursing, dynamic including echoed files...).

On the macros itself I have come with these nifty ones:

- var.inc - var allocation

--- Code: ---;Example usage:
; varloc(tempswaparea)
;player_x = var(1)
;player_y = var(1)
;buffer_bullets = var(128)
;saveSP = var(2)
;attack = var(1)
;deffence = var(1)
;overflowedvar = var(500) ;this will give a warning on assembling because overflowed the safe ram (on calculator causes RAM Clear, normally)

#macro var(var.size)
#define var.counter eval(var.counter+var.inc)
#define var.inc eval(var.size)
#ifdef var.end
#if var.counter>var.end
.error "Variable allocation overflow!"
#endif
#endif
var.counter
#endmacro

#macro varloc(varloc.value,varloc.size)
#define var.counter eval(varloc.value)
#define var.inc 0
#ifdef varloc.size
#define var.end eval(varloc.value+varloc.size-1)
#endif
#endmacro

--- End code ---
- convhex.inc convert a label, number, equate or define to hexadecimal representation

--- Code: ---;Example usage:
; .echo convhex(%10000000)
;label:
; .echo convhex(label)

#macro zconvhex(hexn)
#if hex_remain == 0
"\$",return
#else
#define hex_digit eval(hexn & \$0F)
#define hex_remain eval(hexn/16)
#ifndef return
#define return ""
#endif
#if hex_digit == 0
#define hex_char "0"
#endif
#if hex_digit == 1
#define hex_char "1"
#endif
#if hex_digit == 2
#define hex_char "2"
#endif
#if hex_digit == 3
#define hex_char "3"
#endif
#if hex_digit == 4
#define hex_char "4"
#endif
#if hex_digit == 5
#define hex_char "5"
#endif
#if hex_digit == 6
#define hex_char "6"
#endif
#if hex_digit == 7
#define hex_char "7"
#endif
#if hex_digit == 8
#define hex_char "8"
#endif
#if hex_digit == 9
#define hex_char "9"
#endif
#if hex_digit == 10
#define hex_char "A"
#endif
#if hex_digit == 11
#define hex_char "B"
#endif
#if hex_digit == 12
#define hex_char "C"
#endif
#if hex_digit == 13
#define hex_char "D"
#endif
#if hex_digit == 14
#define hex_char "E"
#endif
#if hex_digit == 15
#define hex_char "F"
#endif

#define return eval(hex_char,return)
zconvhex(eval(hex_remain))
#endif
#endmacro

;wrap the main macro up with another macro to clear the global defines to work more times
#macro convhex(zn)
#define hex_remain -1
#define return ""
zconvhex(zn)
#endmacro

--- End code ---
- breakpoint.inc, this echo to a text file named breakpoints.txt the address of breakpoints
(you need convhex macro)

--- Code: ---;breakpoints.inc
;by Galandros

#include "convhex.inc"
#macro BREAKPOINT
#ifndef BREAK_F
.echo > breakpoints.txt "\nBreakpoints:\n"
.echo >> breakpoints.txt convhex(\$),"\n"
#define BREAK_F
#else
.echo >> breakpoints.txt convhex(\$),"\n"
#endif
#endmacro

;Alternatives to break macro name
#define breakpoint() BREAKPOINT
#define .breakpoint BREAKPOINT

--- End code ---
- relocation
(this was not made by me, actually)

--- Code: ---#macro relocate(location)
#ifdef g_old_location
.error "You cannot nest relocate blocks!"
#else
#define g_old_location eval(\$)
#define g_new_location eval(location)
.org location
#endif
#endmacro

#macro endrelocate()
#ifdef g_new_location
.org \$ - g_new_location + g_old_location
#undefine g_new_location
#undefine g_old_location
#else
.error "No relocate statements corresponds to this endrelocate!"
#endif
#endmacro

--- End code ---

How to use:
1. copy the text to some text file using Notepad, for example.
2. include in the source code with #include macro.inc directive
3. call it within the source code

Notes:
- to see the examples uncomment the lines (simply delete the ";")
- only works on SPASM. I could try TASM compatibility, but meh, SPASM has some many reasons to be used
- I am giving a cookie especially for Eeems. You can use this macros in the ADE. Just give me credit and put in the include file that the macros were given by me (Galandros). You can include in your IDE, just do the same as I wrote before in this line.

More macros will come. I have done convbin (convert equates to binary)
I am going to release a pack of SPASM macros. ;)
You can post your macros, too. Or request macros. I am doing some experiments with SPASM macros because you can do pretty much everything.

mapar007:

Ooh, yes! Hex labels. Awesome! (when I do .echo \$ for breakpointing, I always have to convert the address to hex. Thanks!)

DJ Omnimaga:

By labels I assume you mean a bit like the TI-BASIC ones, right?

Galandros:

Macros exist for some reason. :P

--- Quote from: DJ Omnimaga on March 29, 2010, 04:42:39 am ---By labels I assume you mean a bit like the TI-BASIC ones, right?

--- End quote ---
Not quite. Depends on how you think about it so, yes the functions is a bit like. Assembly labels represent an address of the code. They are useful for having a description what that part of the code does. Labels also works for subroutines.

A closer analogy with BASIC would be if your lines were numbered. The number would represent the address of the code.

mapar007:

I like to think of 'call label' as function() in other languages. But a label in asm can be a routine address, a data block address, or even just a number. (although the latter is more commonly referred to as 'an equate')

Pages: (1/2) > >>