USING ROL & ROR FUNCTIONS TO ENCODE/ENCRYPT ASCII 

From: Victor Alvarado, San Jose, Costa Rica
Subject: Assembler help

Hello Bob:
Can you tell me how I can use the Intel PC assembly language functions rol, rotate left, and ror, rotate right. I can do it in Pascal, but cannot seem to do it correctly in assembler. Thank you, Víctor


Subject: Assembler help

Hi there Víctor:
Happy to illustrate the rol and ror functions for you in PC assembly language. I wrote the tiny program, encrypt.com which uses both the rol, rotate left, and ror, rotate right functions to encode/encrypt a short ASCII sentence and then decode/decrypt it for the user.

Just run encrypt.com from any subdirectory and press enter to step through it and ESC to exit to the DOS> prompt after the bottom line is displayed. The program's output after about 5 press ENTERs looks like this.

This program is about half way between the [BEGINNER] and [INTERMEDIATE] levels, so I have placed it in both categories on my homepages.

The program uses Eric Isaacson's brilliant shareware a86.com assembler. My homepage has a link to Eric's homepage where you may download this assembler and its 100+ pages of instructions, if you wish. It is about 10 times faster and easier to use than either Borland's or Microsoft's assemblers and is my favorite assembler. It handles most all source code up through the Intel Pentium microprocessor.

The 11K source code for this program, encrypt.asm is displayed below to hopefully whet your appetite.


start:  mov ds,cs                       ;data segment same as code segment 
	jmp goahd                       ;go ahead jump around messages
	
savbl   db 0

ms0:    db 'Rotate left (rol) ASCII encryption for Victor Alvarado',0          
ms1: db 'Victor let us encrypt this message',0
ms2: db 'simulated ))))))))))))) ',0 ;encrypted message
ms3: db 'now let us decrypt this message ',0
ms4: db 'Victor let us encrypt this message',0 ;decrypted message
ms5: db 'Above was rotated left 3 bits to encode and rotated right
3 bits to decode. ' db 'Let us complicate the encoding by rotating every 7 ASCII bytes,
first by 7, ' db '2nd by 6, 3rd by 5, 4th by 4, 5th by 3, 6th by 2 and 7th by
1. This will ' db 'drive the "Code Breaker" (Cryptanalyst), bananas trying to
figure it out. ',0 ms6: db 'So what is all this good for? Why not use the above
encode and decode ' db 'algorithms to send an e-mail message to a friend who has
the same algorithms. ' db 'It would be extremely difficult for busybodies or snoopers to
decode & read it. ',0 ms7: db 'Press ESC to exit to the DOS> prompt or ENTER to run
again',0 ms8: db 'NOTE: above BRs and spaces only to line up object code'0 goahd: mov ax,3 ;cls by int 10h ;set video mode call cursof ;turn off the cursor mov es,0b800h ;text video mov di,ms0 ;message 0 above mov si,160 ;2nd line of video from top call display ;diz it mov di,ms1 ;message 1 above mov si,480 ;4th line of video call display ;diz it mov ah,0 ;await int 16h ;key press mov di,ms1 ;message to encypt mov si,ms2 ;encypted message enc1: mov al,[di] ;get byte cmp al,0 ;end of message ? if z jmp wait1 ;await key press inc di ;next one rol al,3 ;rotate left 3 bits mov [si],al ;put it in message 2 inc si ;next one jmp enc1 ;continue wait1: mov di,ms2 ;encypted message mov si,564 ;video mem to display call display ;display it mov ah,0 ;await int 16h ;key press mov di,ms3 ;now decrypt message mov si,800 ;video mem to diplay it call display ;display it mov ah,0 ;await int 16h ;key press mov di,ms2 ;message to decrypt mov si,ms4 ;plain text message decry: mov al,[di] ;get byte cmp al,0 ;end of message ? if z jmp wait2 ;await key press inc di ;next one ror al,3 ;rotate right 3 bits mov [si],al ;put it in message 4 inc si ;next one jmp decry ;continue wait2: mov di,ms4 ;decrypted message mov si,884 ;video mem call display ;display it mov di,ms5 ;story - 4 lines mov si,1120 ;video mem call display ;display it mov ah,0 ;await int 16h ;key press mov di,ms1 ;message to encrypt mov si,1920 ;video mem call display ;display it mov ah,0 ;await int 16h ;key press mov di,ms1 ;message to encypt mov si,ms2 ;encypted message enc2: mov bl,7 ;bytes counter - rotate 1 to 7 bits enc2a: mov al,[di] ;get byte cmp al,0 ;end of message ? if z jmp wait3 ;await key press inc di ;next one cmp bl,7 ;byte counter if z rol al,7 ;rotate al left 7 bits cmp bl,6 ;byte counter if z rol al,6 ;rotate al left 6 bits cmp bl,5 ;byte counter if z rol al,5 ;rotate al left 5 bits cmp bl,4 ;byte counter if z rol al,4 ;rotate al left 4 bits cmp bl,3 ;byte counter if z rol al,3 ;rotate al left 3 bits cmp bl,2 ;byte counter if z rol al,2 ;rotate al left 2 bits cmp bl,1 ;byte counter if z rol al,1 ;rotate al left 1 bit mov [si],al ;put it in message 2 inc si ;next one dec bl ;byte counter if z jmp enc2 ;reset bl to 7 jmp enc2a ;continue wait3: mov di,ms2 ;encypted message mov si,2004 ;video mem to display call display ;display it mov ah,0 ;await int 16h ;key press mov di,ms3 ;now decrypt message mov si,2240 ;video mem to diplay it call display ;display it mov ah,0 ;await int 16h ;key press mov di,ms2 ;message to decrypt mov si,ms4 ;plain text message dec3: mov bl,7 ;bytes counter - rotate 1 to 7 bits dec3a: mov al,[di] ;get byte cmp al,0 ;end of message ? if z jmp wait4 ;await key press inc di ;next one cmp bl,7 ;byte counter if z ror al,7 ;rotate al right 7 bits cmp bl,6 ;byte counter if z ror al,6 ;rotate al right 6 bits cmp bl,5 ;byte counter if z ror al,5 ;rotate al right 5 bits cmp bl,4 ;byte counter if z ror al,4 ;rotate al right 4 bits cmp bl,3 ;byte counter if z ror al,3 ;rotate al right 3 bits cmp bl,2 ;byte counter if z ror al,2 ;rotate al right 2 bits cmp bl,1 ;byte counter if z ror al,1 ;rotate al right 1 bit mov [si],al ;put it in message 2 inc si ;next one dec bl ;byte counter if z jmp dec3 ;reset bl to 7 jmp dec3a ;continue wait4: mov di,ms4 ;decrypted message mov si,2324 ;video mem call display ;display it mov di,ms6 ;Why not use message mov si,2560 ;video mem call display ;display it mov di,ms7 ;exit or continue message mov si,3200 ;video mem call display ;display it mov ah,0 ;await int 16h ;key press cmp ah,1 ;ESC key ? if z jmp exit ;exit to the DOS> prompt jmp start ;start all over again exit: call curson ;restore the cursor mov ax,3 ;cls by int 10h ;set video mode mov ax,4c00h ;exit pointer int 21h ;return to DOS> prompt display: mov al,[di] ;get string byte cmp al,0 ;the end ? if z ret ;return after call if all done mov ah,7 ;medium white color attribute inc di ;next byte to display mov es:[si],ax ;display byte and attribute on video add si,2 ;requires 2 bytes jmp display ;go do next one curson: mov cx,11 ;cursor video bios display curs: mov bh,0 ;this part mov ah,1 ;of video int 10h ;do it ret ;return to instruction after call cursof: mov cx,0e00h ;cursor video move out of sight jmp curs ;continue ;CALL BEEP BELOW NOT USED beep: pusha ;internal PC speaker LOUD beep cli ;clear interrupts mov dx,535 ;beep loudness - higher = quieter mov al,0b6h ;10110110 binary out 43h,al ;program. int timer ctl word port mov al,dl ;al = dl out 42h,al ;program. int timer misc. reg port mov al,dh ;al = dh out 42h,al ;program. int timer misc. reg port in al,61h ;read port 61h 8042 ctrl reg mov ah,al ;ah = al or al,3 ;or al 00000011 binary out 61h,al ;write port 61h 8042 ctrl reg mov ah,0 ;set read time of day counter int 1ah ;get time of day counter add dx,4 ;duration of beep mov bx,dx ;bx = dx l3: int 1ah ;get counter cmp dx,bx ;compare delay jne l3 ;wait till equal mov al,ah ;al = ah out 61h,al ;write port 61h 8042 ctrl reg sti ;set interrupts eep: mov ah,1 ;wait for key release int 16h ;test key pressed jnz releez ;nz = key still pressed popa ;restore stack ret ;return releez: mov ah,0 ;clr int 16h ;key jmp eep ;go test again ELFIN: ;THE END IN Español

Have fun.