Advertisement
Ancutahij

Sorting

Nov 22nd, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. bits 32 ; assembling for the 32 bits architecture
  2.  
  3. ; declare the EntryPoint (a label defining the very first instruction of the program)
  4. global start        
  5.  
  6. ; declare external functions needed by our program
  7. extern exit , printf              ; tell nasm that exit exists even if we won't be defining it
  8. import exit msvcrt.dll    ; exit is a function that ends the calling process. It is defined in msvcrt.dll
  9. import printf msvcrt.dll                         ; msvcrt.dll contains exit, printf and all the other important C-runtime specific functions
  10.  
  11. ; our data is declared here (the variables needed by our program)
  12. segment data use32 class=data
  13.     a dw 1234h,5678h,90h
  14.     l1 equ ($-a)/2
  15.     b dw 104h,0abcdh,10h,1122h
  16.     l2 equ ($-b)/2
  17.     c resb l1+l2
  18.     gasit db 0
  19.     mesaj db "Sirul cifrelor este: ", 0
  20.    
  21. ; our code starts here
  22.  
  23. ;a. Se dau doua siruri de cuvinte a si b. Sa se obtina sirul c astfel:
  24. ;- concatenati sirul octetilor low din primul sir cu sirul octetilor high din al doilea sir.
  25. ;- sirul c se va ordona crescator
  26. ;- numerele din siruri se vor interpreta cu semn
  27. ;- afisati sirul c (numere in baza 16)
  28.  
  29. ;   Exemplu:
  30.  
  31. ;       a dw 1234h,5678h,90h
  32.  
  33. ;       b dw 4h,0abcdh,10h,1122h
  34.  
  35. ;       c resb 7
  36.  
  37. ;   Reprezentarea in memorie:
  38.  
  39. ;       a -> | 34h| 12h| 78h| 56h| 90h| 00h|
  40.  
  41. ;       b -> | 04h| 00h| 0cdh| 0abh| 10h| 00h| 22h| 11h|
  42.  
  43. ;   Dupa concatenare sirul c va fi: 34h,78h,90h,00h,0abh,00h,11h.
  44. ;   Apoi se face ordonarea octetilor.
  45. segment code use32 class=code
  46.     start:
  47.         ;CREARE SIR
  48.         mov ecx, l1
  49.         mov esi, a
  50.         cld
  51.         mov ebx, 0              ; contor pt sirul destinatie
  52.         jecxz eZero
  53.             repeta:
  54.                 lodsw
  55.                
  56.                 mov [c+ebx], al
  57.                 inc ebx
  58.    
  59.             loop repeta
  60.         eZero:
  61.        
  62.         mov ecx, l2
  63.         mov esi, b
  64.         cld
  65.         jecxz zero
  66.             repp:
  67.                 lodsw
  68.                
  69.                 shr ax, 8
  70.                 mov [c+ebx], al
  71.                 inc ebx
  72.             loop repp
  73.         zero:
  74.        
  75.         ;SORTARE
  76.        
  77.         mov ecx, l1
  78.         add ecx, l2
  79.         dec ecx
  80.         jecxz ZERO
  81.             do:
  82.                 ;.gasit db 0               ; variabila locala
  83.                 mov ecx, l1
  84.                 add ecx, l2
  85.                 dec ecx
  86.                 mov ebx, 0                  ; contor pentru sirul ebx
  87.                 mov byte[gasit], 0      ;daca am gasit cel putin o interschimare atunci gasit va avea val 1
  88.                 cattimp:
  89.                    
  90.                     mov al, [c+ebx+1]
  91.                     mov dl, [c+ebx]
  92.                     cmp dl, al
  93.                     jle eMaiMare            ; daca [c+ebx]<= [c+ebx+1] sare mai departe
  94.                         xchg [c+ebx], al
  95.                         mov [c+ebx+1], al
  96.                         mov byte[gasit], 1
  97.                         dec ebx
  98.                     eMaiMare:
  99.                     inc ebx
  100.                 loop cattimp
  101.                 cmp byte[gasit], 0
  102.                 je Sortat
  103.                
  104.             jmp do
  105.         ZERO:
  106.         Sortat:
  107.        
  108.     ; AFISARE SIR SORTAT:  
  109.     push dword mesaj
  110.     call [printf]
  111.     add esp, 4*1
  112.    
  113.    
  114.     mov esi, c
  115.     cld
  116.     mov ecx, edi
  117.    
  118.     jecxz esteZero
  119.         cttimp:
  120.             xor eax, eax
  121.             lodsb
  122.             cbw
  123.             cwde
  124.             pushad
  125.             push dword  eax
  126.             call [printf]
  127.             add esp, 4*1
  128.            
  129.             popad
  130.         loop cttimp
  131.     esteZero:
  132.         ; exit(0)
  133.         push    dword 0      ; push the parameter for exit onto the stack
  134.         call    [exit]       ; call exit to terminate the program
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement