Basic C++ question...in need of immediate help

Discussion in 'OT Technology' started by mrburner, Oct 28, 2003.

  1. mrburner

    mrburner Ron Paul 2008

    Joined:
    May 9, 2001
    Messages:
    7,448
    Likes Received:
    0
    Location:
    Phoenix
    if i have an integer...say 26 stored as j and another integer...say 35 stored as i...

    how do i add them together to make 2635? if i just try to add them, it will give me 61

    that was just an easy example

    i'm actually converting from a hexidecimal input to binary...but the hexidecimal input has 8 characters...so i am having it look at each character seperately and save it as its specific 4 digit binary...but now i need to put all of the binary numbers into 1 long 32 bit string so i can pull out the first 6 binary numbers and conver that into decimal...then pull out the next 4 and convert that to decimal...then pull out the next 4, and convert that...then the next 1 and convert that and then the next 17 and convert that

    so is there any easy way to do this?

    thanks
     
  2. skinjob

    skinjob Active Member

    Joined:
    Jan 6, 2001
    Messages:
    2,337
    Likes Received:
    0
    Location:
    Aztlán
    What are you using to read the input? If you are using istream, you can use setbase(16) from <iomanip> to read hex straight from the stream.

    e.g. cin >> setbase(16) >> int_foo;

    You could even use scanf to read hex, or use sscanf to convert a string representing a hex number to an int.
     
  3. mrburner

    mrburner Ron Paul 2008

    Joined:
    May 9, 2001
    Messages:
    7,448
    Likes Received:
    0
    Location:
    Phoenix
    i have it read an input file that will look basically like this

    AF37209E
    9E51186B
    37C496A2
    ...

    and it reads each character and converts it into a binary 4 digit number...there are 8 total input lines to make a total 32 bit binary number

    now i am using strcat(dest, source) to add the strings together as i wanted

    now i need to pull out the first 6 binary digits from this long 32 bit string and convert those into decimal...then as i said above i have to pull out the next 4,the next 4, the next 1, then the next 17

    does anyone know exactly how strcat works? i added <string.h> so can i use it like this....

    while(input_file)
    {
    int hex = opcode;
    if(hex = 'a' || hex = 'A')
    {
    binary = 1010;
    }
    elseif(hex = 'b' || hex = 'B')
    {
    binary = 1011;
    }
    elseif(hex = 'c' || hex = 'C')
    {
    binary = 1100;
    }
    elseif(hex = 'd' || hex = 'D')
    {
    binary = 1101;
    }
    elseif(hex = 'e' || hex = 'E')
    {
    binary = 1110;
    }
    elseif(hex = 'f' || hex = 'F')
    {
    binary = 1111;
    }
    elseif(hex = '0')
    {
    binary = 0000;
    }
    elseif(hex = '1')
    {
    binary = 0001;
    }
    elseif(hex = '2')
    {
    binary = 0010;
    }
    elseif(hex = '3')
    {
    binary = 0011;
    }
    elseif(hex = '4')
    {
    binary = 0100;
    }
    elseif(hex = '5')
    {
    binary = 0101;
    }
    elseif(hex = '6')
    {
    binary = 0110;
    }
    elseif(hex = '7')
    {
    binary = 0111;
    }
    elseif(hex = '8')
    {
    binary = 1000;
    }
    elseif(hex = '9')
    {
    binary = 1001;
    }
    strcat(fullstring, binary);
    i++;
    }
     
  4. mrburner

    mrburner Ron Paul 2008

    Joined:
    May 9, 2001
    Messages:
    7,448
    Likes Received:
    0
    Location:
    Phoenix
    oh...and how do i convert binary to decimal?
     
  5. skinjob

    skinjob Active Member

    Joined:
    Jan 6, 2001
    Messages:
    2,337
    Likes Received:
    0
    Location:
    Aztlán
    what type is your 'binary' variable?
    binary = 1010; <-- this statement stores decimal 1010 (one-thousand-ten) into the variable binary. C/C++ allows you to express decimal, octal and hex literals, but not binary.

    There is no "conversion" from binary to decimal. Everything in memory is physically stored in binary bits.

    these are equivalent for int binary;
    binary = 10; // decimal 10 == 1010 binary
    binary = 0x0A; // hex 0A == 1010 binary
    binary = 012; // octal 012 == 1010 binary

    I don't see why you're reading those numbers character by character. If you use an ifstream to read the file, then just do this:

    myfile >> setbase(16) >> my_int;

    you'll store that hex value into the integer my_int.
    Then it's just a matter of bit operators to shift and mask off the bits you're interested in.
     
  6. mrburner

    mrburner Ron Paul 2008

    Joined:
    May 9, 2001
    Messages:
    7,448
    Likes Received:
    0
    Location:
    Phoenix
    lemme give you a little background...this is my first year with c++...i really don't know much at all about it...i declared my major late so while i am taking cs152 (programming 2...also the first c++ class) i am taking cs251 (principles of computer organization) and cs251 requires cs152 as a prereq...but my advisor told me to take it anyways

    now with that out of the way...this is an assignment for my cs251 class...so obviously i'm having some difficulty with it...here is a link to the assignment: http://www.wooster.edu/math/sgray/CS251/assignments/InstructionDecodeProgram.pdf

    link to the sample input file: http://www.wooster.edu/math/sgray/CS251/assignments/prog1Input.txt

    link to the resulting output file: http://www.wooster.edu/math/sgray/CS251/assignments/prog1output.txt

    but anyways...i have been working on this for awhile...so far i have it open an input file and output file...then read each character of the input file (hexidecimal) seperately and convert each character to binary...then it saves this in a string

    so i can save a full line of binary into a string and then extract the numbers i need for the opcode and the source and the dest and etc. but i don't know how to convert the binary to decimal so i can print the actual values...i also don't think the program is looping properly from line to line...like i am pretty sure it will go from one character to the next, but when it gets the the end of the line from the input file, how do i go to the next line?

    last thing i'm having problems with is the 2's complement...if i have a string of binary digits, how do i convert it to 2's complement? as far as my understanding goes, to convert to 2's complement you invert the binary numbers (like all 1's turn into 0's and all 0's turn into 1's) and then you add 1 to the end...but for the examples i have been looking up, people have been seeming to get negative numbers with 2's complement if the regular number was positive

    i'd really appreciate a response to any of this as the program is due tomorrow (i know i won't have it done by then) but i just really want to have it done by friday at the latest

    here is my code...thanks in advance

    #include <iostream.h>
    #include <fstream.h>
    #include <conio.h>
    #include <string.h>

    int main()
    {
    char data_string[32];
    //char file_name[];
    //char file_name2[];

    ifstream input_file;
    ofstream output_file;
    int read_ok;
    int counter = 0;
    int total_counter = 0;

    cout << "Please enter the name and path of the input file\n";
    cin >> file_name;
    input_file.open(file_name);

    cout << "Please enter the name and path of the output file\n";
    cin >> file_name2;
    output_file.open(file_name2);

    if(!input_file.is_open() || !output_file.is_open())
    {
    cout << "Error opening one of your files";
    }
    else
    {
    while(!input_file.eof())
    {
    int i = 0;
    char binary[4];
    char fullstring[32];
    while(i <= 7)
    {
    int hex = input_file;
    if(hex = 'a' || hex = 'A')
    {
    binary = "1010";
    }
    elseif(hex = 'b' || hex = 'B')
    {
    binary = "1011";
    }
    elseif(hex = 'c' || hex = 'C')
    {
    binary = "1100";
    }
    elseif(hex = 'd' || hex = 'D')
    {
    binary = "1101";
    }
    elseif(hex = 'e' || hex = 'E')
    {
    binary = "1110";
    }
    elseif(hex = 'f' || hex = 'F')
    {
    binary = "1111";
    }
    elseif(hex = '0')
    {
    binary = "0000";
    }
    elseif(hex = '1')
    {
    binary = "0001";
    }
    elseif(hex = '2')
    {
    binary = "0010";
    }
    elseif(hex = '3')
    {
    binary = "0011";
    }
    elseif(hex = '4')
    {
    binary = "0100";
    }
    elseif(hex = '5')
    {
    binary = "0101";
    }
    elseif(hex = '6')
    {
    binary = "0110";
    }
    elseif(hex = '7')
    {
    binary = "0111";
    }
    elseif(hex = '8')
    {
    binary = "1000";
    }
    elseif(hex = '9')
    {
    binary = "1001";
    }
    strcat(fullstring,binary);
    i++;
    }
    int j = 0;
    char opcode[6];
    while(j <= 5)
    {
    strcat(opcode,fullstring[j]);
    j++;
    }
    int j = 6;
    char dest[4];
    while(j <= 9)
    {
    strcat(dest,fullstring[j]);
    j++;
    }
    int j = 10;
    char source[4];
    while(j <= 13)
    {
    strcat(source,fullstring[j];
    j++;
    }
    int j = 14;
    char format[1];
    while(j = 1)
    {
    strcat(format,fullstring[j];
    j++;
    }
    int j = 15;
    char immed[17];
    while(j <= 31)
    {
    strcat(immed,fullstring[j];
    j++;
    }
    }
    delete instruction[]
    char instruction[8];

    while(i <= 7)
    {
    strcat(instruction,input_file);
    i++;
    }



    output_file << "Instruction: 0x" << instruction;
    output_file << "Opcode = " << opcode << "\n";
    output_file << "Dest reg = " << dest << "\n";
    output_file << "Src reg = " << source << "\n";
    output_file << "Format = " << format << "\n";
    output_file << "Immed = " << immed << "\n";
    output_file << "\n\n\n\n";
    }
    input_file.close();
    }
    }

    /* to convert binary to decimal
    #include <iostream.h>
    void main()
    {
    int binary, number, decimal = 0;
    int HighBit = 64, Max = 1000000;

    cout << "Enter a binary number (7 digits maximum):" << endl;
    cin >> binary;

    number = binary;

    while (HighBit >=1 )
    {
    decimal += binary / Max*HighBit;
    HighBit /= 2;
    binary %= Max;
    Max /= 10;
    }

    cout << "The decimal equivalent to " << number << " is " << decimal << endl;
    }
    */

    oh...and at the end there, commented out, i found a site that said how to convert binary to decimal, but i don't really understand how to implement it...they just had the code and not an explanation


    this much of the code i understand (what i have done)...so i'd really appreciate if you could tell me what i can do with the code to finish the program off...so far what has been said in this thread i don't really understand...thanks
     
  7. skinjob

    skinjob Active Member

    Joined:
    Jan 6, 2001
    Messages:
    2,337
    Likes Received:
    0
    Location:
    Aztlán
    Ok, first let me say that reading that input file one character at a time, then constructing a string of 1's and 0's, then parsing that string to extract opcodes is kinda like taking a trip to the your local grocery store by way of China.

    This will make life easy for you:

    Hint #1: Use what's already available to parse your input.

    while ( !input_file.eof() )
    {
    unsigned int instruction;

    input_file >> setbase(16) >> instruction;
    }

    Each pass through that loop will read one line of your input file. It will parse the value of the hexadecimal number stored in that line, and store that value in the int variable 'instruction'.

    input_file >> setbase(16) >> instruction;

    That one line of code reads a line of characters from the input file and essentially converts the "number" to binary since all integers are physically stored as 32 binary bits.

    Hint #2: Bitmasks

    Now, are you familiar with bitwise and (&) and or (|)?

    int a = 0xAA; // <-- this is binary 10101010 represented in hex
    int b = 0x0F; // <-- this is binary 00001111 represented in hex
    // (remember, you can't express binary literals in C/C++)

    int c = a & b; // bitwise and

    The result of a & b is 0x0A or 00001010 in binary. Notice how c now contains the last four bits of the variable a. This is called bitmasking. The variable b is a bitmask. You set the bits that you are interested in to 1 in the bitmask and use bitwise and to extract those bits from another value. If I wanted the last 7 bits of a number, I'd set the bitmask to 01111111 (0x3F). The result c = a & 0x3F; is 0x2A (00101010).

    Hint #3: Shifting bits

    unsigned int a = 0x0C1B33D0;
    unsigned int b = 0;

    Suppose I want to extract the upper 8 bits (0x0C) of a and store it in variable b. In order to do that, you use the bit shift operator. Operator '>>' shifts bits to the right, '<<' shifts bits to the left. The arguments to the operator are the number you want to shift, and the number of bits you want to shift. In order to get the upper 8 bits of a, I need to shift it right 24 bits:

    b = a >> 24;

    Now b contains 0x0C, the upper 8 bits of a.

    Hint #4: Combine bitmasks with shifting;

    Suppose I wanted the next 8 bits of a (0x1B). If I shift a by 16 bits I'm left with 0x00000C1B. Not exactly the result I wanted, so I mask off the top 24 bits of the shifted result.

    b = (a >> 16) & 0x000000FF;

    Now b == 0x1B.

    I hope this is enough to get you started on the right path. If my explanations aren't clear, let me know.
     
  8. mrburner

    mrburner Ron Paul 2008

    Joined:
    May 9, 2001
    Messages:
    7,448
    Likes Received:
    0
    Location:
    Phoenix
    i have a few questions but thanks a lot for the response...

    in the setbase(16)...what is the 16 for?

    also i am familiar with bitwise and and or but i'm still a little unsure about this bitmasking

    i just don't understand at all how i can go from what i have to this bitmasking...like if i have a hexidecimal line AF37209E...where do i even start? i read that line in with the setbase and that will keep looping until it reaches the end of the file...and you said this converts the number to binary so does that mean that now every line from the input line has been consolidated to it's own 32 bit long line of output binary?

    so if i have this line of 32 bit binary and i want to pull out the first 6 digits and then convert them into decimal, how would i do that?

    i am seriously not the type of person who every stresses about anything...i can even tell you the only 3 times i've been stressed in my life, and this is one of them...i really have no idea what to do, my teacher isn't very helpful

    let me try to explain what i got out of that and maybe you can help me from there

    i start by using the original while loop up at the top with the setbase inside...that will read a line at a time from the input file and store it into instruction...but then it keeps going through the loop and keeps reading new lines into the variable instruction...how are these all stored in there? is it like an array?

    but after it does this, it basically automatically converts it from hex to binary by using bitmasks...how i can use bitmasks to help me convert, i defenitely do not understandcan i say int a = 0xunsigned;

    like how do i get my original line converted?

    so after this i really don't understand the shifting bits section or combining bitmasks with shifting...

    i really want to get this done by tonight because i have a test in my programming class on friday and a discrete math assignment due as well as having to work...this year is killing me :(

    so if there is any way you or anyone else can give me an explanation of my above problems, i would much appreciate it
     
  9. samm

    samm Next in Line

    Joined:
    Dec 22, 2000
    Messages:
    2,630
    Likes Received:
    0
    Location:
    San Jose, CA
    Hex is base 16, 0-9 and A-F. When you read in the inputs as hexadecimal integers, there should be no need to "convert" them to binary. How do you think they are stored in the memory?

    If you need to just pull out the first 6 bits of the inputted number, do it like this:

    six_bits = input_hex_number & 0x0000003F;
    cout << six_bits;
     
  10. skinjob

    skinjob Active Member

    Joined:
    Jan 6, 2001
    Messages:
    2,337
    Likes Received:
    0
    Location:
    Aztlán
    setbase(16) tells the ifstream that the characters it is reading from the file represent a hexadecimal (base 16) number. If the numbers in your input file were decimal, the setbase(...) would be unnecessary since ifstreams read decimal by default.

    In each iteration through that loop, a line from the input file will be read. The characters in that line will be parsed as a hexadecimal number. The value of that number will be stored in the variable instruction. Each successive iteration will overwrite the instruction variable with the value from the current line in the input file. There is no array that stores all the numbers. I gave you that loop as a start. It's up to you to add the code that does the rest.

    Suppose your program reads in the line containing "AF37209E." It stores the value in an integer. The contents of that integer look like this:

    10101111001101110010000010011110 (0xAF37209E)

    You keep asking about converting to decimal. But keep in mind that there is no such thing as a decimal representation within the bits and bytes of the computers memory. Neither is there a hexadecimal representation. Everything in your computer's memory is stored in binary.

    But, when you output to the screen, you're telling the computer to output characters that represent the binary number using decimal or hexadecimal digits.

    unsigned int instruction = 0xAF37209E;
    cout << instruction << endl;
    //^^^ this is equivalent to cout << setbase(10) ...
    cout << setbase(16) << instruction << endl;
    cout << setbase(8) << instruction << endl;

    The out put produced by this code segment looks like:

    2939625630
    af37209e
    25715620236

    As you can see, the instruction variable is not being "converted." Its value stays the same. You're just telling cout to output characters that represent the value stored in that variable using different numerical bases.

    So, how do you extract the first 6 binary digits from AF37209E? You want to store the value of the first 6 bits in an integer variable. The result looks like this:

    00000000000000000000000000101011 (0x0000002B)

    In order to achive this, you need a bitmask to mask off all but the first 6 bits.

    11111100000000000000000000000000 (0xFC000000)

    The result of the bitwise '&' of the original value with the bitmask looks like:
    Code:
    10101111001101110010000010011110
    &
    11111100000000000000000000000000
    ------------------------------
    10101100000000000000000000000000  (0xAC000000)
    
    But, were not finished because those 6 bits are not in the right place. The integer needs to be shifted to the right by 26 bits.

    The code to achieve all this looks like:

    unsigned int instruction = 0xAF37209E;
    unsigned int first_six = instruction & 0xFC000000; // mask off the first 6 bits using a bitmask
    unsigned int result = first_six >> 26; // shift the contents of first_six by 26 bits to the right, and store the result in 'result'
    cout << result << endl;
    cout << setbase(16) << result << endl;

    The output looks like:
    53
    2b
     
    Last edited: Oct 29, 2003
  11. mrburner

    mrburner Ron Paul 2008

    Joined:
    May 9, 2001
    Messages:
    7,448
    Likes Received:
    0
    Location:
    Phoenix
    k...that helps a lot

    i also went into my programming teacher and talked to her about it a little bit

    a couple more questions for now...that last line you gave me saying the output is 53 and on the next line it is 2b...what is the 2b?

    ok...and what you are saying that if i have a hex input without the 0x at the beginning, when i use the setbase to load it in and store it as an unsigned integer, that automatically puts the 0x at the beginning?

    last question for now (i'm gonna be working on this for awhile this afternoon and tonight so i don't think i'll have any other questions after this until then when/if i run into any trouble...2's complement in c++...the assignment says if the format bit is a 1 then the immediate mode (last 17 bits) need to be treated as 2's complement...if the format bit is 0 then the immediate mode needs to be treated as an unsigned integer...i understand that i can make an if statement to do the 2's complement...but i don't really understand the idea of 2's complement...this lab was originally going to be due like a month and a half or 2 months ago but then he found out that nobody knew 2's complement...and didn't teach it to us in the meantime

    i thought that it was where you take a binary set of numbers (i.e. 10101011) and invert it (to 01010100) and then add 1 to that (so 01010101)...but then i saw something about it is supposed to be negative

    then i asked people about it and they said that you only invert it for negatives but they couldn't explain any more...so basically i am very confused on 2's complement and how to implement it in c++...right now i'm thinking inside the if statement i'll write a while loop saying while this character is a 0, change it to a 1 and while it is a 1, change it to a 0 and then at the end add 1
     
  12. skinjob

    skinjob Active Member

    Joined:
    Jan 6, 2001
    Messages:
    2,337
    Likes Received:
    0
    Location:
    Aztlán
    Decimal 53 == Hex 0x2B. Look at the code. I'm outputing the result in decimal and hex.

    instruction = 2939625630; // dec
    instruction = 0xAF37209E; // hex
    instruction = 025715620236; // oct

    Those three lines are equivalent. The convention for C/C++ compilers is to prefix hex literals with 0x, and octal literals with 0.

    instruction = AF37209E;

    This will generate a syntax error because the compiler has no clue without the 0x in front.

    instruction = 25715620236;

    The compiler will interpret this as a decimal number and store the wrong value if you meant to express the value in octal.

    Lookup the bitwise operators! There is an operator to invert all the bits of value stored in memory.

    Negative integers work like this: First, signed ints loose the high bit because it is used as a flag to indicate that the number is negative. If the value is negative, the high bit is set to 1. That's why signed ints have a range of -2billion to +2billion and unsigned ints range from 0 to 4billion. 2billion fits in 31 bits, 4billion fits in 32 bits. Second, the remaining 31 bits represent the value. If the high bit is set, then the value is in 2s complement.

    int n = 10;

    n contains:
    00000000000000000000000000001010

    to convert n to negative, simply flip all the bits and add 1. This sets the high bit, and converts the lower 31 bits to 2s complement.

    Code:
    11111111111111111111111111110101
    +
    00000000000000000000000000000001
    --------------------------------
    11111111111111111111111111110110
    
    But here's a hint. You don't need to do any conversion. Your assignment says that if that bit is set, treat the lower 17 bits as a 2s comp number. It's already a twos comp number! It's just a matter of copying those 17 bits to an integer variable and then setting the upper 15 bits appropriately.
     
  13. mrburner

    mrburner Ron Paul 2008

    Joined:
    May 9, 2001
    Messages:
    7,448
    Likes Received:
    0
    Location:
    Phoenix
    k...this seems more and more doable with every post :)

    now...how do i get the 0x in front of the instruction = AF37209E; if that will generate a syntax error?

    i can't say like 0xinstruction...can i do instruction = 0x + instruction? or something like that? thanks again for all your help
     
  14. skinjob

    skinjob Active Member

    Joined:
    Jan 6, 2001
    Messages:
    2,337
    Likes Received:
    0
    Location:
    Aztlán
    uhhhh... after you type the '=' sign, type '0' and 'x'?

    Sorry, what am I not making clear?

    suppose in your code you type in the line

    int n = 10;

    This declares an int variable and sets it's value to 10.
    This does the same exact thing.

    int n = 0x0000000A;

    The only difference is you're expressing the literal constant in hex. 0x tells the C++ compiler's parser that what follows should be read as a hex number. 0x is a convention to express a literal value in hex. It doesn't modify what's stored in the variable in anyway.

    int n = A; does not work because the compiler is not given the cue to interpret A as a hex number. The compiler has no clue what 'A' is (unless you've declared some variable called A).

    int n = 0xA; that's better
     
  15. skinjob

    skinjob Active Member

    Joined:
    Jan 6, 2001
    Messages:
    2,337
    Likes Received:
    0
    Location:
    Aztlán
    Maybe this will make it clear.

    0x for hex numbers is a convention used when writing code. It's not used for input or output when your program executes. The reason you need to use 0x is because something like AF37209E is a legal variable name.

    I should have said "instruction = AF37209E;" generates an unknown identifier error, because it is correct syntax for assigning one variable to another.

    So, if you want to assign the value AF37209E to an integer variable, the correct syntax is "instruction = 0xAF37209E;"
     
  16. mrburner

    mrburner Ron Paul 2008

    Joined:
    May 9, 2001
    Messages:
    7,448
    Likes Received:
    0
    Location:
    Phoenix
    ok...this is what i'm having trouble with...i'm reading in a line of code, not typing it in so if i set "instruction" equal to the first line of code from my input file, then instruction will equal AF37209E or whatever...since i'm not typing it in, how do i get the 0x at the beginning of that AF37209E since the input file is not set in stone so i can't type in like

    this input will be AF37209E so i want instruction = 0xAF37209E;

    it will read in AF37209E and set it to instruction...i have no idea how to set instruction = 0xAF37209E;

    i'm having trouble explaining exactly what i'm saying here but like look at the sample output file...the first line says instruction: 0xAF37209E (for example)

    and if i just have instruction = AF37209E (my input line) you said that won't automatically convert it to a binary value without the 0x at the beginning, right? let me know if i'm not making myself clear

    and i'm still kinda confused about your 2's complement hint...because it says if the format bit is a 1 then do the last part in 2's complement and if it is 0, leave it as an unsigned integer...how is it already 2's complement?
     
  17. skinjob

    skinjob Active Member

    Joined:
    Jan 6, 2001
    Messages:
    2,337
    Likes Received:
    0
    Location:
    Aztlán
    int good_hex = 0xAF37209E;
    int bad_hex = AF37209E;

    That is sample code contained in a hypothetical source file that is fed to a C++ compiler to be turned into a hypothetical executable. I am using the sample code to illustrate the use of the "0x" prefix to express a literal integer constant in hexadecimal. You use the "0x" prefix to distinguish a hexadecimal number from a valid identifier name in your source code.

    1BE6000F
    1BE7FFF1
    7CA20342
    94250000
    669BFFFF
    6699FFFF

    That is the contents of your input file. There is no relation between the two. Your input file does not contain "code."

    Whether or not the program input prefixes hexadecimal representations with "0x" does not matter. istream >> setbase(16) will read ABCDEF12 just as well as 0xABCDEF12.

    For output, there is an IO manipulator flag that will change the behavior of ostream (e.g. cout) to output base prefixes.

    cout << setbase(16) << 10;
    cout << setiosflags(ios_base::showbase);
    cout << setbase(16) << 10;

    The output you see on screen from this bit of code is:

    a
    0xa

    Is it clear now?

    The way I interpret that sentence is that if the INT_FORMAT bit is set, those 17 bits in the immediate mode field contain a 2's comp representation of a negative integer. If the bit is not set, those bits are a normal binary representation of a positive integer.
     
  18. skinjob

    skinjob Active Member

    Joined:
    Jan 6, 2001
    Messages:
    2,337
    Likes Received:
    0
    Location:
    Aztlán
    I didn't see this before. In fact this will make it far more easier than bitmasking and shifting bits. But, I'll leave it up to you to study unions and bit fields on your own. This was at least a good exercise in bit operators.
     
  19. mrburner

    mrburner Ron Paul 2008

    Joined:
    May 9, 2001
    Messages:
    7,448
    Likes Received:
    0
    Location:
    Phoenix
    when talking to someone earlier, they told me that i can't use his method if i want to program in c++...that it was only for c so i went ahead and did the bitmasks/shifting bits method we've been talking about...now a couple more questions now that i'm actually in the lab and working on this (before i was in various computer classes and just trying to clarify)

    about your 2's complement idea...if i had an input where the format reads 1, the program would automatically have a negative number for the immed value? because on the homework assignment sheet, he has another example output which has the format at 1 and the immed at -568

    also...here is my code so far...how big should i make file_name[] and file_name2[]...if i leave the brackets empty, the compiler complains...also...is my code missing anything other than that?

    #include <iostream.h>
    #include <fstream.h>
    #include <conio.h>
    #include <string.h>

    int main()
    {
    char data_string[32];
    //char file_name[];
    //char file_name2[];

    ifstream input_file;
    ofstream output_file;
    int read_ok;
    int counter = 0;
    int total_counter = 0;

    cout << "Please enter the name and path of the input file\n";
    cin >> file_name;
    input_file.open(file_name);

    cout << "Please enter the name and path of the output file\n";
    cin >> file_name2;
    output_file.open(file_name2);

    if(!input_file.is_open() || !output_file.is_open())
    {
    cout << "Error opening one of your files";
    }

    else
    {
    while(!input_file.eof())
    {
    unsigned int instruction, opcode, opcode2, dest, dest2, source, source2, format, format2, immed;
    input_file >> setbase(16) >> instruction;

    opcode = instruction & 0xFC000000;
    dest = instruction & 0x03C00000;
    source = instruction & 0x003C0000;
    format = instruction & 0x00020000;
    immed = instruction & 0x0001FFFF;

    opcode2 = opcode >> 26;
    dest2 = dest >> 22;
    source2 = source >> 18;
    format2 = format >> 17;

    // if(format2 = 1)
    // {
    // format2

    output_file << "Instruction: " << setbase(16) << instruction << endl;
    output_file << "Opcode = " << opcode2 << endl;
    output_file << "Dest reg = " << dest2 << endl;
    output_file << "Src reg = " << source2 << endl;
    output_file << "Format = " << format2 << endl;
    output_file << "Immed = " << immed << endl;
    output_file << "\n\n\n\n";
    }
    input_file.close();
    }
    }


    and by the way, i cannot thank you enough for the help...there is no way i could have done this without you
     
  20. mrburner

    mrburner Ron Paul 2008

    Joined:
    May 9, 2001
    Messages:
    7,448
    Likes Received:
    0
    Location:
    Phoenix
    oh...and is there a .h file i need to add to get setbase to work? the only 2 errors i'm getting right now are from setbase
     
  21. skinjob

    skinjob Active Member

    Joined:
    Jan 6, 2001
    Messages:
    2,337
    Likes Received:
    0
    Location:
    Aztlán
    #include <iomanip>
     
  22. skinjob

    skinjob Active Member

    Joined:
    Jan 6, 2001
    Messages:
    2,337
    Likes Received:
    0
    Location:
    Aztlán
    Are you on a Microsoft OS? Microsoft C++?

    260 is enough for filesystem paths. There is in fact a #define MAX_PATH 260 in windefs.h
     
  23. mrburner

    mrburner Ron Paul 2008

    Joined:
    May 9, 2001
    Messages:
    7,448
    Likes Received:
    0
    Location:
    Phoenix
    nevermind...searched and found it was iomanip.h...but any answers to the 2's complement question and whether or not my code looks alright above would be appreciated (i took out conio.h, string.h, and added iomanip.h)
     
  24. mrburner

    mrburner Ron Paul 2008

    Joined:
    May 9, 2001
    Messages:
    7,448
    Likes Received:
    0
    Location:
    Phoenix
    didn't refresh the page before i posted

    i'm using codewarrior on a mac os x.2 machine if that helps...so i should use 260 in those? do you see anything else about the code that should be changed?
     
  25. skinjob

    skinjob Active Member

    Joined:
    Jan 6, 2001
    Messages:
    2,337
    Likes Received:
    0
    Location:
    Aztlán
    The only thing you're missing is accounting for the 2's comp value if that bit is set.

    immed = instruction & 0x0001FFFF;

    The result of that will never be interpreted as a negative number since the high bit is never set (Also, you need to declare immed as a signed int). After you mask off the lower 17 bits, you have to fill in the upper 15 bits correctly so that immed is interpreted as a 2's comp negative integer.

    Was this person refering to the use of bit fields? If so, this person was speaking from his anal orifice. Anything that applies to C, applies to C++.
     

Share This Page