title: Read binary file and output base64: how to extract 6-bit groups?
body: <p>I've started studying x64 assembly programming and my now exercise from school is to create a binary to base 64 converter.</p>
  <p>I've already found the way to read a input file and but now i need to separate the binary number into groups of 6 number and then convert them.</p>
  <p>I have now a file .asm that can read an input file and then show it in the command shell. But now I'm stuck on going through the data, read binary data and translate them into base 64.</p>
  <p>Here's my file:</p>
  <pre><code>%define SYS_EXIT 60
  %define SYS_READ 0
  %define SYS_WRITE 1
  %define SYS_OPEN 2
  %define SYS_CLOSE 3
  %define STDOUT 1
  
  %define BUFFER_SIZE 2048
  
  section .data
      fd dw 0
      
      Base64Table: db &quot;ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/&quot;
      Space: db &quot; &quot;
      Counter: dd 0
  section .bss
      file_buffer resb BUFFER_SIZE
  section .text
  global  _start
  _start:
      add rsp, byte 10h                       ;take the argument
      pop rdi                                         ;pop from the stack to put filename in rdi for sys_open command
      
      mov rax, SYS_OPEN                   ;system_call sys_open
      mov rsi, 0                                      ;flags 0, filename already gived before
      syscall                                         ;start call
      mov [fd], rax                               ;fd linked to rax value
      
      mov rcx, [Counter]
  
  _read_write:
      mov rax, SYS_READ                       ;system_call sys_read
      mov rdi, [fd]                               ;file descriptor
      mov rsi, file_buffer                        ;char -&gt; buffer
      mov rdx, BUFFER_SIZE                    ;buffer size 
      syscall
      
  
      cmp rax, 0                                  ;compare if end of string
      je _exit                                            ;jump equal to _exit
      inc rcx
      cmp rcx , 6
      je _addSpace
  
      mov rdx, rax                                ;length equal to system_call id 
      mov rax, SYS_WRITE                      ;system_call sys_write
      mov rdi, STDOUT                         ;id for STDOUT 1
      mov rsi, file_buffer                        ;size of output
      syscall
  
      jp _read_write                              ;jump to _read_write
  
  
  _addSpace:
      mov rcx, 0
      mov [Counter], rcx
      mov rax, SYS_WRITE                      ;system_call sys_write
      mov rdi, STDOUT                         ;id for STDOUT 1
      mov rsi, Space                              ;size of output
      mov rdx, 1
      syscall
      
      jp _read_write
  
  _exit:
      mov rax, SYS_CLOSE                      ;system_call sys_close
      mov rdi, fd                                 ;file descriptor
      syscall
  
      mov [file_buffer], dword 10         ;add file_buffer \n -&gt; dword 10
      mov rax, SYS_WRITE                      ;system_call sys_write
      mov rdi, STDOUT                         ;sys_write call stdout
      mov rsi, file_buffer                        ;chars to write
      mov rdx, 1                                  ;length to write
      syscall
  
      mov rax, 60                                 ;system_call exit
      mov rdi, 0                                  ;int error code
      syscall
  </code></pre>