SE250:lab-2:sbha077

From Marks Wiki
Revision as of 05:19, 3 November 2008 by Mark (Sọ̀rọ̀ | contribs) (43 revision(s))
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Lab 2 Report

What was done

We were required to carry out some calculations and tasks to find the sizes and address locations of various pointers and variables that we initialised.

Then we had to observe the differences between compiling under different architectures (win-intel x86 and linux-ppc x64).

Task 1

#include <stdio.h>

int main() {

int *ip1;
long *ip2;
double *ip3;
float *ip4;

printf("%ld %ld %ld %ld\n",sizeof(ip1),sizeof(ip2),sizeof(ip3),sizeof(ip4));

return 0;

}

RESULTS:

Intel x86 >> 4 4 4 4
PPC x64 >> 4 4 4 4 

OBSEVATIONS:

Intel x86 >> The values remain constant. Quite expected because they are pointers and the values don't change no matter what data type they are.
PPC x64 >> Same values as the Intel machine.

Task 2a

#include <stdio.h>

int main() {

int x, y;
printf("&x = %p; &y = %p; diff = %ld\n",&x, &y, (long)(&x-&y));

return 0;

}

RESULTS:

Intel x86 >> &x = 0012FF70; &y = 0012FF74; diff = -1
PPC x64 >> &x = 0xffb499ec; &y = 0xffb499e8; diff = 1

OBSERVATIONS:

Intel x86 >> The addresses are stored in "earlier" parts of the memory. The difference, as quite expected, negative
PPC x64 >> The addresses are stored in "later" parts of the memory. The difference is strangely positive.

Task 2b

#include <stdio.h>

int main() {

int x, y;
printf("&x = %p; &y = %p; diff = %ld\n",&x, &y, (long)&x - (long)&y);

return 0;

}

RESULTS:

Intel x86 >> &x = 0012FF70; &y = 0012FF74; diff = -4
PPC x64 >> &x = 0xff9629ec; &y = 0xff9629e8; diff = 4

OBSERVATIONS:

Intel x86 and PPC x64 >> Same observations as before

Task 3a

#include <stdio.h>

int main() {

int x;
char arr[4];
int y;
printf("&x = %p; &y = %p; diff = %ld\n",&x, &y, (long)(&x-&y));

return 0;

}

RESULTS:

Intel x86 >> &x = 0012FF70; &y = 0012FF74; diff = -1
PPC x64 >> &x = 0xff90e9ec; &y = 0xff90e9e4; diff = 2

OBSERVATIONS:

Intel x86 >> The difference between the x and y values on memory is negative which does not fit to reason again.
PPC x64 >> The difference is positive and fairly larger (more than 1 anyway) and seems more plausible.

Task 3b

#include <stdio.h>

int main() {

int x;
char arr[ 4 ];
int y;

printf("&x = %p; &y = %p; diff = %ld\n",&x, &y, (long)(&x-&y));
printf("address of &arr = %p\n",&arr);
printf("value of %d\n", &arr);
printf("size of the arr+4 = %ld\n", arr+4);
printf("value of the %d\n", &arr[4]);

return 0;

}

RESULTS:

Intel x86 >>

&x = 0012FF70; &y = 0012FF74; diff = -1
address of &arr = 0012FF6C
value of &arr = 1245036
size of the arr+4 = 1245040
value of the &arr[4] = 1245040

PPC x64 >>

&x = 0xffc369ec; &y = 0xffc369e4; diff = 2
address of &arr = 0xffc369e8
value of &arr = -3970584
size of the arr+4 = -3970580
value of the &arr[4] = -3970580

OBSERVATIONS:

Intel x86 >> This does not make sense! I get -1 as differnce?!?
PPC x64 >> Even worse! Negative sizes and address values? I'm sure I typed everything correctly so...I'm lost!

Task 4a

#include <stdio.h>

int x, y;

int main() {

printf("&x = %p; &y = %p; diff = %ld\n",&x, &y, (long)(&x-&y));

return 0;

}

RESULTS:

Intel x86 >>  &x = 0040D984; &y = 0040D980; diff = 1

PPC x64 >> &x = 0x10010a64; &y = 0x10010a68; diff = -1

Task 4b

RESULTS:

Intel x86 >> &x = 0040D984; &y = 0040D980; diff = 4

PPC x64 >> &x = 0x10010a64; &y = 0x10010a68; diff = -4

OBSERVATIONS:

Intel x86 and PPC x64 >> The positive negative difference issue again! Somebody explain!

Task4c

RESULTS:

Intel x86 >> &x = 0040D984; &y = 0040D980; diff = 1

PPC x64 >> &x = 0x10010a64; &y = 0x10010a68; diff = -1

Task 4d

RESULTS:

Intel x86 >>

&x = 0040D9E4; &y = 0040D9E0; diff = 1
address of &arr = 0012FF74
value of &arr = 1245044
size of the arr+4 = 1245048
value of the &arr[4] = 1245048

PPC x64 >>

&x = 0x10010b38; &y = 0x10010b3c; diff = -1 
address of &arr = 0xffd789ec
value of &arr = -2651668
size of the arr+4 = -2651664
value of the &arr[4] = -2651664

Task 5

RESULTS:

Intel x86 >>
 
The value of *p1 = 1245040
The value of *p2 = 1245044

PPC x64 >>

The value of *p1 = -2762260
The value of *p2 = -2762264

Task 6a

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char *local_str() {
char s[8] = "0123456";
return s;
}

char *local_str2() {
char s[8] = "abcdefg";
return s;
}

char *static_str() {
static char s[8] = "tuvwxyz";
return s;
}

char *malloc_str() {
char *s = malloc(8);
strcpy(s, "hijklmn");
return s;
}

int main() {
char *sp;

sp = local_str();
printf("sp = %p(%s)\n",sp,sp);

sp = local_str2();
local_str2();
printf("sp = %p(%s)\n",sp,sp);

sp = static_str();
local_str2();
printf("sp = %p(%s)\n",sp,sp);


sp = malloc_str();
local_str2();
printf("sp = %p(%s)\n",sp,sp);

}

RESULTS:

Intel x86 >>

sp = 0012FF60(x ↕)
sp = 0012FF60(x ↕)
sp = 0040C010(tuvwxyz)
sp = 00323148(hijklmn)

PPC x64 >>

sp = 0xffa129b4(�þîÐ�)
sp = 0xffa129b4(abcdefg)
sp = 0x10010cc4(tuvwxyz)
sp = 0x10011008(hijklmn)

Task 6b

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char *local_str() {
char s[8] = "0123456";
return s;
}

char *local_str2() {
char s[8] = "abcdefg";
return s;
}

char *static_str() {
static char s[8] = "tuvwxyz";
return s;
}

char *malloc_str() {
char *s = malloc(8);
strcpy(s, "hijklmn");
return s;
}

int main() {
 
char *sp;

sp = local_str();
printf("sp = %p(%s)\n",sp,sp);

strcpy( sp, "XXXXXXX" );
printf( "sp X’d = %p(%s)\n", sp, sp );

sp = local_str();
local_str2();
printf("sp = %p(%s)\n",sp,sp);

strcpy( sp, "XXXXXXX" );
printf( "sp X’d = %p(%s)\n", sp, sp );

sp = static_str();
local_str2();
printf("sp = %p(%s)\n",sp,sp);

strcpy( sp, "XXXXXXX" );
printf( "sp X’d = %p(%s)\n", sp, sp );

sp = malloc_str();
local_str2();
printf("sp = %p(%s)\n",sp,sp);

strcpy( sp, "XXXXXXX" );
printf( "sp X’d = %p(%s)\n", sp, sp );

return 0;

}

RESULTS:

Intel x86 >>

sp = 0012FF60(x ↕)
sp XÆd = 0012FF60(x ↕)
sp = 0012FF60(x ↕)
sp XÆd = 0012FF60(x ↕)
sp = 0040C010(tuvwxyz)
sp XÆd = 0040C010(XXXXXXX)
sp = 00323148(hijklmn)
sp XÆd = 00323148(XXXXXXX)

PPC x64 >>

sp = 0xffb359b4(�þîÐ�)
sp X’d = 0xffb359b4(XXXXXXX)
sp = 0xffb359b4(abcdefg)
sp X’d = 0xffb359b4(XXXXXXX)
sp = 0x10010dc0(tuvwxyz)
sp X’d = 0x10010dc0(XXXXXXX)
sp = 0x10011008(hijklmn)
sp X’d = 0x10011008(XXXXXXX)

Task 7

#include <stdio.h>

struct {
char my_char;
short my_short;
int my_int;
long my_long;
float my_float;
double my_double;
} my_struct;

int main () {

printf("&my struct = %p\n", &my_struct );
printf("offsets:\n"
"my_char: %ld\n"
"my_short: %ld\n"
"my_int: %ld\n"
"my_long: %ld\n"
"my_float: %ld\n"
"my_double: %ld\n",
(long)&my_struct - (long)&my_struct.my_char,
(long)&my_struct - (long)&my_struct.my_short,
(long)&my_struct - (long)&my_struct.my_int,
(long)&my_struct - (long)&my_struct.my_long,
(long)&my_struct - (long)&my_struct.my_float,
(long)&my_struct - (long)&my_struct.my_double);

return 0;

}

RESULTS:

Intel x86 >>

&my struct = 0040D9C0
offsets:
my_char: 0
my_short: -2
my_int: -4
my_long: -8
my_float: -12
my_double: -16

PPC x64 >>

&my_struct = 0x10010ac0
offsets:
my_char: 0
my_short: -2
my_int: -4
my_long: -8
my_float: -12
my_double: -16

OBSERVATIONS:

Intel x86 and PPC x64 >> Interestingly, the values of the off-sets are the same BUT the memory locations are different which was quite expected.

Task 8

#include <stdio.h>

union {
char my_char;
short my_short;
int my_int;
long my_long;
float my_float;
double my_double;
} my_union;

int main () {

printf("&my_union = %p\n", &my_union );
printf("offsets:\n"
"my_char: %ld\n"
"my_short: %ld\n"
"my_int: %ld\n"
"my_long: %ld\n"
"my_float: %ld\n"
"my_double: %ld\n",
(long)&my_union - (long)&my_union.my_char,
(long)&my_union - (long)&my_union.my_short,
(long)&my_union - (long)&my_union.my_int,
(long)&my_union - (long)&my_union.my_long,
(long)&my_union - (long)&my_union.my_float,
(long)&my_union - (long)&my_union.my_double);

return 0;

}

RESULTS:

Intel x86 >>

&my_union = 0040D9C0
offsets:
my_char: 0
my_short: 0
my_int: 0
my_long: 0
my_float: 0
my_double: 0

PPC x64 >>

&my_union = 0x10010ab0
offsets:
my_char: 0
my_short: 0
my_int: 0
my_long: 0
my_float: 0
my_double: 0

OBSERVATIONS:

Intel x86 and PPC x64 >> The memory locations as expected are different but all the offset values being ZERO!?!?
I might have forgotten stuff from the leture which explains why I don't understand this phenomenon.

Task 9

RESULTS:

Intel x86 >>

sp1 = 0012FF74, sp2 = 0012FF70
sp2 = 0012FF70, sp3 = 0012FF6C

PPC x64 >>

sp1 = 0xffac59ec, sp2 = 0xffac59e8
sp2 = 0xffac59e8, sp3 = 0xffac59e4

OBSERVATIONS:

Intel x86 and PPC x64 >> Due to memory allocation, the pointers are stored in similar regions of the memory.

Task 10

#include <stdio.h>

char *local_str( ) {

return 0;

}

int main() {

printf("local_str = %p\n", local_str );
return 0;

}

RESULTS:

Intel x86 >> local_str = 00401000

PPC x64 >> local_str = 0x1000046c

OBSERVATIONS:

Intel x86 and PPC x64 >> I'm tired of making these observations now! OK...final one! The functions live in the start / initial part of the memory.