MP 10
MP 10
SOURCE CODE:
%macro read_stdin_terminos ;macro to read stdin_terminos
mov eax, 36h
mov ebx, 0
mov ecx, 5401h
mov edx, terminos ;terminos to buffer defined in bss section
int 80h ;where data in stdin_terminos is stored after readin
%endmacro
Section .data
asterisk db “*” ;character to display in place of entered key
enterkey db 10 ;ASCII code of enter key
pwd db “123” ;password set in program as ‘123’
correct db “access granted”, 10 ;message to be displayed id password is correct
correctlen equ $-correct
incorrect db “access denied”, 10 ;message to be displayed id password is incorrect
incorrectlen equ $-incorrect
section .bss
key reb 2 ;store entered keys here one by one
pwdbyuser resb 24 ;buffer for storing entered password
terminus resb 36 ;buffer for storing terminos structure
section .text
global start
_start:
read_stdin_terminos ;read terminos structure
mov eax, 0AH ;make echo & canonical mode off
not eax ;0AH becomes F5H
and [terminus+12], eax ;only 4th & 2nd bit is set to 0
write_stdin_terminos ;write back terminos structure
mov esi, 0 ;esi to pointer used to store keys in buffer ‘pwdbyuser’
getpasswordkeys:
mov eax, 3 ;read a key pressed by user
mov ebx, 0 ;but it will not be displayed on screen since echo is turned OFF
mov ecx, key ;save it in buffer key
mov edx, 1
int 80h
mov al, byte[key] ;take that key in al
cmp al, 10 ;& check it it is enter key
je passwordenterd ;if equal to enter key, then go for checking the password
mov byte[pwdbyuser + esi], al ;else save it in location pwdbyuser one by one
mov edx, 1 ;display * on screen instead of key pressed by user
mov ecx, asterisk
mov eax, 4
mov ebx, 1
int 80h
inc esi ;point esi to next location in buffer ‘pwdbyuser’
jmp getpasswordkeys ;go back to accept next key
passwordenterd:
mov cx, 3 ;since stored password 123 has three characters
mov esi, pwdbyuser ;esi points to password entered by user
mov edi, pwd ;edi points to password stored in the program
keepchecking:
mov al, [esi] ;take a character pointed by esi into al
mov bl, [edi] ;take a character pointed by edi into bl
cmp al, bl ;compare them
jne disp_incorrect ;if not equal then go to part which displays access denied
inc esi ;else increment esi to point to next character in user password
inc edi ;& increment edi to point to next character in stored password
dec cx ;decrement character counter which is in cx
jnz keepchecking
disp_incorrect:
mov rax, 1 ;display message “access denied”
mov rdi, 1
mov rsi, incorrect
mov rdx, incorrectlen
syscall
skip:
read_stdin_terminos ;read terminos structure
or dword[terminos + 12], 0AH ;make canonical & echo mode ON by
;ORing with 0Ah
write_stdin_terminos ;which makes terminos structure