English Amiga Board


Go Back   English Amiga Board > Coders > Coders. General

 
 
Thread Tools
Old 11 May 2007, 00:19   #1
Jherek Carnelia
Dazed and Confused
Jherek Carnelia's Avatar
 
Join Date: Dec 2001
Location: portsmouth/uk
Posts: 227
C code prob continued - How a PC stores bytes?

Following on from an earlier post of mine (C code prob), I have to admit that I am baffled by the results returned by a small prog I have written.

I save a memory dump from Action Replay 3 (via WinUAE) to a file.

For instance, the values are - $aabb $ccdd $eeff $1122.

This is read into a PC (not an Amiga, where all is fine). Because of the differences between a PC and an Amiga, a check is done to determine whether or not the host is a PC or an Amiga.

If the host is a PC, the bytes are swapped, giving

$bbaa $ddcc $ffee $2211

Then I print the contents of the filebuffer as unsigned integers which gives

$bbaa $ddcc $ffee $2211 - so far, so good.

BUT, when I print the contents as bytes, I get -

$aa $bb $cc $dd $ee $ff $11 $22

Which is the original order before they were swapped.!

Got me stumped, I have to say!

Anyone know what's going on here? (the files are attached for those that care to try it)

Last edited by Jherek Carnelia; 09 April 2011 at 23:22.
Jherek Carnelia is offline  
AdSense AdSense  
Old 11 May 2007, 03:07   #2
Zetr0
Ya' like it Retr0?
Zetr0's Avatar
 
Join Date: Jul 2005
Location: United Kingdom
Age: 43
Posts: 9,769
looks to me the problem is in the print statements

Original code
Code:
	/* print the bytelist as short integers*/
	for(i = 0; i<BUFSIZE; i++) 
	{
		printf("$%04x ", *(buffer+i));  
        }
	printf("\n");
		
	/* print the bytelist as bytes */
	v_buffer = (unsigned char*)buffer;
	for(x = 0; x < 4; x++)
                printf("*(v_buffer+%d) = $%02x\n", x, *(v_buffer+x));

	getchar();
Try
Code:
	/* print the bytelist as short integers*/
	for(i = 0; i<BUFSIZE; i++) 
	{
		printf("$%04x ", buffer[ i ] );  
        }
	printf("\n");
		
	/* print the bytelist as bytes */

	for(x = 0; x < 4; x++)
                printf("bytelist byte = $%02x\n", buffer[ x ]);

	getchar();
I am unsure why you are using pointer references for print statements, its strange that I learned C when i was 16 coded it for the longest time, only to have used c++ for the last 6/7 years so its most likely that i forgot half of what i need to know LOL..

anyway i hope it helps.
Zetr0 is offline  
Old 11 May 2007, 04:17   #3
eLowar
Citizen of Elthesh
eLowar's Avatar
 
Join Date: Sep 2003
Location: London, UK
Posts: 949
*(buffer+i) and buffer[i] are identical. Adding i (in pointer arithmetic) and dereferencing is exactly what operator[] does on pointers.

As for your output, keep in mind that little endian means "increasing numeric significance with increasing memory addresses" (thanks Wikipedia for a concise description). When you print whole words, you get the most numerically significant byte on the left (as humans write numbers), while when you print bytes by address you get it on the right (higher numeric significance means higher address).

It's late so I might not be thinking right, but I think that's it.

Edit: I just scrolled down a bit in the Wikipedia article that I stole the definition above from and it actually shows the same effect in the example for little endian storage. So there you go.

Last edited by eLowar; 11 May 2007 at 04:30.
eLowar is offline  
Old 11 May 2007, 17:50   #4
Jherek Carnelia
Dazed and Confused
Jherek Carnelia's Avatar
 
Join Date: Dec 2001
Location: portsmouth/uk
Posts: 227
I'm glad I don't program in assembler on PCs. It must be a nightmare.
Jherek Carnelia is offline  
Old 11 May 2007, 18:09   #5
eLowar
Citizen of Elthesh
eLowar's Avatar
 
Join Date: Sep 2003
Location: London, UK
Posts: 949
I don't know. Higher address, higher value is pretty straightforward, isn't it? I mean it's not how you write numbers, but it means the exponent grows with the address offset.
eLowar is offline  
Old 11 May 2007, 19:10   #6
Npl
Registered User
 
Join Date: Aug 2004
Location: Vienna / Austria
Age: 38
Posts: 257
Quote:
Originally Posted by Jherek Carnelia
I'm glad I don't program in assembler on PCs. It must be a nightmare.
you typically dont have to care about endianess most of the times. Even if, its not really harder to deal with assembler than other languages.

x86 Assembler is a real nightmare tough, but for other reasons (lack of GPRs, nonorthogonal ISA ).
Npl is offline  
AdSense AdSense  
 


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Similar Threads
Thread Thread Starter Forum Replies Last Post
Keyrah V2 available in stores from may 20th Schoenfeld News 24 25 July 2013 18:26
An idea for continued games development... using Amiga Galahad/FLT Amiga scene 91 29 December 2010 12:45
C code prob Jherek Carnelia Coders. General 22 08 May 2007 23:28
0 bytes ADF Files Bueller support.WinUAE 2 30 September 2005 14:50
The continued rape of the common man Fred the Fop Retrogaming General Discussion 14 30 September 2002 16:52

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +2. The time now is 05:46.


Powered by vBulletin® Version 3.8.8 Beta 1
Copyright ©2000 - 2017, vBulletin Solutions, Inc.
Page generated in 0.22986 seconds with 14 queries