;=========================================================
; File:    Decode.asm
; Author:  Bary W Pollack
; Date:    Feb. 17, 2010
; Course:  GSP130
; Purpose: Decode a message using the Caesar Cipher
;=========================================================

include 'emu8086.inc'
        
        org  100h                 ; set location counter to 100h
        
        jmp  CodeStart            ; jump to start of the program

DataStart:

Encoded db   "Eftuspz!bmm!ivnbot", '"', 0
Decoded db   200 dup(0)           ; allocate 200 bytes 
Newline db   13, 10, 0
Fini    db   13, 10, "Fini", 13, 10, 0

; Decode a message using the Caesar Cipher
CodeStart:
        
        mov  ax, offset Decoded   ; load base of Decoded
        mov  bx, offset Encoded   ; load base of Encoded
        call Decode               ; decode the message
        
        mov  si, offset Encoded   ; echo encoded message
        call print_string
        mov  si, offset Newline   ; issue a newline
        call print_string
        
        mov  si, offset Decoded   ; echo decoded message
        call print_string 
        mov  si, offset Newline   ; issue a newline
        call print_string
        
        mov  si, offset Fini      ; issue final message
        call print_string
        
        ret                       ; return to caller

;==========================================================
; Decode the message using the Caesar Cipher
;   AX - address of output buffer (decoded message)
;   BX - address of input buffer  (encoded message
Decode  PROC
        mov  Pout, AX             ; save pointer to output buffer
        mov  Pin, BX              ; save pointer to input buffer
Lup:    
        mov  bx, Pin              ; load index register with @input
        cmp  [bx], 0              ; check for end of message
        je   EndDec
        mov  cx, [bx]             ; load input character
        dec  cx                   ; decrement value by one
        mov  bx, Pout             ; save output character
        mov  [bx], cx
        inc  Pin                  ; increment input buffer pointer
        inc  Pout                 ; increment output buffer pointer
        jmp  Lup                  ; go back for more
        
EndDec: 
        mov  cx, 0                ; zero-terminate the output
        mov  bx, Pout             ; message
        mov  [bx], cx
        
        ret                       ; return to caller

Pin     dw   ?                    ; pointer to input buffer
Pout    dw   ?                    ; pointer to output buffer
Decode  ENDP

DEFINE_PRINT_STRING
DEFINE_SCAN_NUM
DEFINE_PRINT_NUM
DEFINE_PRINT_NUM_UNS
