123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174 |
- section .bss
- buf resq 1
- oct resq 1
- len resq 1
- bin resb 4096
- num resq 1
- wrd resq 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,'#'
- jne .dont_skip_comment
- call skip_comment
- jmp _start
- .dont_skip_comment:
-
- cmp rax,':'
- jne .dont_read_label
- call read_word
- mov rax, '!'
- call putchar
- jmp _start
- .dont_read_label:
- call hex_digit
- shl rax,4
- mov [oct],rax
- call getchar
- call hex_digit
- or [oct],rax
- mov rax,[oct]
- mov rbx,[len]
- mov byte [bin+rbx],al
- inc rbx
- mov [len],rbx
-
- 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
- die
- mov rax,60
- mov rdi,1
- syscall
- skip_comment:
- call getchar
-
- cmp rax,-1
- je die
-
- cmp rax,0x0A
- jne skip_comment
- ret
- 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
- read_word
- mov rbx,[num]
- mov rax,rbx
- add rax,1
- mov [num],rax
- shl rbx,5 ;; = *32
- lea rbx,[wrd + rbx]
- .loop:
- call getchar
- cmp rax,-1
- je .done
- cmp rax,' '
- je .done
- cmp rax,0x0A
- je .done
- mov byte [rbx],al
- inc rbx
- jmp .loop
- .done
- mov byte [rbx],0
- ret
- 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
|