Code Monkey home page Code Monkey logo

pl-homeworks-4-stackmachine's Introduction

堆栈机的C++实现

指令格式

Opcode 指令名 指令长度 说明
0 Cst(i) 2 整数常量i
1 Add 1 加法运算,使用栈顶前两个元素运算
2 Mul 1 乘法运算,使用栈顶前两个元素运算
3 Var(i) 2 引用变量,距离栈顶深度为i
4 Pop 1 丢弃栈顶元素
5 Swap 1 交换栈顶前两个元素
6 Call(addr,n) 3 函数调用,PC变为addr,参数数量为n
7 Ret(n) 2 函数返回,参数数量为n
8 IfZero(addr) 2 栈顶为零值则跳转,PC变为addr,否则PC正常增加
9 Goto(addr) 2 无条件跳转,PC变为addr
10 Exit 1 程序退出,返回值为栈顶元素
11 Leq 1 小于等于运算,用栈顶前两个元素运算,先入栈元素减去后入栈元素小于等于零则结果为1(true),否则为0(false)

Calling convention

函数调用传递的参数个数由Call(f, n)Ret(n)指令中的n决定。下列叙述假设栈空间向高地址方向增长。

函数调用

设函数f的参数数量为n,则调用时的栈帧数据为:

数据说明 数据位置(栈顶为SP)
实参1 SP-n
实参i(1<i<n) SP-i
实参n SP-1
返回地址 SP

函数返回

设函数f的参数数量为n,则函数f返回时的栈帧数据为:

数据说明 数据位置(栈顶为SP) 对应操作
实参1 SP-n-1 丢弃
实参i(1<i<n) SP-i-1 丢弃
实参n SP-2 丢弃
返回地址 SP-1 作为新的PC值,丢弃
返回值 SP 保留,仍位于栈顶

pl-homeworks-4-stackmachine's People

Contributors

lxr2010 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.