-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.s
More file actions
167 lines (133 loc) · 2.37 KB
/
Copy pathmain.s
File metadata and controls
167 lines (133 loc) · 2.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
.file "main.c"
.text
.globl main
.type main, @function
.extern printf
main:
pushq %rbp
movq %rsp, %rbp
call print_hello
# use r15 for counter
xorq %r15, %r15
# run to 15
movq $100, %r12
loop:
inc %r15
movq %r15, %r11
# compare mod 3
testthree:
xor %rdx, %rdx
movq %r15, %rax
movq $0x3, %r13
div %r13
movq %rdx, %r14
cmpq $0, %rdx
jne testfive
call print_fizz
testfive:
xor %rdx, %rdx
movq %r15, %rax
movq $0x5, %r13
div %r13
cmpq $0, %rdx
jne failed
call print_buzz
call print_nl
jmp end_loop
failed:
cmpq $0, %r14
jne neither
call print_nl
jmp end_loop
neither:
movq %r15, %r11
call print_number
end_loop:
cmp %r12, %r15
jl loop
popq %rbp
ret
.size main, .-main
print_hello:
pushq %rbp
movq %rsp, %rbp
# hello print
movq $1, %rax
movq $1, %rdi
movq $hello_msg, %rsi
movq $hello_len, %rdx
syscall
popq %rbp
ret
.size print_hello, .-print_hello
.globl main
.type main, @function
print_fizz:
pushq %rbp
movq %rsp, %rbp
# hello print
movq $1, %rax
movq $1, %rdi
movq $fizz_msg, %rsi
movq $fizz_len, %rdx
syscall
popq %rbp
ret
.size print_fizz, . - print_fizz
.globl main
.type main, @function
print_buzz:
pushq %rbp
movq %rsp, %rbp
# hello print
movq $1, %rax
movq $1, %rdi
movq $buzz_msg, %rsi
movq $buzz_len, %rdx
syscall
popq %rbp
ret
.size print_buzz, . - print_buzz
.globl main
.type main, @function
print_nl:
pushq %rbp
movq %rsp, %rbp
# hello print
movq $1, %rax
movq $1, %rdi
movq $nl_msg, %rsi
movq $nl_len, %rdx
syscall
popq %rbp
ret
.size print_nl, . - print_nl
.globl main
.type main, @function
print_number:
pushq %rbp
movq %rsp, %rbp
# hello print
movq %r11, %rsi
movq $number_format, %rdi
call printf
popq %rbp
ret
.size print_number, . - print_number
.globl main
.type main, @function
.data
hello_msg:
.ascii "Hello Assembler\n"
hello_len = . - hello_msg
fizz_msg:
.ascii "Fizz"
fizz_len = . - fizz_msg
buzz_msg:
.ascii "Buzz"
buzz_len = . - buzz_msg
nl_msg:
.ascii "\n"
nl_len = . - nl_msg
number_format:
.asciz "%d\n"