Blob


1 .section .rodata
2 fmt: .asciz "Hello World\n"
3 ifmt: .asciz "%d\n"
5 .section .data
6 arr: .dword 1, 2, 3, 4, 5
8 .section .text
9 .global main
10 main:
11 addi sp, sp, -16
12 sd s0, 8(sp)
13 sd ra, 0(sp)
15 jal hello
17 jal printarr
18 jal nl
20 la a0, arr
21 li a1, 1
22 jal swap
24 jal printarr
26 jal nl
28 la a0, arr
29 li a1, 0
30 jal avg
31 mv s0, a0
32 jal print
34 mv a0, s0
35 jal even
36 jal print
38 mv a0, zero
39 ld ra, 0(sp)
40 ld s0, 8(sp)
41 addi sp, sp, 16
42 ret
44 // void hello (void);
45 hello:
46 la a0, fmt
47 j printf
49 // void nl (void);
50 nl:
51 li a0, 10
52 j putchar
54 // void print (int);
55 print:
56 addi sp, sp, -8
57 sd ra, 0(sp)
59 mv a1, a0
60 la a0, ifmt
61 jal printf
63 ld ra, 0(sp)
64 addi sp, sp, 8
65 ret
67 // void printarr (void);
68 printarr:
69 addi sp, sp, -32
70 sd ra, 16(sp)
71 sd s0, 8(sp)
72 sd s1, 0(sp)
74 la s0, arr
75 mv s1, zero
77 ploop:
78 add a0, s0, s1
79 ld a0, 0(a0)
80 jal print
82 addi s1, s1, 8
83 li t0, (5 * 8)
84 bne s1, t0, ploop
87 ld s1, 0(sp)
88 ld s0, 8(sp)
89 ld ra, 16(sp)
90 addi sp, sp, 32
91 ret
93 // void swap (long *v, size_t n);
94 swap:
95 slli a1, a1, 3
96 add t0, a0, a1
97 ld t1, 0(t0)
98 ld t2, 8(t0)
99 sd t2, 0(t0)
100 sd t1, 8(t0)
101 ret
103 // long avg(long *v, size_t n);
104 avg:
105 slli a1, a1, 3
106 add a0, a0, a1
107 ld t0, 0(a0)
108 ld t1, 8(a0)
109 add a0, t0, t1
110 srli a0, a0, 1
111 ret
113 // bool even(long);
114 even:
115 andi a0, a0, 1
116 xor a0, a0, 1
117 ret