Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- .globl _start
- .text
- _start:
- pop %rcx # count command line argumens
- pop %rax # program name
- mov $cnt, %rax
- mov %rcx, (%rax)
- _parse_loop:
- mov $0, %rcx
- _match_operation:
- mov $op_description_table, %rsi
- mov (%rsi, %rcx, 8), %rax # op_name
- mov 8(%rsi, %rcx, 8), %rbx # op_len
- pop %rsi # start address of first buffer
- push %rsi
- push %rcx
- mov %rax, %rdi # start address of second buffer
- mov %rbx, %rcx # search len
- cld
- repe cmpsb
- pop %rcx
- jne _match_operation_end
- __match_operation_handle_op:
- pop %rsi # delete op name
- mov $op_description_table, %rsi
- mov 16(%rsi, %rcx, 8), %rax # op_handler
- mov 24(%rsi, %rcx, 8), %rbx # op_argument
- push %rbx
- call *%rax
- jmp _parse_loop_end
- _match_operation_end:
- mov $row, %rax
- add (%rax), %rcx
- mov $all_row, %rax
- cmp (%rax), %rcx
- je _parse_loop_end
- jmp _match_operation
- _parse_loop_end:
- mov $cnt, %rax
- mov (%rax), %rcx
- dec %rcx
- mov %rcx, (%rax)
- loop _parse_loop
- mov $op_arguments_table, %rsi
- push 8(%rsi)
- push (%rsi)
- mov $16, %rdi
- call *(%rsi, %rdi)
- push %rax
- call _int_to_str
- __write_res:
- push %rax
- mov $1, %rax
- mov %rax, %rdi
- pop %rsi
- mov $l_buf, %rdx
- syscall
- jmp _exit
- # functions save its result in 'rax'
- _add:
- pop %rax
- pop %rbx # op1
- pop %rdx # op2
- add %rdx, %rbx
- push %rax # save address
- mov %rbx, %rax
- xor %rdx, %rdx
- ret
- _sub:
- pop %rax
- pop %rbx # op1
- pop %rdx # op2
- sub %rdx, %rbx
- push %rax
- mov %rbx, %rax
- xor %rdx, %rdx
- ret
- _mul:
- pop %rcx
- pop %rax
- pop %rbx
- mul %rbx
- push %rcx
- ret
- _div:
- pop %rbx
- xor %rdx, %rdx
- pop %rax
- pop %rcx
- div %rcx
- xor %rdx, %rdx
- push %rbx
- ret
- _translate_operand:
- pop %rax # ret address
- pop %rdx # where to save
- pop %rbx # number for parsing
- push %rax
- push %rdx
- push %rbx
- call _str_to_int
- __translate_operand_str:
- push %rax
- call _save_to
- mov $cnt, %rax
- mov (%rax), %rcx
- dec %rcx
- mov %rcx, (%rax)
- ret
- _save_opearation:
- pop %rax
- mov $op_arguments_table, %rsi
- add $16, %rsi
- pop %rbx # what save
- push %rax # ret address
- push %rsi # where save
- push %rbx # what save
- call _save_to
- ret
- _save_to:
- pop %rax # ret address
- pop %rbx # what save
- pop %rdx # where save
- mov %rbx, (%rdx)
- push %rax
- ret
- _str_to_int: # translate str to int
- pop %rbx # pop ret address
- pop %rsi # int string
- push %rbx # push ret address
- mov $0, %rcx
- mov $0, %rax
- # push neg condition to stack
- mov $0, %rbx
- push %rbx
- mov (%rsi), %rdx
- cmp $'-', %dl
- jne __str_to_int_parse_loop
- pop %rbx
- inc %rbx
- push %rbx
- inc %rsi
- __str_to_int_parse_loop:
- mov (%rsi), %rdx
- mov %dl, %bl
- inc %rsi
- cmp $0, %rbx
- je __str_to_int_fin
- sub $'0', %rbx
- mov $10, %rcx
- xor %rdx, %rdx
- mul %rcx
- add %rbx, %rax
- jmp __str_to_int_parse_loop
- __str_to_int_fin:
- pop %rbx
- cmp $0, %rbx
- je __str_to_int_ret
- neg %rax
- __str_to_int_ret:
- ret
- _int_to_str: # translate int to str
- pop %rbx # ret address
- mov $buf, %rdi
- __int_to_str_stack_process:
- pop %rax
- mov $0, %rcx
- cmp $0, %rax
- je __int_to_str_ret
- test %rax, %rax
- jns __int_to_str_loop
- push %rcx
- mov $'-', %cl
- mov %cl, (%rdi)
- pop %rcx
- inc %rdi
- neg %rax
- __int_to_str_loop:
- push %rcx
- xor %rdx, %rdx
- mov $10, %rcx
- div %rcx
- pop %rcx
- inc %rcx
- add $'0', %rdx
- push %rdx
- cmp $0, %rax
- jne __int_to_str_loop
- __int_to_str_reverse:
- pop %rax
- stosb
- loop __int_to_str_reverse
- jmp __int_to_str_stack_process
- __int_to_str_ret:
- mov $buf, %rax
- push %rbx
- ret
- _str_is_equal:
- pop %rbx
- pop %rsi
- pop %rdi
- pop %rcx
- push %rbx
- cld
- repe cmpsb
- xor %rax, %rax
- jne __str_is_equal_ret
- inc %rax
- __str_is_equal_ret:
- ret
- _exit:
- mov $60, %rax
- mov $0, %rdi
- syscall
- .data
- b_op1: .ascii "-op1"
- l_b_op1 = . - b_op1
- b_op2: .ascii "-op2"
- l_b_op2 = . - b_op2
- op_add: .ascii "--add"
- l_op_add = . - op_add
- op_sub: .ascii "--sub"
- l_op_sub = . - op_sub
- op_mul: .ascii "--mul"
- l_op_mul = . - op_mul
- op_div: .ascii "--div"
- l_op_div = . - op_div
- buf: .ascii "0 \n"
- l_buf = . - buf
- cnt: .quad 0
- op_arguments_table:
- .quad 13 # op1 in 0b
- .quad 14 # op2 in 0b
- .quad _add # address of operation
- row: .quad 4
- all_row: .quad 24
- op_description_table:
- .quad op_add
- .quad l_op_add
- .quad _save_opearation
- .quad _add
- .quad op_sub
- .quad l_op_sub
- .quad _save_opearation
- .quad _sub
- .quad op_mul
- .quad l_op_mul
- .quad _save_opearation
- .quad _mul
- .quad op_div
- .quad l_op_div
- .quad _save_opearation
- .quad _div
- .quad b_op1
- .quad l_b_op1
- .quad _translate_operand
- .quad op_arguments_table
- .quad b_op2
- .quad l_b_op2
- .quad _translate_operand
- .quad op_arguments_table + 8
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement