Advertisement
Pein_Calamity

Assembly x86 Projects

Dec 4th, 2019
2,856
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ;Rodney Seals, x86 Assembly Lab
  2. ;2019 Fall
  3. ;1) Write a program with a loop and indexed addressing that calculates the sum of all the gaps between successive array elements.
  4. ;The array elements are double words, sequenced in ascending order. So, for example, the array {0,2,5,9,10} has gaps 2,3,4, and 1, whose sum equals 10.
  5. ;2) Using a loop and indexed addressing, write code that rotates the members of an array forward by one position.
  6. ;The value at the end of the array must wrap around the first position. For example, the array {10,20,30,40,} would be transformed into {40,10,20,30}.
  7. ;3) Use a loop with indirect or indexed addressing to reverse the elements in an integer array in place. Do not copy element to any other array.
  8. ;Use the SIZEOF, TYPE, and LENGTHOF operators to make the program as flexible as possible if the array size and type should change in the future.
  9. ;4) Write a program with a loop and indexed addressing, that exchanges every pair of values in array with an even number of elements.
  10. ;Therefore, item i will exchange with item i+1, and item i+2 will exchange with item i+3, and so on.  
  11. ; pg 153
  12.  
  13.  
  14. ------------------------------------------------------------------------------------------------------------
  15. ;Program 1
  16. .data
  17. NumberSet1 dword 2,4,8,16,32
  18. total dword
  19. compare1 dword
  20. compare2 dword
  21.  
  22. .code
  23. main PROC
  24. mov esi,OFFSET NumberSet1
  25. mov eax,NumberSet1[esi]
  26.  
  27. mov ecx,4                       ;declares 4 loops
  28. loopyboi:
  29.     mov eax,compare1            ;puts value of array element into the first comparison number
  30.     add esi,4                   ;moves the array to the next element
  31.     add eax,[esi]
  32.     eax EQU compare2            ;puts value of following array element into next comparison number
  33.     sub compare2,compare1       ;subtracts lower value from the higher value.
  34.     add compare2,ebx            ;adds the remainder into total.
  35.     loop loopyboi
  36.  
  37. total EQU ebx;                  ;Throws the total into a variable cus why not.
  38. ------------------------------------------------------------------------------------------------------------
  39. ;program 2
  40. .data
  41. NumberSet1 dword 2,4,6,8,10
  42. LastNumber dword
  43. NumberSet2 dword 0,0,0,0,0
  44.  
  45. .code
  46. main PROC
  47. mov esi,OFFSET NumberSet1       ;putting indexes in
  48. mov edi,OFFSET NumberSet2       ;indexing
  49. ADD edi,TYPE NumberSet2         ;gets the size of the spaces
  50. mov eax,LENGTHOF NumberSet1     ;gets the number of spaces
  51.  
  52. mov ecx,4                       ;loops 4 times
  53. loopyboi:
  54.     mov eax,[esi]               ;putting values from array 1 to 2
  55.     mov [edi],eax
  56.     loop loopiboi
  57.  
  58. mov edi, OFFSET NumberSet2      ;pushing the values
  59. mov eax,[esi]
  60. mov [edi],eax
  61. ------------------------------------------------------------------------------------------------------------
  62. ;program 3
  63. .data
  64. TheOnlyArray dword 1,3,9,27,81
  65. dword count
  66.  
  67.  
  68.  
  69. .code
  70. main PROC
  71. mov esi,OFFSET TheOnlyArray     ;Gets us the first part of the array
  72. edi EQU esi                     ;Makes EDI equal to ESI
  73. add edi,SIZEOF TheOnlyArray     ;adding the array size to get to the end
  74. sub edi,TYPE TheOnlyArray       ;making the number clean without the added addresses and stuff
  75. add count,TYPE TheOnlyArray     ;Just putting this here for my sanity.
  76.  
  77.  
  78. mov ecx,LENGTHOF TheOnlyArray   ;for looping (not sure whether to subtract the value by one or divide by 4 or not but I cant compile to find out. I think this is the right method.
  79. loopyboi:
  80.     mov eax,[esi]               ;this should look familiar in the previous code.
  81.     mov ebx,[edi]               ;the difference is here where instead of storing them in another array, im preparing to exchange their values.
  82.     xchg eax,ebx                ;exchanges the value from eax to put it in ebx. This is good for not needing another.
  83.     mov [esi],eax               ;data origonally in eax is now swapped with what was in ebx and vise-versa
  84.     mov [edi],ebx
  85.     add esi,TYPE
  86.     sub edi,count               ;moves the values in the array one space before looping again
  87.     add esi,count
  88.     LOOP loopiboi:
  89. ------------------------------------------------------------------------------------------------------------
  90. ;program 4
  91. .data
  92. NumberSet1 byte 2,4,8,16,32             ;mixin it up with a byte instead of dword.  Read somewhere that its more efficient to use as small registries as possible
  93.  
  94. .code
  95. main PROC
  96. mov esi, OFFSET NumberSet1
  97. mov eax,NumberSet1[esi]
  98.  
  99. mov ecx,4
  100. loopyboi:
  101.     mov eax,[esi]                       ;comment placeholder
  102.     add esi,TYPE NumberSet1
  103.     add [esi],1                         ;its a byte sized snack. Move forward 1 space, if you pass go collect 200 dollars.
  104.     xchg eax,[esi]                      ;swaps the values of whats in the forward register with the one behind it
  105.     mov [esi],eax
  106.     add esi,TYPE NumberSet1+NumberSet1  ;gotta jump 2 so that I can swap the next pair.
  107.     LOOP loopyboi:
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement