-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathto-upper-error-logging.s
More file actions
247 lines (193 loc) · 5.49 KB
/
Copy pathto-upper-error-logging.s
File metadata and controls
247 lines (193 loc) · 5.49 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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
.section .data
.equ SYS_OPEN, 5
.equ SYS_WRITE, 4
.equ SYS_READ, 3
.equ SYS_CLOSE, 6
.equ SYS_EXIT, 1
.equ O_RDONLY, 0
.equ O_CREAT_WRONLY_TRUNC, 03101
.equ STDIN, 0
.equ STDOUT, 1
.equ STDERR, 2
.equ LINUX_SYSCALL, 0x80
.equ END_OF_FILE, 0
.equ NUMBER_ARGUMENTS, 2
# This will be the error string printed to STDERR.
error_message:
.asciz "An error occured..."
# Size of error message
.equ error_message_size, 19
.section .bss
.equ BUFFER_SIZE, 500
.lcomm BUFFER_DATA, BUFFER_SIZE
.section .text
.equ ST_SIZE_RESERVE, 8
.equ ST_FD_IN, -4
.equ ST_FD_OUT, -8
.equ ST_ARGC, 0
.equ ST_ARGV_0, 4
.equ ST_ARGV_1, 8
.equ ST_ARGV_2, 12
.globl _start
_start:
movl %esp, %ebp
subl $ST_SIZE_RESERVE, %esp
open_files:
open_fd_in:
movl $SYS_OPEN, %eax
movl ST_ARGV_1(%ebp), %ebx
movl $O_RDONLY, %ecx
movl $0666, %edx
int $LINUX_SYSCALL
error_check_first_open_file:
# If %eax is a negative number, there was an error.
cmpl $0, %eax
jle open_first_file_error
# Otherwise continue with the program.
jge store_fd_in
# %eax was negative, write to STDERR
open_first_file_error:
# Open System Call (4) moved into %eax
movl $SYS_WRITE, %eax
# Move the STDERR file descriptor into the %ebx register
movl $STDERR, %ebx
# Move the address of the first character in the error_message string into the %ecx register
movl $error_message, %ecx
# move the size of the string (19 bytes) into the %edx register.
movl $error_message_size, %edx
# Transfer control to Linux
int $LINUX_SYSCALL
# If there was an error opening the first file, end the program.
jmp end_program
store_fd_in:
movl %eax, ST_FD_IN(%ebp)
open_fd_out:
movl $SYS_OPEN, %eax
movl ST_ARGV_2(%ebp), %ebx
movl $O_CREAT_WRONLY_TRUNC, %ecx
movl $0666, %edx
int $LINUX_SYSCALL
error_check_second_open_file:
# If %eax is a negative number, there was an error.
cmpl $0, %eax
jle open_second_file_error
# Otherwise continue with the program.
jmp store_fd_out
open_second_file_error:
# Open System Call (4) moved into %eax
movl $SYS_WRITE, %eax
# Move the STDERR file descriptor into the %ebx register
movl $STDERR, %ebx
# Move the address of the first character in the error_message string into the %ecx register
movl $error_message, %ecx
# move the size of the string (19 bytes) into the %edx register.
movl $error_message_size, %edx
# Transfer control to Linux
int $LINUX_SYSCALL
# If there was an error opening the second file, close the first file and end the program.
movl $SYS_CLOSE, %eax
movl ST_FD_IN(%ebp), %ebx
int $LINUX_SYSCALL
jmp end_program
store_fd_out:
movl %eax, ST_FD_OUT(%ebp)
read_loop_begin:
movl $SYS_READ, %eax
movl ST_FD_IN(%ebp), %ebx
movl $BUFFER_DATA, %ecx
movl $BUFFER_SIZE, %edx
int $LINUX_SYSCALL
error_check_read_call:
# If %eax is a negative number, there was an error.
cmpl $0, %eax
jle read_error
# Otherwise continue with program
jmp read_loop_begin_continue
read_error:
# Open System Call (4) moved into %eax
movl $SYS_WRITE, %eax
# Move the STDERR file descriptor into the %ebx register
movl $STDERR, %ebx
# Move the address of the first character in the error_message string into the %ecx register
movl $error_message, %ecx
# move the size of the string (19 bytes) into the %edx register.
movl $error_message_size, %edx
# Transfer control to Linux
int $LINUX_SYSCALL
# If there was an error reading the file, close both files and end the program.
jmp end_loop
read_loop_begin_continue:
cmpl $END_OF_FILE, %eax
jle end_loop
continue_read_loop:
pushl $BUFFER_DATA
pushl %eax
call convert_to_upper
popl %eax
addl $4, %esp
movl %eax, %edx
movl $SYS_WRITE, %eax
movl ST_FD_OUT(%ebp), %ebx
movl $BUFFER_DATA, %ecx
int $LINUX_SYSCALL
error_check_write_call:
# If %eax is a negative number, there was an error.
cmpl $0, %eax
jle write_error
# Otherwise continue with the program.
jmp continue_read_loop_end
write_error:
# Open System Call (4) moved into %eax
movl $SYS_WRITE, %eax
# Move the STDERR file descriptor into the %ebx register
movl $STDERR, %ebx
# Move the address of the first character in the error_message string into the %ecx register
movl $error_message, %ecx
# move the size of the string (19 bytes) into the %edx register.
movl $error_message_size, %edx
# Transfer control to Linux
int $LINUX_SYSCALL
# If there was an error writing the file, close both files and end the program.
jmp end_loop
continue_read_loop_end:
jmp read_loop_begin
end_loop:
movl $SYS_CLOSE, %eax
movl ST_FD_OUT(%ebp), %ebx
int $LINUX_SYSCALL
movl $SYS_CLOSE, %eax
movl ST_FD_IN(%ebp), %ebx
int $LINUX_SYSCALL
end_program:
movl $SYS_EXIT, %eax
movl $0, %ebx
int $LINUX_SYSCALL
.equ LOWERCASE_A, 'a'
.equ LOWERCASE_Z, 'z'
.equ UPPER_CONVERSION, 'A' - 'a'
.equ ST_BUFFER_LEN, 8
.equ ST_BUFFER, 12
convert_to_upper:
pushl %ebp
movl %esp, %ebp
movl ST_BUFFER(%ebp), %eax
movl ST_BUFFER_LEN(%ebp), %ebx
movl $0, %edi
cmpl $0, %ebx
je end_convert_loop
convert_loop:
movb (%eax,%edi,1), %cl
cmpb $LOWERCASE_A, %cl
jl next_byte
cmpb $LOWERCASE_Z, %cl
jg next_byte
addb $UPPER_CONVERSION, %cl
movb %cl, (%eax,%edi,1)
next_byte:
incl %edi
cmpl %edi, %ebx
jne convert_loop
end_convert_loop:
movl %ebp, %esp
popl %ebp
ret