CS 2011, C Term 2001: Lab 5

                                                                                Name: __________________

                                                                            Section:_______________

Objective

To be able to incorporate an assembly-language procedure into a C++ program. To get some more practice using shift and rotate instructions.

Problem

We have provided you with a C++ program that reads in an integer, calls an assembly language procedure, and prints out the results of the procedure call. Your job is to write the assembly language procedure, and link it to the C++ main program.

The assembly language procedure does one of two things, depending upon the value of the argument passed to it. If the argument is an even number, the assembly language procedure simply swaps the two bytes in the word. If the argument is an odd number, the procedure returns the number of bits in the word that are set to 1.

Don't worry if you can't finish the entire lab. You can work on it at home and turn it in during lab next week if you don't finish in the 50-minute lab period.

Sample execution

In the first execution, the number 4 (an even number), with bit pattern 0000 0000 0000 0100, is swapped to produce the number 1024, with bit pattern 0000 0100 0000 0000.

Enter an integer:  4
Result:  1024

In the second execution, the number 37 (an odd number), with bit pattern 0000 0000 0010 0101, returns the number 3 (the number of 1 bits in the binary version of 37).

Enter an integer:  37
Result: 3

Linking C++ and Turbo Assembler Files

Follow these steps to create your program:

  1. Create a temporary directory on the C: drive. Name it cs2011. Make cs2011 the current directory.

  2. Obtain a copy of the C++ program that calls the assembly-language procedure. The C++ program is available at: http://www.cs.wpi.edu/~jburge/courses/c01/cs2011/labs/lab5.cpp  For easier downloading, there is a link to this file on the course homepage (look for lab 5).

  3. Write the assembly language procedure described on page 1. Your procedure will be returning a single integer, so it will be passed back to the main program in AX. Name your file bits.asm.  Here's some partial code (hints) to get you started:

  4.  

    	.386				;Microsoft Visual C++ generates 32-bit applications
    	public	_bits			;C++ puts an underscore before the entry label
    	.model	small
    
    	.code
    _bits	proc
    	push		ebp		;set up EBP (32-bit BP) so that you can grab the main
    	mov		ebp, esp	;program's argument from the stack
    
    	mov		ax, [ebp+8]	;get the argument from the stack
    					;(Each item on the stack is 32 bits (4 bytes)
    					; that's why the argument isa t [EBP+8])
    
    					;check to see if AX is even or odd
    					;(use SHR and check the carry)
    
    odd:					;get a fresh copy of the stack argument
    					;go through a loop 16 times
    					;each time throuh the loop, shift 1 position
    					;count how many times a 1 gets shifted into
    					;the carry
    
    					;go to done
    					;(your answer should be in AX)
    
    eve:					;get a fresh copy of the stack argument
    					;swap bytes of the argument (use ROL)
    					;(your answer should be in AX)
    
    done:	pop		ebp		;restore EBP
    	ret				;return to main program
    _bits endp				;(C++ cleans up the stack)
    
    
    	end
    	
  5.  Using TASM, assemble your procedure with the command line:

    tasm/ml/mx bits.asm

    The switches are needed to make all symbols case-sensitive, as they are in C++.

  6. You will now use Microsoft Visual C++ to create an executable file:
  7. Run the program using the data from the sample executions.


  8. Answer the following questions:

 

 

 

 

 

You will need to hand in this handout at the end of your lab session to receive credit for the lab.