From 57d686415464e11ca0d7641416dedf56e3e25bc8 Mon Sep 17 00:00:00 2001 From: Constantin Anderssen <constantin.anderssen@etu.unistra.fr> Date: Fri, 10 Jan 2025 22:56:59 +0100 Subject: [PATCH] float print with 2 decimals --- Codes_en_COREC/tests/test3_max.corec | 6 +-- lib.c | 73 +++++++++++++++++++++++++--- lib.h | 10 +++- 3 files changed, 77 insertions(+), 12 deletions(-) diff --git a/Codes_en_COREC/tests/test3_max.corec b/Codes_en_COREC/tests/test3_max.corec index 924f715..1c980d5 100644 --- a/Codes_en_COREC/tests/test3_max.corec +++ b/Codes_en_COREC/tests/test3_max.corec @@ -4,11 +4,11 @@ prog Test3 { def Main{ Loc : (array1,50) Rec : { - array1[0] = 5.32; + array1[0] = 0; array1[1] = 12; array1[2] = 7.89; array1[3] = 3.14; - array1[4] = 42; + array1[4] = 42.01; array1[5] = 18.27; array1[6] = 1.11; array1[7] = 6; @@ -55,7 +55,7 @@ prog Test3 { array1[48] = 6.25; array1[49] = 9.9; - printstr("Expected result : \n5.32 12.00 7.89 3.14 42.00 18.27 1.11 6.00 21.95 14.01 9.00 2.71 8.56 17.00 4.44 10.11 3.33 22.00 19.50 7.77 \n13.37 5.00 25.25 8.00 20.20 11.11 15.00 30.30 16.66 4.00 18.00 7.07 12.34 9.87 14.00 6.66 5.55 19.00 2.22 20.00 \n3.00 18.88 4.04 22.22 8.80 16.00 11.23 7.50 6.25 9.90"); + printstr("Expected result : \n0.00 12.00 7.89 3.14 42.01 18.27 1.11 6.00 21.95 14.01 9.00 2.71 8.56 17.00 4.44 10.10 3.33 22.00 19.50 7.77 \n13.37 5.00 25.25 8.00 20.20 11.11 15.00 30.30 16.66 4.00 18.00 7.07 12.34 9.87 14.00 6.66 5.55 19.00 2.22 20.00 \n3.00 18.87 4.04 22.22 8.80 16.00 11.23 7.50 6.25 9.89"); printstr("\nObtained Result : \n"); print(array1) // contain all the values exchanges here if the code is correct diff --git a/lib.c b/lib.c index cad7ea9..21676bb 100644 --- a/lib.c +++ b/lib.c @@ -506,6 +506,8 @@ void code_mips_dump(struct symtable * t, struct code * c, FILE* fout){ fprintf(fout,".data\n"); fprintf(fout,"\tlf: .asciiz \"\\n\"\n"); fprintf(fout,"\tspace: .asciiz \" \"\n"); + fprintf(fout,"\tdot: .asciiz \".\"\n"); + fprintf(fout,"\tzero: .asciiz \"0\"\n"); fprintf(fout,"\ttemp_float: .float 0.0\n"); if(t==NULL){fprintf(stderr,"Code mips dump: symtable null\n"); exit(1);} @@ -974,7 +976,7 @@ void print_print(struct symtable * t,struct code * c, FILE* fout,int i) // initilize t4 to be used as the counter of elements printed on one line (at 20 we print a newline) fprintf(fout,"%sli $t4, 0\n",tabulation); - + /* ----------------------------------------------------------------- */ // we avoid having 2 identical labels by putting the quad index at the end of it // /!\ start of the loop /!\ -- fprintf(fout,"%sprint_array_loop%d:\n",tabulation,i); @@ -986,8 +988,45 @@ void print_print(struct symtable * t,struct code * c, FILE* fout,int i) fprintf(fout,"%sadd $t3, $t2, $t1\n",tabulation); // load the current float element in the fprintf(fout,"%slwc1 $f0, ($t3)\n",tabulation); - // function that print the asked register - print_reg("f0",fout,FLOAT); + + fprintf(fout,"%sli $t9, 100\n",tabulation); + fprintf(fout,"%smtc1 $t9, $f2\n",tabulation); + fprintf(fout,"%scvt.s.w $f2, $f2\n",tabulation); + + fprintf(fout,"%smul.s $f0, $f0, $f2\n",tabulation); + fprintf(fout,"%scvt.w.s $f0, $f0\n",tabulation); + fprintf(fout,"%smfc1 $t5, $f0\n",tabulation); + + fprintf(fout,"%sli $t6, 100\n",tabulation); + fprintf(fout,"%sdiv $t5, $t6\n",tabulation); + fprintf(fout,"%smflo $t7\n",tabulation); + + // extracting the decimal part of the number into t8 + fprintf(fout,"%smfhi $t8\n",tabulation); + + // print the integer part of the number + print_reg("t7", fout, INTEGER); + // printing the dot an going on to the decimal part + fprintf(fout,"%sDOT\n",tabulation); + // print with padding if the decimal part is less than 10 + fprintf(fout,"%sli $t9, 10\n",tabulation); + fprintf(fout,"%sblt $t8, $t9, print_array_loop%d_frac\n",tabulation,i); + // print the fractional part without padding + print_reg("t8", fout, INTEGER); + // jump to the contine function to print space and loop + fprintf(fout,"%sj print_array_loop%d_continue\n",tabulation,i); + // /!\ end of the loop /!\ -- + remove_indent(); + /* ----------------------------------------------------------------- */ + // prints the decimal part with a trailing zero + fprintf(fout,"%sprint_array_loop%d_frac:\n",tabulation,i); + add_indent(c,t,fout); + fprintf(fout,"%sZERO\n",tabulation); + print_reg("t8", fout, INTEGER); + remove_indent(); + /* ----------------------------------------------------------------- */ + fprintf(fout,"%sprint_array_loop%d_continue:\n",tabulation,i); + add_indent(c,t,fout); // macro that print a space fprintf(fout,"%sSPACE\n",tabulation); // increment to next address @@ -996,15 +1035,15 @@ void print_print(struct symtable * t,struct code * c, FILE* fout,int i) // increment the counter of elements printed on this line fprintf(fout,"%saddi $t4, $t4, 1\n",tabulation); fprintf(fout,"%sli $t5, 20\n",tabulation); - fprintf(fout,"%sbeq $t4, $t5, print_array_loop_aux%d\n",tabulation,i); + // compare the counter of elements printed on this line with your wished amount (20) + fprintf(fout,"%sbeq $t4, $t5, print_array_loop%d_lf\n",tabulation,i); // jump to the start of the loop fprintf(fout,"%sj print_array_loop%d\n",tabulation,i); - // /!\ end of the loop /!\ -- remove_indent(); - + /* ----------------------------------------------------------------- */ // auxiliary function that enables us to have a new line every 20 floats printed - fprintf(fout,"%sprint_array_loop_aux%d:\n",tabulation,i); + fprintf(fout,"%sprint_array_loop%d_lf:\n",tabulation,i); add_indent(c,t,fout); fprintf(fout,"%sLF\n",tabulation); // we reset the counter of elements printed on one line @@ -1012,7 +1051,7 @@ void print_print(struct symtable * t,struct code * c, FILE* fout,int i) // jump back to the print loop fprintf(fout,"%sj print_array_loop%d\n",tabulation,i); remove_indent(); - + /* ----------------------------------------------------------------- */ fprintf(fout,"%send_array_loop%d:\n",tabulation,i); fprintf(fout,"%sLF\n",tabulation); break; @@ -1298,6 +1337,8 @@ void print_macros(FILE* fout){ fprintf(fout,"\n# MACROS definition\n\n"); print_LF_macro(fout); print_SPACE_macro(fout); + print_ZERO_macro(fout); + print_DOT_macro(fout); fprintf(fout,"# End of MACROS definition\n"); } @@ -1317,6 +1358,22 @@ void print_SPACE_macro(FILE* fout){ fprintf(fout,".end_macro\n"); } +void print_ZERO_macro(FILE* fout){ + fprintf(fout,".macro ZERO\n"); + fprintf(fout,"\tli $v0, 4\n"); + fprintf(fout,"\tla $a0, zero\n"); + fprintf(fout,"\tsyscall\n"); + fprintf(fout,".end_macro\n"); +} + +void print_DOT_macro(FILE* fout){ + fprintf(fout,".macro DOT\n"); + fprintf(fout,"\tli $v0, 4\n"); + fprintf(fout,"\tla $a0, dot\n"); + fprintf(fout,"\tsyscall\n"); + fprintf(fout,".end_macro\n"); +} + // ETC FEEL FREE TO ADD MORE SECTIONS // -------- DEBUG RELATED -------- diff --git a/lib.h b/lib.h index fb93bd8..4a8d89f 100644 --- a/lib.h +++ b/lib.h @@ -284,10 +284,18 @@ void print_macros(FILE* fout); */ void print_LF_macro(FILE* fout); -/** @brief print_SPACE_macro add a macro to print a SPCE in MIPS code +/** @brief print_SPACE_macro add a macro to print a SPACE in MIPS code */ void print_SPACE_macro(FILE* fout); +/** @brief print_ZERO_macro add a macro to print a ZERO in MIPS code + */ +void print_ZERO_macro(FILE* fout); + +/** @brief print_DOT_macro add a macro to print a DOT in MIPS code + */ +void print_DOT_macro(FILE* fout); + // -------- DEBUG RELATED -------- #ifdef LIBDEBUG -- GitLab