CS301 – Fall 2003

Prof. Hartman

Homework #4

Due Wednesday, October 22nd by 4:45 pm

 

  1. Explain how many times each of the following LOOP operations loop:

a)                  mov ecx,1
L5: 
    
     loop L5


b)      L5:  mov ecx,10
    
     loop L5


c)                  mov ecx,0
L5:
    
     loop L5


d)                  mov ecx,10
L5:
     inc ecx
     loop L5


  1. Assume ecx and edx contained unsigned data and that eax and ebx contain signed data. Determine the cmp (when necessary) and conditional jump instructions for the following: (Your code should jump to somewhere if the condition is true.)

a)      Is eax equal to or smaller than ebx?

b)      Is ecx equal to or smaller than edx?

c)      Is eax greater than ebx?

d)      Is ecx greater than edx?

e)      Does edx contain 0?

f)        Did an overflow occur?

  1. Code and test a program that contains the following set of instructions. Determine the value in the destination register, and the values of OF, ZF, SF, and CF.

a)      mov al, 0FFh
add al, 1

b)      mov bl, 24h
sub bl,bl

c)      mov  cl, 10101010b
add  cl, 01010101b

d)      mov  dl, 11001100b
add  dl, 01110011b

  1. Write an assembly language program that counts the number of  “1” bits in a doubleword in two different ways. First, use a rotate or shift command (inside a loop). Second, encode the method shown below in C into assembly language.

int bit_count(unsigned int x )

{

int cnt = 0;

 

while( x != 0) {

     x = x & (x-1);

     cnt++;

     }

return cnt;

}

Write your code using subroutines, and use no static variables – that is, your .data and .bss segments should be empty except for string data. (If you need local variables, use stack space as in  local1.asm or local2.asm) Pass any parameters to your routines on the stack, and return the answer in EAX. Your program should ask the user to enter an integer, and report the answer that each subroutine returns.

 

You may use the I/O routines from Carter’s book. Refer to the handout about using NASM with Windows and Visual C++ for links to the I/O files if you don’t have them. For full credit you must turn in a well-commented listing including your name and the problem number.