Work / Virtual Machine / Example Source Code

2011-02-19 23:24:35

Example VM Program

Here is an example 'Hello World' program written in vm assembly:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
; $Header: /vm/common/examples/projects/helloWorld/main.vms 0    19/03/2011 22:51 fusi $

segment .data
    helloStr    db      "Hello World!", 0
   
segment .text
    import  std.cout
    export  main

; void main()
main:
    entr    1               ; create a stack frame with 1 slot
    movd    0, helloStr     ; move address of helloStr into local var 0
    farc    std.cout        ; print helloStr to stdout
    leav                    ; exit stack frame
    halt                    ; halt execution

The above compiles to the following byte-code which is 136 bytes in length:

1
2
3
4
5
6
7
8
9
00000000 73 76 6d 78 01 00 00 00 43 2c 0f 4f 00 00 00 00 svmx....C,.O....
00000010 20 00 00 00 0a 00 00 00 68 65 6c 6c 6f 57 6f 72  .......helloWor
00000020 6c 64 00 00 01 00 00 00 08 00 00 00 73 74 64 2e ld..........std.
00000030 63 6f 75 74 01 00 00 00 18 00 00 00 01 00 00 00 cout............
00000040 04 00 00 00 6d 61 69 6e 00 00 00 00 10 00 00 00 ....main........
00000050 24 00 00 00 48 65 6c 6c 6f 20 57 6f 72 6c 64 21 $...Hello World!
00000060 00 00 00 00 01 00 00 00 01 00 00 00 23 00 00 00 ............#...
00000070 00 00 00 00 00 00 00 00 06 00 00 00 66 61 72 63 ............farc
00000080 03 00 00 00 61 00 00 00 ....a...

Which when run in a compatible execution core will print the message 'Hello World!' to stdout :)

vm-source-helloWorld-2012.07.08.zip

Size: 30.49 KB, Date: 2012-07-08 20:23, MD5: 21001f3fd7bce502f58eb866bcaf0baf