newcomp - CS313E: Elements of Software Design A Language...

Info iconThis preview shows pages 1–7. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: CS313E: Elements of Software Design A Language Interpreter Dr. Bill Young Department of Computer Sciences University of Texas at Austin Last updated: November 21, 2011 at 06:44 CS313E Slideset 11: 1 A Language Interpreter An Extended Example The goals of this slideset are: 1 Put together many of the lessons youve learned this semester. 2 Give you more experience in reading some fairly complex code. 3 Show you an example of structuring a reasonably-sized Python program. You will not be expected to know the specifics of the code in this slideset, but you are expected to know how to use the constructs used and to be able to understand the individual code fragments. The complete code is linked from the syllabus webpage. Id suggest that you study this code and make sure you understand it even if you couldnt produce it. CS313E Slideset 11: 2 A Language Interpreter A Simple Language The project: Construct in Python an interpreter for simple assembly language programs such as: # This simple subroutine takes an argument on the # stack, squares it, and leaves the result on the # stack square: dup mul ret # The test routine. Read in an integer, square it # and output the result. main: decl n in n push n call square pop ans out ans hlt CS313E Slideset 11: 3 A Language Interpreter The Language Our language consists of the following statements, though we wont use most of them in our sample programs. Arguments in brackets are optional: DECL var [val]-- declare a new variable, default to 0 for value OUT var/int-- output the value of var IN var-- print a prompt, wait for input, store in var PUSH var/int-- push value onto the stack POP [var]-- pop TOS into variable var (or discard value) TOP var-- copy TOS into variable var, leave on stack DUP-- duplicate TOS and push DEC-- decrement the TOS value by 1 INC-- increment the TOS value by 1 ADD-- pop two values, add, push result SUB-- pop two values, sub top from top2, push result Continues on the next page. CS313E Slideset 11: 4 A Language Interpreter The Language (2) MUL-- pop two values, multiply, push result MOD-- pop two values, compute s2 mod s1, push result JMP label-- change pc to point to label CMP var/int-- compare TOS to var or value, set flags JEQ label-- jump to label if comparison = 0 JNE label-- jump to label if comparison != 0 JLT label-- jump to label is comparison < 0 JGT label-- jump to label is comparison > 0 JGE, label-- jump to label if comparison >= 0 JLE label-- jump to label if comparison <= 0 CALL label-- jump to label, save return address on callstack RET-- pop return address from callstack, jump HLT-- halt the machine CS313E Slideset 11: 5 A Language Interpreter The Tasks What problems do we have to solve?...
View Full Document

This note was uploaded on 12/01/2011 for the course CS 329E taught by Professor Mitra during the Spring '11 term at University of Texas at Austin.

Page1 / 31

newcomp - CS313E: Elements of Software Design A Language...

This preview shows document pages 1 - 7. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online