123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138 |
- section .bss
- buf resq 1
- oct resq 1
- len resq 2
- bin resb 4096
- section .data
- global _start
- _start:
- call getchar
- cmp rax,-1
- je .done
-
- cmp rax,' '
- je _start
- cmp rax,0x09
- je _start
- cmp rax,0x0A
- je _start
-
- cmp rax,'#'
- je skip_comment
-
- call hex_digit
- shl rax,4
- mov [oct],rax
- call getchar
- call hex_digit
- or [oct],rax
- mov rax,[oct]
- mov word bx,[len]
- mov byte [bin+rbx],al
- inc rbx
- mov [len],bx
-
- jmp _start
- .done:
- mov rax, 1 ; sys_write
- mov rdi, 2 ; stdout=2?
- mov rsi, bin
- mov rdx, 0
- mov word dx, [len]
- syscall
-
- ; mov rax,0
- ; mov rbx,[len]
- ; call putchar
- ; inc rax
- ; cmp rax,rbx
- ; jl .done
-
- mov rax,60
- mov rdi,0
- syscall
- skip_comment:
- call getchar
-
- cmp rax,-1
- je die
-
- cmp rax,0x0A
- je _start
- jmp skip_comment
- hex_digit:
- cmp rax,'0'
- jl .next1
-
- cmp rax,'9'
- jg .next1
-
- sub rax,'0'
- ret
- .next1:
- cmp rax,'a'
- jl .next2
-
- cmp rax,'f'
- jg .next2
-
- sub rax,('a'-10)
- ret
- .next2:
- cmp rax,'A'
- jl die
-
- cmp rax,'F'
- jg die
-
- sub rax,('A'-10)
- ret
- die
- mov rax,60
- mov rdi,1
- syscall
- getchar:
- mov rax,0x00
- mov rdi,0 ;stdin fd
- lea rsi,[buf]
- mov rdx,1 ;count
- syscall
-
- cmp rax,1
- jne .getchar_fail
- mov rax,0
- mov al,[buf]
- ret
- .getchar_fail:
- mov rax,-1
- ret
- putchar:
- mov [buf],al
- mov rax,0x01
- mov rdi,1 ;stdout fd
- lea rsi,[buf]
- mov rdx,1
- syscall
- ret
|