Z80 asm/disasm in Z80 asm [solution found]

The place for codemasters or beginners to talk about programming any language for the Spectrum.
User avatar
ketmar
Manic Miner
Posts: 207
Joined: Tue Jun 16, 2020 5:25 pm

Re: Z80 asm/disasm in Z80 asm, request

Post by ketmar »

intermediate results:

Image

assembler size: 752
assembler tables size: 443

sizes are without expression parser and label manager. i'm expecting the whole thing to fit into 2kb (math expression parsing can be quite big). even 2.5/3 kb is still ok -- it is not a big price to pay to have full-featured assembler in dsForth.

i may include expression parser into assembler module, leaving only label manager as external dependence, so people may avoid writing it again and again. it is still nicely isolated from the other asm code, so it would be possible to replace it with your own.

ah, the license. BBC Basic (Z80) is zlib-licensed, so the asm will use the same license too.


p.s.: contrary to my other projects, i'm avoiding self-modifying code here, so the whole thing could be used in ROMs.
0 x

User avatar
ketmar
Manic Miner
Posts: 207
Joined: Tue Jun 16, 2020 5:25 pm

Re: Z80 asm/disasm in Z80 asm, request

Post by ketmar »

here is the soruce code repository, so you can watch my progress if you want to. ;-) please, note that it is heavy WIP yet.
0 x

User avatar
ketmar
Manic Miner
Posts: 207
Joined: Tue Jun 16, 2020 5:25 pm

Re: Z80 asm/disasm in Z80 asm, request

Post by ketmar »

as usual, i was slightly overoptimistic. ;-) with expression parser (and without label manager) assembler size is ~2300 bytes. still acceptable, considering that expression parser is quite powerful (it supports operator precedence, parentheses, 16-bit mul/div/mod/shl/shr/and/or/xor/bitneg), and numeric parser supports wide range of prefixes and suffixes ("&"/"#"/"$"/"%"/"0x"/"0b"/"0o" as prefixes, "O"/"B"/"H" as suffixes). this number parser is more than 300 bytes by itself! ;-)

i am sure that Einar will be able to make it at least 50% smaller, but it is already good for me. ;-) my actual budget is ~3KB, and even with label manager it should still fit in 2500-2700 bytes.

i tried to comment the code, and make it easily reusable (including some comments in the original asm source), so feel free to look at it if you'll need built-in assembler for your z80 project. except for several runtime variables, the code is fully ROMable. it never calls any input/output routines too. IY register is used for input buffer pointer, tho, and IX register is used for output buffer pointer.

i will prolly make some more small changes to the code (like moving expression parser to the main asm module, and adding callback for getting label value into it), but there should be no big changes.

here's a k00l screenshot of the testing app, because people love screenshots!
SpoilerShow
Image
0 x

User avatar
ketmar
Manic Miner
Posts: 207
Joined: Tue Jun 16, 2020 5:25 pm

Re: Z80 asm/disasm in Z80 asm, request

Post by ketmar »

i moved everything asm-related into one source file (assembler and expression parser), added missing code to expression parser so it now understands labels (i.e. calls label manager callback for that), and added simple label manager to show how it could be written.

i also dropped disasm module to the repository.

in case you missed it, here's the repo with the sources.
0 x

User avatar
ketmar
Manic Miner
Posts: 207
Joined: Tue Jun 16, 2020 5:25 pm

Re: Z80 asm/disasm in Z80 asm [solution found]

Post by ketmar »

managed to strip out 30 bytes from assembler, and add "division by zero" check. less is more! ;-)
0 x

Post Reply