Вопрос

I am trying to write a translator that translates VM language in Intel x86 assembly language (MASM). Unfortunately I cannot find a proper translation for lt (less than), gt (greater than) or eq (equal), but I would expect that there is something like that in the instruction set. The closest thing I could find was cmp (compare) followed by a conditioned jump. But nothing without a jump.

For example when I want to translate if (x>1 and x<3) do ... the VM code looks something like

push local 0
push constant 1
gt
push local 0
push constant 3
lt
and
if-goto IF_TRUE0

my problem is now that I don't know how to translate that gt and lt as they are not directly followed by a jump, but rather are both part of a single jump condition.

Это было полезно?

Решение

Use another conditional jump. A naive translation would be something like

        cmp local0, 1
        jle .L1
        cmp local0, 3
        jge .L1
        ;; code of true case
.L1:

Note that your translator will have to look a bit further than just the gt/lt operation and its arguments to figure out how a comparison should be translated.

Другие советы

If you want to do the translation automatically (you want to write something like a JIT compiler) you have to think about how the "gt" instruction works:

Example for "GT":

Stack before: X, Y, ...
Stack after: 1, ... if (X<Y)

You need multiple x86 instructions for one "LT" instruction. Example:

  pop ax  ; this is X
  pop cx  ; this is Y
  xor dx,dx ; set edx to 0
  cmp cx,ax
  jle some_label
  mov dx,1
some_label:
  push dx

Using 32-bit code you may use the "setgt" instruction:

pop eax  ; this is X
pop ecx  ; this is Y
xor edx,edx ; set edx to 0
cmp ecx,eax
setgt dl
push edx
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top