Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- .386
- .model flat,stdcall
- option casemap:none
- include \masm32\include\masm32rt.inc
- include \masm32\macros\macros.asm
- include \masm32\include\msvcrt.inc
- includelib \masm32\lib\msvcrt.lib
- .data
- array dd 8, 1, 88, 25, 23, 9394, 7540, 168, 425
- printarray dd 8, 1, 88, 25, 23, 9394, 7540, 168, 425
- PrintLineP db 'PreSwap numOne = %d numTwo = %d', 10, 0
- PrintLinePo db 'PostSwap numOne = %d numTwo = %d', 10, 0
- PrintNum db '%d '
- PrintNewLine db 10, 0
- numElements dd 0
- numOne dd 0
- numTwo dd 0
- exchFlg dd 1
- count dd 0
- .code
- start:
- printSetup:
- LEA EBX, array
- mov ECX, [EBX]
- mov numElements, ECX
- mov ECX, 1
- add EBX, 4
- printLoop:
- mov EAX, [EBX]
- PUSH EAX
- PUSH EBX
- PUSH ECX
- invoke crt_printf,addr PrintNum, EAX ; printf()
- POP ECX
- POP EBX
- POP EAX
- ADD EBX, 4
- INC ECX
- CMP ECX, numElements
- jbe printLoop
- invoke crt_printf, addr PrintNewLine
- cmp numOne, 10
- je getChar
- begin:
- LEA EBX, array ; Grabs the start of the array
- mov ECX, [EBX] ; Moves the number of elements in to a reg
- mov numElements, ECX ; Move that into a variable
- mov ECX, 1 ; Set up ECX to count, start at 1
- add EBX, 4 ; Increment the address over by 4 to grab the next int
- bubble:
- mov EAX, [EBX] ; Grab the first element
- mov numOne, EAX ; Store it
- mov EAX, [EBX+4] ; Grab the second element
- mov numTwo, EAX ; Store it
- ; Debug print
- PUSH EAX
- PUSH EBX
- PUSH ECX
- invoke crt_printf,addr PrintLineP, numOne, numTwo ; printf()
- POP ECX
- POP EBX
- POP EAX
- invoke crt__getch ; getch()
- ; Check to see if numOne, the first element, is smaller than
- ; the second element, EAX or numTwo
- cmp numOne, EAX
- ; If numOne was less than EAX, then we dont need to swap
- ; Jumps to inc bubble which will increment N (ECX) and
- ; increment the address, (add EBX, 4)
- jbe incBubble
- ; If numOne was greater than numTwo
- ; then we swap by moving numTwo's value into a register
- ; and then moving that into EBX + 0 (Nth element)
- mov EAX, numTwo
- mov [EBX], EAX
- ; Repeat the process to move numOne into EBX+4, N+1 element
- mov EAX, numOne
- mov [EBX+4], EAX
- ; Grab the values again to print them out, debug
- mov EAX, [EBX]
- mov numOne, EAX
- mov EAX, [EBX+4]
- mov numTwo, EAX
- PUSH EAX
- PUSH EBX
- PUSH ECX
- invoke crt_printf,addr PrintLinePo, numOne, numTwo ; printf()
- POP ECX
- POP EBX
- POP EAX
- invoke crt__getch ; getch()
- incBubble:
- ; If it reaches here via a jump or simple execution,
- ; then it increments the address and counter
- add EBX, 4
- inc ECX
- ; Checks to see if we've reached the end of the list and if we haven't then
- ; continues
- cmp ECX, numElements
- jb bubble
- mov numOne, 10
- jmp printSetup
- getChar:
- invoke crt__getch ; getch()
- end start
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement