Thread: help plz

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    6

    Question help plz

    hi all, i have this code that takes in some instructions like
    READ a // reads value of a from stdinput
    READ b //read value of b from stdinput
    LOAD a r2 //load value of variable into register (r2)
    LOADC #6 r3 // load constant(6) into register r3
    STORE c r1 // stores value of register (which is 1) into variable
    ADD r1 r2 r3 .. .. //adds contents of r2 and r3 and store it into r3.

    The program is supposed to interpret each instruction as i have noted above.

    all the variables will go in the mem[5] array and registers in reg[1000] array.
    this is what i have done so far, but i think i am no mapping the variables into the array right.

    Code:
    #include <stdio.h>
    #include "CodeGen.h"
    #include "InstrUtils.h"
    
    int main(int argc, char *argv[]) {
    
      FILE *infile;
      
      Instruction *instr; 
      
      int instrCounter = 0;
      
      int reg[1000];  // array of registers
      
     char a, b, c , d, e;
    	   
      int mem[5]; //array for variables
      
     
      int x, y, z;
      
    Instruction *current;      
      
      instr = ReadInstructionList(infile); //file contains instructions
      current = instr;
      
      while(current!=NULL){
      
             if(current->field1 == a)  // not sure??
                 mem[0] = current->field1;
             else if 
                (current->field1 == b)
                mem[1] = current->field1;
             else if 
                (current->field1== c)
                mem[2] = current->field1;
             else if 
                (current->field1== d)
                mem[3] = current->field1;
              else  
               mem[4] = current->field1;
    
      
             switch (current->opcode) {
          
             case READ:
           {
                  instrCounter++;
               printf("tinyL>> enter value for \"%c\": ", current->field1);
                scanf("%d",mem);  //did i pass right address here??
                break;
        }
            case WRITE:
          {
                instrCounter++;
                 printf("tinyL>> %c = %d\n", current->field1, mem);
                break;
          }
             default:
               printf("ILLEGAL INSTRUCTION\n");
          
           case LOAD:
           {     
              x = current->field1;
              y = current->field2;
              reg[y] =x;
              instrCounter++;
               break;
          }
             case LOADC:
            { 
            x = current->field1;
            y = current->field2;
            reg[y] = x;
             instrCounter++;
            break;
        }
            case STORE:
            x=current->field1;
            y=current->field2;
            if(x == a)
                mem[0] = y;
           else if 
                (x == b)
                mem[1] = y;
           else if 
                 (x== c)
                mem[2] = y;
            else if 
                 (x== d)
                mem[3] = y;
            else  
               mem[4] = y;
               instrCounter++;
               break;
        
            case ADD:
             x=current->field1;
             y=current->field2;
             z=current->field3;
             reg[x] = reg[y]+reg[z];  
        
             instrCounter++;
             break;
                
            }
     current = current->next;
    
      }
        
        printf("Executed %d instructions\n", instrCounter);
        return 0;
      
      }
    i would really appreciate if someone can give me some hints about this,
    thanks a lot.
    Last edited by kfatima; 10-13-2002 at 03:32 PM.

  2. #2
    Registered User
    Join Date
    Oct 2002
    Posts
    12

    Lightbulb Before you paste your code here.

    Read this: http://cboard.cprogramming.com/showt...threadid=25765

    you should use, code tags.
    Like this, we can see your code better, and help you better

  3. #3
    Registered User
    Join Date
    Oct 2002
    Posts
    6
    oops, sorry about that, now it's a nice formatted code

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Maybe you think it's nice, still looks messy to me

    Here's what I would consider better formatting, and some guesses at bug fixes.
    Without your two custom .h files, it's hard to guess what all those structures mean.

    Code:
    #include <stdio.h>
    #include "CodeGen.h"
    #include "InstrUtils.h"
    
    int main(int argc, char *argv[]) {
        FILE *infile;
        Instruction *instr; 
        int instrCounter = 0;
        int reg[1000];  // array of registers
        int mem[5];     // array for variables
        int x, y, z;
        Instruction *current;      
    
        instr = ReadInstructionList( infile ); //file contains instructions
        current = instr;
    
        while ( current != NULL) {
            switch(current->opcode) {
            case READ:
                x = current->field1;
                printf( "tinyL>> enter value for \"%c\": ", x );
                scanf( "%d", &mem[x-'a'] );
                instrCounter++;
                break;
            case WRITE:
                x = current->field1;
                printf( "tinyL>> %c = %d\n", x, mem[x-'a'] );
                instrCounter++;
                break;
            case LOAD:
                x = current->field1;
                y = current->field2;
                reg[y] = x;
                instrCounter++;
                break;
            case LOADC:
                x = current->field1;
                y = current->field2;
                reg[y] = x;
                instrCounter++;
                break;
            case STORE:
                x=current->field1;
                y=current->field2;
                mem[x-'a'] = y;         //!! replaces all your if/else
                instrCounter++;
                break;
            case ADD:
                x=current->field1;
                y=current->field2;
                z=current->field3;
                reg[x] = reg[y] + reg[z];
                instrCounter++;
                break;
            default:
                printf( "ILLEGAL INSTRUCTION\n" );
                break;
            }
            current = current->next;
        }
    
        printf( "Executed %d instructions\n", instrCounter );
        return 0;
    }
    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. can any1 plz make this assignment
    By jean in forum C Programming
    Replies: 17
    Last Post: 05-13-2009, 09:19 PM
  2. plz help me...
    By sweetchakri in forum C Programming
    Replies: 1
    Last Post: 03-03-2009, 11:50 PM
  3. [Request] Need Help Plz
    By TylerD in forum Tech Board
    Replies: 4
    Last Post: 01-03-2009, 09:54 AM
  4. Anyone plz help me
    By Rose_Flowers in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 09-17-2003, 12:01 PM
  5. help plz plz
    By nsssn73 in forum C++ Programming
    Replies: 2
    Last Post: 06-03-2002, 08:44 AM