HasanRasulov

reversed_sum.asm

Jan 24th, 2021 (edited)
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. %include 'functions.asm'
  2.  
  3.  
  4. section .data           ;Uninitialized data
  5.    num_1 db '123' ,0h
  6.    num_2 db '2' ,0h
  7. section .bss
  8.    num1 resb LEN
  9.    num2 resb LEN      
  10. section .text
  11.  
  12.  
  13. reverse:
  14.     push    ebx             ; preserve ebx on the stack to be restored after function runs
  15.     push    ecx             ; preserve ecx on the stack to be restored after function runs
  16.     push    edx             ; preserve edx on the stack to be restored after function runs
  17.     push    esi
  18.  
  19.     mov     esi, eax        ; move pointer in eax into esi (our number to convert)    
  20.     call    slen            ; initialise eax with decimal value 0
  21.     mov     ecx, eax
  22.     dec     ecx             ; initialise ecx with decimal value 0
  23.     mov     eax, 0
  24.  
  25. .multiplyLoop:
  26.     xor     ebx, ebx        ; resets both lower and uppper bytes of ebx to be 0
  27.     mov     bl, [esi+ecx]   ; move a single byte into ebx register's lower half
  28.    
  29.     sub     bl, 48          ; convert ebx register's lower half to decimal representation of ascii value
  30.     add     eax, ebx        ; add ebx to our interger value in eax
  31.     mov     ebx, 10         ; move decimal value 10 into ebx
  32.     mul     ebx
  33.  
  34.              
  35.     dec     ecx
  36.     cmp     ecx, -1         ; compare ebx register's lower half value against ascii value 48 (char value 0)
  37.     je      .finished       ; jump if less than to label finished
  38.     jmp     .multiplyLoop   ; continue multiply loop
  39.  
  40. .finished:
  41.     cmp     ecx, 0          ; compare ecx register's value against decimal 0 (our counter register)
  42.     je      .restore        ; jump if equal to 0 (no integer arguments were passed to atoi)
  43.     mov     ebx, 10         ; move decimal value 10 into ebx
  44.     div     ebx             ; divide eax by value in ebx (in this case 10)
  45.  
  46. .restore:
  47.     pop     esi             ; restore esi from the value we pushed onto the stack at the start
  48.     pop     edx             ; restore edx from the value we pushed onto the stack at the start
  49.     pop     ecx             ; restore ecx from the value we pushed onto the stack at the start
  50.     pop     ebx             ; restore ebx from the value we pushed onto the stack at the start
  51.     ret
  52.  
  53.  
  54. global  _start
  55. _start:
  56.  
  57.  
  58.    
  59.     ;mov eax,3
  60.     ;mov ebx,2
  61.     ;mov ecx,num1
  62.     ;mov edx,LEN
  63.     ;int 80h
  64.    
  65.     ;mov byte [num1+LEN-1], 0h  
  66.     mov eax,num_1
  67.  
  68.     call reverse  ;reverse first number
  69.     push eax      ;push result to stack
  70.     call iprintLF
  71.    
  72.  
  73.     ;mov eax,3
  74.     ;mov ebx,2
  75.     ;mov ecx,num2
  76.     ;mov edx,LEN
  77.     ;int 80h
  78.    
  79.     ;mov byte [num2+LEN-1], 0h  
  80.     mov eax,num_2  ;move second number to eax
  81.  
  82.     call reverse   ;reverse second number
  83.     call iprintLF
  84.    
  85.     pop ebx        ;pop first reversed number
  86.     add eax,ebx    ;add them up
  87.    
  88.  
  89.     call iprintLF
  90.  
  91.    
  92.     ; below code section is for reverse the sum
  93.     ; we can't use reverse function beacuse it accepts string but we have decimal value
  94.        
  95.  
  96.      mov    ebx,    10  ;move 10 to ebx as divident to get remainder
  97.      xor    ecx,    ecx ;clear ecx for  
  98.  
  99.   godivide:
  100.     xor edx,    edx ; clear remainder
  101.     div ebx     ; divide eax by 10
  102.         cmp     edx ,   0          
  103.         je      godivide        ;skip the zero
  104.         add     edx,   '0'      ;convert digit to ascii char
  105.         push    eax             ;reserve
  106.         push    ebx             ;reserve
  107.     push    edx             ;reserve
  108.          
  109.        
  110.         mov ecx,esp             ;get the adress of first digit from stack(remainder edx)            
  111.         mov edx,1
  112.         mov ebx,STDOUT
  113.         mov eax,SYS_WRITE
  114.         int 80h
  115.        
  116.         pop edx                  ;restore
  117.         pop ebx                  ;restore
  118.         pop eax                  ;restore
  119.    
  120.     cmp eax,    0   ; if eax
  121.     jne godivide    ; is not equal to zero, divide again
  122.  
  123.  
  124.  
  125.     call quit    
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136.  
  137.  
  138.  
  139.  
Add Comment
Please, Sign In to add comment