带有3个查找表的xlat

时间:2015-03-28 03:48:22

标签: assembly x86

我一直试图让这段代码工作一段时间。我无法看到如何使用3个查找表。有人可以指出我做错了什么,并指出我正确的方向。我想比较,所以我可以使用上限,下限和数字。我在多个查找表上找不到多少。非常感谢您提供的任何帮助。

 .model small

.stack 100h

.data

prompt1 db "ENTER YOUR MESSAGE HERE: $" 
prompt2 db 13,10,13,10,"ENCRYPTED/DECRYPTED MESSAGE: $"

a_tab db "ZYXWVUTSRQPONMLKJIHGFEDCBA"
l_tab db "zyxvwutsrqponmlkjihgfedcba"
n_tab db "9876543210"


buff  db 51          ;MAXIMUM NUMBER OF CHARACTERS ALLOWED (50).
      db ?           ;NUMBER OF CHARACTERS ENTERED BY USER.
      db 51 dup('$') ;CHARACTERS ENTERED, ENDED BY CHR(13).

.code

main:
mov ax, @data
mov ds, ax                      ; set up addressability

lea dx, prompt1                 ; load and print the string PROMPT
mov ah, 9
int 21h

;CAPTURE STRING FROM KEYBOARD (WITH "BUFF" FORMAT).
lea dx, buff
mov ah, 0AH ;0AH REQUIRES THE THREE-LEVEL FORMAT OF BUFF.
int 21h

;--------------------------------------------------------------------    
;MAKE "SI" TO POINT TO FIRST CAPTURED CHARACTER.
lea si, buff
inc si ;NOW "SI" POINTS TO THE NUMBER OF CHARACTERS ENTERED BY USER.
inc si ;NOW "SI" POINTS TO FIRST ENTERED CHARACTER.



encryptmessage:
mov al, [si]        ;GET CHARACTER.
cmp al, 32          ;check if blank space
je space


cmp al, 13          ;CHECK IF END REACHED.
je  encrypted

cmp al, 'Z'-'A'
jbe uppercase

cmp al, 'z'-'a'
jbe lowercase

cmp al, '9'-'0'
jbe number

uppercase:
mov bx, offset a_tab
sub al, 'A'         ;ENCRYPT CHARACTER

lowercase:
mov bx, offset l_tab
sub al, 'a'         ;ENCRYPT CHARACTER

number:
mov bx, offset n_tab
sub al, '0'         ;ENCRYPT NUMBER

xlat 
space:
mov [si], al        ;PUT CHARACTER BACK.
inc si              ;NEXT CHARACTER.
jmp encryptmessage
encrypted:   



lea dx, prompt2
mov ah, 9
int 21h

lea dx, buff
add dx, 2
mov ah, 9
int 21h

mov ah, 4cH
int 21h

end main

2 个答案:

答案 0 :(得分:2)

这里比较最简单的方法是尊重字符的ASCII顺序。首先是数字,然后是大写字符,最后是小写字符。它们之间的孔不需要翻译,因此它们被视为空间。您不再需要分别检查32。

encryptmessage:
  mov al, [si]        ;GET CHARACTER.
  cmp al, 13          ;CHECK IF END REACHED.
  je  encrypted

  cmp al, '0'
  jb  Space
  cmp al, '9'
  jbe Number
  cmp al, 'A'
  jb  Space
  cmp al, 'Z'
  jbe Uppercase
  cmp al, 'a'
  jb  Space
  cmp al, 'z'
  ja  Space
Lowercase:
  mov bx, offset l_tab
  sub al, 'a'
  jmp Translate
Uppercase:
  mov bx, offset a_tab
  sub al, 'A'
  jmp Translate
Number:
  mov bx, offset n_tab
  sub al, '0'
Translate:
  xlat 
Space:
  mov [si], al        ;PUT CHARACTER BACK.
  inc si              ;NEXT CHARACTER.
  jmp encryptmessage
encrypted: 

在您的程序结束时,您打印加密的消息,但考虑如果用户确实输入了50个字符会发生什么!终止CR将不再是必需的 $ 字符。我建议你将缓冲区扩大1个字节。

buff  db 51          ;MAXIMUM NUMBER OF CHARACTERS ALLOWED (50).
      db ?           ;NUMBER OF CHARACTERS ENTERED BY USER.
      db 51 dup('$') ;CHARACTERS ENTERED, ENDED BY CHR(13).
      db '$'

答案 1 :(得分:0)

我想到的是每张桌子的两次比较。与'Z' - 'A'相比,告诉我们有26个字母 - 不是有用的信息。

cmp al, 'Z'
ja notupper
cmp al, 'A'
jae uppercase

notupper:
cmp al, 'z'
ja notlower
; etc.

cmp al, 'Z' ja notupper cmp al, 'A' jae uppercase notupper: cmp al, 'z' ja notlower ; etc.

当你到达(等)时,将uppercase设置到你的桌子上,并进行相应的减法(就像你一样),但不要“摔倒”到下一个案例,但跳转到您的bx标签。我把它留给你想象的地方应该是。 :)