;; bto.asm Binary To Octal ;; 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 bto.asm && ld -s bto.o -o bto ;; For 64 bit Linux systems ;; nasm -f elf64 bto.asm && ld -s bto.o -o bto ;; ;; 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 input buffer obufr: resb 1 ; reserve 1 byte output buffer loopctr: resb 1 ; reserve 1 byte loop counter section .text global _start _start: ;; Loop once for each char of output. mov al, 16 ; set the loop counter charloop: mov esi, loopctr mov [byte esi], byte al ; save the loop counter mov al, 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 ; set esi to input pointer mov al, [esi] ; move char to al shr al, 6 ; get the top two bits add al, 0x30 ; convert to printable call writechar mov esi, ibufr ; set esi to input pointer mov al, [esi] ; move char to al shr al, 3 ; get middle bits and al, 7 add al, 0x30 ; convert to printable call writechar mov esi, ibufr ; set esi to input pointer mov al, [esi] ; move char to al and al, 7 ; get last three bits add al, 0x30 ; convert to printable call writechar mov al, byte ' ' call writechar mov esi, loopctr mov al, [esi] ; move char to al dec al ; decrement it jg charloop ; loop if counter > 0 mov al, byte 10 ; counter = 0 move \n to buffer call writechar mov al, 16 ; set the counter back to 16 jmp charloop done: mov esi, obufr ; set esi to output pointer mov [byte esi], byte 10 ; move final \n to buffer 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 xor eax, eax ; clear eax inc eax ; set syscall to exit (= 1) int 0x80 ; syscall writechar: mov esi, obufr ; set esi to output pointer mov [byte esi], byte al ; put result in output buffer 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