1 / 82

Troisième Partie Chapitre 4 Un Processeur à Pile

Troisième Partie Chapitre 4 Un Processeur à Pile. A Generic Processor. T. B. . . . 5 4 3 2 1 0. . . . 5 4 3 2 1 0. I. P. code. data. Code memory is addressable, random access. During program execution, it is “read only”

zev
Download Presentation

Troisième Partie Chapitre 4 Un Processeur à Pile

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. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Troisième Partie Chapitre 4 Un Processeur à Pile

  2. A Generic Processor T B . . . 5 4 3 2 1 0 . . . 5 4 3 2 1 0 I P code data

  3. Code memory is addressable, random access. During program execution, it is “read only” The “Program counter” (P) contains the address in code memory of the next instruction to be fetched When an instruction is fetched it is kept in the Instruction register (I) to be executed by the control unit. The operation of this stack machine will be described by means of an executable C++ program that interprets the instructions just as the actual hardware would do. Stack processorCode Memory Usage

  4. Data memory is addressable, random access. Part of it can be managed as a LIFO stack Base register (B) Top of Stack register (T) Data memory is used to store: Variables of active blocks (= activation record) Temporary variables Return addresses of subroutines Static and dynamic data links Procedure parameters Stack processorData Memory Usage

  5. Data handling : CHS : Change sign of top of stack (tos) EXC : Exchange two topmost elements of stack ADD : Add two topmost elements of stack SUB : Subtract tos from underlying element MUL : Multiply two topmost elements of stack DIV : Divide underlying element by tos EQ? : Are two topmost elements equal ? NE? : Are two topmost elements different ? LT? : Is tos less than underlying element ? LE? : Is tos less than or equal to underlying element ? GT? : Is tos greater than underlying element ? GE? : Is tos greater than or equal to underlying element ? Stack processorInstructions

  6. Data transfer : LIT,a : load a constant on tos. LOD,l,a : copy value from specified address to tos STO,l,a : move value from tos to specified address. LDI,l,a : copy value from indirect address to tos STI,l,a : move value from tos to indirect address. Miscellaneous : INT,a : Increment stack pointer by constant value. LAD,l,a : Load specified address on tos. Stack processorInstructions

  7. Control : JMP,a : Jump to specified address in code JPT,a : Jump if the value TRUE is on tos. JPF,a : Jump if the value FALSE is on tos JSR,a : Call subroutine at specified address in code RET : Return from subroutine HLT : Stop execution Stack processorInstructions

  8. Interpreter : Data Types enum Opcode {EXC,CHS,ADD,SUB,MUL,DIV, EQ?,NE?,LT?,LE?,GT?,GE?, LIT,LOD,STO,LDI,STI,LIT,LAD, JMP,JPT,JPF,JSR,RET,HLT }; struct Instr {Opcode opc; int l,a; }

  9. Interpreter : Variables instr Code[1000]; int D[1000]; instr I; int P,B,T;

  10. Main Loop Running = true; do { I = Code[P]; P = P + 1; switch (I.opc) {case CHS : ... break; case EXC : ... break; ... case HLT : Running = false; } } while Running

  11. Data memory is addressable, random access. Part of it can be managed as a LIFO stack Base register (B) Top of Stack register (T) Data memory is used to store: Variables of active blocks (= activation record) Temporary variables Return addresses of subroutines Static and dynamic data links Procedure parameters Stack processorData Memory Usage

  12. Instruction Interpretation (1) // CHS : Change sign of top of stack (tos) case CHS : D[T] = - D[T]; break; // EXC : Exchange two topmost elements of stack case EXC : D[T+1] = D[T-1]; D[T-1] = D[T]; D[T] = D[T+1]; break; // ADD : Add two topmost elements of stack case ADD : T = T-1; D[T] = D[T] + D[T+1]; break;

  13. Instruction Interpretation (2) // EQ? : Are two topmost elements equal ? case EQ? : T = T-1; D[T] = D[T+1] == D[T];break; // NE? : Are two topmost elements different ? case NE? : T = T-1; D[T] = D[T+1] != D[T];break; // LT? : Is tos less than underlying element ? case LT? : T = T-1; D[T] = D[T+1] < D[T];break;

  14. Instruction Interpretation (3) // JMP,a : Jump to specified address in code case JMP : P = I.a; break; // JPT,a : Jump if the value TRUE is on tos. case JPT : T = T-1; if( D[T+1])P = I.a; break; // JPF,a : Jump if the value FALSE is on tos case JPF : T = T-1; if(!D[T+1])P = I.a; break;

  15. Evaluate B FALSE TRUE B L1 S2 S1 L2 Compilation Pattern : IF if (B){S1}; else {S2}; Evaluate B JPF L1 Code for S1 JMP L2 L1 :Code for S2 L2: ...

  16. Compilation Pattern : Loop while (B) do {S} ; L1 :Evaluate B JPF L2 Code for S JMP L1 L2 : . . . L1 Evaluate B FALSE B TRUE S L2

  17. L1 S Evaluate B TRUE B FALSE Compilation Pattern : Loop do {S} while (B); L1 :Code for S Evaluate B JPT L1 . . .

  18. Data memory is addressable, random access. Part of it can be managed as a LIFO stack Base register (B) Top of Stack register (T) Data memory is used to store: Variables of active blocks (= activation record) Temporary variables Return addresses of subroutines Static and dynamic data links Procedure parameters Stack processorData Memory Usage

  19. Data Transfer instructionsfor local variables T B . . . 5 4 3 2 1 0 . . . 5 4 3 2 1 0 0.a I P code data

  20. Instruction Interpretation (4)Restricted to local variables // LOD,0,a : copy value from specified address to tos case LOD : T = T+1; D[T] = D[B + I.a]; break; // STO,0,a : move value from tos to specified address. case STO : D[B+I.a] = D[T]; T = T-1; break;

  21. Instruction Interpretation (5) // LIT,a : load a constant on tos. case LIT : T = T + 1; D[T] = I.a; break; // INT,a : modify stack pointer ( a pos. or neg.) case INT : T = T + I.a; break;

  22. 3 b a a+3 Compilation Pattern : expression x = (a+3) * b // a,b,x local variables Reverse polish notation : a 3 + b *, LOD,0,a LIT,3 ADD LOD,0,b MUL STO,0,x 1 2 3 4 1 a+3 (a+3)*b 2 3 4

  23. Evaluate N and M a = N L2 FALSE a < M S a = a+1 L1 Compilation Pattern : for loop for (a = N;a <= M; a++) S;

  24. Compilation Pattern : for loop for (a = N;a <= M; a++) S; Evaluate N and M a = N Evaluate M Evaluate N L2STO 0,a INT 1 restore access to a EXC GT? M < N => skip loop JPT L1 go to next statement INT 2 save M for future use EXC code for S INT -1 LAD 0,a put value of a on tos LIT 1 put a 1 above it ADD JMP L2 restart the loop L2 FALSE a <= M S a = a+1 L1 N T M B a:

  25. Compilation Pattern : for loop for (a = N;a <= M; a++) S; Evaluate N and M a = N Evaluate M Evaluate N L2STO 0,a INT 1 restore access to a EXC GT? M < N => skip loop JPT L1 go to next statement INT 2 save M for future use EXC code for S INT -1 LAD 0,a put value of a on tos LIT 1 put a 1 above it ADD JMP L2 restart the loop L2 FALSE a <= M S a = a+1 L1 N T M B a: N

  26. Compilation Pattern : for loop for (a = N;a <= M; a++) S; Evaluate N and M a = N Evaluate M Evaluate N L2STO 0,a INT 1 restore access to a EXC GT? M < N => skip loop JPT L1 go to next statement INT 2 save M for future use EXC code for S INT -1 LAD 0,a put value of a on tos LIT 1 put a 1 above it ADD JMP L2 restart the loop L2 FALSE a <= M S a = a+1 L1 N T M B a: N

  27. Compilation Pattern : for loop for (a = N;a <= M; a++) S; Evaluate N and M a = N Evaluate M Evaluate N L2STO 0,a INT 1 restore access to a EXC GT? M < N => skip loop JPT L1 go to next statement INT 2 save M for future use EXC code for S INT -1 LAD 0,a put value of a on tos LIT 1 put a 1 above it ADD JMP L2 restart the loop L2 FALSE a <= M S a = a+1 L1 M T N B a: N

  28. Compilation Pattern : for loop for (a = N;a <= M; a++) S; Evaluate N and M a = N Evaluate M Evaluate N L2STO 0,a INT 1 restore access to a EXC GT? M < N => skip loop JPT L1 go to next statement INT 2 save M for future use EXC code for S INT -1 LAD 0,a put value of a on tos LIT 1 put a 1 above it ADD JMP L2 restart the loop L2 FALSE a <= M S a = a+1 L1 M T false B a: N

  29. Compilation Pattern : for loop for (a = N;a <= M; a++) S; Evaluate N and M a = N Evaluate M Evaluate N L2STO 0,a INT 1 restore access to a EXC GT? M < N => skip loop JPT L1 go to next statement INT 2 save M for future use EXC code for S INT -1 LAD 0,a put value of a on tos LIT 1 put a 1 above it ADD JMP L2 restart the loop L2 FALSE a <= M S a = a+1 L1 M T false B a: N

  30. Compilation Pattern : for loop for (a = N;a <= M; a++) S; Evaluate N and M a = N Evaluate M Evaluate N L2STO 0,a INT 1 restore access to a EXC GT? M < N => skip loop JPT L1 go to next statement INT 2 save M for future use EXC code for S INT -1 LAD 0,a put value of a on tos LIT 1 put a 1 above it ADD JMP L2 restart the loop L2 FALSE a <= M S a = a+1 L1 M T false B a: N

  31. Compilation Pattern : for loop for (a = N;a <= M; a++) S; Evaluate N and M a = N Evaluate M Evaluate N L2STO 0,a INT 1 restore access to a EXC GT? M < N => skip loop JPT L1 go to next statement INT 2 save M for future use EXC code for S INT -1 LAD 0,a put value of a on tos LIT 1 put a 1 above it ADD JMP L2 restart the loop L2 FALSE a <= M S a = a+1 L1 false T M B a: N

  32. Compilation Pattern : for loop for (a = N;a <= M; a++) S; Evaluate N and M a = N Evaluate M Evaluate N L2STO 0,a INT 1 restore access to a EXC GT? M < N => skip loop JPT L1 go to next statement INT 2 save M for future use EXC code for S INT -1 LAD 0,a put value of a on tos LIT 1 put a 1 above it ADD JMP L2 restart the loop L2 FALSE a <= M S a = a+1 L1 false T M B a: N

  33. Compilation Pattern : for loop for (a = N;a <= M; a++) S; Evaluate N and M a = N Evaluate M Evaluate N L2STO 0,a INT 1 restore access to a EXC GT? M < N => skip loop JPT L1 go to next statement INT 2 save M for future use EXC code for S INT -1 LAD 0,a put value of a on tos LIT 1 put a 1 above it ADD JMP L2 restart the loop L2 FALSE a <= M S a = a+1 L1 false T M B a: N

  34. Compilation Pattern : for loop for (a = N;a <= M; a++) S; Evaluate N and M a = N Evaluate M Evaluate N L2STO 0,a INT 1 restore access to a EXC GT? M < N => skip loop JPT L1 go to next statement INT 2 save M for future use EXC code for S INT -1 LAD 0,a put value of a on tos LIT 1 put a 1 above it ADD JMP L2 restart the loop L2 FALSE a <= M S a = a+1 L1 N T M B a: N

  35. Compilation Pattern : for loop for (a = N;a <= M; a++) S; Evaluate N and M a = N Evaluate M Evaluate N L2STO 0,a INT 1 restore access to a EXC GT? M < N => skip loop JPT L1 go to next statement INT 2 save M for future use EXC code for S INT -1 LAD 0,a put value of a on tos LIT 1 put a 1 above it ADD JMP L2 restart the loop L2 FALSE a <= M S a = a+1 L1 1 N T M B a: N

  36. Compilation Pattern : for loop for (a = N;a <= M; a++) S; Evaluate N and M a = N Evaluate M Evaluate N L2STO 0,a INT 1 restore access to a EXC GT? M < N => skip loop JPT L1 go to next statement INT 2 save M for future use EXC code for S INT -1 LAD 0,a put value of a on tos LIT 1 put a 1 above it ADD JMP L2 restart the loop L2 FALSE a <= M S a = a+1 L1 1 N+1 T M B a: N

  37. Compilation Pattern : for loop for (a = N;a <= M; a++) S; Evaluate N and M a = N Evaluate M Evaluate N L2STO 0,a INT 1 restore access to a EXC GT? M < N => skip loop JPT L1 go to next statement INT 2 save M for future use EXC code for S INT -1 LAD 0,a put value of a on tos LIT 1 put a 1 above it ADD JMP L2 restart the loop L2 FALSE a <= M S a = a+1 L1 N+1 T M B a: N+1

  38. Data memory is addressable, random access. Part of it can be managed as a LIFO stack Base register (B) Top of Stack register (T) Data memory is used to store: Variables of active blocks (= activation record) Temporary variables Return addresses of subroutines Static and dynamic data links Procedure parameters Stack processorData Memory Usage

  39. Data Memory Usage (1) MODULE BLUE PROCEDURE YELLOW; PROCEDURE ORANGE; BEGIN … END ORANGE; Data Memory : BEGIN … ORANGE ... END YELLOW; PROCEDURE PURPLE; PROCEDURE RED; BEGIN …END RED; PROCEDURE GREEN; BEGIN ... END GREEN; BLUE BEGIN … RED; GREEN;... END PURPLE; Activation Record BEGIN…;YELLOW; PURPLE;... END BLUE.

  40. Data Memory Usage (2) MODULE BLUE PROCEDURE YELLOW; PROCEDURE ORANGE; BEGIN … END ORANGE; Data Memory : BEGIN … ORANGE ... END YELLOW; PROCEDURE PURPLE; PROCEDURE RED; BEGIN …END RED; PROCEDURE GREEN; YELLOW BEGIN ... END GREEN; BLUE BEGIN … RED; GREEN;... END PURPLE; Activation Records BEGIN…;YELLOW; PURPLE;... END BLUE.

  41. Data Memory Usage (3) MODULE BLUE PROCEDURE YELLOW; PROCEDURE ORANGE; BEGIN … END ORANGE; Data Memory : BEGIN … ORANGE ... END YELLOW; PROCEDURE PURPLE; PROCEDURE RED; BEGIN …END RED; PROCEDURE GREEN; ORANGE YELLOW BEGIN ... END GREEN; BLUE BEGIN … RED; GREEN;... END PURPLE; Activation Records BEGIN…;YELLOW; PURPLE;... END BLUE.

  42. Data Memory Usage (4) MODULE BLUE PROCEDURE YELLOW; PROCEDURE ORANGE; BEGIN … END ORANGE; Data Memory : BEGIN … ORANGE ... END YELLOW; PROCEDURE PURPLE; PROCEDURE RED; BEGIN …END RED; PROCEDURE GREEN; YELLOW BEGIN ... END GREEN; BLUE BEGIN … RED; GREEN;... END PURPLE; Activation Records BEGIN…;YELLOW; PURPLE;... END BLUE.

  43. Data Memory Usage (5) MODULE BLUE PROCEDURE YELLOW; PROCEDURE ORANGE; BEGIN … END ORANGE; Data Memory : BEGIN … ORANGE ... END YELLOW; PROCEDURE PURPLE; PROCEDURE RED; BEGIN …END RED; PROCEDURE GREEN; BEGIN ... END GREEN; BLUE BEGIN … RED; GREEN;... END PURPLE; Activation Records BEGIN…;YELLOW; PURPLE;... END BLUE.

  44. Data Memory Usage (6) MODULE BLUE PROCEDURE YELLOW; PROCEDURE ORANGE; BEGIN … END ORANGE; Data Memory : BEGIN … ORANGE ... END YELLOW; PROCEDURE PURPLE; PROCEDURE RED; BEGIN …END RED; PROCEDURE GREEN; PURPLE BEGIN ... END GREEN; BLUE BEGIN … RED; GREEN;... END PURPLE; Activation Records BEGIN…;YELLOW; PURPLE;... END BLUE.

  45. Data Memory Usage (7) MODULE BLUE PROCEDURE YELLOW; PROCEDURE ORANGE; BEGIN … END ORANGE; Data Memory : BEGIN … ORANGE ... END YELLOW; PROCEDURE PURPLE; PROCEDURE RED; BEGIN …END RED; PROCEDURE GREEN; RED PURPLE BEGIN ... END GREEN; BLUE BEGIN … RED; GREEN;... END PURPLE; Activation Records BEGIN…;YELLOW; PURPLE;... END BLUE.

  46. Data Memory Usage (8) MODULE BLUE PROCEDURE YELLOW; PROCEDURE ORANGE; BEGIN … END ORANGE; Data Memory : BEGIN … ORANGE ... END YELLOW; PROCEDURE PURPLE; PROCEDURE RED; BEGIN …END RED; PROCEDURE GREEN; PURPLE BEGIN ... END GREEN; BLUE BEGIN … RED; GREEN;... END PURPLE; Activation Records BEGIN…;YELLOW; PURPLE;... END BLUE.

  47. Data Memory Usage (9) MODULE BLUE PROCEDURE YELLOW; PROCEDURE ORANGE; BEGIN … END ORANGE; Data Memory : BEGIN … ORANGE ... END YELLOW; PROCEDURE PURPLE; PROCEDURE RED; BEGIN …END RED; PROCEDURE GREEN; GREEN PURPLE BEGIN ... END GREEN; BLUE BEGIN … RED; GREEN;... END PURPLE; Activation Records BEGIN…;YELLOW; PURPLE;... END BLUE.

  48. Data Memory Usage (10) MODULE BLUE PROCEDURE YELLOW; PROCEDURE ORANGE; BEGIN … END ORANGE; Data Memory : BEGIN … ORANGE ... END YELLOW; PROCEDURE PURPLE; PROCEDURE RED; BEGIN …END RED; PROCEDURE GREEN; PURPLE BEGIN ... END GREEN; BLUE BEGIN … RED; GREEN;... END PURPLE; Activation Records BEGIN…;YELLOW; PURPLE;... END BLUE.

  49. Data Memory Usage (11) MODULE BLUE PROCEDURE YELLOW; PROCEDURE ORANGE; BEGIN … END ORANGE; Data Memory : BEGIN … ORANGE ... END YELLOW; PROCEDURE PURPLE; PROCEDURE RED; BEGIN …END RED; PROCEDURE GREEN; BEGIN ... END GREEN; BLUE BEGIN … RED; GREEN;... END PURPLE; Activation Record BEGIN…;YELLOW; PURPLE;... END BLUE.

  50. Data memory is addressable, random access. Part of it can be managed as a LIFO stack Base register (B) Top of Stack register (T) Data memory is used to store: Variables of active blocks (= activation record) Temporary variables Return addresses of subroutines Static and dynamic data links Procedure parameters Stack processorData Memory Usage

More Related