Boriel Basic Forum
Learning ASM and ZX Basic Compiler - Printable Version

+- Boriel Basic Forum (https://forum.boriel.com)
+-- Forum: Compilers and Computer Languages (https://forum.boriel.com/forumdisplay.php?fid=12)
+--- Forum: ZX Basic Compiler (https://forum.boriel.com/forumdisplay.php?fid=11)
+---- Forum: Help & Support (https://forum.boriel.com/forumdisplay.php?fid=16)
+---- Thread: Learning ASM and ZX Basic Compiler (/showthread.php?tid=371)



Learning ASM and ZX Basic Compiler - oblo - 2011-08-22

Hi all

I know it's a bit odd to ask this, because the compiler doesn't ask us to learn ASM but...
I'm starting to learn some ASM and besides read some books, I'm trying to get some examples from <!-- m --><a class="postlink" href="http://stu.pido.us/wiki/retrogames:index">http://stu.pido.us/wiki/retrogames:index</a><!-- m --> and <!-- m --><a class="postlink" href="http://members.fortunecity.com/jonathan6/egghead/id2.html">http://members.fortunecity.com/jonathan ... d/id2.html</a><!-- m -->, but every piece of code I try to compile between ASM and END ASM, end with a compiler error, for instance:

Code:
ASM ld a,2 ; upper screen call 5633 ; open channel loop ld de,string ; address of string ld bc,eostr-string ; length of string to print call 8252 ; print our string jp loop ; repeat until screen is full string defb '(your name) is cool' eostr equ $ END ASM

...ends with "temp.bor:11: Error: illegal preprocessor character '$'. Compilation failed" And:

Code:
ASM ; scroll izquierda ; ld hl, 22527 ld c, 192 buc_2 ld b, 32 and a buc_1 rl (hl) dec hl djnz buc_1 jr nc, noca_1 ld (var), hl ld ix, (var) set o, (ix+32) noca_1 dec c jr nz, buc_2 ret ; ; scroll derecha ; ld hl, 16384 ld c, 192 buc_4 ld b, 32 and a buc_3 rr (hl) inc hl djnz buc_3 jr nc, noca_2 ld (var), hl ld ix, (var) set 7, (ix-32) noca_2 dec c jr nz, buc_4 ret var equ 23728 END ASM

...ends with "temp.bor:3: Error: Syntax error. Unexpected token 'LD' [LD]. Compilation failed"

Is there any consideration to use ASM code in the compiler, the code examples are wrong... or have I to use another compiler, like PASMO, to compile ASM?

Thanks in advance and cheers

EDIT: I've just found this post (<!-- l --><a class="postlink-local" href="https://forum.boriel.com/bug-reports/illegal-preprocessor-errors-on-some-files-solved-t603.html">bug-reports/illegal-preprocessor-errors-on-some-files-solved-t603.html</a><!-- l -->) but I can't manage to solve it Sad


Re: Learning ASM and ZX Basic Compiler - britlion - 2011-08-24

oblo Wrote:I know it's a bit odd to ask this, because the compiler doesn't ask us to learn ASM but...
I'm starting to learn some ASM and besides read some books, I'm trying to get some examples from <!-- m --><a class="postlink" href="http://stu.pido.us/wiki/retrogames:index">http://stu.pido.us/wiki/retrogames:index</a><!-- m --> and <!-- m --><a class="postlink" href="http://members.fortunecity.com/jonathan6/egghead/id2.html">http://members.fortunecity.com/jonathan ... d/id2.html</a><!-- m -->, but every piece of code I try to compile between ASM and END ASM, end with a compiler error, for instance:

Actually, since I started playing with the compiler, I've learned a pile of assembly - up to doing some quite complex things (have a look at the library section of the wiki; I've put quite a few assembly routines in there). What you have to remember is that this is designed to compile code, so you are better off wrapping it in ZX basic, such that it's called from there. Also, different assemblers (or in this case, compilers) have their own little foibles.

oblo Wrote:
Code:
ASM ld a,2 ; upper screen call 5633 ; open channel loop ld de,string ; address of string ld bc,eostr-string ; length of string to print call 8252 ; print our string jp loop ; repeat until screen is full string defb '(your name) is cool' eostr equ $ END ASM

...ends with "temp.bor:11: Error: illegal preprocessor character '$'. Compilation failed" And:

I've reworked this routine into a pattern that is probably good programming practice for such things. I've put the end on, even though it never really gets there - it jumps back to the loop bit forever.

Code:
SUB fastcall printstring () ASM ;If we are calling routines, we need to make sure we put things back as we found them. ; This routine uses a,bc and de so we'll save those. The ROM calls use HL as well. PUSH AF PUSH BC PUSH DE PUSH HL ; and now the main routine. ld a,2 ; upper screen call 5633 ; open channel loop: ld de,string ; address of string ld bc,eostr-string ; length of string to print call 8252 ; print our string jp loop ; repeat until screen is full (this loops forever) string: defb "(your name) is cool" eostr: ; If we were exiting, we'd get to here, so Now we'd recover our saved registers POP HL POP DE POP BC POP AF END ASM END SUB ' Main code ' Let's call our routine printstring() ' End of program.

Things to note -

I've wrapped this into a ZX Basic Subroutine, so it's called like any other ZX Basic Subroutine.

Labels are followed with a colon. That was the main problem with the original version - it's loop: to have a label called loop.

You can't do label EQU $, but you can subtract the value of one label from another in a very similar way.

Quoting in a DEFB requires " each side, not '


For your other routines, try making labels with a : at the end.

For some examples, have a look at the Wiki: <!-- m --><a class="postlink" href="http://www.boriel.com/wiki/en/index.php/ZX_BASIC:Library">http://www.boriel.com/wiki/en/index.php ... IC:Library</a><!-- m -->

Hope this helps!


Re: Learning ASM and ZX Basic Compiler - boriel - 2011-08-24

Note: EQU $ is something that I don't think should be supported because It can be written this way:
Code:
Asm string: defb "(your name) is cool" eostr: ; EQU $ is no needed End Asm
In fact you can use $ for relative jumps, like JR $-5 and the like.


Re: Learning ASM and ZX Basic Compiler - britlion - 2011-08-24

boriel Wrote:Note: EQU $ is something that I don't think should be supported because It can be written this way:
Code:
Asm string: defb "(your name) is cool" eostr: ; EQU $ is no needed End Asm
In fact you can use $ for relative jumps, like JR $-5 and the like.

Exactly my solution! Using EQU $ is a bit long winded. It just means "a label which has a value of here" when all labels have that anyway!


Re: Learning ASM and ZX Basic Compiler - oblo - 2011-08-25

Thanks to both! I hope learn a lot of ASM playing with it Smile

Cheers