Thread: program that replaces three spaces with a tab

  1. #1
    Registered User
    Join Date
    Apr 2018
    Posts
    43

    program that replaces three spaces with a tab

    So for a course at University I had to write a program that replaces three spaces with a tab.
    Then I had to change the to extend that it looks like machine language. We have to use go to statements and labels.

    Warning the code is pretty ugly. I have added comments to make it clearer. I can understand if it's still unclear because goto statements are really ugly to read.

    Code:
    #include <stdio.h>
    
    int R0 = 0;      //constant 0 
    char R2 = '\t';      // constant tab
    char R3 = '\n';  // constant '\n'
    int R4 = 0;      //c           //char to store currently read character
    int R5 = 0;      //counter    //counter variable for read input characters
    int R6;           //i           //counter variable for for-loop      
    int R7;           //diff        //to calculate difference for if-statements
    char *R8;        //tmp_pointer //temporary pointer for array access
    char *R9;        //tmp0_pointer //temporary pointer for array access
    char buffer[15]; //buffer to store read input
    
    char RA =EOF; // EOF
    
    int main()
    {
        //read characters from standard input until '\n' is detected
        INPUT_LOOP: 
    R4= getchar();
    R7 = R4-R3;
    if(R7==R0){                     
    goto END_INPUT_LOOP;
    }
    
    R8 = buffer+R5; 
    *R8= R4;
            R5=R5+1;
            
              //break out of loop when buffer size is reached
          R7=R5 -14;
          if(R7 >R0){
          goto END_INPUT_LOOP;
          }
           
           //break out of programm if EOF is detected
           RA =EOF;
           RA= RA-R4;
          if(RA==0){
              goto END_INPUT_LOOP;
          }
          
         goto INPUT_LOOP;
         
         
    
    
    // is equivalent to i =0 in while loop
        END_INPUT_LOOP: R6=R0;
        
    OUTPUT_LOOP:
    // prints out read input until buffer size is 0
    R7= 14-R6;
    if(R7 == R0) {
            goto  END_OUTPUT_LOOP; 
        }
    
    
    R9 = buffer+R6; //buffer+i
           
         
          
          if(*(R9)==' ') goto A1; // if first input is space go to  label A1
          goto D3;                                                 
        A1: if(*(R9+1)==' ') goto A2; // if second input is space go to  label A2
            goto D3;
        A2: if(*(R9+2)==' ') goto set;  // if third input is space go to  label set
        
         goto D3;
         
         // 
            D3: 
            putchar(*R9);
            R6= R6+1; // this is the same as i++;      
         
         goto OUTPUT_LOOP;
         
         
         
         // this label is active when the three if conditions are met
              set:
             
              
              *R9= R2; // sets the first space to a tab
               
    
               putchar(*R9); // prints out the tab
               R6 = R6 +3;  // moves the index
               goto OUTPUT_LOOP;  
       
    
               
            // prints out newline and returns 0
        END_OUTPUT_LOOP:
      
        putchar(R3);
    
        return R0;  
    }


    Now this code does what i want it to. for example if the input is a a it replaces the three spaces with a tab.

    However for the exercise I am only allowed to compare if statements with 0.
    So I changed the code but the output is just weird.

    Code:
    #include <stdio.h>
    
    int R0 = 0;      //constant 0 
    char R2 = '\t';      // constant tab
    char R3 = '\n';  // constant '\n'
    int R4 = 0;      //c           //char to store currently read character
    int R5 = 0;      //counter    //counter variable for read input characters
    int R6;           //i           //counter variable for for-loop      
    int R7;           //diff        //to calculate difference for if-statements
    char *R8;        //tmp_pointer //temporary pointer for array access
    char *R9;        //tmp0_pointer //temporary pointer for array access
    char buffer[15]; //buffer to store read input
    
    char RA =EOF; // EOF
    
    int main()
    {
        //read characters from standard input until '\n' is detected
        INPUT_LOOP: 
    R4= getchar();
    R7 = R4-R3;
    if(R7==R0){                     
    goto END_INPUT_LOOP;
    }
    
    R8 = buffer+R5; 
    *R8= R4;
            R5=R5+1;
            
              //break out of loop when buffer size is reached
          R7=R5 -14;
          if(R7 >R0){
          goto END_INPUT_LOOP;
          }
           
           //break out of programm if EOF is detected
           RA =EOF;
           RA= RA-R4;
          if(RA==0){
              goto END_INPUT_LOOP;
          }
          
         goto INPUT_LOOP;
         
         
    
    
    // is equivalent to i =0 in while loop
        END_INPUT_LOOP: R6=R0;
        
    OUTPUT_LOOP:
    // prints out read input until buffer size is 0
    R7= 14-R6;
    if(R7 == R0) {
            goto  END_OUTPUT_LOOP; 
        }
    
    
    R9 = buffer+R6; //buffer+i
           
         char RX =' '; 
          
          *R9=*R9-RX;
          
          if(*(R9)==0) goto A1; // if first input is space go to  label A1
          goto D3;                                                 
        A1: if(*(R9+1)==0) goto A2; // if second input is space go to  label A2
            goto D3;
        A2: if(*(R9+2)==0) goto set;  // if third input is space go to  label set
        
         goto D3;
         
         // 
            D3: 
            putchar(*R9);
            R6= R6+1; // this is the same as i++;      
         
         goto OUTPUT_LOOP;
         
         
         // this label is active when the three if conditions are met
              set:
             
              
              *R9= R2; // sets the first space to a tab
               
    
               putchar(*R9); // prints out the tab
               R6 = R6 +3;  // moves the index
               goto OUTPUT_LOOP;  
       
    
               
            // prints out newline and returns 0
    
        END_OUTPUT_LOOP:
      
        putchar(R3);
    
        return R0;  
    }

    I have no idea why it's like this. I am a beginner at C.

    I know that it's an ugly code but I have to do it for the assignment. The variable names are supposed to resemble registers.

    Thank you in advance.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    It seems odd to combine none of the power of writing in assembler with none of the flexibility of writing in a high level language.

    What's the purpose of this exercise?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Apr 2018
    Posts
    43
    to change the code so that it can run in visual x toy

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    Well as a first step, better indentation would be a big bonus.
    Indentation style - Wikipedia

    Do you have a program which does what you want, as written in sane C as opposed to some bastardised version for a toy?

    Because if you have, it's generally a lot simpler to simply 'hand assemble' the code which is working, rather than to try and re-implement something from scratch.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 12-10-2013, 02:29 AM
  2. Replies: 1
    Last Post: 02-27-2012, 06:38 AM
  3. Replies: 7
    Last Post: 10-03-2009, 10:58 PM
  4. compares 2 arguments and replaces characters
    By help123 in forum C Programming
    Replies: 9
    Last Post: 04-14-2005, 09:22 AM
  5. Replies: 3
    Last Post: 01-26-2002, 08:39 PM

Tags for this Thread