10 May 2007, 23:19 | #1 |
Dazed and Confused
Join Date: Dec 2001
Location: portsmouth/uk
Posts: 242
|
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 22:22. |
11 May 2007, 02:07 | #2 |
Ya' like it Retr0?
Join Date: Jul 2005
Location: United Kingdom
Age: 49
Posts: 9,768
|
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(); 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(); anyway i hope it helps. |
11 May 2007, 03:17 | #3 |
Citizen of Elthesh
Join Date: Sep 2003
Location: 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 03:30. |
11 May 2007, 16:50 | #4 |
Dazed and Confused
Join Date: Dec 2001
Location: portsmouth/uk
Posts: 242
|
I'm glad I don't program in assembler on PCs. It must be a nightmare.
|
11 May 2007, 17:09 | #5 |
Citizen of Elthesh
Join Date: Sep 2003
Location: 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.
|
11 May 2007, 18:10 | #6 | |
Registered User
Join Date: Aug 2004
Location: Vienna / Austria
Age: 44
Posts: 257
|
Quote:
x86 Assembler is a real nightmare tough, but for other reasons (lack of GPRs, nonorthogonal ISA ). |
|
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 17:26 |
An idea for continued games development... using Amiga | Galahad/FLT | Amiga scene | 91 | 29 December 2010 11:45 |
C code prob | Jherek Carnelia | Coders. General | 22 | 08 May 2007 22:28 |
0 bytes ADF Files | Bueller | support.WinUAE | 2 | 30 September 2005 13:50 |
The continued rape of the common man | Fred the Fop | Retrogaming General Discussion | 14 | 30 September 2002 15:52 |
|
|