No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

tab_instruc.c 2.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. #include "tab_instruc.h"
  2. int current_index = 1;
  3. struct operation_t tab_op[MAXTAILLE];
  4. void add_operation(enum opcode_t opcode, int arg1, int arg2, int arg3){
  5. if (current_index == MAXTAILLE){
  6. printf("Taillemax tableau operations atteinte\n");
  7. }
  8. else{
  9. struct operation_t new_op = {opcode, arg1, arg2, arg3};
  10. tab_op[current_index] = new_op;
  11. current_index++;
  12. }
  13. }
  14. void create_jump_to_main(int line){
  15. struct operation_t new_op = {JMP,line, 0, 0};
  16. tab_op[0] = new_op;
  17. }
  18. char * get_asm_line_from_op(struct operation_t op){
  19. char * buffer = malloc(sizeof(char)*200);
  20. switch (op.opcode){
  21. case (ADD):
  22. sprintf(buffer,"ADD %d %d %d\n",op.arg1, op.arg2, op.arg3);
  23. break;
  24. case (MUL):
  25. sprintf(buffer,"MUL %d %d %d\n",op.arg1, op.arg2, op.arg3);
  26. break;
  27. case (SOU):
  28. sprintf(buffer,"SOU %d %d %d\n",op.arg1, op.arg2, op.arg3);
  29. break;
  30. case (DIV):
  31. sprintf(buffer,"DIV %d %d %d\n",op.arg1, op.arg2, op.arg3);
  32. break;
  33. case (COP):
  34. sprintf(buffer,"COP %d %d\n",op.arg1, op.arg2);
  35. break;
  36. case (AFC):
  37. sprintf(buffer,"AFC %d %d\n",op.arg1, op.arg2);
  38. break;
  39. case (AFCA):
  40. sprintf(buffer,"AFCA %d %d\n",op.arg1, op.arg2);
  41. break;
  42. case (JMP):
  43. sprintf(buffer,"JMP %d\n",op.arg1);
  44. break;
  45. case (JMF):
  46. sprintf(buffer,"JMF %d %d\n",op.arg1, op.arg2);
  47. break;
  48. case (INF):
  49. sprintf(buffer,"INF %d %d %d\n",op.arg1, op.arg2, op.arg3);
  50. break;
  51. case (SUP):
  52. sprintf(buffer,"SUP %d %d %d\n",op.arg1, op.arg2, op.arg3);
  53. break;
  54. case (EQU):
  55. sprintf(buffer,"DIV %d %d %d\n",op.arg1, op.arg2, op.arg3);
  56. break;
  57. case (PRI):
  58. sprintf(buffer,"PRI %d\n",op.arg1);
  59. break;
  60. case (READ):
  61. sprintf(buffer,"READ %d %d\n",op.arg1, op.arg2);
  62. break;
  63. case (WR):
  64. sprintf(buffer,"WR %d %d\n",op.arg1, op.arg2);
  65. break;
  66. case (CALL):
  67. sprintf(buffer,"CALL %d %d\n",op.arg1, op.arg2);
  68. break;
  69. case (RET):
  70. sprintf(buffer,"RET\n");
  71. break;
  72. case (GET):
  73. sprintf(buffer,"GET %d\n",op.arg1);
  74. break;
  75. case (STOP):
  76. sprintf(buffer,"STOP\n");
  77. break;
  78. }
  79. return buffer;
  80. }
  81. void create_asm(){
  82. FILE * output = fopen("output.txt","w");
  83. for (int i = 0; i < current_index; i++){
  84. char * line = get_asm_line_from_op(tab_op[i]);
  85. fputs(line, output);
  86. free(line);
  87. }
  88. }
  89. int get_current_index(){return current_index;}
  90. void patch(int index, int arg){
  91. if (tab_op[index].opcode == JMP){
  92. tab_op[index].arg1 = arg;
  93. }
  94. else if (tab_op[index].opcode == JMF){
  95. tab_op[index].arg2 = arg;
  96. }
  97. }