Advertisement
Guest User

Lab 6 - 21

a guest
Nov 28th, 2018
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ; 21. Being given a string of words, obtain the string (of bytes) of the digits in base 10 of each word from this string.
  2.  
  3. bits 32 ; assembling for the 32 bits architecture
  4.  
  5. ; declare the EntryPoint (a label defining the very first instruction of the program)
  6. global start        
  7.  
  8. ; declare external functions needed by our program
  9. extern exit               ; tell nasm that exit exists even if we won't be defining it
  10. import exit msvcrt.dll    ; exit is a function that ends the calling process. It is defined in msvcrt.dll
  11.                           ; msvcrt.dll contains exit, printf and all the other important C-runtime specific functions
  12.  
  13. ; our data is declared here (the variables needed by our program)
  14. segment data use32 class=data
  15.     sir dw 12345, 20778, 4596  ; the initial string sir
  16.     l equ ($-sir) / 2 ; the length of the string (in words)
  17.     ten dw 10 ; variabile used for getting the right most digit of a number
  18.     dest times l db 0 ; reserve a memory space of l for the destination string dest and initialize it
  19.  
  20. ; our code starts here
  21. segment code use32 class=code
  22.     start:
  23.         mov esi, sir ; in esi we will store the FAR address of the string 's'
  24.         cld ; parse the string from left to right (DF = 0)
  25.         mov ecx, l ; we will parse the elements of the string in the loop with l iterations
  26.         mov edi, dest ; in edi we will store the FAR address of the string 'd'
  27.        
  28.         jecxz finishProgram ; end the program if ECX is zero (s is an empty string)
  29.        
  30.         digitsInBase10:
  31.             lodsw ; in AX we will have the current word from the string
  32.            
  33.             extractDigit:
  34.                 mov dx, 0 ; clear the DX registry -> DX:AX = AX
  35.                
  36.                 div word[ten] ; in DX we will have the right most digit of DX:AX and in AX the initial number without the right most digit
  37.                
  38.                 push eax ; back up AX to the stack for just a second, so we can store move DX in AX
  39.                
  40.                 mov ax, dx ; AX = DX so that AL (the low byte from AX) can be stored in the destination string
  41.                 stosb ; store the value of the byte AL in EDI
  42.                
  43.                 pop eax ; okay, we got it back, whoh
  44.                
  45.                 cmp ax, 0 ; check whether the current number is 0
  46.                 jne extractDigit ; if so, stop the loop and get to the next number (we've found all the digits)
  47.            
  48.         loop digitsInBase10 ; if there are more elements (ecx>0) resume the loop.
  49.        
  50.         finishProgram: ; the end of the program
  51.    
  52.         ; exit(0)
  53.         push    dword 0      ; push the parameter for exit onto the stack
  54.         call    [exit]       ; call exit to terminate the program
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement