Advertisement
Redxone

[x86 Assembly] Password and menu example

Dec 1st, 2017
289
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. .model small
  2. .stack 100h
  3.  
  4. print macro txt
  5.     mov ah, 9h
  6.     lea dx, txt
  7.     int 21h
  8. endm
  9.  
  10. read macro buffer
  11.     mov ah, 08h
  12.     int 21h
  13.    
  14. endm  
  15.  
  16.  
  17. buffsize equ 08h
  18. hide     equ '*'
  19.  
  20. .data                                    
  21.     pasPrompt db 10,13,'Enter password:$'
  22.     passWrong db 10,13,'Access Denied.$'
  23.     inpComp   db 10,13,'Input completed.$'
  24.     selPrompt db 10,13,'Select an option:$'
  25.     optionOne db 10,13,  '[1] One.$'
  26.     oOneMessage   db 10,13,'You have selected the first option.$'
  27.     oTwoMessage   db 10,13,'You have selected the second option.$'
  28.     oInvalid db 10,13,'Invalid selection.'
  29.     oExiting db 10,13,'Exiting...$'
  30.     optionTwo db 10,13,  '[2] Two.$'
  31.     optionThree db 10,13,'[3] Exit.$'
  32.     pass db 'password'                      
  33.     buffLen  db 0
  34.     passbuff db 8 DUP(0)
  35. .code
  36.     mov ax, @data
  37.     mov ds, ax    
  38.                            
  39.     ; We need to initialize our ES as well as our DS, the ES is used in REP instructions.
  40.     mov es, ax  
  41.    
  42.     Main:
  43.    
  44.     print pasPrompt
  45.    
  46.     readPass:
  47.        ; First, we need to reset out strings length.
  48.        ; Next, we load the source index of our buffer.
  49.        ; This index will later be used to insert chars into byte by byte.
  50.        xor cl, cl          
  51.        lea si, passbuff  
  52.        
  53.     readPass_Loop:    
  54.        ; Read char by char until CX is max buffer size
  55.        ; Results go into 'passbuff'
  56.        ; Display hidden char each input  
  57.        ; 0D = enter key                          
  58.        
  59.        ; Now lets get a char as the start of our password input.
  60.        mov ah, 08h
  61.        int 21h
  62.        
  63.        ; If char==enter then
  64.        ;   compare the passwords
  65.        cmp al, 0Dh
  66.        je readPass_Done  
  67.              
  68.        ; else
  69.        ;   If we are not pressing enter
  70.        ;   Move the char into our buffer
  71.        ;   Increment the char read.
  72.        ; Store char into buffer.
  73.        mov [si], al
  74.        
  75.        ;Check if we are at the last char
  76.        ; If so, check password.
  77.        cmp cl, buffSize
  78.        je readPass_Done
  79.        
  80.        ; Increment length.
  81.        ; Move pointer to next char in buffer.
  82.        inc cl
  83.        inc si        
  84.        
  85.        ; Print password "star"
  86.        mov ah, 02h
  87.        mov dl, hide
  88.        int 21h      
  89.        
  90.        jmp readPass_Loop
  91.        
  92.        readPass_Done:
  93.           ; Load the length of our read string into a variable.
  94.           mov [buffLen], cl
  95.                      
  96.           ; Make sure we are comparing from password length
  97.           ; and NOT buffer length, if we used our buffer length
  98.           ; simply entering the first letter of our password would let us in.
  99.           xor cx, cx
  100.           mov cl, buffSize
  101.          
  102.           ; Set direction of comparison.          
  103.           cld      
  104.          
  105.           ; Load our 2 passwords to be checked by CMPSB <Compare Strings by Bytes>
  106.           lea si, [pass]
  107.           lea di, [passbuff]                                    
  108.          
  109.           ; Check if password is correct.
  110.           ; Using REP CMPSB <Repeat Compare String Bytes>
  111.           rep cmpsb                      
  112.           ; If equal go to menus
  113.           ; If not, display "Access Denied"
  114.           je Menus
  115.           jne Denied
  116.          
  117.        Denied:
  118.           print passWrong
  119.        jmp exit
  120.          
  121.        Menus:      
  122.           ; Clear password buffer.
  123.           ; Clear counter register(s), retrieve pointer to password input buffer.
  124.           xor cx, cx
  125.           lea si, passbuff  
  126.           ; Clear buffer char by char until it is completely cleared.
  127.           clearloop:
  128.             mov [si], 0h
  129.             inc si
  130.             inc cl
  131.             cmp cl, buffsize
  132.             jne clearloop
  133.           ; Print "GUI"
  134.           print selPrompt  
  135.           print optionOne
  136.           print optionTwo
  137.           print optionThree
  138.           ; Prompt for an input, check against ASCII for 1,2,3    
  139.           getOption:
  140.             mov ah, 8h
  141.             int 21h
  142.             cmp al, 31h
  143.             je opOne
  144.             cmp al, 32h
  145.             je opTwo
  146.             cmp al, 33h
  147.             je opThree
  148.             ; If the previous 3 chars were not pressed proceed
  149.             ; to tell the user that the option was invalid.
  150.             ; Then go back to the menu.
  151.             jne opInvalid
  152.           opOne:
  153.             print oOneMessage
  154.           jmp Menus
  155.          
  156.           opTwo:
  157.             print oTwoMessage  
  158.           jmp Menus
  159.          
  160.           opThree:
  161.             print oExiting
  162.           jmp Main  
  163.          
  164.           opInvalid:
  165.             print oInvalid
  166.           jmp Menus
  167.      
  168.        
  169.       exit:      
  170.         mov ah, 4ch
  171.         int 21h
  172. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement