assembly - Having trouble encrypting and decrypting with cmp -
i'm having issue cmp. need incorporate 3 tables encrypt , decrypt. can play code either n_tab, l_tab, or a_tab can't seem make sense of incorporating three. appreciated. thank
.model small .stack 100h .data prompt1 db "enter message here: $" prompt2 db "encrypted: $" prompt3 db "decrypted: $" a_tab db "zyxwvutsrqponmlkjihgfedcba" buff db 50 dup(0) .code main: mov ax, @data mov ds, ax ; set addressability mov si, offset buff ; put buff lea dx, prompt1 ; load , print string prompt mov ah, 9 int 21h mov si, di mov ah, 3fh mov cx, 50 int 21h mov si, di mov bx, offset a_tab mov si, di l1: lodsb push bx mov ah, 6 mov dl, 0ffh int 21h pop bx mov dl, al mov al, 0 sub al, 'a' xlat loop l1 lea dx, prompt2 ; load , print string prompt mov ah, 9 int 21h mov si, di mov ah, 4ch int 21h end main
there few problems see i'll describe. sorry don't have dos or dos emulator or windows set test this, i'm reading through code. there may other issues well, these significant ones:
mov ah, 3fh mov bx, 0 mov cx, 50 mov dx, offset buff ; put buff int 21h
the start of segment sets file i/o read file handle 0
(standard input). when int 21h
returns, actual number of bytes read in ax
, seems ignored, ultimately.
then loop starts...
l1: mov dl, al mov bx, offset a_tab
you're beginning encryption loop. you've moved low byte of number of bytes read user dl
, loaded a_tab
address bx
set xlat
expects table address in ds:bx
, index table value in al
, loading result al
.
xlat
so here have trouble. in al
when xlat
first performed? had number of bytes read in standard input, not intended. assume mov dl, al
somehow intended next character out of buffer, need al
before xlat
call. means have read out of buffer. whither buffer index or advancing buffer pointer in loop? doesn't appear setup.
moving on...
mov ah, 6 mov dl, 0ffh int 21h
this outputs character that's in al
terminal. whatever retrieved a_tab
using number of bytes read terminal index should seeing first character output.
remember instruction @ l1:
above did mov dl, al
? seems have no effect since dl
hasn't been used yet , being overwritten 0ffh
above dos interrupt.
lea dx, prompt2 ; load , print string prompt loop l1
this says you're loading , printing prompt string, seems that's you're starting (with lea
) loop
l1
. assume loop
instruction belongs before lea
instruction here. loop
decrement counter in cx
, jump l1
if result not zero. however, in cx
? answer: uncertain because last dos interrupt executed output character may have altered cx
since it's not guaranteed preserve registers. , if did last known value of cx
mov cx, 50
set maximum buffer size (not actual buffer size, returned buffer read ignored code). interrupt might have obliterated cx
.
i'm supposing trying index translation table input characters. have first decide whether you're doing upper case, lower case, or both input, have enough entries in output table, a_tab
account that. need read in character, subtract off lowest possible character value (which 'a'
if you're using upper case letters), use value index table.
you're seeing garbled output because there's no telling what's in al
time execute xlat
instruction each time through loop.
to make code work properly, here steps follow:
before loop
- read input string buffer, doing fine, , save number of bytes read loop counter,
cx
. of x86loop
instructions usecx
counter. - set spare register,
si
address of current buffered input character (setsi
buf
start).si
register ideal buffer indexing (looklods
,stos
, etc, instructions).
during loop
- read byte input buffer
al
(you can uselodsb
instruction automatically incrementsi
next load) - if you're reading caps input, subtract
'a'
character inal
. if lower case, subtract'a'
. need zero-base indexa_tab
. - put address of
a_tab
bx
- execute
xlat
instruction load translated resultal
. - save registers care onto stack using
push
; includescx
register current loop counter ,si
- use dos interrupt output character in
al
- restore registers saved in step 5 using
pop
- go around loop
loop
instruction decrement loop counter,cx
.
these overall steps. may want other things around well, should on right track. should read documentation online special instructions such xlat
, lodsb
understand behavior. in addition, anytime call dos interrupt or function, has conventions how pass arguments, , how information might returned in registers. other registers not mentioned need might used interrupt or function without knowing, should save registers before doing such calls, using push
example, , restore them after call using pop
.
Comments
Post a Comment