Name: __________________
Section:_______________
To be able to incorporate an assembly-language procedure into a C++ program. To get some more practice using shift and rotate instructions.
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.
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
Follow these steps to create your program:
.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
You will need to hand in this handout at the end of your lab session to receive credit for the lab.