1. Originally Posted by matsp
Lets start with the "octal" bit. First of all it's 1, 2, 3, 4, 5, 6, 7, 10, 11, ... there's no 8 OR 9 in the number system.

Second, in the computer, numbers are stored in binary. Octal and hex are popular ways to represent binary numbers so that they can be human readable and still easy to translate back to binary. This works because in octal, 3 bits (binary digits) are represented by one octal digit (0..7) and in hex 4 bits are represented by one hex digit. So it's very easy for a human to work out that 031 in octal is 000 011 001. We can also do the same the other way, using hex: 0 0001 1001 becomes 19.

Decimal is much harder to deal with because the only way to do it is to divide by 2 or 10 depending on which way we're going, until there is nothing more to divide. This is because 10 is not a "nice" number of bits - each multiple of ten is approximately 3.3 bits... It would have been MUCH easier if we humans were born like cartoon characters (e.g. the Simpsons), with one thumb and three fingers. Then we would have used octal as our number base in the first place, and we wouldn't have had this problem.

As to knowing if something is a number or not, you probably really want to put that into a function that resolves the symbols into numbers too, since you don't really care which they are when you see a particular instruction - you just want to translate it into a number, whcihever it is. Note that for labels that are forwards in the code, you will need a two-pass approach, so you read the entire file once, store away where each instruction belongs, and what the value of each label is, and then pass through it again and flll in any "gaps".

--
Mats
thanks Dear Mats, so to achieve an octal based counter is to convert the decimal one to the octal ! ok big deal . tanx

about the labels i havent started that part yet.i have some idea about how to implement and or convert them , ill tell you then .

i have a question . do i have to dump the whole stuff user inputs to the emulated memory of the machine? or i should pars it and then the parsed string that is a combination of operation code , offset and possible addressing modes , and then dump them in memory?noticing that we have a 16 bit line for each instruction ?
and then execute it from its memory?
Code:
```    ORG 9      //stating where to start

10. RED 6
11. LDA 5
12. SBA 6
13. JNG 17
14. WRI 5
15. JMP 20
17. WRI 6
20. HLT

-----------------
Machine Language
1110   000  000  000  101
1110   000  000  000  110
0001   000  000  000  101
0100   000  000  000  110
1000   000  000  001  111
1111   000  000  000  101
0111   000  000  010  000
1111   000  000  000  110
0000   000  000  000  000```
would you help me on this?

2. Actually, if you are READING numbers, it's about converting an octal set of digits into a binary number, and if you are writing octal numbers out, it's about converting a binary number to octal digits - but the process is the same as for base 10 - if you read in decimal digits (say 123), do you know how to actually convert that into an integer value?

I'm pretty sure that what you want in your emulated memory is the parsed and translated version of what the user inputs - but that's just what I think, I don't know the specification of your project.

--
Mats

3. Originally Posted by matsp
Actually, if you are READING numbers, it's about converting an octal set of digits into a binary number, and if you are writing octal numbers out, it's about converting a binary number to octal digits - but the process is the same as for base 10 - if you read in decimal digits (say 123), do you know how to actually convert that into an integer value?

I'm pretty sure that what you want in your emulated memory is the parsed and translated version of what the user inputs - but that's just what I think, I don't know the specification of your project.

--
Mats
many tanx .

4. why doesnt this loop work?
it just loops only once!
Code:
```
char label[15];
string string1 = "start:              sta                  50";

for (int i=0 ; string1[i]!=':'; i++ )
{ label[i]=string1[i];

}label[++i]='\0';
int b=i;```
but when i use it to print the string1 characters it works fine!
Code:
```    for (int i=0;string1[i]!=':';i++)
cout<<string1[i];```

5. and another question how is it possible to access any element in a string with this format :char * string=" blah blah"?
what do we call this kind of string ? !

6. >> why doesnt this loop work?
The loop probably does work, but you have another problem on a different line:

label[++i] = '\0';
foo.cpp(14) : error C2065: 'i' : undeclared identifier

Remember that if you initialize a variable in the for statement, it is local to the for statement. Meaning the variable i is undeclared where you are using it.

In the future, please indicate that you've compiled your code by including any errors and/or warnings with your post.

>> how is it possible to access any element in a string with this format :char * string=" blah blah"?
If my memory serves me correctly, this used to not be the case. The ability to do that eventually came as a convenience to C programmers, who would declare an array first and then declare another pointer to ... erm, point at it. Particularly, a convenience to the C programmers who complained about it. After all, either an array or just a pointer worked in the given situation, and in pre-ANSI C you had to come up with two variable names for the same data. It's in C++ mainly because of its design being based on top of the C language.

Do not abuse this feature. String literals are meant to be read-only, and should only be treated as constants in your code.

>> what do we call this kind of string ? !
pointer to string literal, perhaps. *shrug*

7. It is essentially the same thing.
Code:
```char* MyName1 = "Cody";
char MyName2[5] = "Cody";

std::cout << MyName1[0] << " Should be 'C' " << MyName2[0] << " Should also be 'C'\n";```

8. >> It is essentially the same thing.
Don't just say that. You'll likely just confuse him more.

9. Originally Posted by Raigne
It is essentially the same thing.
Code:
```char* MyName1 = "Cody";
char MyName2[5] = "Cody";

std::cout << MyName1[0] << " Should be 'C' " << MyName2[0] << " Should also be 'C'\n";```
There is one subtle difference tho':
The first form is a constant, and you are not supposed (and most often NOT ABLE) to modify that string. It should really be
Code:
`const char* MyName1 = "Cody";`
, so that the compiler tells you off for modifying the string, instead of discovering when the code crashes (or behaves weird) that you're writing to a constant - it's always easier to fix compile-time errors than those that occur in runtime.

In the case of the latter, you have 5 bytes of space that is initialized with "Cody" (with a nul-char at the end). You can put any other 4 characters in it's place - if you put a 5th in there, you have no space for the end-terminator, which could [read: will most often] lead to trouble.

--
Mats

10. Originally Posted by citizen
>> why doesnt this loop work?
The loop probably does work, but you have another problem on a different line:

label[++i] = '\0';
foo.cpp(14) : error C2065: 'i' : undeclared identifier

Remember that if you initialize a variable in the for statement, it is local to the for statement. Meaning the variable i is undeclared where you are using it.

In the future, please indicate that you've compiled your code by including any errors and/or warnings with your post.

>> how is it possible to access any element in a string with this format :char * string=" blah blah"?
If my memory serves me correctly, this used to not be the case. The ability to do that eventually came as a convenience to C programmers, who would declare an array first and then declare another pointer to ... erm, point at it. Particularly, a convenience to the C programmers who complained about it. After all, either an array or just a pointer worked in the given situation, and in pre-ANSI C you had to come up with two variable names for the same data. It's in C++ mainly because of its design being based on top of the C language.

Do not abuse this feature. String literals are meant to be read-only, and should only be treated as constants in your code.

>> what do we call this kind of string ? !
pointer to string literal, perhaps. *shrug*
man tanx dear citizen . i was completely forgot that! now its fine! tanx
Originally Posted by Raigne
It is essentially the same thing.
Code:
```char* MyName1 = "Cody";
char MyName2[5] = "Cody";

std::cout << MyName1[0] << " Should be 'C' " << MyName2[0] << " Should also be 'C'\n";```
tanx bro, but this way it didnt work for me , instead *(Myname1+subscript) worked pretty great!
Originally Posted by matsp
There is one subtle difference tho':
The first form is a constant, and you are not supposed (and most often NOT ABLE) to modify that string. It should really be
Code:
`const char* MyName1 = "Cody";`
, so that the compiler tells you off for modifying the string, instead of discovering when the code crashes (or behaves weird) that you're writing to a constant - it's always easier to fix compile-time errors than those that occur in runtime.

In the case of the latter, you have 5 bytes of space that is initialized with "Cody" (with a nul-char at the end). You can put any other 4 characters in it's place - if you put a 5th in there, you have no space for the end-terminator, which could [read: will most often] lead to trouble.

--
Mats
tanx again dear Mats.

by the way i have another question
how can i make a new data type!
for example i want to make a data that is capable of holding 3 char instead of 1 char
for example see the following codes , im planning to do such a thing! if it is not possible how can i achieve my plan in this case?
storing a label in a label register (each element of label register is capable of holding a 3 characters long string .!so that whenever i need a specific label name , by referring to labelregister[index]. i can retrieve the name!
i tried using :
#define mychar char[3]
to make new data type capable of holding my labels ! but no luck!

mychar char[3];
mychar label[15];

char name[3];

label[0]=name;