Motivation $�������� ���������� ��������� 0000000000010000 0000000000010000 class Main { class Main { ������������������ 1110111111001000 1110111111001000 static int x; 0000000000010001 static int x; 0000000000010001 1110101010001000 1110101010001000 �������������� ��!��� 0000000000010000 function void main() { 0000000000010000 function void main() { �������������� 1111110000010000 // Inputs and multiplies two numbers 1111110000010000 // Inputs and multiplies two numbers 0000000000000000 �������"�������# var int a, b, x; 0000000000000000 var int a, b, x; 1111010011010000 1111010011010000 let a = Keyboard.readInt(“Enter a number”); let a = Keyboard.readInt(“Enter a number”); 0000000000010010 0000000000010010 let b = Keyboard.readInt(“Enter a number”); let b = Keyboard.readInt(“Enter a number”); 1110001100000001 1110001100000001 Compiler let x = mult(a,b); 0000000000010000 let x = mult(a,b); 0000000000010000 return; 1111110000010000 return; 1111110000010000 0000000000010001 } 0000000000010001 } 0000000000010000 } 0000000000010000 } 1110111111001000 1110111111001000 0000000000010001 0000000000010001 // Multiplies two numbers. // Multiplies two numbers. 1110101010001000 1110101010001000 function int mult(int x, int y) { function int mult(int x, int y) { 0000000000010000 0000000000010000 var int result, j; 1111110000010000 var int result, j; 1111110000010000 let result = 0; let j = y; 0000000000000000 let result = 0; let j = y; 0000000000000000 while ~(j = 0) { 1111010011010000 while ~(j = 0) { 1111010011010000 let result = result + x; 0000000000010010 let result = result + x; 0000000000010010 1110001100000001 let j = j – 1; 1110001100000001 let j = j – 1; 0000000000010000 } 0000000000010000 } 1111110000010000 return result; 1111110000010000 return result; 0000000000010001 } 0000000000010001 } ... ... } } Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 7: Virutal Machine, Part I slide 3
Compilation models direct compilation: 2-tier compilation: . . . . . . language 1 language 2 language n language 1 language 2 language n intermediate language . . . . . . hardware hardware hardware hardware hardware hardware platform 1 platform 2 platform m platform 1 platform 2 platform m . requires n m translators requires n + m translators �%� ����������������� � &���������������� ������ ������� ���'�����������������(�������������������� � ������������������ ������ ������� ���'�����������������(��������������������# Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 7: Virutal Machine, Part I slide 4
The big picture ���������������������� language . . . . . . Jack Some Some Other language � ���������(����"��%��� language ����)������������������� � �����"����((�������'� Jack Some Some Other compiler ����������������������'� compiler compiler * ���� ��!�����������+ ������� �������� , ����� Intermediate code � -���"����������������� ����������(�����"������� VM VM imp. implementation VM imp. !������������������� over the Hack VM over CISC over RISC platform emulator platforms platforms � -���"����������������� ��!�������((������%�'�# Hack CISC RISC written in . . . machine machine machine a high-level language language language language . . . . . . Hack CISC RISC Any other digital platforms, each equipped computer machine machine computer with its own VM implementation Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 7: Virutal Machine, Part I slide 5
Our VM model is stack-oriented ���������������������������������� � <���������!��������!�������������� �����'��������� � �������������'����������"���!���������� � �����(����������'���������������������� static +�����%��%����������� � ���������"�����'�����������������(����%������������� Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 7: Virutal Machine, Part I slide 10
Data types �������������(������� ���������=> "���������'������������"���������� � �����������!�������� �=> "���).�������������� -32768 +�###�+� 32767 � � ��:�������!��������� � 0 ���� -1 +����������(��� true ���� false � � ������������������������ ������'��������� Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 7: Virutal Machine, Part I slide 11
Memory access operations ����� �������� �"�(���� ��(���� ��� �������� ���������� � ������������3�&���������������� � 1�������������%��(�� � ��!���������%����/���(�%��������������������������# Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 7: Virutal Machine, Part I slide 12
Evaluation of arithmetic expressions �������� ��������� // z=(2-x)-(y+5) ������������� // z=(2-x)-(y+5) push 2 push 2 x ��(������� static 0 + push x push x sub y ��(������� static 1 +����� sub push y push y z ��(������� static 2 � push 5 push 5 add add sub sub pop z pop z Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 7: Virutal Machine, Part I slide 13
Evaluation of Boolean expressions �������� ��������� // (x<7) or (y=8) ������������� // (x<7) or (y=8) push x push x x ��(������� static 0 +����� push 7 push 7 lt y ��(������� static 1 � lt push y push y push 8 push 8 eq eq or or ��������'� true ���� false �������������� 0 ���� -1 +� ��������!��'� Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 7: Virutal Machine, Part I slide 14
Arithmetic and Boolean commands in the VM language (wrap-up) Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 7: Virutal Machine, Part I slide 15
Recommend
More recommend