-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathREAL_reversepol.s
More file actions
166 lines (126 loc) · 3.11 KB
/
Copy pathREAL_reversepol.s
File metadata and controls
166 lines (126 loc) · 3.11 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
/*Output*/
.section .data
output_str: .string
/*Operator strings & err messages*/
.section .rodata
add_str: .asciz "+"
sub_str: .asciz "-"
mul_str: .asciz "*"
div_str: .asciz "/"
parse_error_str: .string "%s: invalid token: `s'\n"
red_error_opa_str: .string "%s: not enough operands"
red_error_opt_str: .string "%s: not enough operators"
/*endptr memory allocation*/
.section .bss
endptr
.space 8 /*reserves 8 bytes for uninitialized endptr*/
.globl main
.type main, @function
.extern strtol
.extern fprintf
.extern printf
.extern strcmp
main:
/*NOTE: PROGRAM IS CURRENTLY UNFINISHED*/
/*rdi = argc
rsi = argv
rdx = envp*/
/*r12 = callee-saved reg that -> current token
r13 = callee-saved reg that stores rsp*/
parsing_loop:
leaq 8(%rsi), %rsi /*loads the next address of rsi*/
movq (%rsi), %r12 /*loads the arg array to r12*/
test %r12, %r12
je print_result /*checks for a null token, meaning it's done*/
add_cmp:
push %rsi /*saves rsi*/
movq %r12, %rsi /*moves token ptr to rsi for strcmp*/
leaq add_str(%rip), %rdi /*loads "+" into rdi*/
movq %rsp, %r13
andq $-16, %rsp /*aligns stack by 16 by clearing 4 lower bytes*/
call strcmp /*even though strcmp doesn't need alignment*/
movq %r13, %rsp /*restores stack*/
pop %rsi
test %eax, %eax /*we want eax to be 0*/
jne sub_cmp
pop %r8
addq %r8, (%rsp)
jmp parsing_loop
sub_cmp:
push %rsi
movq %r12, %rsi
leaq sub_str(%rip), %rdi
movq %rsp, %r13
andq $-16, %rsp /*once again realigns the stack to call strcmp*/
call strcmp
movq %r13, %rsp /*restores stack*/
pop %rsi
test %eax, %eax
jne mul_cmp
pop %r8 /*Does sub operation after comparing string*/
sub %r8, (%rsp)
jmp parsing_loop
mul_cmp: /*sidenote: imul always goes into rax/rdx*/
push %rsi
movq %r12, %rsi
leaq mul_str(%rip), %rdi
movq %rsp, %r13
andq $-16, %rsp /*aligns stack*/
call strcmp
movq %r13, %rsp
pop %rsi
test %eax, %eax
jne div_cmp
pop %r8 /*Does mul operation after comparing string*/
pop %rax
imulq %r8
push %rax
jmp parsing_loop
div_cmp:
push %rsi
movq %r12, %rsi
leaq div_str(%rip), %rdi
movq %rsp, %r13
andq $-16, %rsp /*aligns stack*/
call strcmp
movq %r13, %rsp
pop %rsi
test %eax, %eax
jne num_convert
pop %r8
pop %rax
cqo /*extends rax to rdx:rax*/
idivq %r8 /*Divides rax by r8, result in rax*/
jmp parsing_loop
/*UNFINISHED*/
num_convert:
movq %r12, %rdi
leaq endptr(%rip), %rsi
xor %rdx, %rdx
call strtol /*strtol(s, &endptr, 0) has an endptr arg that
will point to any letters. Otherwise points to
nullbyte.
If endptr = s, nothing
If endptr -> null, good
Else, BAD */
movq endptr(%rip), %r8
cmp %r8, %r12 /*Checks if no chars were converted to longs*/
/*change later, i guess?*/
je parse_error
cmpb $0, (%r8) /*Checks if chars only partially converted*/
jne parse_error
print_result:
ret
parse_error:
/*Possibly finished*/
leaq parse_error_str(%rip), %rdi /*format str*/
movq %r15, %rsi /*first arg: argv[0] prog name*/
movq %r12, %rdx /*second arg: invalid token*/
movq %rsp, %r13
andq $-16, %rsp
call printf
movq %r13, %rsp
mov $1, %eax
leave
ret
exit_0: