Virtual machine: assembly instructions

From: Morten Aune Lyrstad (morten_at_wantsno.spam)
Date: 01/10/05


Date: Mon, 10 Jan 2005 00:17:13 +0100

Hi there! I am writing a stack based virtual machine for scripting in
games and applications in c++ (and quite successfully too, I might add
:-)) but I have a fundamental problem:

I have NEVER actually programmed with assembly.

Theoretically, I know what assembly language is, and how it works. But
as I have no experience with the practical use, I don't know the
'optimal' use. Could someone please fill me in on how to best program
the most common functions:

do {
} while (...)

while (...) {
}

for (i = 0; i < 10; i++) {
}

if (...) {
} else {
}

Hm... is there something I am forgetting?

And then follows my own attempt at a for loop:

---------------- SAGA CODE ----------------
push [0] ; Push the address of the first variable onto the stack
push 10 ; Push a value onto the stack
store ; Store stack[0] onto the address in stack[1]

push [1] ;\
push 0 ;} Do the same with the second variable
store ;/

loopStart:
push [1] ; Push the address of the second variable onto the stack
load ; Load the value in the variable and push it onto the stack

push [0] ;} Load the first variable
load ;/

; Now stack[0] is 10, and stack[1] is 0.

cmpi ; Compare the two topmost values on the stack.
; Pushes 1 if equal or 0 if not.

push [1] ;} Push the address of the second variable onto the stack
; (for the 'store' further down)

push [1] ;} Load the second variable again
load ;/
push 1 ; Push 1 onto the stack
addi ; Add the two topmost values on the stack

store ; Store the new value into the second variable
; (the address was pushed further up)

; Now the two variables should respectively be 10 and 1.

jmpzero loopStart ; Jump to the loopStart label
; if the top of the stack is zero.

; Otherwise, we're done.
-------------- SAGA CODE END --------------

List of current operations:

end: Quit the program immediately

/*** Stack operations ***/
push: push value onto the stack
pop: pop the top of the stack

/*** Conversion operations ***/
itof: convert integer to float
ftoi: convert float to integer

/*** Comparison operations ***/
// true = 1 and false = 0; always pushed onto int stack.
lessi: If top+1 of int stack is less than top; push true else push false
morei: If top+1 of int stack is more than top; push true else push false
lesseqli: If top+1 of int stack is less than top; push true else push false
moreeqli: If top+1 of int stack is more than top; push true else push false
lessf: If top+1 of float stack is less than top; push true else push
false
moref: If top+1 of float stack is more than top; push true else push
false
lesseqlf: If top+1 of float stack is less than top; push true else push
false
moreeqlf: If top+1 of float stack is more than top; push true else push
false
cmpf: If top+1 of float stack equals top; push true else push false
cmpi: If top+1 of int stack equals top; push true else push false

/*** Jumping operations ***/
jmp: Jump to operation specified in the top of the int stack
jmpfalse: jmp conditionally if top of stack equals 0
jmptrue: jmp conditionally if top of stack is not equal 0

/*** Memory management operations ***/
load: from memory address specified at top of stack, push value onto
stack
store: store *top at memory address specified with (top+1), pop both
from stack

/*** Math operations ***/
addi: Add two integers, pop them and push the answer
addf: Add two floats, pop them and push the answer
subi: Subtract two integers, pop them and push the answer
subf: Subtract two floats, pop them and push the answer
muli: Multiply two integers, pop them and push the answer
mulf: Multiply two floats, pop them and push the answer
divi: Divide two integers, pop them and push the answer
divf: Divide two floats, pop them and push the answer



Relevant Pages

  • Re: Is it wise to push all-in early in a tournament?
    ... The best times to make a push play ... But why call when you can push and take down a nice pot when your opponent ... But remember that if this "big bet" is a significant portion of your stack, ... I consider it my goal in any tournament to do the latter as much as ...
    (rec.gambling.poker)
  • Re: Is it wise to push all-in early in a tournament?
    ... whether or not the rest of the table is splashing chips around. ... I may push more than I want to and you may be more conservative than ... Do I want to play this opponent after the flop? ... push at anytime and I may not want to risk my stack on a suckout when I ...
    (rec.gambling.poker)
  • Re: Using C and Assembly code: 64Bit Calling convention
    ... RSP holds 1210 at this point. ... 0's local stack area. ... you start to push or move anything onto the stack. ... The only exception from this rule ...
    (comp.lang.asm.x86)
  • Re: Building Unification Table - tranforming prolog like notation into lisp notation
    ... |> problem comes from tranlating prolog notation into lisp notation. ... ;; Here is a quickly done stack based solution for the problem Slobodan ... (defstruct composite-term str indices) ... (push (cons (composite-term-str item) ...
    (comp.lang.lisp)
  • Re: Theory of swimming
    ... > third law that is behind the push that makes us float in water. ...
    (sci.physics)