- By
**issac** - Follow User

- 469 Views
- Updated On :

Introduction to SIMPLE A reduced instruction set High Level Language (HLL) The Simple programming Language Simple has only 7 statements rem - the remainder of the line is a comment input - read from the keyboard print - write to the terminal goto - jump to a line number

Related searches for Lecture 4

Download Presentation
## PowerPoint Slideshow about 'Lecture 4' - issac

**An Image/Link below is provided (as is) to download presentation**

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript

### Introduction to SIMPLE

### A SIMPLE to SML Compiler

A reduced instruction set High Level Language (HLL)

1BA3 – G Lacey – Semester 1 Lecture 4

The Simple programming Language

- Simple has only 7 statements
- rem - the remainder of the line is a comment
- input - read from the keyboard
- print - write to the terminal
- goto - jump to a line number
- if/goto - if a condition is true jump to another line
- let - evaluate a mathematical expression
- end - end of the program

- Every line has a line number
- Simple only operates on integers
- Simple only has lower case
- Variable names have a single letter

1BA3 – G Lacey – Semester 1 Lecture 4

The Simple programming Language

- Variables can only be integers and do not need to be declared before being used
- Simple mathematical operators
- Addition, subtraction, multiplication, division are +, -, *, /
- greater than >, greater than or equal to >=
- less than <, less than or equal to <=
- equal to ==, not equal to !=
- assign the value to =
- parentheses ( )

- Examples
- 10 rem this is a comment
- 22 input x
- 33 if x == 0 goto 10
- 40 let r = a + b * c / ( a - d )
- 50 print x

1BA3 – G Lacey – Semester 1 Lecture 4

read b

sum = a + b

print sum

end

Determine and print the sum of two numbers10 rem add and print the sum of 2 nums

20 input a

30 input b

40 let c = a + b

50 print c

60 end

1BA3 – G Lacey – Semester 1 Lecture 4

read b

true

if a > b

false

print a

print b

end

Print the larger of 2 numbers1BA3 – G Lacey – Semester 1 Lecture 4

Print the larger of two numbers x 60 print a y 70 end

- 10 input a
- 20 input b
- 30 if a >= b goto ? x
- 40 print b
- 50 goto ? y

1BA3 – G Lacey – Semester 1 Lecture 4

Compute the sum of a series of positive numbers

- Series is terminated by a negative number
- Maximum number of integers in the series is 10
- This problem was solved in a previous lecture using SML

1BA3 – G Lacey – Semester 1 Lecture 4

counter = 0

read a

true

if a < 0

false

true

if counter

== 10

print sum

false

counter +1

end

sum + a

Compute the sum of a series of positive numbers1BA3 – G Lacey – Semester 1 Lecture 4

Compute the sum of a series of positive numbers

10 rem read series of numbers and print sum

12 rem initialise counter and sum

15 let c = 0

20 let s = 0

22 rem start programme

25 input x

30 if x > 0 goto ?

35 if c == 10 goto ?

40 let c = c + 1

45 let s = s + x

47 rem restart loop

50 goto 40

53 rem print sum and end programme

55 print s

60 end

1BA3 – G Lacey – Semester 1 Lecture 4

An introduction to the concepts in translating HLL into Assembly

1BA3 – G Lacey – Semester 1 Lecture 4

Compilers

- Compilers translate High Level Languages (HLLs) like Java and C++ into Machine code
- Two reasons for using HLLs
- Ease of programming
- It is easier to solve large problems using HLLs because most of the machine specific detail is hidden.

- Machine Independence
- Different processors use different instruction sets. This detail is taken care of by the compiler. Thus a c++ program written on a mac will also compile on a p.c.

- Ease of programming

1BA3 – G Lacey – Semester 1 Lecture 4

Translating Simple to Simpletron Machine Language

- Compiling a Simple programme involves translating the Simple Code to SML
- Each Simple statement has to be translated into SML instructions
- Each variable must be allocated memory space
- Each constant must be allocated memory space

1BA3 – G Lacey – Semester 1 Lecture 4

Compiling Simple

- Allocating Data Space for variables and constants
- start at the max memory and work down.
- E.G. in Simpletron begin by allocating memory space 99, then 98, 97...

- Compiling Instructions
- Instructions are placed at the bottom of memory working up.
- E.G. in Simpletron the processor always starts at 00. The first instruction is placed at 00, the next at 01, then 02 ...

- Running out of space
- If the programme is too big (too much code and too many variables and constants) the instruction space will run into the data space.

1BA3 – G Lacey – Semester 1 Lecture 4

Example Production Rules for translating Simple into SML

- 10 input x
- input corresponds to the READ operation SML thus the operator part of the SML instruction is 10
- x is a variable and must be allocated memory space if none has been allocated already.
- the instruction placed in memory location 00 is 1099

- 20 print x
- print corresponds to the WRITE operator in SML thus the operator is 11
- x has already been allocated memory location 99
- the instruction in memory location 01 is 1199

- 30 goto 10
- goto corresponds to the BRANCH operator in SML thus the operator is 40
- the instruction compiled from line number 10 was placed in memory location 00 thus the operand is 00
- the instruction 4000 is placed in memory location 02

- 40 end
- corresponds to HALT in SML thus 4300 goes into location 03

1BA3 – G Lacey – Semester 1 Lecture 4

Example Compilation

Simple Code SML Instructions

10 input x

20 print x

30 goto 10

40 end

1BA3 – G Lacey – Semester 1 Lecture 4

More Complex Production Rules

- To translate if and goto statements the compiler must generate more than one statement.
- First :
- load x into the accumulator

- Second :
- branch if accumulator is zero to instruction from line number 10

Simple SML

10 input x

20 if x == 0 goto 10

1BA3 – G Lacey – Semester 1 Lecture 4

Another Example

Simple SML

10 input x

20 input y

30 if x > y goto 10

1BA3 – G Lacey – Semester 1 Lecture 4

Download Presentation

Connecting to Server..