Follow Slashdot blog updates by subscribing to our blog RSS feed

 



Forgot your password?
typodupeerror
×
User Journal

Journal clone53421's Journal: Base64 encoder (80x86)

So, this is your standard MIME base64 encoding process. It takes 3 bytes of data, packs them into a 24-bit value, splits it into 6-bit chunks, and uses them as indexes into a 64-character table. Once you type three letters, it gives you the four-character representation (nothing is echoed until the full 3 characters are typed).

The lines are wrapped at 76 characters per the MIME spec, and if the input terminates with one or two extra bytes (i.e. not an even multiple of 3 bytes) the 24-bit field is zero-padded at the end. In this case the last 1 or 2 characters of the four-character representation are replaced with = (instead of A) to avoid having extra NULL bytes at the end of the data after decoding.

For example, the string "Hello World" (without quotes), which has three sets of three characters with two characters left over, is encoded as: SGVsbG8gV29ybGQ=

See also: Base64 decoder (80x86)

Same drill: paste in Notepad, save as base64en_asm.bat, run.

@echo off
goto batch
 
a
;Base64 encoder
;Accepts input from STDIN and prints to STDOUT
;Input bytes are buffered and encoded at intervals of 3 bytes
;Esc or Ctrl-Z exit, 0-2 buffered bytes may be encoded at this point
        mov di,0227 ;_inbuffer
        mov si,01e7 ;_charset
;_input
        mov bx,0002 ;loop counter
        xor dx,dx
        mov ah,08
;_inloop
        int 21 ;get a key
        cmp al,1b ;check for esc
        jnz 0129 ;_continue
;_finish
        cmp bx,02
        jnz 0120 ;_pad
        mov ah,02
        mov cx,0001
        jmp 01aa ;_crlf
;_pad
;pad buffer with nulls so we can encode it
        mov byte ptr [bx+di],0
        inc dx ;cx counts how many bytes we null-padded
        dec bx
        jns 0120 ;_pad
        jmp 0132 ;_output
;_continue
        cmp al,1a ;check for ^z
        jz 0113 ;_finish
        mov [bx+di],al ;store byte in buffer
        dec bx
        jns 010d ;_inloop
;_output
;encode bytes stored in 3-byte input buffer
        xor bh,bh
        xor al,al
        mov ah,[di+2] ;get the first byte from the input buffer
        mov cl,06
        rol ax,cl
        mov bl,al
        mov cl,[bx+si]
        mov [di+6],cl ;put the first character in the output buffer
        mov cl,06
        shr ax,cl
        mov al,[di+1] ;get the 2nd input byte
        mov cl,04
        shl ax,cl
        mov bl,ah
        mov cl,[bx+si]
        mov [di+5],cl ;put the 2nd character in the output buffer
        cmp dx,0002 ;see if we need to pad output
        jnz 0162 ;_output2
        dec dx
        mov byte ptr [di+4],3d ;pad with =
        jmp 0175 ;_output3
;_output2
        xor ah,ah
        mov cl,04
        shl ax,cl
        mov al,[di] ;get the 3rd input byte
        shl ax,1
        shl ax,1
        mov bl,ah
        mov cl,[bx+si]
        mov [di+4],cl ;put the 3rd character in the output buffer
;_output3
        cmp dx,0001 ;see if we need to pad output
        jnz 0180 ;_output4
        mov byte ptr [di+3],3d ;pad with =
        jmp 018b ;_print
;_output4
        shr al,1
        shr al,1
        mov bl,al
        mov cl,[bx+si]
        mov [di+3],cl ;put the 4th character in the output buffer
;_print
        mov cx,dx ;save dx because we need it
        mov bl,03
        add di,03 ;point to the output buffer now
        mov ah,02
;_printloop
        mov dl,[bx+di] ;get the next character to be printed
        int 21
        dec bx
        jns 0194 ;_printloop
        cmp cx,0001
        jz 01aa ;_crlf
        add byte ptr [di+4],04 ;increment the column counter
        cmp byte ptr [di+4],4c ;check column width
        jnz 01b6 ;_nocrlf
;_crlf
        mov byte ptr [di+4],00 ;reset the column counter
        mov dl,0d
        int 21
        mov dl,0a
        int 21
;_nocrlf
        sub di,03 ;point to the input buffer again
        cmp cx,0001
        jz 01c1 ;_quit
        jmp 0106 ;_input
;_quit
        mov ah,03
        xor bh,bh
        int 10 ;get cursor position
        mov ah,08
        int 10 ;preserve character under cursor
        mov ah,02
        mov bl,al
        mov cx,dx
        mov dl,1a
        int 21 ;send ^z
        mov dx,cx
        int 10 ;restore cursor position
        mov ah,0a
        mov al,bl
        mov cx,1
        int 10 ;restore character under cursor
        mov ax,4c01
        int 21 ;quit to dos
;_charset
        db "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
;_inbuffer
        db 0,0,0
;_outbuffer
        db 0,0,0,0
;_count
        db 0
;ASCIIZ
db "base64en.com",0
;compile
MOV AX,CS
MOV DS,AX
MOV AH,3C
XOR CX,CX
MOV DX,022f ;ASCIIZ
INT 21
MOV BX,AX
MOV CX,022f ;ASCIIZ
MOV DX,0100 ;where the program starts
SUB CX,DX
MOV AH,40
INT 21
MOV AH,3E
INT 21
MOV AX,4C00
INT 21
 
r ip
23c
g
q
 
:exists
echo You don't need to run this. Run BASE64EN.COM instead.
goto run
 
:batch
if exist base64en.com goto exists
 
if exist %0 goto extension
debug < %0.bat
goto run
 
:extension
debug < %0
 
:run
pause
cls
base64en.com

This discussion has been archived. No new comments can be posted.

Base64 encoder (80x86)

Comments Filter:

Anyone can make an omelet with eggs. The trick is to make one with none.

Working...