johnny
johnny

Reputation: 171

Storing answer at a specific address

I am trying to solve a question with a statement "Two numbers, x and are stored in memory locations $0079 and $0179 respectively. Write a C code that reads both number using two pointers. Compare the numbers, if they are the same, add them. If they are different subtract them. Write the result of the operation at the address $0279"

Now the solution which I have in mind is this

Int *foo = 0079;
Int *foo1 = 0179;
Int*result = 0279;
If (*foo == *foo1)
{
*result = *foo+*foo1;
}else if(*foo>*foo1)
{
*result = *foo-*foo1;
}else
{
*result = *foo1-*foo2;

}

but I have doubts about this that should I use 0x0079 instead of 0079 or should I use (int *)0800000079 as one of my friend suggested

Upvotes: 0

Views: 85

Answers (2)

Clifford
Clifford

Reputation: 93476

You may have doubts about this code, but your compiler won't - it will reject it outright.

Literal integer constants beginning with a zero digit are interpreted as octal (base 8), and as such may only have digits 0 to 7. 0079 for example is therefore invalid.

These addresses are therefore intended to be either decimal (e.g. 79) or hexadecimal (base 16 - eg. 0x79). Addresses are normally expressed in hexadecimal, so lets assume that 0x0079 was intended.

I assume the suggestion 0800000079 is a typo and was intended to be 0x00000079? That being the case 0x79 is the same value as 0x00000079 - the typecast to int* is valid though not strictly necessary since an implicit cast will occur otherwise.

An odd-numbered addresses seem unlikely for an int data type which would normally be word aligned - some architectures require it, others will generate additional code to access unaligned addresses. If the odd-numbered address is correct, it suggests perhaps an 8 bit target, while 0x00000079 suggests a 32-bit address bus - which seems an unlikely combination.

Essentially the question seems implausible and ill-defined; but taking it at face value your solution is unnecessarily complex; I would suggest:

#define OP1    (*((int*)0x0079))
#define OP2    (*((int*)0x0179))
#define RESULT (*((int*)0x0279))

if( OP1 == OP2 )
{
    RESULT = OP1 + OP2 ;
}
else
{
    RESULT = OP1 - OP2 ;
}

The use of the macros here simplify the syntax somewhat. Your suggestion has an unnecessary test for OP1 > OP2 and a final else clause that does the same thing. There are only two mutually exclusive conditions - equal-to or not-equal-to, so you only need to explicitly test for one condition, because everything else must meet the other. In C++ ( your tags suggest some language indecision) you might avoid the macro (almost always a good idea) by using a reference type:

int& op1 = *((int*)0x0079) ;
int& op2 = *((int*)0x0179) ;
int& result = *((int*)0x0279) ;

if( op1 == op2 )
{
    result = op1 + op2 ;
}
else
{
    result = op1 - op2 ;
}

Upvotes: 2

Lightness Races in Orbit
Lightness Races in Orbit

Reputation: 385144

Your friend's suggestion is very strange. I don't know why he or she has proposed (int *)0800000079, though you'll probably need the cast.

Back to sanity, the question of 0x0079 vs 0079 is not a code problem, but an issue with interpreting the problem requirements:

  • if $0079 denotes hex, then this is 0x0079 in C++ (not 0079, an invalid octal literal);
  • if you don't know what $0079 denotes, you'll have to ask the author for more detail.

For what it's worth, in several assembly languages (6502, Motorola), Pascal, Delphi, some versions of BASIC (Commodore), Game Maker Language, Godot, and Forth, $0079 would denote a hexadecimal value.

Upvotes: 2

Related Questions