Question & Answer: I have the follwing C code. I need to combine the put() function with the processline() function and put it…..

I have the follwing C code. I need to combine the put() function with the processline() function and put it in the main function. You don’t need to worry about what the program does.

processline calls put. Then main function calls processline

So integrate put() into processline(), then intergrate processline() into the main function.

C code:

#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>

unsigned char * mem;
int reg[9];
int pc;
int memsize;

union converter{
unsigned int integer;
unsigned char bytes[4];

};

void put(char* directive, char* address, char* data){

if (strcmp(directive,”.size”)==0){
int size = (int)strtol (address, NULL, 16);

//NEED TO FIND OUT EXACT MEANING OF size DIRECTIVE, num of chars or bytes

mem = (unsigned char *) malloc(sizeof(char)*size);

memsize = size*sizeof(char);

}

else if(strcmp(directive,”.text”)==0){
int add = (int) strtol (address,NULL,16);
pc = add;

char byte[3];

while(*data!=’’){
byte[0] = *data;
data++;
byte[1] = *data;
data++;
byte[2] = ‘’;

//unsigned int bytedata = (unsigned int) strtol (byte,NULL,16);
//printf(“%x”,(char)bytedata);
mem[add] = (char) strtol(byte,NULL,16);
add++;
}

}

else if(strcmp(directive,”.string”)==0){
int add = (int) strtol (address,NULL,16);
data ++;
mem[add] = *data;
data++;
//printf(“%s”,data);
int i =1;
while(*data!=’’){

mem[add+i] = *data;
i++;
data++;
}
}

else if(strcmp(directive,”.long”)==0){
int add = (int) strtol (address,NULL,16);

//printf(“Data: %sn”,data);

unsigned int value = (int) strtol (data,NULL,16);
union converter con;

con.integer = value;

//printf(“Value: %dn”,value);

//printf(“n%x%x%x%x”,con.bytes[0],con.bytes[1],con.bytes[2],con.bytes[3]);

mem[add] = con.bytes[0];
mem[add+1] = con.bytes[1];
mem[add+2] = con.bytes[2];
mem[add+3] = con.bytes[3];

}

else if(strcmp(directive,”.byte”)==0){
int add = (int) strtol (address,NULL,16);

int bytedata = (int) strtol (data,NULL,16);

mem[add] = (char) bytedata;

}

}

void processLine(char * line){

char* directive;
char* address;
char* data;

directive = strtok(line,”    “);
//printf(“Directive: %sn”,directive);

address = strtok(NULL,”   “);
size_t lena =strlen(address)-1;
if(address[lena]==’n’){
address[lena] = ‘’;
}
//printf(“Address: %sn”,address);

data = strtok(NULL,”   “);
if(data!=NULL){
size_t lend = strlen(data)-1;
if(data[lend]==’n’){
data[lend]=’’;
}
}
//printf(“Data: %sn”,data);

put(directive,address,data);

}

int main(int argc, char ** argv){

if(argc!=2){

printf(“Argument must be specified.n”);
exit(0);

}

if (strcmp(argv[1],”-h”)==0){

printf(“Usage: y86emu file.y86.n”);
exit(0);

}

FILE *file;

file = fopen(argv[1], “r”);

if (file == NULL){
printf(“There is no file, or it could not be opened.n”);
exit(0);
}

char * line = NULL;
size_t len = 0;
ssize_t read;

while((read = getline(&line, &len, file)) != -1){

processLine(line);

}

Expert Answer

 

———————————————————
Sample output:
———————————————————-

[azizitest@localhost venky]$ vi input.txt
[azizitest@localhost venky]$ cc sample2.c
[azizitest@localhost venky]$ ./a.out input.txt
Directive: hi
Address: hello
Data: (null)
Directive: welcome
Address: to
Data: the
Directive: software
Address: solution
Data: (null)
Directive: from
Address: chennai
Data: (null)
Directive: Please
Address: give
Data: report
[azizitest@localhost venky]$

——————————————————————–
input.txt:
——————————————————————-
hi hello
welcome to the new
software solution
from chennai
Please give report
~
~
~———————————————————————-
Modified Source-code:
————————————————————————-
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
unsigned char * mem;
int reg[9];
int pc;
int memsize;
union converter
{
unsigned int integer;
unsigned char bytes[4];
};

void put(char* directive, char* address, char* data)
{
if (strcmp(directive,”.size”)==0)
{
int size = (int)strtol (address, NULL, 16);
//NEED TO FIND OUT EXACT MEANING OF size DIRECTIVE, num of
chars or bytes
mem = (unsigned char *) malloc(sizeof(char)*size);
memsize = size*sizeof(char);
}
else if(strcmp(directive,”.text”)==0)
{
int add = (int) strtol (address,NULL,16);
pc = add;
char byte[3];
while(*data!=’’)
{
byte[0] = *data;
data++;
byte[1] = *data;
data++;
byte[2] = ‘’;
unsigned int bytedata = (unsigned int) strtol
(byte,NULL,16);

printf(“%x”,(char)bytedata);

mem[add] = (char) strtol(byte,NULL,16);
add++;

}

}
else if(strcmp(directive,”.string”)==0)
{
int add = (int) strtol (address,NULL,16);
data ++;
mem[add] = *data;
data++;
printf(“%s”,data);
int i =1;
while(*data!=’’)
{
mem[add+i] = *data;
i++;
data++;
}
}

else if(strcmp(directive,”.long”)==0)
{
int add = (int) strtol (address,NULL,16);
printf(“Data: %sn”,data);
unsigned int value = (int) strtol (data,NULL,16);
union converter con;
con.integer = value;
printf(“Value: %dn”,value);
printf(“n%x%x%x%x”,con.bytes[0],con.bytes[1],con.bytes[2],con.bytes[3]);
mem[add] = con.bytes[0];
mem[add+1] = con.bytes[1];
mem[add+2] = con.bytes[2];
mem[add+3] = con.bytes[3];
}
else if(strcmp(directive,”.byte”)==0)
{
int add = (int) strtol (address,NULL,16);
int bytedata = (int) strtol (data,NULL,16);
mem[add] = (char) bytedata;
}
}

void processLine(char * line)
{
char* directive;
char* address;
char* data;
directive = strtok(line,” “);
printf(“Directive: %sn”,directive);
address = strtok(NULL,” “);
size_t lena =strlen(address)-1;
if(address[lena]==’n’)
{
address[lena] = ‘’;
}
printf(“Address: %sn”,address);
data = strtok(NULL,” “);
if(data!=NULL)
{
size_t lend = strlen(data)-1;
if(data[lend]==’n’)
{
data[lend]=’’;
}
}
printf(“Data: %sn”,data);
put(directive,address,data);
}

int main(int argc, char ** argv){

if(argc!=2)
{
printf(“Argument must be specified.n”);
exit(0);
}
if (strcmp(argv[1],”-h”)==0)
{
printf(“Usage: y86emu file.y86.n”);
exit(0);
}
FILE *file;
file = fopen(argv[1], “r”);
if (file == NULL)
{
printf(“There is no file, or it could not be opened.n”);
exit(0);
}
char * line = NULL;
size_t len = 0;
ssize_t read;
while((read = getline(&line, &len, file)) != -1)
{
processLine(line);
}
return 0;
}

Still stressed from student homework?
Get quality assistance from academic writers!