DOC PREVIEW
MIT 6 001 - Register Machines

This preview shows page 1 out of 3 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 3 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 3 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

6.001, Spring 2004—Recitation 24 1MASSACHVSETTS INSTITVTE OF TECHNOLOGYDepartment of Electrical Engineering and Computer Science6.001—Structure and Interpretation of Computer ProgramsSpring 2004Recitation 24Register MachinesExpression Types(const C) A constant value. It acts somewhat like quote. To get the number one, you would use (const1). To get the list (1 2) you would use (const (1 2)).(reg R) Retrieve the value of a register R. To get the value of the register arg0, you would use (regarg0).(label L) Retrieve the offset of the given label L. To get the value of the label loop-top, you woulduse (label loop-top).(op O) Perform operation O on some values. Following the (op O), you should list the input argu-ments to the operation, which may be consts, regs, or labels. An expression may only contain1 op. In order to compute the result of adding 1 to the register arg0, you would use(op +) (reg arg0) (const 1).Instruction Types(assign reg expr)Sets register reg to be the result of expression expr. The assigned register doesn’t need a tagbecause it is always a register being assigned. For example, to increment the result register:(assign result (op +) (reg result) (const 1))(perform expr)Sometimes an expr is done only for it’s side-effect and its value is unwanted. perform worksjust like assign, but without assigning a register. For example, to output register tmp:(perform (op write-line) (reg tmp))(test expr)This is equivalent to assigning the cr. The cr register is used to determine whether to takea branch. For example, to set the cr based on whether the register x is less than 10:(test (op <) (reg x) (const 10))(goto expr)Sets the pc to be the result of expr, which is usually a label or a register. Effectively continuesthe execution at another point in the code. To jump to the lab el loop-top:(goto (label loop-top))(branch expr)If the value in the cr is true, acts like a goto. Otherwise it does nothing. To conditionallyjump to the label loop-done:(branch (label loop-done))6.001, Spring 2004—Recitation 24 2Writing CodeWrite double: code to compute 2x, given x in arg0, and leave the output in result.double(assign result (op *) (reg arg0) (const 2))(goto (reg continue))1. Write func: code to compute x2+ y, given x in arg0, y in arg1, and leave the output inresult.2. Write abs: code to compute |x|, give x in arg0, leave the output in result. abs is not anavailable primitive.3. Write infinite-loop: code that neve r halts.4. Determine what the following code does, then write the scheme code that does the samething.foo(test (op <) (reg arg0) (reg arg1))(branch (label foo-done))(assign arg0 (op -) (reg arg0) (reg arg1))(goto (label foo))foo-done(assign result (op =) (reg arg0) (const 0))(goto (reg continue))6.001, Spring 2004—Recitation 24 35. Write sum-digits: code that computes the sum of the digits in arg0, leaving the output inresult. You may assume quotient and remainder are available as primitive operations.6. Write reduce-to-digit: code that sums the digits of a number repeatedly until a resultwith only a single digit rem ains. For example, 185 ⇒ 14 ⇒ 5. As usual, input in arg0, butoutput in result a cons cell whose car is the original number and cdr is the digit it reducesto (result of previous example is (185 .


View Full Document

MIT 6 001 - Register Machines

Documents in this Course
Quiz 1

Quiz 1

6 pages

Databases

Databases

12 pages

rec20

rec20

2 pages

Quiz II

Quiz II

15 pages

Streams

Streams

5 pages

Load more
Download Register Machines
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Register Machines and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Register Machines 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?