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.