HasanRasulov

palin.asm

Jan 24th, 2021
1,106
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. %include 'functions.asm'
  3.  
  4.  
  5. section .data          
  6. section .bss
  7.    input resb LEN
  8.    num resb LEN+1        ;store result of itoa function
  9. section .text
  10.  
  11. itoa:
  12.      push ebx      ; preserve ebx on the stack to be restored after function runs
  13.      push ecx      ; preserve ecx on the stack to be restored after function runs
  14.      push edx      ; preserve edx on the stack to be restored after function runs
  15.  
  16.      mov ebx,10    ; store 10 to divide the number to get reminder
  17.      xor ecx,ecx   ; clear and store number of digits  
  18.  
  19.      godivide:
  20.             xor  edx,   edx ; clear remainder
  21.         div  ebx        ; divide eax by 10
  22.         push edx        ; put edx on stack
  23.         inc  ecx        ; increment number of digits
  24.         cmp  eax,   0   ; if eax
  25.         jne  godivide   ; is not equal to zero, divide again
  26.         mov  ebx,   0
  27.     nextdigit:
  28.         pop eax     ;pop of digit
  29.         add eax,      30h   ;add '0' to eax
  30.         mov [num+ebx],eax   ;move current digit to buffer
  31.         inc           ebx   ;increment index
  32.         dec ecx     ;move down through digits
  33.         cmp ecx,      0 ;if ecx
  34.         jg  nextdigit   ;is greater than 0, jump nextdigit
  35.  
  36.     ;mov byte [num+LEN],0h
  37.     mov eax,num                 ;move final result to eax
  38.  
  39.     pop edx                     ; restore edx from the value we pushed onto the stack at the start
  40.     pop ecx                     ; restore ecx from the value we pushed onto the stack at the start  
  41.     pop ebx                     ; restore ebx from the value we pushed onto the stack at the start
  42. ret
  43.  
  44.  
  45.  
  46.  
  47. reverse:
  48.     push    ebx             ; preserve ebx on the stack to be restored after function runs
  49.     push    ecx             ; preserve ecx on the stack to be restored after function runs
  50.     push    edx             ; preserve edx on the stack to be restored after function runs
  51.     push    esi
  52.  
  53.     mov     esi, eax        ; move pointer in eax into esi (our number to convert)    
  54.     call    slen            ; initialise eax with decimal value 0
  55.     mov     ecx, eax
  56.     dec     ecx             ; initialise ecx with decimal value 0
  57.     mov     eax, 0
  58.  
  59. .multiplyLoop:
  60.     xor     ebx, ebx        ; resets both lower and uppper bytes of ebx to be 0
  61.     mov     bl, [esi+ecx]   ; move a single byte into ebx register's lower half
  62.    
  63.     sub     bl, 48          ; convert ebx register's lower half to decimal representation of ascii value
  64.     add     eax, ebx        ; add ebx to our interger value in eax
  65.     mov     ebx, 10         ; move decimal value 10 into ebx
  66.     mul     ebx
  67.  
  68.              
  69.     dec     ecx
  70.     cmp     ecx, -1         ; compare ebx register's lower half value against ascii value 48 (char value 0)
  71.     je      .finished       ; jump if less than to label finished
  72.     jmp     .multiplyLoop   ; continue multiply loop
  73.  
  74. .finished:
  75.     cmp     ecx, 0          ; compare ecx register's value against decimal 0 (our counter register)
  76.     je      .restore        ; jump if equal to 0 (no integer arguments were passed to atoi)
  77.     mov     ebx, 10         ; move decimal value 10 into ebx
  78.     div     ebx             ; divide eax by value in ebx (in this case 10)
  79.  
  80. .restore:
  81.     pop     esi             ; restore esi from the value we pushed onto the stack at the start
  82.     pop     edx             ; restore edx from the value we pushed onto the stack at the start
  83.     pop     ecx             ; restore ecx from the value we pushed onto the stack at the start
  84.     pop     ebx             ; restore ebx from the value we pushed onto the stack at the start
  85.     ret
  86.  
  87. global  _start
  88.  
  89. _start:
  90.  
  91.  
  92.    
  93.     mov eax,SYS_READ
  94.     mov ebx,STDIN
  95.     mov ecx,input          ;read input
  96.     mov edx,LEN
  97.     int 80h
  98.    
  99.     ;mov byte [input+LEN-1], 0h  
  100.     mov eax,input
  101.     call atoi              ;convert string to decimal
  102.     mov edx,eax            ;move result to edx  
  103.     mov eax,input          ;prepare arg for reverse  
  104.  loop:      
  105.  
  106.     call reverse           ;reverse string number  
  107.    
  108.     cmp eax,edx            ;compare decimal reversed with orginal
  109.     je print               ;if equal jump to print
  110.  
  111.     inc edx                ;otherwise,incerement original
  112.     mov eax,edx            ;move to eax and convert it to string for reverse function  
  113.     call itoa
  114.     jmp loop               ;jump uncontionally
  115.  
  116. print:
  117.     mov eax,edx
  118.     call iprintLF          ;print decimal result  
  119.     call quit              ;exit program
  120.  
  121.  
  122.  
  123.  
  124.  
  125.  
  126.  
  127.  
RAW Paste Data