Download Random Access Machines - Programming Languages | CS 550 and more Papers Programming Languages in PDF only on Docsity!
CS 550 - Programming Languages
Random Access Machines
Jeremy R. Johnson
Introduction
Objective: To introduce a simple model of a computer thatwill be used to operationally define the semantics of the MiniLanguage. In the following lecture, a compiler will beconstructed that translates Mini Language Programs toequivalent programs that execute on a RAM using RAMassembly language (RAL).
A Random Access Machine (RAM) is an abstract model ofcomputation that resembles a simple idealized computer. It isequivalent in computational power to a Turing machine (canperform any computation). Despite its simplicity it providessome intuition as to how a program executes on a computer.In practice the size of the memory is bounded.
A Random Access Machine
Control Unit
AC
Program
Memory
AC = accumulator
register
Instruction Set
LDA X; Load the AC with the contents of memory address X
LDI
X; Load the AC indirectly with the contents of address X
STA X; Store the contents of the AC at memory address X
STI
X; Store the contents of the AC indirectly at address X
ADD X; Add the contents of address X to the contents of the AC
SUB X; Subtract the contents of address X from the AC
JMP X; Jump to the instruction labeled X
JMZ X; Jump to the instruction labeled X if the AC contains 0
JMN X; Jump to the instruction labeled X if the contents of the AC ;is negative
HLT
; Halt execution
Sample RAM Program
LDI
3; get ith entry from A
ADD 4; add offset to compute index j
STA 5; store index j
LDI
5; get jth entry from B
JMZ 9; if entry 0, go to 9
LDA 3; if entry 1, get index i
STA 2; and store it at 2.
HLT
; stop execution
LDA 1; get constant 1
- STI 5; and store it in B
- LDA 3; get index i 12. SUB 4; subtract limit 13. JMZ 8; if i = limit, stop 14. LDA 3; get index i again 15. ADD 1; increment i 16. STA 3; store new value of i 17. JMP 1;
AC
Memory
constant
answer
Index i
Limit of A
Index j
A
B
Exercises
Modify STOR so that when a computation finishes and theinput sequence contained a duplicate integer, we know whatthat integer was.
Modify STOR so that it uses array indexing when accessingthe array A instead of pointer arithmetic (i.e. the index into Ashould be an array index, starting with 1, rather than anaddress of a location in the array).
Write a RAL program which takes two input integers ataddresses 1 and 2 and multiplies them storing the result ataddress 4.