Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ;Rodney Seals, x86 Assembly Lab
- ;2019 Fall
- ;1) Write a program with a loop and indexed addressing that calculates the sum of all the gaps between successive array elements.
- ;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.
- ;2) Using a loop and indexed addressing, write code that rotates the members of an array forward by one position.
- ;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}.
- ;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.
- ;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.
- ;4) Write a program with a loop and indexed addressing, that exchanges every pair of values in array with an even number of elements.
- ;Therefore, item i will exchange with item i+1, and item i+2 will exchange with item i+3, and so on.
- ; pg 153
- ------------------------------------------------------------------------------------------------------------
- ;Program 1
- .data
- NumberSet1 dword 2,4,8,16,32
- total dword
- compare1 dword
- compare2 dword
- .code
- main PROC
- mov esi,OFFSET NumberSet1
- mov eax,NumberSet1[esi]
- mov ecx,4 ;declares 4 loops
- loopyboi:
- mov eax,compare1 ;puts value of array element into the first comparison number
- add esi,4 ;moves the array to the next element
- add eax,[esi]
- eax EQU compare2 ;puts value of following array element into next comparison number
- sub compare2,compare1 ;subtracts lower value from the higher value.
- add compare2,ebx ;adds the remainder into total.
- loop loopyboi
- total EQU ebx; ;Throws the total into a variable cus why not.
- ------------------------------------------------------------------------------------------------------------
- ;program 2
- .data
- NumberSet1 dword 2,4,6,8,10
- LastNumber dword
- NumberSet2 dword 0,0,0,0,0
- .code
- main PROC
- mov esi,OFFSET NumberSet1 ;putting indexes in
- mov edi,OFFSET NumberSet2 ;indexing
- ADD edi,TYPE NumberSet2 ;gets the size of the spaces
- mov eax,LENGTHOF NumberSet1 ;gets the number of spaces
- mov ecx,4 ;loops 4 times
- loopyboi:
- mov eax,[esi] ;putting values from array 1 to 2
- mov [edi],eax
- loop loopiboi
- mov edi, OFFSET NumberSet2 ;pushing the values
- mov eax,[esi]
- mov [edi],eax
- ------------------------------------------------------------------------------------------------------------
- ;program 3
- .data
- TheOnlyArray dword 1,3,9,27,81
- dword count
- .code
- main PROC
- mov esi,OFFSET TheOnlyArray ;Gets us the first part of the array
- edi EQU esi ;Makes EDI equal to ESI
- add edi,SIZEOF TheOnlyArray ;adding the array size to get to the end
- sub edi,TYPE TheOnlyArray ;making the number clean without the added addresses and stuff
- add count,TYPE TheOnlyArray ;Just putting this here for my sanity.
- 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.
- loopyboi:
- mov eax,[esi] ;this should look familiar in the previous code.
- mov ebx,[edi] ;the difference is here where instead of storing them in another array, im preparing to exchange their values.
- xchg eax,ebx ;exchanges the value from eax to put it in ebx. This is good for not needing another.
- mov [esi],eax ;data origonally in eax is now swapped with what was in ebx and vise-versa
- mov [edi],ebx
- add esi,TYPE
- sub edi,count ;moves the values in the array one space before looping again
- add esi,count
- LOOP loopiboi:
- ------------------------------------------------------------------------------------------------------------
- ;program 4
- .data
- 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
- .code
- main PROC
- mov esi, OFFSET NumberSet1
- mov eax,NumberSet1[esi]
- mov ecx,4
- loopyboi:
- mov eax,[esi] ;comment placeholder
- add esi,TYPE NumberSet1
- add [esi],1 ;its a byte sized snack. Move forward 1 space, if you pass go collect 200 dollars.
- xchg eax,[esi] ;swaps the values of whats in the forward register with the one behind it
- mov [esi],eax
- add esi,TYPE NumberSet1+NumberSet1 ;gotta jump 2 so that I can swap the next pair.
- LOOP loopyboi:
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement