Big-Endian on Little-Endian
In this post, I will share how I set up big-endian emulation on my little-endian Intel machine to tets a program for byte order related issues. I used the QEMU PowerPC emulator to set up the big-endian emulation. The steps to do so are documented in the list below.
-
Install QEMU.
apt-get update && apt-get install qemu
-
Download
mol-0.9.72.1.tar.bz2
from http://sourceforge.net/projects/mac-on-linux/files/ and copy the file namedvideo.x
from the downloaded tarball to/usr/share/qemu/
. This is necessary to preventqemu-system-ppc
from complaining about it.wget https://sourceforge.net/projects/mac-on-linux/files/mac-on-linux/mol-0.9.72.1/mol-0.9.72.1.tar.bz2 tar -xjf mol-0.9.72.1.tar.bz2 sudo cp mol-0.9.72.1/mollib/drivers/video.x /usr/share/qemu/
-
Create a QEMU hard disk image.
qemu-img create powerpc.img 2G
-
Download Debian for PowerPC and install it on the QEMU hard disk image.
wget http://cdimage.debian.org/debian-cd/5.0.4/powerpc/iso-cd/debian-504-powerpc-CD-1.iso qemu-system-ppc -m 512 -boot d -hda powerpc.img -cdrom debian-504-powerpc-CD-1.iso
-
Boot the QEMU PowerPC emulator with the new hard disk image.
qemu-system-ppc -m 512 -hda powerpc.img
-
Write a small program inside the new Debian system, say,
endian.c
like this:#include <stdio.h> int main() { int n = 1; printf(*((char *) &n) ? "little-endian\n" : "big-endian\n"); return 0; }
-
Compile and execute the C program.
$ gcc endian.c && ./a.out big-endian