64 bit assembler > ASMC Development

High Level Language in MASM

(1/49) > >>

nidud:
There is an attempt to include HLL in MASM, but it is currently not fully implemented. The first problem with the current implementation is that it doesn’t work, and the second problem is that it's not High Level Language.

Analysing the problem

All expansions are pre-processed by the assembler, and this is line-based. The code
--- Code: ---label: mov edx,func(strlen,string)
--- End code ---
expands to
--- Code: ---invoke strlen,string
label: mov edx,eax
--- End code ---

This creates problems for the HLL section so this must be addressed first.

Syntax (.while, .if and .elseif):


--- Code: ---while (<expression>) { <statement> }
--- End code ---

<statement> is executed repeatedly as long as the value of <expression> remains non-zero. This will currently fail if <expression> is expanded.


--- Code: ---<expanded expression>
jmp expression
statement:
<statement>
expression:
<expression>
jnz statement
--- End code ---


--- Code: ---if (<expression1>) { <statement1> }
else if (<expression2>) { <statement2> }
--- End code ---

This will currently fail if <expression2> is expanded.


--- Code: ---<expanded expression1>
expression1:
<expression1>
jz expression2
<statement1>
[color=red]<expanded expression2>[/color]
jmp endif
expression2:
<expression2>
jz endif
<statement2>
endif:
--- End code ---

Fixing the problem

All labels must be corrected to the actual offset of the code.
From
--- Code: ---label: .if func(strlen,string)
--- End code ---
To
--- Code: ---label:
.if func(strlen,string)
--- End code ---

Expansion of <expression> must be delayed and expanded correctly.


--- Code: ---jmp expression
statement:
<statement>
expression:
<expanded expression>
<expression>
jnz statement
--- End code ---

A late expansion of a macro is difficult but possible. It dos however need a full rescan of the source for each pass, so the level of impact to the existing code becomes a huge undertaking. It will also have an impact on the speed of the compilation, so expansion should be done in the first pass to avoid messing up the existing logic.

The problem is not to move the expansion to the right place, but to find a way to split the fixup of labels in the exit code.

Pass one:

--- Code: ---;.while func(foo) - delay expansion
* jmp expression
* statement:
<statement>
--- End code ---
The exit code will be called when .endw if found, an this expands as follows:

--- Code: ---* expression:
* test eax,eax
* jnz statement
--- End code ---

It’s not possible to move the expansion without braking up the code, so whatever approach used this must be done:

--- Code: ---* expression:
<expanded expression>
* test eax,eax
* jnz statement
--- End code ---
So the label must be inserted in pass one, and removed in the next passes. The same approach also applie to the .elseif expansion.


--- Code: ---<expanded expression1>
<expression1>
jz expression2
<statement1>
jmp endif
expression2:
<expanded expression2>
<expression2>
jz endif
<statement2>
endif:
--- End code ---

A test case is made to see if this work as expected. It passes the regression test provided with jwasm v2.08, but no effort has been made to fix the listing, which is destroyed in this process.

In addition to these updates there is also a few non-MASM changes made to this release:
The and/or eax,eax test is replaced by test eax,eax.
The .untilcxz now use dec ecx, jnz label and not loop.

TODO:
To implement a full high level syntax in <expression> also means that assignment of value (as done by habran in the .for loop) and invoke is included:

--- Code: ---.if ((ecx = strlen(string)) > edi)
--- End code ---

jj2007:

--- Quote from: nidud on November 13, 2012, 02:33:57 AM ---The .untilcxz now use dec ecx, jnz label and not loop.
--- End quote ---

Caution with the flags - this might break some existing code.

include \masm32\MasmBasic\MasmBasic.inc   ; download
   Init
   mov ecx, 5
   mov eax, -3
   .Repeat
      inc eax
   .Untilcxz   ; old version
   .if Zero?
      Print Str$("ZERO:   \teax=%i", eax)
   .else
      Print Str$("Non-Zero:\teax=%i", eax)
   .endif
   Print Str$(", ecx=%i\n", ecx)

   mov ecx, 5
   mov eax, -3
   .Repeat
      inc eax
      dec ecx   ; new version
   .Until Zero?
   .if Zero?
      Print Str$("ZERO:   \teax=%i", eax)
   .else
      Print Str$("Non-Zero:\teax=%i", eax)
   .endif
   Print Str$(", ecx=%i", ecx)
   Inkey
   Exit
end start

qWord:
Why are you talking about MASM? You have made an syntax extension to JWASM thus it allows code that isn't compatible with MASM.

EDIT: you may add an option to disable the extension, because existing code that relies on that special expansion behavior won't work anymore.

nidud:

--- Quote from: qWord on November 13, 2012, 03:05:39 AM ---Why are you talking about MASM?

--- End quote ---
The HLL syntax was introduced by MASM, not JWASM.
The problems addressed here is MASM syntax.


--- Quote from: qWord on November 13, 2012, 03:05:39 AM ---You have made an syntax extension to JWASM thus it allows code that isn't compatible with MASM.

--- End quote ---
True, as mention in text.

JJ, I see the first one, but:

--- Quote from:  jj2007 --- .Until Zero?

--- End quote ---
I'll do some testing.

What is the output from the original test now?

nidud:

--- Code: ---.386
.model flat

.code

mov ecx, 5
mov eax, -3
.repeat
inc eax
dec ecx ; new version
.until ZERO?

end
--- End code ---
expands to:
--- Code: --- MOV ECX,00000005h
MOV EAX,FFFFFFFDh
CS000A: INC EAX
DEC ECX
JNZ SHORT CS000A

--- End code ---

--- Code: ---.386
.model flat

.code

mov eax, -3
.repeat
inc eax
.until ZERO?

end
--- End code ---
expands to:
--- Code: --- MOV EAX,FFFFFFFDh
CS0005: INC EAX
JNZ SHORT CS0005

--- End code ---

Navigation

[0] Message Index

[#] Next page

Go to full version