Virtual machine: assembly instructions
From: Morten Aune Lyrstad (morten_at_wantsno.spam)
Date: 01/10/05
- Next message: Willem: "Re: Virtual machine: assembly instructions"
- Previous message: newcool_at_gmail.com: "Fast efficient way to test a small number for primality?"
- Next in thread: Willem: "Re: Virtual machine: assembly instructions"
- Reply: Willem: "Re: Virtual machine: assembly instructions"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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
- Next message: Willem: "Re: Virtual machine: assembly instructions"
- Previous message: newcool_at_gmail.com: "Fast efficient way to test a small number for primality?"
- Next in thread: Willem: "Re: Virtual machine: assembly instructions"
- Reply: Willem: "Re: Virtual machine: assembly instructions"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|