Results 1 to 5 of 5

Thread: dm Simple VM

  1. #1

    Thread Starter
    Fanatic Member BenJones's Avatar
    Join Date
    Mar 2010
    Location
    Wales UK
    Posts
    629

    Post dm Simple VM

    hi, This is my little Toy VM I try to made tonight it a little basic at the moment since it my first real try at something like this. I try and add more stuff as I go along anyway hope you like the first version.

    Comments are welcome.

    vb Code:
    1. 'DM++ Virutal Machine Alpha 1
    2. Option Explicit
    3.  
    4. 'Registers
    5. Private Registers(8) As Integer
    6.  
    7. 'vm stuff
    8. Private Enum EOpCodes
    9.     RET = 0
    10.     PUSH
    11.     POP
    12.     IADD
    13.     ISUB
    14.     IMUL
    15.     IDIV
    16.     ISTORE
    17.     ILOAD
    18.     IPRINT
    19. End Enum
    20.  
    21. Private Const MAX_CODE = 100
    22. Private progcode(MAX_CODE) As Integer
    23. Private pCodeCnt As Integer
    24. Private PC As Integer
    25. Private Opcode As EOpCodes
    26.  
    27. 'Stack start code
    28. Private a_stack(100) As Integer
    29. Private StkPc As Integer
    30.  
    31. Private Function StackTop() As Integer
    32.     If (StkPc < 0) Then StkPc = 0
    33.     StackTop = a_stack(StkPc)
    34. End Function
    35.  
    36. Private Sub sPop()
    37.     StkPc = (StkPc - 1)
    38. End Sub
    39.  
    40. Private Sub sPush(item As Integer)
    41.     If (StkPc < 0) Then StkPc = 0
    42.     a_stack(StkPc) = item
    43.     StkPc = (StkPc + 1)
    44. End Sub
    45.  
    46. 'End of stack code
    47.  
    48. 'Start of vm
    49. Private Sub ResetVM()
    50.     PC = 0
    51.     StkPc = 0
    52.     Erase a_stack
    53.     Erase progcode
    54. End Sub
    55.  
    56. Private Sub VM()
    57. Dim value1 As Integer
    58. Dim value2 As Integer
    59. Dim RegAddr As Integer
    60.  
    61.     While (PC < pCodeCnt)
    62.         'Get byte.
    63.         Opcode = progcode(PC)
    64.        
    65.         Select Case Opcode
    66.             Case EOpCodes.PUSH
    67.                 PC = (PC + 1)
    68.                 Call sPush(progcode(PC))
    69.             Case EOpCodes.IADD
    70.                 Call sPop
    71.                 value1 = StackTop()
    72.                 Call sPop
    73.                 value2 = StackTop()
    74.                 'Push back the answer
    75.                 Call sPush(value1 + value2)
    76.             Case EOpCodes.ISUB
    77.                 Call sPop
    78.                 value1 = StackTop()
    79.                 Call sPop
    80.                 value2 = StackTop()
    81.                 'Push back the answer
    82.                 Call sPush(value2 - value1)
    83.             Case EOpCodes.IMUL
    84.                 Call sPop
    85.                 value1 = StackTop()
    86.                 Call sPop
    87.                 value2 = StackTop()
    88.                 'Push back the answer
    89.                 Call sPush(value1 * value2)
    90.             Case EOpCodes.ISTORE
    91.                 PC = (PC + 1)
    92.                 'Store in regsiter get addr
    93.                 RegAddr = progcode(PC)
    94.                 'Store value into register.
    95.                 Call sPop
    96.                 Registers(RegAddr) = StackTop
    97.             Case EOpCodes.ILOAD
    98.                 PC = (PC + 1)
    99.                 'Get register address.
    100.                 RegAddr = progcode(PC)
    101.                 'Get value
    102.                 'Push onto the stack.
    103.                 Call sPush(Registers(RegAddr))
    104.             Case EOpCodes.IPRINT
    105.                 'Get top of stack
    106.                 Call sPop
    107.                 Call MsgBox("Stack Top = " & CInt(StackTop()))
    108.             Case EOpCodes.RET
    109.                 'Close
    110.                 Call Unload(frmmain)
    111.         End Select
    112.        
    113.         'INC Program Counter
    114.         PC = (PC + 1)
    115.     Wend
    116.    
    117. End Sub
    118. 'End of vm
    119.  
    120. Private Sub EmitCode(code As Integer)
    121.     progcode(pCodeCnt) = code
    122.     pCodeCnt = (pCodeCnt + 1)
    123. End Sub
    124.  
    125. Private Sub cmdExit_Click()
    126.     Call Unload(Me)
    127. End Sub
    128.  
    129. Private Sub cmdRun_Click()
    130.     'Simple PUSH,ADD Print example
    131.     'PUSH 10
    132.     'PUSH 10
    133.     'IADD
    134.     'IPRINT
    135.     'RET
    136.    
    137.     Call EmitCode(PUSH)
    138.     Call EmitCode(10)
    139.     Call EmitCode(PUSH)
    140.     Call EmitCode(16)
    141.     Call EmitCode(IADD)
    142.     Call EmitCode(IPRINT)
    143.     Call EmitCode(RET)
    144.     'Run VM
    145.     Call VM
    146.    
    147.     'Example register demo Push,Store,Load
    148.     'PUSH 16
    149.     'ISTORE 1
    150.     'ILOAD 1
    151.     'PUSH 2
    152.     'IADD
    153.     'PRINTI
    154.    
    155.     'Emit test program registers
    156.     Call EmitCode(PUSH)
    157.     Call EmitCode(16)
    158.     Call EmitCode(ISTORE)
    159.     Call EmitCode(1)        'Set Regsiter 1 stack top value
    160.     Call EmitCode(ILOAD)    'Get register 1
    161.     Call EmitCode(1)
    162.     Call EmitCode(PUSH)
    163.     Call EmitCode(2)
    164.     'Add 2 to the value on the stack
    165.     Call EmitCode(IADD)
    166.     Call EmitCode(IPRINT)
    167.     Call EmitCode(RET)
    168.     'Run VM
    169.     Call VM
    170. End Sub
    Last edited by BenJones; Jul 31st, 2015 at 07:30 PM.

  2. #2

    Thread Starter
    Fanatic Member BenJones's Avatar
    Join Date
    Mar 2010
    Location
    Wales UK
    Posts
    629

    Post Re: dm Simple VM

    Here is a new update of DM++ Virtual Machine. I now created the assembler. This allows you to take a source file in the DM++ language and then convert to byte code that can be run with the virtual machine. I also added global memory support for storing integers; I also added printing of strings only const string support at the moment support made stack and program code arrays larger and some other little tweaks.

    Here some code that you can assemble as a test,

    Code:
    ; x = 8
    ICONST 8
    GSTORE 0
    
    ;y = x
    GLOAD 0
    GSTORE 1
    
    ;output y
    GLOAD 1
    PRINT_I
    RET
    Hope you like the new update.
    Please note this is still an ongoing project so keep checking back. Please leave comments.
    Attached Files Attached Files

  3. #3
    New Member
    Join Date
    Nov 2012
    Posts
    5

    Re: dm Simple VM

    Hi Ben, finally got here, looking good

  4. #4

    Thread Starter
    Fanatic Member BenJones's Avatar
    Join Date
    Mar 2010
    Location
    Wales UK
    Posts
    629

    Re: dm Simple VM

    Quote Originally Posted by dave carter View Post
    Hi Ben, finally got here, looking good
    Glad you like it check back for updates. I am going to try and keep on this project. I seem to start things and never finish them, hopfully I can finish this one.

  5. #5
    New Member
    Join Date
    Nov 2012
    Posts
    5

    Re: dm Simple VM

    Quote Originally Posted by BenJones View Post
    I seem to start things and never finish them, hopfully I can finish this one.
    start something and not finish, where i done heard that before I got a shed of hard drives full of stuff i started... these days i just want the bearings and magnets inside the hd housing and hope i didn't trash anything potentially valuable.

    This VM project does look particularly cool.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width