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.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. #include "tab_instruc.h"
  2. int current_index = 0;
  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. char * get_asm_line_from_op(struct operation_t op){
  15. char * buffer = malloc(sizeof(char)*200);
  16. switch (op.opcode){
  17. case (ADD):
  18. sprintf(buffer,"ADD %d %d %d\n",op.arg1, op.arg2, op.arg3);
  19. break;
  20. case (MUL):
  21. sprintf(buffer,"MUL %d %d %d\n",op.arg1, op.arg2, op.arg3);
  22. break;
  23. case (SOU):
  24. sprintf(buffer,"SOU %d %d %d\n",op.arg1, op.arg2, op.arg3);
  25. break;
  26. case (DIV):
  27. sprintf(buffer,"DIV %d %d %d\n",op.arg1, op.arg2, op.arg3);
  28. break;
  29. case (COP):
  30. sprintf(buffer,"COP %d %d\n",op.arg1, op.arg2);
  31. break;
  32. case (AFC):
  33. sprintf(buffer,"AFC %d %d\n",op.arg1, op.arg2);
  34. break;
  35. case (AFCA):
  36. sprintf(buffer,"AFCA %d %d\n",op.arg1, op.arg2);
  37. break;
  38. case (JMP):
  39. sprintf(buffer,"JMP %d\n",op.arg1);
  40. break;
  41. case (JMF):
  42. sprintf(buffer,"JMF %d %d\n",op.arg1, op.arg2);
  43. break;
  44. case (INF):
  45. sprintf(buffer,"INF %d %d %d\n",op.arg1, op.arg2, op.arg3);
  46. break;
  47. case (SUP):
  48. sprintf(buffer,"SUP %d %d %d\n",op.arg1, op.arg2, op.arg3);
  49. break;
  50. case (EQU):
  51. sprintf(buffer,"DIV %d %d %d\n",op.arg1, op.arg2, op.arg3);
  52. break;
  53. case (PRI):
  54. sprintf(buffer,"PRI %d\n",op.arg1);
  55. break;
  56. case (READ):
  57. sprintf(buffer,"READ %d %d\n",op.arg1, op.arg2);
  58. break;
  59. case (WR):
  60. sprintf(buffer,"WR %d %d\n",op.arg1, op.arg2);
  61. break;
  62. }
  63. return buffer;
  64. }
  65. void create_asm(){
  66. FILE * output = fopen("output.txt","w");
  67. for (int i = 0; i < current_index; i++){
  68. char * line = get_asm_line_from_op(tab_op[i]);
  69. fputs(line, output);
  70. free(line);
  71. }
  72. }
  73. int get_current_index(){return current_index;}
  74. void patch(int index, int arg){
  75. if (tab_op[index].opcode == JMP){
  76. tab_op[index].arg1 = arg;
  77. }
  78. else if (tab_op[index].opcode == JMF){
  79. tab_op[index].arg2 = arg;
  80. }
  81. }