本文介绍了汇编语言检查登录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

.model small
.stack
.data
  a db "Hello$"
  login db "Please Log in: $"
  lg db ?
  helloMsg db 0dh, 0ah, "Hello Message$"

.code
main proc
  mov ax, @data
  mov ds, ax

  mov ah, 09h
  lea dx, login
  int 21h

  mov ah, 0ah
  int 21h
  mov lg, bl

  mov cx, 5
  mov si, 0
ck:
  mov ah, 02h
  mov dl, [a+si]
  mov bl, [lg+si]
  cmp dl, bl
  je hi
  int 21h
  inc si
  loop ck
  jne bye


hi:
  mov ah, 09h
  lea dx, helloMsg
  int 21h

bye:
  mov ah, 4ch
  int 21h



main endp
  end main

我试图登录系统,并使用cmp命令比较输入和声明为"Hello"的ID,我不知道我的编码有什么问题不能在这里进行比较.来自文凭学生

I have tried to logging into the system and using the cmp command to compare the input and the id I declare "Hello", I don't know whats wrong with my coding cannot compare here. From Diploma Student

推荐答案

为了使用int 21hah=0ah从键盘捕获字符串,您需要声明一个包含三个部分的变量:第一个字节,用于指定允许的最大字符数(最大长度),中断将存储用户输入的字符数的一秒字节以及一个字符数组,该数组的长度必须与允许的最大字符数匹配,加一个,因为用户将按ENTER键(字符0dh = 13)结束,所以该键也已存储.

In order to capture a string from keyboard with int 21h, ah=0ah, you need to declare a variable with three sections: the first byte to specify the maximum number of chars allowed (max length), one second byte where the interrupt will store the number of chars that the user entered, and an array of chars, the length of this array must match the maximum chars allowed, plus one, because the user will finish with ENTER key (char 0dh = 13), this key is stored too.

捕获输入后,必须将用户字符串(变量a)与字符数组(输入变量的第三部分)进行比较.

After the input is captured, the user string (variable a) must be compared to the array of chars (the third section of the input variable).

下一个代码中有注释可帮助您理解:

Next code has comments to help you understand :

.model small
.stack 100h
.data
  a db "Hello$"
  login db "Please Log in: $"
  lg db 21          ;<=== MAXIMUM NUMBER OF CHARS ALLOWED (20).
     db ?           ;<=== NUMBER OF CHARS THAT USER ENTERED (?).
     db 21 dup(0)   ;<=== ARRAY OF CHARS (FINISH WITH 0DH=13).
  helloMsg db 0dh, 0ah, "Hello Message$"

.code
main proc
  mov ax, @data
  mov ds, ax

  mov ah, 09h
  lea dx, login
  int 21h

  mov ah, 0ah
  MOV DX, OFFSET lg     ;<=== TELL INT 21H TO STORE CAPTURED STRING HERE.
  int 21h

  MOV SI, OFFSET LG + 2 ;<=== POINT TO THE ARRAY OF CHARS.
  MOV DI, OFFSET A      ;<=== POINT TO THE USER.
ck:
;CHECK END OF USER.
  MOV DH, [DI]
  CMP DH, '$'
  JE  hi                ;<=== END REACHED. ALL CHARS MATCH.

;CHECK END OF INPUT.
  MOV DL, [SI]          ;<=== CURRENT ENTERED CHAR.
  CMP DL, 13
  JE  bye               ;<=== END REACHED. INPUT IS SHORTER.

;COMPARE CURRENT ENTERED CHAR TO CURRENT USER CHAR.
  CMP DL, DH
  jne bye               ;<=== CURRENT CHARS ARE DIFFERENT.
  INC SI                ;<== NEXT ENTERED CHAR.
  INC DI                ;<== NEXT USER CHAR.
  JMP CK                ;<== REPEAT.

hi:
  mov ah, 09h
  lea dx, helloMsg
  int 21h

bye:
  mov ah, 4ch
  int 21h

main endp
  end main

比较时必须考虑四种情况:

Four cases must be considered when comparing :

  1. 两个字符串(用户和捕获的)相等.例如:"Hello"-"Hello".
  2. 字符串不同.例如:"Hello"-"Stackoverflow".
  3. 用户字符串较短.例如:"Hello"-"Hellox".
  4. 输入的字符串较短.例如:"Hello"-"Hell".

先前的程序可以处理情况1、2和4.例如,如果用户输入"Hell",则程序将检测到两个字符串都不相同.

Previous program handles cases 1, 2 and 4. For example, if the user enters "Hell", the program detects both strings are not the same thing.

对于情况3,当用户输入"Hellox"时,程序会说两个字符串相等,这是不正确的.发生这种情况是因为比较循环一直重复到其中一个字符串结束为止,在这种情况下,用户字符串之前(较短)结束.我将这样保留程序,因为我不想使其变得更复杂,您可以尝试使其适用于情况3.无论如何,如果您确实希望该程序处理情况3,我将进行更改,这将需要两个比较.

For case 3, when user enters "Hellox", the program says both strings are equal, which is not true. This happens because the comparation loop repeats until one of the strings ends, in this case, the user string ends before (it's shorter). I will leave the program like this because I don't want to make it more complicated, you can try to make it work for case 3. Anyway, if you really really want the program to handle case 3, I will make the change, it will require two more comparations.

这篇关于汇编语言检查登录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 19:03