Guest User

Untitled

a guest
Feb 19th, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.52 KB | None | 0 0
  1. TITLE MASM Template (main.asm)
  2.  
  3. INCLUDE Irvine32.inc
  4. NUMS_ROWS = 7d
  5. NUMS_COLS = 9d
  6. SWITCHES_LENGTH = 80d
  7. CHANGES_LENGTH = 20d
  8. SWITCHES_LENGTH = 20d
  9.  
  10. .data
  11. ; 7 rows, 9 cols
  12. ; Initial data in array
  13. NUMS WORD 09EEBh, 0B0CFh, 061E5h, 089EDh, 0AF17h, 0D8D1h, 06C1Dh, 0594Eh, 0CF55h
  14. WORD 03767h, 063C6h, 0AE84h, 0412Fh, 0B226h, 046C1h, 0879Bh, 076B6h, 093FFh
  15. WORD 0AFFFh, 05B8Fh, 06164h, 01CF7h, 09A41h, 0A525h, 0A5A1h, 08F05h, 07E4Ch
  16. WORD 0827Ah, 090B0h, 0722Dh, 0BCCFh, 033ABh, 0DC76h, 085B6h, 0AA5Fh, 03FB3h
  17. WORD 04BACh, 0B822h, 07768h, 0BF1Bh, 05783h, 07EEBh, 09F22h, 0B85Bh, 05312h
  18. WORD 05971h, 0B1B6h, 0B16Dh, 054B3h, 073C8h, 0586Bh, 08170h, 06F16h, 092A0h
  19. WORD 09680h, 0A23Bh, 0B45Dh, 01E91h, 0415Ah, 0B5D9h, 02D02h, 06748h, 03D39h
  20.  
  21. ; Elements to be swapped in array (stored in pair)
  22. SWITCHES BYTE 4,3, 1,0, 2,6, 3,6, 4,3, 5,3, 4,6, 0,1, 2,1, 5,2
  23. BYTE 5, 4, 5, 6, 5, 5, 4, 6, 1, 4, 3, 5, 1, 3, 2, 7, 3, 4, 2, 2
  24. BYTE 6, 7, 4, 6, 1, 4, 1, 8, 2, 3, 2, 6, 5, 8, 6, 3, 3, 7, 6, 1
  25. BYTE 0, 3, 4, 2, 2, 5, 4, 4, 5, 5, 2, 0, 5, 7, 6, 6, 5, 6, 3, 3
  26.  
  27. ; Output strings
  28. strNUMS BYTE "Unsorted: ", 0
  29. strSORTED BYTE "Sorted: ", 0
  30. strADDED BYTE "Changed: ", 0
  31. strSWAPPED BYTE "Swapped: ", 0
  32.  
  33. ; Returns the row-major-order index of a given element in NUMS
  34. ; Requires:
  35. ; eax - row
  36. ; ebx - col
  37. ; Returns:
  38. ; edx - RMO index
  39. GetIndex PROC uses eax
  40. ; eax is already the row
  41. mov edx, NUMS_COLS
  42. mul edx ; eax = row * TOTAL COLUMNS
  43. add eax, ebx ; eax = row * total columns + COLUMN
  44. mov edx, 2
  45. mul edx ; eax *= SIZE OF WORD
  46. mov edx, eax
  47. ret
  48. GetIndex ENDP
  49.  
  50. DoSwitches PROC uses ecx eax ebx esi
  51. mov ecx, SWITCHES_LENGTH
  52. mov esi, OFFSET SWITCHES
  53. L1:
  54. mov eax, [esi] ; eax = row 1
  55. mov ebx, [esi+1] ; ebx = col 1
  56. call GetIndex ; edx = index 1
  57.  
  58. mov eax, [esi+2] ; eax = row 2
  59. mov ebx, [esi+3] ; ebx = col 2
  60.  
  61. push ecx
  62.  
  63. mov ecx, edx ; ecx = index 1
  64. call GetIndex ; edx = index 2
  65.  
  66. call DumpRegs
  67.  
  68. mov ax, WORD PTR [NUMS+ecx]
  69. mov dx, WORD PTR [NUMS+edx]
  70. mov [WORD PTR NUMS+edx],ax
  71. mov [WORD PTR NUMS+ecx],bx
  72.  
  73. pop ecx
  74.  
  75. add esi, 4 ; move to next set of data
  76. loop L1
  77. ret
  78. DoSwitches ENDP
  79.  
  80. main PROC
  81. call ClrScr
  82.  
  83. mov edx, OFFSET strNUMS
  84. call WriteString
  85. call PrintNums
  86.  
  87. mov edx, OFFSET strSORTED
  88. call WriteString
  89. call BubbleSort
  90. call PrintNums
  91.  
  92. mov edx, OFFSET strADDED
  93. call WriteString
  94. call AddChanges
  95. call PrintNums
  96.  
  97. mov edx, OFFSET strSWAPPED
  98. call WriteString
  99. call DoSwitches
  100. ;call PrintNums
  101.  
  102. exit
  103. main ENDP
  104. END main
Add Comment
Please, Sign In to add comment