C 語言編寫的小型虛擬機:CarpVM

jopen 10年前發布 | 19K 次閱讀 CarpVM C/C++開發

CarpVM 是一個用 C 語言編寫的小型虛擬機。可以嵌入在應用程序中使用。

包含的指令集有:

  • HALT (code): Halts and attempts to clean up stack, data memory, and label memory before exiting with given exit code.

  • NOP (): Does nothing. Seriously.

  • LOAD (reg, val): Loads given integer value into given register.

  • MOV (dst, src): Copies contents of src register into dst register.

  • ADD (): Pops the top two integers from the stack and pushes their sum.

  • SUB (): Pops the top two integers from the stack and pushes the difference (lower minus upper).

  • MUL (): Pops the top two integers from the stack and pushes their product.

  • MOD (rega, regb): Computes rega % regb and stores in ERX.

  • NOT (reg): Computes bitwise NOT of reg and stores in reg.

  • XOR (): Pops the top two integers from the stack and XORs them.

  • OR (): Pops the top two integers from the stack and ORs them.

  • AND (): Pops the top two integers from the stack and ANDs them.

  • INCR (reg): Increments value in given register.

  • DECR (reg): Decrements value in given register.

  • INC (): Increments the value at the top of the stack.

  • DEC (): Decrements the value at the top of the stack.

  • PUSHR (reg): Pushes value in given register.

  • PUSH (val): Pushes given value.

  • POP (val): Pops an integer from the stack and dumps it into GBG.

  • CMP (): Pops the top two integers from the stack and checks if equal. 0 means equal. Result will be pushed onto the stack.

  • MOV (rega, regb): Move value in rega to regb.

  • JZ (addr): Jumps to given absolute address if top of the stack is 0.

  • RJZ (diff): Adds differential to current EIP (relative jump) if top of the stack is 0.

  • JNZ (addr): Jumps to given absolute address if top of the stack is not 0.

  • RJNZ (diff): Adds differential to current EIP (relative jump) if top of the stack is not 0.

  • JMP (addr): Jumps to given absolute address.

  • RJMP (diff): Adds differential to current EIP (relative jump).

  • DBS (key, val): Sets data memory at key (string pointer) to given value.

  • DBG (key, reg): Gets value from data memory at key (string pointer) and dumps it into given register.

  • CALL (key, nargs): Save state and set EIP to value in data memory at key.

  • RET (val): Push return value and load state.

  • PREG (reg): Prints contents of given register.

  • PTOP (): Peeks top of stack and prints top value.

項目主頁:http://www.baiduhome.net/lib/view/home/1407202369348

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!