root / tests / hello-mips.c @ 36bb244b
History | View | Annotate | Download (1.6 kB)
1 | e4630047 | ths | /*
|
---|---|---|---|
2 | e4630047 | ths | * MIPS o32 Linux syscall example
|
3 | e4630047 | ths | *
|
4 | e4630047 | ths | * http://www.linux-mips.org/wiki/RISC/os
|
5 | e4630047 | ths | * http://www.linux-mips.org/wiki/MIPSABIHistory
|
6 | e4630047 | ths | * http://www.linux.com/howtos/Assembly-HOWTO/mips.shtml
|
7 | e4630047 | ths | *
|
8 | e4630047 | ths | * mipsel-linux-gcc -nostdlib -mno-abicalls -fno-PIC -mabi=32 \
|
9 | e4630047 | ths | * -O2 -static -o hello-mips hello-mips.c
|
10 | e4630047 | ths | *
|
11 | e4630047 | ths | */
|
12 | e4630047 | ths | #define __NR_SYSCALL_BASE 4000 |
13 | e4630047 | ths | #define __NR_exit (__NR_SYSCALL_BASE+ 1) |
14 | e4630047 | ths | #define __NR_write (__NR_SYSCALL_BASE+ 4) |
15 | e4630047 | ths | |
16 | e4630047 | ths | static inline void exit1(int status) |
17 | e4630047 | ths | { |
18 | e4630047 | ths | register unsigned long __a0 asm("$4") = (unsigned long) status; |
19 | e4630047 | ths | |
20 | e4630047 | ths | __asm__ __volatile__ ( |
21 | e4630047 | ths | " .set push \n"
|
22 | e4630047 | ths | " .set noreorder \n"
|
23 | e4630047 | ths | " li $2, %0 \n"
|
24 | e4630047 | ths | " syscall \n"
|
25 | e4630047 | ths | " .set pop "
|
26 | e4630047 | ths | : |
27 | e4630047 | ths | : "i" (__NR_exit), "r" (__a0) |
28 | e4630047 | ths | : "$2", "$8", "$9", "$10", "$11", "$12", "$13", "$14", "$15", "$24", |
29 | e4630047 | ths | "memory");
|
30 | e4630047 | ths | } |
31 | e4630047 | ths | |
32 | e4630047 | ths | static inline int write(int fd, const char *buf, int len) |
33 | e4630047 | ths | { |
34 | e4630047 | ths | register unsigned long __a0 asm("$4") = (unsigned long) fd; |
35 | e4630047 | ths | register unsigned long __a1 asm("$5") = (unsigned long) buf; |
36 | e4630047 | ths | register unsigned long __a2 asm("$6") = (unsigned long) len; |
37 | e4630047 | ths | register unsigned long __a3 asm("$7"); |
38 | e4630047 | ths | unsigned long __v0; |
39 | e4630047 | ths | |
40 | e4630047 | ths | __asm__ __volatile__ ( |
41 | e4630047 | ths | " .set push \n"
|
42 | e4630047 | ths | " .set noreorder \n"
|
43 | e4630047 | ths | " li $2, %2 \n"
|
44 | e4630047 | ths | " syscall \n"
|
45 | e4630047 | ths | " move %0, $2 \n"
|
46 | e4630047 | ths | " .set pop "
|
47 | e4630047 | ths | : "=r" (__v0), "=r" (__a3) |
48 | e4630047 | ths | : "i" (__NR_write), "r" (__a0), "r" (__a1), "r" (__a2) |
49 | e4630047 | ths | : "$2", "$8", "$9", "$10", "$11", "$12", "$13", "$14", "$15", "$24", |
50 | e4630047 | ths | "memory");
|
51 | e4630047 | ths | |
52 | e4630047 | ths | /* if (__a3 == 0) */
|
53 | e4630047 | ths | return (int) __v0; |
54 | e4630047 | ths | /*
|
55 | e4630047 | ths | errno = __v0;
|
56 | e4630047 | ths | return -1;
|
57 | e4630047 | ths | */
|
58 | e4630047 | ths | } |
59 | e4630047 | ths | |
60 | e4630047 | ths | void __start(void) |
61 | e4630047 | ths | { |
62 | e4630047 | ths | write (1, "Hello, World!\n", 14); |
63 | e4630047 | ths | exit1 (42);
|
64 | e4630047 | ths | } |