Hi all,
I have a function. I use the asm instruction 'neg' to create a twos compliment negative number, in my case 1 becomes -1 which is 1000 0001.
I return from my function.
How would i go about testing that returned number for whether its a -1 or a possible pointer that I also return from my function?
Thanks,
Moose
returning negatives in asm
RE:returning negatives in asm
ok,
i've given the negative representation of sign and magnitude.
twos compliment of -1 would be
11111111
But that could still be considered an address right? So how do you tell the difference?
Moose
i've given the negative representation of sign and magnitude.
twos compliment of -1 would be
11111111
But that could still be considered an address right? So how do you tell the difference?
Moose
RE:returning negatives in asm
If your function can return multiple things then you will need to create some sort of provision. For example, a return value that is negative is a negative number and a return value that is positive is a pointer. The downside to this approach is that you will lose half the address values that your pointer can take.
An alternative is to return a union structure that has a TYPE variable. Then you can do a conditional statement on the TYPE to determine how you should read the VALUE variable.
There is probably a better solution by changing the implementation on what you are trying to do. The question that comes to mind is why you have one function that can return two different types of values? Why not have two functions that each return only 1 type of value?
An alternative is to return a union structure that has a TYPE variable. Then you can do a conditional statement on the TYPE to determine how you should read the VALUE variable.
There is probably a better solution by changing the implementation on what you are trying to do. The question that comes to mind is why you have one function that can return two different types of values? Why not have two functions that each return only 1 type of value?
RE:returning negatives in asm
Ok, well the function in question is returning a pointer to a linear list but returns false if it fails to return the pointer.
Reason being, im trying to mimic the methods of other high level languages to keep some sort of consistency in my assembly code.
For example, recently i've been doing some php work that lookse like
while($db_array = mysql_fetch_array($resource_name)) {
In that line, an array of info is returned, else false is returned which is why the while loop exits because the statement is false.
What im interested in is how that would look in assembly code. How would i go about implementing that idea?
I understand that a high level language would only return one data type and that php isn't exactly what you'd call similar to C but im fairly sure C would do the same in the same situation.
Two functions?
So, the first function would tell me whether its actions succeded? The second would read the results from those actions?
Thanks for your reply,
Moose
Reason being, im trying to mimic the methods of other high level languages to keep some sort of consistency in my assembly code.
For example, recently i've been doing some php work that lookse like
while($db_array = mysql_fetch_array($resource_name)) {
In that line, an array of info is returned, else false is returned which is why the while loop exits because the statement is false.
What im interested in is how that would look in assembly code. How would i go about implementing that idea?
I understand that a high level language would only return one data type and that php isn't exactly what you'd call similar to C but im fairly sure C would do the same in the same situation.
Two functions?
So, the first function would tell me whether its actions succeded? The second would read the results from those actions?
Thanks for your reply,
Moose
RE:returning negatives in asm
Ok, I understand now what you are trying to do.
What I would do is return a null pointer if the function fails. Then do a test to see if the pointer is null or not. Something like:
ptr = myfunc();
if(ptr)
{
//Do stuff
}
In C, I'm unaware of any time that the assignment statement returns false. A common mistake people do is:
if(a = b)
{
//Do stuff
}
Instead of using ==. "Do stuff" in this case is always excuted (to my knowledge) because the assignment returns true.
You might be able to implement what you want in C++ by overriding the = operator for a class. Maybe something like:
int operator= (<correcttype>); //Return !0
int operator= (<elsetype>); //Return 0
I don't know if that is possible or not without experimenting.
An assignment is just like a special function. You might try:
while(myassignfunc(ptr,myfunc()))
{
//Do stuff
}
Or maybe implement some sort of macro that translates into the above while statement.
Maybe someone with more knowledge over these languages might have a better answer. PHP is an interpreted Semi-OOP language and C... Well... C does have its limitations and this may be one of them.
What I would do is return a null pointer if the function fails. Then do a test to see if the pointer is null or not. Something like:
ptr = myfunc();
if(ptr)
{
//Do stuff
}
In C, I'm unaware of any time that the assignment statement returns false. A common mistake people do is:
if(a = b)
{
//Do stuff
}
Instead of using ==. "Do stuff" in this case is always excuted (to my knowledge) because the assignment returns true.
You might be able to implement what you want in C++ by overriding the = operator for a class. Maybe something like:
int operator= (<correcttype>); //Return !0
int operator= (<elsetype>); //Return 0
I don't know if that is possible or not without experimenting.
An assignment is just like a special function. You might try:
while(myassignfunc(ptr,myfunc()))
{
//Do stuff
}
Or maybe implement some sort of macro that translates into the above while statement.
Maybe someone with more knowledge over these languages might have a better answer. PHP is an interpreted Semi-OOP language and C... Well... C does have its limitations and this may be one of them.
RE:returning negatives in asm
A null pointer would be all zero.
I am doing this in assembly and strictly, couldnt a pointer of all zeros still be a legal pointer.
I can store information at 0mb after all, or if the pointer was an offset from a segment register, e.g. ds:0 would still work.
Are C pointers always referenced from 0mb physical ram?
Moose
I am doing this in assembly and strictly, couldnt a pointer of all zeros still be a legal pointer.
I can store information at 0mb after all, or if the pointer was an offset from a segment register, e.g. ds:0 would still work.
Are C pointers always referenced from 0mb physical ram?
Moose
RE:returning negatives in asm
In theory, a pointer to address 0 is a legal pointer. In practice, it's an abysmally bad idea to store anything at memory address zero. If you're using paging, you should always leave the first 4KB of RAM "not present" so that any attempt to access memory location 0 causes a page fault, and just kill any program that tries to use a null pointer. That's what Linux, modern Windows, and most other operating systems do, and it increases system stability immensely.
The reason for this is, despite the fact that in theory this is a valid memory location, in practice the C programming language uses a null pointer, which is just a pointer to location 0, to indicate "false" or "error" or "invalid pointer". Now, you can use whatever idiom you want in your own assembly code to encode this idea, but if you call any common libraries, or anything ever calls your code, this may cause problems down the road. If you get a null pointer from a library function, it's trying to indicate failure, not that the results are at location 0, and if you return a null pointer to anyone else, they will assume you're flagging an error rather than referencing the base of memory. User programs should never access memory location 0, and even OS code ought to remap the RAM to some other virtual address so that that physical RAM is referenced at some other virtual address. Doing so will help in debugging your OS code immensely.
The reason for this is, despite the fact that in theory this is a valid memory location, in practice the C programming language uses a null pointer, which is just a pointer to location 0, to indicate "false" or "error" or "invalid pointer". Now, you can use whatever idiom you want in your own assembly code to encode this idea, but if you call any common libraries, or anything ever calls your code, this may cause problems down the road. If you get a null pointer from a library function, it's trying to indicate failure, not that the results are at location 0, and if you return a null pointer to anyone else, they will assume you're flagging an error rather than referencing the base of memory. User programs should never access memory location 0, and even OS code ought to remap the RAM to some other virtual address so that that physical RAM is referenced at some other virtual address. Doing so will help in debugging your OS code immensely.