Advertisement
Guest User

Untitled

a guest
Jun 26th, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  .386
  2. .model flat,stdcall
  3. option casemap:none
  4.  
  5. include     \masm32\include\masm32rt.inc
  6. include     \masm32\macros\macros.asm
  7.  
  8. include     \masm32\include\msvcrt.inc
  9. includelib  \masm32\lib\msvcrt.lib
  10.  
  11. .data
  12.  
  13.  
  14. array           dd 8, 1, 88, 25, 23, 9394, 7540, 168, 425
  15. printarray      dd 8, 1, 88, 25, 23, 9394, 7540, 168, 425
  16. PrintLineP      db      'PreSwap numOne = %d numTwo = %d', 10, 0
  17. PrintLinePo     db      'PostSwap numOne = %d numTwo = %d', 10, 0
  18. PrintNum        db      '%d '
  19. PrintNewLine    db      10, 0
  20. numElements     dd 0
  21. numOne          dd 0
  22. numTwo          dd 0
  23. exchFlg         dd 1
  24. count           dd 0
  25.  
  26. .code
  27.  
  28. start:
  29.  
  30. printSetup:
  31.  
  32.     LEA EBX, array
  33.     mov ECX, [EBX]
  34.     mov numElements, ECX
  35.     mov ECX, 1
  36.     add EBX, 4
  37.  
  38. printLoop:
  39.     mov EAX, [EBX]
  40.  
  41.     PUSH EAX
  42.     PUSH EBX
  43.     PUSH ECX
  44.  
  45.     invoke  crt_printf,addr PrintNum, EAX      ; printf()
  46.  
  47.     POP ECX
  48.     POP EBX
  49.     POP EAX
  50.  
  51.     ADD EBX, 4
  52.     INC ECX
  53.  
  54.     CMP ECX, numElements
  55.  
  56.     jbe printLoop
  57.  
  58.     invoke crt_printf, addr PrintNewLine
  59.  
  60.  
  61.     cmp numOne, 10
  62. je getChar
  63.  
  64.  
  65.  
  66. begin:
  67.  
  68.     LEA EBX, array ; Grabs the start of the array
  69.     mov ECX, [EBX] ; Moves the number of elements in to a reg
  70.     mov numElements, ECX ; Move that into a variable
  71.     mov ECX, 1 ; Set up ECX to count, start at 1
  72.  
  73.     add EBX, 4 ; Increment the address over by 4 to grab the next int
  74.  
  75. bubble:
  76.     mov EAX, [EBX] ; Grab the first element
  77.     mov numOne, EAX ; Store it
  78.     mov EAX, [EBX+4] ; Grab the second element
  79.     mov numTwo, EAX ; Store it
  80.  
  81.     ; Debug print
  82.     PUSH EAX
  83.     PUSH EBX
  84.     PUSH ECX
  85.  
  86.     invoke  crt_printf,addr PrintLineP, numOne, numTwo      ; printf()
  87.  
  88.     POP ECX
  89.     POP EBX
  90.     POP EAX
  91.  
  92.     invoke  crt__getch                          ; getch()
  93.  
  94.     ; Check to see if numOne, the first element, is smaller than
  95.     ; the second element, EAX or numTwo
  96.     cmp numOne, EAX
  97.  
  98.     ; If numOne was less than EAX, then we dont need to swap
  99.     ; Jumps to inc bubble which will increment N (ECX) and
  100.     ; increment the address, (add EBX, 4)
  101.     jbe incBubble
  102.  
  103.         ; If numOne was greater than numTwo
  104.         ; then we swap by moving numTwo's value into a register
  105.         ; and then moving that into EBX + 0 (Nth element)
  106.         mov EAX, numTwo
  107.         mov [EBX], EAX
  108.         ; Repeat the process to move numOne into EBX+4, N+1 element
  109.         mov EAX, numOne
  110.         mov [EBX+4], EAX
  111.  
  112.         ; Grab the values again to print them out, debug
  113.         mov EAX, [EBX]
  114.         mov numOne, EAX
  115.         mov EAX, [EBX+4]
  116.         mov numTwo, EAX
  117.  
  118.         PUSH EAX
  119.         PUSH EBX
  120.         PUSH ECX
  121.  
  122.         invoke  crt_printf,addr PrintLinePo, numOne, numTwo      ; printf()
  123.  
  124.         POP ECX
  125.         POP EBX
  126.         POP EAX
  127.  
  128.         invoke  crt__getch                          ; getch()
  129.  
  130.     incBubble:
  131.         ; If it reaches here via a jump or simple execution,
  132.         ; then it increments the address and counter
  133.         add EBX, 4
  134.         inc ECX
  135.  
  136.         ; Checks to see if we've reached the end of the list and if we haven't then
  137.         ; continues
  138.         cmp ECX, numElements
  139.  
  140. jb bubble
  141.  
  142. mov numOne, 10
  143. jmp printSetup
  144.  
  145. getChar:
  146.  
  147. invoke  crt__getch                          ; getch()
  148.            
  149. end         start
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement