;; otb.asm Octal To Binary ;; version 0.0.3a ;; ;; Copyright 2016 Kevin G. Austin ;; ;; Licensed under the Apache License, Version 2.0 (the "License"); ;; you may not use this file except in compliance with the License. ;; You may obtain a copy of the License at ;; ;; http://www.apache.org/licenses/LICENSE-2.0 ;; ;; Unless required by applicable law or agreed to in writing, software ;; distributed under the License is distributed on an "AS IS" BASIS, ;; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. ;; See the License for the specific language governing permissions and ;; limitations under the License. ;; ;; to build : ;; For 32 bit Linux systems ;; nasm -f elf otb.asm && ld -s otb.o -o otb ;; For 64 bit Linux systems ;; nasm -f elf64 otb.asm && ld -s otb.o -o otb ;; ;; Uncomment the next line for 32 bit systems ;; BITS 32 ;; Uncomment the next line for 64 bit systems ;; BITS 64 section .bss ibufr: resb 1 ; reserve 1 byte buffer obufr: resb 1 ; reserve 1 byte buffer section .text global _start _start: ;; Loop once for each char of output. charloop: call readchar ; get the first two bits mov esi, ibufr ; set esi to buffer pointer mov al, [esi] ; move buffer char to al cmp al, 10 je charloop ; found newline sub al, 0x30 ; convert from printable shl al, 6 mov esi, obufr ; set esi to buffer pointer mov [byte esi], byte al ; save al buffer call readchar ; get the middle three bits mov esi, ibufr ; set esi to buffer pointer mov bl, [esi] ; move buffer char to bl sub bl, 0x30 ; convert from printable shl bl, 3 mov esi, obufr ; set esi to buffer pointer mov al, [esi] ; move buffer char to al or al, bl ; add middle three bits mov esi, obufr ; set esi to buffer pointer mov [byte esi], byte al ; save al buffer call readchar ; get the last three bits mov esi, ibufr ; set esi to buffer pointer mov bl, [esi] ; move buffer char to bl sub bl, 0x30 ; convert from printable mov esi, obufr ; set esi to buffer pointer mov al, [esi] ; move buffer char to al or al, bl ; add last three bits mov esi, obufr ; set esi to buffer pointer mov [byte esi], byte al ; put result back in buffer call writechar call readchar ; get the space jmp charloop done: xor eax, eax ; clear eax inc eax ; set syscall to exit (= 1) int 0x80 ; syscall writechar: mov eax, 4 ; syscall 4 = write mov bl, 1 ; 1 = stdout mov ecx, obufr ; pointer to char buffer mov dl, 1 ; write 1 char int 0x80 ; syscall or eax, eax ; test return jle done ; <= 0 means failure ret readchar: mov eax, 3 ; syscall 3 = read mov bl, 0 ; 0 = stdin mov ecx, ibufr ; pointer to buffer mov dl, 1 ; read 1 char int 0x80 ; syscall or eax, eax ; test return jle done ; <= 0 means done mov esi, ibufr ; pointer to buffer mov al, [esi] ; move buffer char to al cmp eax, 10 je endtests ; found newline cmp eax, 32 je endtests ; found space cmp eax, 47 jle done ; found illegal char cmp eax, 58 jge done ; found illegal char endtests: ret