-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrpc.s
More file actions
181 lines (168 loc) · 2.83 KB
/
Copy pathrpc.s
File metadata and controls
181 lines (168 loc) · 2.83 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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
.section .rodata
op_add:
.asciz "+"
op_sub:
.asciz "-"
op_mul:
.asciz "*"
op_div:
.asciz "/"
print_result_str:
.asciz "%ld\n"
reduction_error_operand_str:
.asciz "%s: reduction error: too many operands\n"
reduction_error_operator_str:
.asciz "%s: reduction error: too many operators\n"
parse_error_str:
.asciz "%s: invalid token: %s\n"
.section .bss
endptr:
.space 8
progname:
.space 8
.section .text
.extern fprintf
.extern strtol
.extern strcmp
.extern stderr
.extern stdout
.globl main
.type main, @function
main:
push %rbp
movq %rsp, %rbp
cmp $1, %rdi
jle end
movq (%rsi), %r8
movq %r8, progname
movq %rsp, %r15
/*loads the next address of rsi,
specificallly argv[1] first since argv[0] is the the prog name*/
parsing_loop:
leaq 8(%rsi), %rsi
movq (%rsi), %r12
test %r12, %r12
je print_result
/*Tests if argv[1] equals 0, if yes go to print result*/
add_cmp:
push %rsi
movq %r12, %rsi
leaq op_add(%rip), %rdi
movq %rsp, %r13
andq $-16, %rsp
call strcmp
movq %r13, %rsp
pop %rsi
test %eax, %eax
jne sub_cmp
cmpq %r15, %rsp
ja reduction_error_operator
pop %r8
addq %r8, (%rsp)
jmp parsing_loop
sub_cmp:
push %rsi
movq %r12, %rsi
leaq op_sub(%rip), %rdi
movq %rsp, %r13
andq $-16, %rsp
call strcmp
movq %r13, %rsp
pop %rsi
test %eax, %eax
jne mul_cmp
cmpq %r15, %rsp
ja reduction_error_operator
pop %r8
subq %r8, (%rsp)
jmp parsing_loop
mul_cmp:
push %rsi
movq %r12, %rsi
leaq op_mul(%rip), %rdi
movq %rsp, %r13
andq $-16, %rsp
call strcmp
movq %r13, %rsp
pop %rsi
test %eax, %eax
jne div_cmp
cmpq %r15, %rsp
ja reduction_error_operator
pop %r8
pop %rax
imul %r8, %rax
push %rax
jmp parsing_loop
div_cmp:
push %rsi
movq %r12, %rsi
leaq op_div(%rip), %rdi
movq %rsp, %r13
andq $-16, %rsp
call strcmp
movq %r13, %rsp
pop %rsi
test %eax, %eax
jne num_convert
cmpq %r15, %rsp
ja reduction_error_operator
pop %r8
pop %rax
cqo
idivq %r8
push %rax
jmp parsing_loop
num_convert:
push %rsi
movq %r12, %rdi
leaq endptr(%rip), %rsi
xor %rdx, %rdx
movq %rsp, %r13
andq $-16, %rsp
call strtol
movq %r13, %rsp
pop %rsi
movq endptr(%rip), %r8
cmp %r8, %r12
je parse_err
cmpb $0, (%r8)
jne parse_err
push %rax
jmp parsing_loop
print_result:
leaq 8(%r15), %r15
cmp %r15, %rsp
ja reduction_error_operator
movq stdout(%rip), %rdi
leaq print_result_str(%rip), %rsi
pop %rdx
xor %rax, %rax
call fprintf
jmp end
parse_err:
movq progname(%rip), %rdi
leaq parse_error_str(%rip), %rsi
movq %r12, %rdx
xor %rax, %rax
call fprintf
movl $1, %eax
jmp end
reduction_error_operand:
movq progname(%rip), %rdi
leaq reduction_error_operand_str(%rip), %rsi
xor %rax, %rax
call fprintf
movl $1, %eax
jmp end
reduction_error_operator:
movq progname(%rip), %rdi
leaq reduction_error_operator_str(%rip), %rsi
xor %rax, %rax
call fprintf
movl $1, %eax
jmp end
end:
movq %rbp, %rsp
pop %rbp
ret