In: Computer Science
One of the very practical uses of assembly language programming is its ability to optimize the speed and size of computer programs. While programmers do not typically write large-scale applications in assembly language, it is not uncommon to solve a performance bottle neck by replacing code written in a high level language with an assembly language procedure.
In this programming project you will be given a C++ program that generates an array of pseudorandom integers and sorts the array using the selection sort algorithm.
Your job is to write an assembly language procedure that also sorts the array of pseudorandom integers using the selection sort algorithm. The C++ program will time multiple repetitions of the sort performed by both the C++ code and your assembly language procedure. The C++ program will compare the result. If all goes as expected, your assembly language procedure should be faster than the C++ code.
Chapter 13 of your textbook contains a discussion of how to interface an assembly language procedure with a high-level programming language like C++.
The Visual Studio solution for the C++ program that you are given has been packaged and compressed into a file called “ProjectFour.zip”. Create a location on your computer for this project. Download the compressed file, “ProjectFour.zip”, and unpack it into that location in your computer.
Look in the unpacked folder for a file named “ProjectFour.sln”. The “.sln” file extension stands for solution. Double clicking on this file will start up the Visual Studio solution for ProjectFour and allow you to execute the C++ program.
Modify ProjectFour by following these steps:
Use Ctrl+F5 or click on “Debug” in the Menu Bar followed by “Start Without Debugging” to execute the program. The MASM assembler will assemble AsmSelectionSort.asm into an object file that is then linked into your project.
A “stub” assembly language procedure has been provided so that you can execute the C++ program to get a feel for how it works. Your job is to improve on the efficiency of the C++ compiled code by writing an assembly language procedure that is faster. Click on the file named “AsmSelectionSort.asm” in the Solution Explorer pane. This file is your starting point for creating an assembly language version of the selection sort routine.
As always, start small. DO NOT be the Cookie Monster and gobble up the whole project at once. Steps you might consider, but are not limited to are:
This project will provide you with the opportunity to:
Answer:
One of the very practical uses of assembly language programming is its ability to optimize the speed and size of computer programs.
SELECT_SORT PROC PUSH AX ; push AX onto the STACK PUSH BX ; push BX onto the STACK PUSH CX ; push CX onto the STACK PUSH DX ; push DX onto the STACK PUSH DI ; push DI onto the STACK CMP BX, 1 ; compare BX with 1 JLE @SKIP_SORTING ; jump to label @SKIP_SORTING if BX<=1 DEC BX ; set BX=BX-1 MOV CX, BX ; set CX=BX MOV AX, SI ; set AX=SI @OUTER_LOOP: ; loop label MOV BX, CX ; set BX=CX MOV SI, AX ; set SI=AX MOV DI, AX ; set DI=AX MOV DL, [DI] ; set DL=[DI] @INNER_LOOP: ; loop label INC SI ; set SI=SI+1 CMP [SI], DL ; compare [SI] with DL JNG @SKIP ; jump to label @SKIP if [SI]<=DL MOV DI, SI ; set DI=SI MOV DL, [DI] ; set DL=[DI] @SKIP: ; jump label DEC BX ; set BX=BX-1 JNZ @INNER_LOOP ; jump to label @INNER_LOOP if BX!=0 MOV DL, [SI] ; set DL=[SI] XCHG DL, [DI] ; set DL=[DI] , [DI]=DL MOV [SI], DL ; set [SI]=DL LOOP @OUTER_LOOP ; jump to label @OUTER_LOOP while CX!=0 @SKIP_SORTING: ; jump label POP DI ; pop a value from STACK into DI POP DX ; pop a value from STACK into DX POP CX ; pop a value from STACK into CX POP BX ; pop a value from STACK into BX POP AX ; pop a value from STACK into AX RET ; return control to the calling procedure SELECT_SORT ENDP