# Assembler Optimizer

Page 36/49
29 | 30 | 31 | 32 | 33 | 34 | 35 | | 37 | 38 | 39 | 40 | 41

Btw, funny discovery of the night, it seems that different assemblers also differ in the semantics of "\$"! For example, the following statement:

```    org 0x4000
dw 0, \$
```

Would map to this using Glass/sjasm:

```    org 0x4000
dw 0, 0x4000
```

But to this in asMSX!

```    org 0x4000
dw 0, 0x4002
```

It seems that in order to be compatible, the "\$" symbol is not safe to use anywhere but in the first element of a data statement. I'll have to split them into several statements if a "\$" is detected in MDL to prevent this incompatibility from happening!

Strictly speaking the asMSX seems the correct approach; but obviously it depends of the use case
With sjASM I've used it inside code, usually to make relative jumps without specifing a label (e.g.: inside macros).

So does

```org 4000h
ld hl,\$
```

output

```ld hl,4001h
```

?

I use \$ a lot with Sjasm, particularly (like jltursan) for small relative jumps that need no label.
In macros as well as in the main code, like this :

```and     a
jr      z,\$+3
dec     a
...```

It's logical that within one "unit" the value of '\$' remains constant. Doing otherwise would be very surprising:

```org 3ffeh
ld hl,\$```

If the value of '\$' would be updated per byte this would produce 'ld hl,40ff'. And that can never be the intention ;-)
Then the question becomes what is a "unit".

Personally I think about

```dw <x>,<y>
```

as a shorthand notation for

```dw <x>
dw <y>
```

So it would surprise me if both would not produce the same result.

But that's my personal opinion. I can also understand the reasoning behind the Glass/sjasm interpretation.

```org 4000h
ld hl,\$
```

Produces in all assemblers I've tried it (luckily! haha)

```ld hl,4000h
```

But indeed, there's lots of interpretations that all make sense! I was just surprised! It's similar to the different semantics of the "org" keyword. In some assemblers only changes the value of "\$", and that's it. But in some assemblers org actually causes the subsequent code to be literally moved to a different place in the resulting binary. Again, both make sense, but it can be confusing when coming from one assembler and trying to use another

A bit more problematic is that I also discovered yesterday that different assemblers are assigning different precedence to different operators. So, in some assemblers the expression "1 << 4 + 1" is interpreted as "(1 << 4) + 1" and in others as "1 << (4 + 1)". I discovered it the hard way, trying to figure out why was MDL not able to correctly compile one of the projects recently added to my test suite. So, I have a "todo" item in MDL to re-parenthesize expressions when the precedences of the current assembler dialect differ from the standard preferences commonly used in languages such as c/c++ ( https://en.cppreference.com/w/cpp/language/operator_precedence ).

The choices for the address of pc-relative references (\$) are either:

1. Statement-relative
2. Data-relative

Statement-relative is used in assembly source, data-relative is used for relocation entries in relocatable object output files. In my opinion it is inconsistent for an assembler to use statement-relative for everything except for vararg statements like db/dw.

@Grauw: I mostly agree. I think the only difference is that I see 'dw x,y' as a shorthand notation for two statements while you see it as a single statement. A bit similar to how a single macro invocation statement expands to (is a shorthand notation for) multiple instruction statements.
Both points of view are valid. But it can be unexpected when you had the other thing in mind.

If you would have *me* to *guess*, I would expect `dw 0,\$` to produce 0000, 4002 ... so this is surprising also for me. Interesting. (of course it makes sense the way that the \$ is with regard to the `dw` itself, but I had in head logic which wouter_ describes, treating values in `dw` quite separately.

In case of sjasmplus there's more dichotomy to it. The `dw 0,\$` does indeed produce `0000, 4000`, but the instructions multi-argument feature allows to write: `ld hl,\$,de,\$` which does produce `ld hl,\$4000 : ld de,\$4003` (!)
(as usual, the multi-argument feature of sjasmplus is creating mess... )

---- different topic ----
santiontanon: did I notice correctly you are slowly adding support for variants of CPUs? If yes, what about ZX Next Z80N? It's regular Z80 with about 30 extra instructions: http://ped.7gods.org/Z80N_table_ClrHome.html
(I mean in long term, I don't care if it takes another year or five, but whether you even consider it at all).

I am indeed interested in adding more CPUs!! As a matter of fact, I've tried to keep most of the code CPU-agnostic (although some parts still have a few z80 things hard-coded hehe) in the hope of one day being able to add maybe even some motorola CPUs (far future! haha). So, definitively interested on this, thanks for the pointer! Do you know of any assembler code base that uses this and I could use to test?

Page 36/49
29 | 30 | 31 | 32 | 33 | 34 | 35 | | 37 | 38 | 39 | 40 | 41