1
2 #include <stdio.h>
3
4 typedef unsigned int UInt;
5 typedef unsigned short UShort;
6
read16le(void * a)7 UInt read16le ( void* a )
8 {
9 UInt res;
10 __asm volatile(
11 " lhbrx %0,0,%1 \n" // Get half word and reverse the bytes
12 : "=b" (res) // %0 - Output operand
13 : "b" (a) // %1 - Input operand
14 : "memory" // Consider memory clobberred for aliasing
15 );
16 return res;
17 }
18
read16be(void * a)19 UInt read16be ( void* a )
20 {
21 UInt res;
22 __asm volatile(
23 " lhzx %0,0,%1 \n" // Get half word and reverse the bytes
24 : "=b" (res) // %0 - Output operand
25 : "b" (a) // %1 - Input operand
26 : "memory" // Consider memory clobberred for aliasing
27 );
28 return res;
29 }
30
read32le(void * a)31 UInt read32le ( void* a )
32 {
33 UInt res;
34 __asm volatile(
35 " lwbrx %0,0,%1 \n" // Get half word and reverse the bytes
36 : "=b" (res) // %0 - Output operand
37 : "b" (a) // %1 - Input operand
38 : "memory" // Consider memory clobberred for aliasing
39 );
40 return res;
41 }
42
read32be(void * a)43 UInt read32be ( void* a )
44 {
45 UInt res;
46 __asm volatile(
47 " lwzx %0,0,%1 \n" // Get half word and reverse the bytes
48 : "=b" (res) // %0 - Output operand
49 : "b" (a) // %1 - Input operand
50 : "memory" // Consider memory clobberred for aliasing
51 );
52 return res;
53 }
54
write16be(void * a,UInt data)55 void write16be ( void* a, UInt data )
56 {
57 __asm volatile(
58 " sthx %0,0,%1\n"
59 :
60 : "b" (data), "b" (a)
61 : "memory"
62 );
63 }
64
write16le(void * a,UInt data)65 void write16le ( void* a, UInt data )
66 {
67 __asm volatile(
68 " sthbrx %0,0,%1\n"
69 :
70 : "b" (data), "b" (a)
71 : "memory"
72 );
73 }
74
write32be(void * a,UInt data)75 void write32be ( void* a, UInt data )
76 {
77 __asm volatile(
78 " stwx %0,0,%1\n"
79 :
80 : "b" (data), "b" (a)
81 : "memory"
82 );
83 }
84
write32le(void * a,UInt data)85 void write32le ( void* a, UInt data )
86 {
87 __asm volatile(
88 " stwbrx %0,0,%1\n"
89 :
90 : "b" (data), "b" (a)
91 : "memory"
92 );
93 }
94
main(void)95 int main ( void )
96 {
97 UInt foo = 0x12345678;
98 printf("ld be16 0x%08x\n", read16be( &foo ));
99 printf("ld le16 0x%08x\n", read16le( &foo ));
100 printf("ld be32 0x%08x\n", read32be( &foo ));
101 printf("ld le32 0x%08x\n", read32le( &foo ));
102
103 foo = 0x12345678; write16be( &foo, 0xABCD );
104 printf("st be16 0x%08x\n", foo);
105
106 foo = 0x12345678; write16le( &foo, 0xABCD );
107 printf("st le16 0x%08x\n", foo);
108
109 foo = 0x12345678; write32be( &foo, 0xABCD1425 );
110 printf("st be32 0x%08x\n", foo);
111
112 foo = 0x12345678; write32le( &foo, 0xABCD1425 );
113 printf("st le32 0x%08x\n", foo);
114
115 return 0;
116 }
117