Question

In: Computer Science

One of the very practical uses of assembly language programming is its ability to optimize the...

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:

  1. Click on the project name, “ProjectFour” in the Solution Explorer pane.
  2. Click on the “Project” choice in the menu bar at the top of the screen.
  3. Select “Build Customizations”.
  4. In the Visual C++ Build Customization Files dialog box, check the checkbox next to masm(.targets,.props). Choose OK to save your selection and close the dialog box.
  5. On the menu bar, choose “Project”, then choose “Add Existing Item”.
  6. In the Add New Item dialog box, select the file named “AsmSelectionSort.asm”. Choose Add to add the file to your project and close the dialog box.

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:

  • Have your assembly language procedure return the number of elements in the array. This will tell you if what is being passed as an argument is the value you expected.
  • Have your assembly language procedure return the value of the first element in the array. This will tell you if you understand how to address and retrieve the value of an element in the array.
  • Have your procedure return the value of the second (or fifth) element in the array. This will tell you if you understand how to address and retrieve the value of a particular element in the array.

This project will provide you with the opportunity to:

  • Link an assembly language procedure to an existing C++ program.
  • Create an assembly language version of a C++ program.
  • Demonstrate your ability to work with a one-dimensional array.
  • Show that you can implement a while loop in assembly language.
  • Demonstrate your ability to implement nested loops in assembly language.
  • Display your understanding of what an assembly language procedure is and how they can be used.
  • Provides a chance for you to show that you understand how to compare values and take conditional action based on the results.
  • Observe how assembly language procedures can be used to optimize programs written in a high-level language like C++.

Solutions

Expert Solution

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

Related Solutions

Assembly Language Programming Construct an assembly language program fragment equivalent to the following C/C++ statement: if...
Assembly Language Programming Construct an assembly language program fragment equivalent to the following C/C++ statement: if (M <= N + 3 && (C == ‘N’ || C == ‘n’)) C = ‘0’; else C = ‘1’; Assume that M and N are 32-bit signed integer variables, and C is an 8-bit ASCII character variable. All variables are stored in memory, and all general-purpose registers are available for use.
Assemby language - MIPS Your assembly should implement the C code directly – i.e.,do not ’optimize’...
Assemby language - MIPS Your assembly should implement the C code directly – i.e.,do not ’optimize’ the C code to change the order of operations or reduce computations. Write MIPS assembly code implementing the following C/C++ statement: y = 13 - 11*x; One way of doing the multiply without a multiply instruction is by using many add instructions (x+x+...+x). For this problem you should do it with fewer additions. Hint: We know how to express any integer as a sum...
“Computer programming is creating a sequence of very precise instructions written in a language a computer...
“Computer programming is creating a sequence of very precise instructions written in a language a computer understands, to perform a specified task with a computer.” Discuss in detail the concept of extreme precision in computer programming.
Translate the following pseudocode to MIPS assembly programming language cout << “\n Please input a number...
Translate the following pseudocode to MIPS assembly programming language cout << “\n Please input a number for $s0”; cin >> $s0; cout << “\n Please input a number for $s1”; cin >> $s1; cout << “\n Please input a number for $s2”; cin >> $s2; $t0 = $s0 / 8 - 2 * $s1 + $s2; cout << “\n the Value of the expression “$s0 / 8 - 2 * $s1 + $s2” is ”; cout >> $t0; return;
Assembly Language Programming Exercise 5. Listing File for AddTwoSum ( 5 pts ) Generate a listing...
Assembly Language Programming Exercise 5. Listing File for AddTwoSum ( 5 pts ) Generate a listing file for the AddTwoSum program and write a description of the machine code bytes generated for each instruction. You might have to guess at some of the meanings of the byte values. Hint: Watch my tutorial and read a little bit of Ch 4.
How would I code the following in assembly language? Use the Keil programming environment to code...
How would I code the following in assembly language? Use the Keil programming environment to code the C8051F330 SiLabs 8051 micro controller. All the documentation is available on efundi. The program shall be done in assembler and you shall use the DJNZ instruction to generate a delay time to flash the LED. The LED shall flash in the following sequence: 1. On for 50mS, 2. Off for 400mS, 3. On for 50mS, 4. Off for 1.5S, 5. Repeat the sequence...
Assembly Language Programming Exercise. Problem # 1: 1. Integer Expression Calculation( 5 pts ) Using the...
Assembly Language Programming Exercise. Problem # 1: 1. Integer Expression Calculation( 5 pts ) Using the AddTwo program from Section 3.2 as a reference, write a program that calculates the following expression, using registers: A = (A + B) − (C + D). Assign integer values to the EAX, EBX, ECX, and EDX registers.
Microsoft Visual C++ Assembly language Problem 3. Write a program that uses a loop to calculate...
Microsoft Visual C++ Assembly language Problem 3. Write a program that uses a loop to calculate the first seven values of the Fibonacci number sequence, described by the following formula: Fib(1) = 1, Fib(2) = 2, … Fib(n) = Fib(n-1) + Fib(n-2). Place each value in the EAX register and display it with a call DumpRegs statement inside the loop For example: Fib(1) = 1, Fib(2) = 2, Fib(3) = Fib(1) + Fib(2) = 3, Fib(4) = Fib(2) + Fib(3)...
CS 238 – Assembly Language Programming Fall 2019 Assignment 1 (Due: September 10, 2019) Submission Instructions:...
CS 238 – Assembly Language Programming Fall 2019 Assignment 1 (Due: September 10, 2019) Submission Instructions: Online submissions on Blackboard are preferred. Feel free to edit this Word document to insert your answers. Multiple online submissions on Blackboard are allowed, but only the last online submission made by the midnight of September 10 will be graded. Alternatively, a paper submission is possible, but it needs to be done in class on September 10. 1. Data can be interpreted as required...
ASSEMBLY LANGUAGE PROGRAMMING 1. The decimal equivalent of the signed 2’s complement 8-bit binary number 11010101B...
ASSEMBLY LANGUAGE PROGRAMMING 1. The decimal equivalent of the signed 2’s complement 8-bit binary number 11010101B is ______________. 2. The decimal equivalent of the unsigned 8-bit hex number 0B4H is ______________. 3. The value of the expression ‘H’ – ‘B’ is less than / equal to / greater than that of the expression ‘L’ – ‘C’. 4. If the .data segment contains declarations A BYTE 2 DUP (‘a’), ‘+’ B BYTE 3 DUP (‘b’), 0 C BYTE 4 DUP (‘c’),...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT