Reputation: 1
Here is the code (written in assebly at&t syntax):
.section .data
.equ N, 1024
v: .skip N * 4
fin: .string "input.txt"
fout: .string "output.txt"
descriptor: .long 0
sz: .long 0
n: .long 0
Q: .long 0
x: .long 0
y: .long 0
p: .long 0
q: .long 0
format_int: .string "%d"
format_output: .string "fd:(%d,%d)\n"
format_failure: .string "fd:(0,0)\n"
type: .long 0
buffer: .space 256
.section .text
.global _start
_start:
movl $5, %eax
movl $fin, %ebx
movl $0, %ecx
movl $0, %edx
int $0x80
movl %eax, %edi
movl $5, %eax
movl $fout, %ebx
movl $577, %ecx
movl $438, %edx
int $0x80
movl %eax, %esi
push %edi # main.S:37: Error: operand type mismatch for `push'
lea buffer, %eax
push %eax # main.S:39: Error: operand type mismatch for `push'
movl $3, %eax
movl $4, %edx
int $0x80
movl %eax, Q
.loop:
cmpl $0, Q
jle .end
push %edi # main.S:50: Error: operand type mismatch for `push'
push $buffer
movl $3, %eax
movl $4, %edx
int $0x80
movl %eax, type
cmpl $1, type
je .add
cmpl $2, type
je .get
cmpl $3, type
je .delete
cmpl $4, type
je .defragmentation
.add:
call ADD
jmp .decrement
.get:
push descriptor
push %edi # main.S:72: Error: operand type mismatch for `push
push $buffer
movl $3, %eax
movl $4, %edx
int $0x80
call GET
jmp .decrement
.delete:
call DELETE
jmp .decrement
.defragmentation:
call DEFRAGMENTATION
jmp .decrement
.decrement:
subl $1, Q
jmp .loop
.end:
movl $6, %eax
movl %edi, %ebx
int $0x80
movl $6, %eax
movl %esi, %ebx
int $0x80
movl $1, %eax
xor %ebx, %ebx
int $0x80
ADD:
push %edi # main.S:106: Error: operand type mismatch for `push'
push n
push $buffer
movl $3, %eax
movl $4, %edx
int $0x80
movl %eax, n
.add_loop:
cmpl $0, n
jle .add_end
push %edi # main.S:118: Error: operand type mismatch for `push'
push descriptor
push sz
push $buffer
movl $3, %eax
movl $4, %edx
int $0x80
movl %eax, descriptor
movl %ebx, sz
movl sz, %eax
addl $7, %eax
shrl $3, %eax
movl %eax, sz
movl $1, ok
movl $0, %ecx
.inner_loop:
cmpl $N, sz
jge .found_space
movl $1, ok
movl $0, %ebx
.check_space:
cmpl sz, %ebx
jge .space_checked
cmpl $0, v(%ecx,%ebx, 4)
jne .not_available
incl %ebx
jmp .check_space
.not_available:
movl $0, ok
jmp .space_checked
.space_checked:
cmpl $1, ok
jne .next_space
movl $0, %ebx
.allocate_space:
cmpl sz, %ebx
jge .allocated
movl $descriptor, v(%ecx, %ebx, 4)
incl %ebx
jmp .allocate_space
.allocated:
push %edi # main.S:167: Error: operand type mismatch for `push'
push descriptor
push %ecx # main.S:169: Error: operand type mismatch for `push'
push sz
push format_output
movl $4, %eax
movl %esi, %ebx
movl $buffer, %ecx
movl $256, %edx
int $0x80
jmp .add_end
.next_space:
incl %ecx
jmp .inner_loop
.found_space:
push descriptor
push format_failure
movl $4, %eax
movl %esi, %ebx
movl $buffer, %ecx
movl $256, %edx
int $0x80
subl $1, n
jmp .add_loop
.add_end:
ret
min:
cmp %ebx, %eax
jle .min_return
mov %ebx, %eax
.min_return:
ret
max:
cmp %ebx, %eax
jge .max_return
mov %ebx, %eax
.max_return:
ret
GET:
movl $N, x
movl $0, y
movl $0, %ecx
.get_loop:
cmpl $N, %ecx
jge .get_end
movl v(,%ecx,4), %eax
cmpl descriptor, %eax
jne .next_get
movl %ecx, %eax
call min
movl %eax, x
movl %ecx, %eax
call max
movl %eax, y
.next_get:
incl %ecx
jmp .get_loop
.get_end:
cmpl $N, x
je .not_found
jmp .output_get
.not_found:
movl $0, x
.output_get:
push x
push y
push format_get
movl $4, %eax
movl $1, %ebx
movl $buffer, %ecx
movl $256, %edx
int $0x80
addl $16, %esp
ret
DELETE:
push descriptor
push format_int
push $buffer
movl $3, %eax
movl $4, %edx
int $0x80
movl %eax, descriptor
movl $0, %ecx
.delete_loop:
cmpl $N, %ecx
jge .delete_end
movl v(,%ecx,4), %eax
cmpl descriptor, %eax
jne .next_delete
movl $0, v(,%ecx,4)
.next_delete:
incl %ecx
jmp .delete_loop
.delete_end:
movl $0, %ecx
.output_delete:
cmpl $N, %ecx
jge .delete_output_end
cmpl $0, v(,%ecx,4)
je .next_output
push v(,%ecx,4)
push format_output
movl $4, %eax
movl $1, %ebx
movl $buffer, %ecx
movl $256, %edx
int $0x80
call GET
jmp .next_output
.next_output:
incl %ecx
jmp .output_delete
.delete_output_end:
ret
DEFRAGMENTATION:
movl p, %eax
movl q, %ebx
movl $0, %eax
movl $0, %ebx
.defrag_loop:
cmpl $N, %ebx
jge .defrag_end
cmpl $0, v(,%ebx, 4)
je .skip
movl v(,%ebx,4), %eax
movl %eax, v(,%eax,4)
movl $0, v(,%ebx,4)
incl %eax
.skip:
incl %ebx
jmp .defrag_loop
.defrag_end:
movl $0, %ecx
.output_defrag:
cmpl $N, %ecx
jge .defrag_output_end
cmpl $0, v(, %ecx, 4)
je .next_defrag_output
push v(,%ecx, 4)
push format_output
movl $4, %eax
movl $1, %ebx
movl $buffer, %ecx
movl $256, %edx
int $0x80
addl $12, %esp
call GET
jmp .next_defrag_output
.next_defrag_output:
incl %ecx
jmp .output_defrag
.defrag_output_end:
ret
When I try to compile it, i run into these errors:
main.S: Assembler messages:
main.S:37: Error: operand type mismatch for `push'
main.S:39: Error: operand type mismatch for `push'
main.S:50: Error: operand type mismatch for `push'
main.S:72: Error: operand type mismatch for `push'
main.S:106: Error: operand type mismatch for `push'
main.S:118: Error: operand type mismatch for `push'
main.S:167: Error: operand type mismatch for `push'
main.S:169: Error: operand type mismatch for `push'
I'm sure they are all related to each other. Not sure if it is relevant, but this is the input I used:
10 (no of operations)
4 (defrag)
3 (delete)
1 (add)
2 (get)
124
109 32 (fd, fd_size)
196 39
197 16
219 25
I realized I needed to load values into registers before using push. I replaced push descriptor
with movl descriptor, %eax
followed by push %eax
, and similarly did this for other memory addresses like buffer. It still didn't work, so I must not be understanding something really fundamental...
Upvotes: 0
Views: 36