splash365

Palindrome using stack [Assembly]

Sep 18th, 2021 (edited)
1,606
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. .model small
  2. .stack 100h
  3.  
  4. .data
  5. arr      db 100 dup(?)  
  6. len      db  ?
  7. flag     db  1
  8. msg_1    db  "Enter a string: $"
  9. msg_2    db  "Forward  : $"
  10. msg_3    db  "Backward : $"
  11. msg_4    db  "It is palindrome! $"
  12. msg_5    db  "It is not palindrome! $"
  13. endl     db  0ah,0dh,"$"
  14.  
  15. .code
  16. main proc
  17.     mov ax, @data
  18.     mov ds, ax
  19.    
  20.     mov ah, 9
  21.     lea dx, msg_1
  22.     int 21h
  23.    
  24.     lea SI, arr ; SI contains the offset of arr
  25.     mov len, 0
  26.    
  27.     INPUT:
  28.     mov ah, 1
  29.     int 21h
  30.     cmp al, 13
  31.     je END_INPUT ; if enter pressed then stop taking input
  32.    
  33.     cmp al, '0' ; check if al >= '0', otherwise continue taking input
  34.     jge CHECK_1
  35.     jmp INPUT
  36.    
  37.     CHECK_1:
  38.     cmp al, '9' ; check if al >= '0 and al <= '9', then it's valid and store in array
  39.     jle STORE
  40.    
  41.     cmp al, 'A' ; check if al >= 'A', otherwise continue taking input
  42.     jge CHECK_2
  43.     jmp INPUT
  44.    
  45.     CHECK_2:
  46.     cmp al, 'Z' ; check if al >= 'A' and al <= 'Z', then it's valid and store in array
  47.     jle STORE
  48.    
  49.     cmp al, 'a'
  50.     jge CHECK_3 ; check if al >= 'a', otherwise continue taking input
  51.     jmp INPUT
  52.    
  53.     CHECK_3:
  54.     cmp al, 'z' ; check if al >= 'a' and al <= 'z', then it's valid and store in array
  55.     jle STORE
  56.     jmp INPUT
  57.    
  58.     STORE:
  59.     mov [SI], al ; store in array
  60.     inc SI ; increment SI to point to the next index
  61.     inc len ; increment the length
  62.     mov ah, 0 ; clear ah to avoid garbadge
  63.     push ax ; push the input character into the stack
  64.     jmp INPUT
  65.     END_INPUT:
  66.    
  67.     mov ah, 9
  68.     lea dx, endl
  69.     int 21h
  70.     lea dx, msg_2
  71.     int 21h
  72.    
  73.     lea SI, arr ; SI contains the offset of arr
  74.    
  75.     CHECK_4:
  76.     cmp SP, 100h
  77.     je END_CHECK_4
  78.    
  79.     mov ah, 2
  80.     mov dl, [SI] ; print array in forward direction
  81.     int 21h
  82.    
  83.     pop bx    
  84.     ; in stack reversed string is stored
  85.     ; bl contains the correspoinding reverse character    
  86.     ; after printing [SI], store the corresponding reverse character in [SI]
  87.     ; that means after the CHECK_4 loop completes, the array will be reversed
  88.     mov [SI], bl
  89.     inc SI
  90.            
  91.     cmp bl, dl ; check forward and backward character
  92.     je CHECK_4 ; if equal, then continue
  93.    
  94.     ; xor with 32 changes the letter case
  95.     xor dl, 32 ; else change the letter case and check again
  96.     cmp bl, dl
  97.     je CHECK_4 ; if equal, then continue
  98.    
  99.     mov flag, 0 ; else set flag = 0, means mismatch found!
  100.     jmp CHECK_4  
  101.     END_CHECK_4:
  102.    
  103.     mov cl, 1
  104.     lea SI, arr
  105.    
  106.     mov ah, 9
  107.     lea dx, endl
  108.     int 21h
  109.     lea dx, msg_3  
  110.     int 21h
  111.    
  112.     ; by this time, the array is reversed, now print the array
  113.     PRINT_BACKWARD:
  114.     cmp cl, len
  115.     jg END_PRINT_BACKWARD
  116.    
  117.     mov ah, 2  
  118.     mov dl, [SI]
  119.     int 21h
  120.    
  121.     inc cl
  122.     inc SI  
  123.     jmp PRINT_BACKWARD
  124.     END_PRINT_BACKWARD:
  125.    
  126.     mov ah, 9
  127.     lea dx, endl
  128.     int 21h
  129.    
  130.     cmp flag, 1 ; if flag = 1, then it'palindrome, otherwise not!
  131.     je PALINDROME
  132.    
  133.     mov ah, 9
  134.     lea dx, msg_5
  135.     int 21h  
  136.     jmp EXIT
  137.    
  138.     PALINDROME:
  139.     mov ah, 9
  140.     lea dx, msg_4
  141.     int 21h  
  142.    
  143.     EXIT:
  144.    
  145.     main endp
  146. end main
RAW Paste Data