Midterm Answers

Download as pdf or txt
Download as pdf or txt
You are on page 1of 9

COMPE271

 –  FALL  2010  
MIDTERM    1  

ANSWERS  
Question  1       (15  points)    :      Write  a  procedure  is_little_endian  that  will  return  1  
when  compiled  and  run  on  a  little-­‐-­‐-­‐  endian  machine,  and  will  return  0  when  
compiled  and  run  on  a  big-­‐-­‐-­‐endian  machine.  This  program  should  run  on  any  
machine,  regardless  of  its  word  size.  You  may  write  the  procedure  in  C  or  in  
Assembly  language.  

 Answer:      
.globl     is  little_endian  
_is  little_endian:  
pushl  %ebp  
movl  %esp,%ebp  
movl  $1,-­‐4(%ebp)  
movb  -­‐4(%ebp),%bl  
movl  $0,%eax  
andl  %bl,%bl  
jz  out  
movl  $1,  %eax  
 out:  
  popl   %ebp  
  ret  
Question  2  (10  points)  :          
Assume   we   are   running   code   on   a   10-­‐bit   machine   using   two's   complement  
arithmetic   for   signed   integers.   A   "short"   integer   is   encoded   using   5   bits.   Fill   in   the  
empty  boxes  in  the  table  below.  
The  following  definitions  are  used  in  the  table.  
short  sy=  -­‐15;  
int  y  =  sy;  
int  x  =  -­‐117;  
unsigned  ux  =  x;  
 
Note  :  You  will  not  fill  in  entries  marked  with  "-­‐".  

Expression     Decimal  representation     Binary  Representation  


-­‐     138     0010001010  
-­‐     -­‐62     1111000010  
-­‐     -­‐174     1101010010  
ux     907     1110001011  
y     -­‐15     1111110001  
x>>1     -­‐58   1111000101  
Tmax     -­‐     0111111111  
 
Question  3  (10  points)  :      
Consider  the  following  10-­‐bit  floating  point  representation  based  on  the  IEEE  
floating  point  format:  
       *  There  is  a  sign  bit  in  the  most  significant  bit.  
       *  The  next  3  bits  are  the  exponent.  The  exponent  bias  is  23-­‐1-­‐1  =  3  

       *  The  last  6  bits  are  the  fraction.  


       *  The  representation  encodes  numbers  of  the  form:  V=(-­‐1)s  x  M  x  2E,  where  M  is  
the  significant  and  E  is  the  biased  exponent.    
The  rules  are  like  those  in  the  IEEE  standard(normalized,  denormalized,  
representation  of  0,  infinity,  and  NAN).  FILL  in  the  table  below.  Here  are  the  
instructions  for  each  field:  
*  Binary:  The  10  bit  binary  representation.  

       *  M:  The  value  of  the  significant.  This  should  be  a  number  of  the  form  x  or  x  ÷  y,  ,  
where  x  is  an  integer,  and  y  is  an  integral  power  of  2.  Examples  include  0,  3  ÷  4.  

       *  E:  The  integer  value  of  the  exponent.  


       *  Value:The  numeric  value  represented.    
 

Note:  you  need  not  fill  in  entries  marked  with  ”—”.  

Description     Binary   M     E     Value    


8.5     0110000100   -­‐     -­‐     -­‐    
-­‐     0111001010   10/63   111    or  7     Not  a    
Number  
19     0111000000   0   111   -­‐   Number  
too  large  
0.001     0000000000   0   0   -­‐   Number  
too  small  
 
Question  4  (15  points)  :  
Given  the  following  main  function  which  accepts  a  string,  calls  an  assembly  function  
reverseString  and  later  prints  the  string.  The  assembly  function  reverseString  
should  reverse  the  string  in  the  same  memory  space.  Write  the  assembly  function  
reverseString  to  reverse  the  string  passed  as  an  argument.    
#include  "stdio.h"    
int  main(int  argc,  char*  argv[])    
{    
char  myString[256];    
printf(“\n  Enter  the  string  :”);    
scanf(“%s”,myString);    
reverseString(myString);    
printf(“\n  Reversed  String  is  :%s”,myString);    

}    
 

.globl  _reverseString    
_reverseString:    

pushl       %ebp    
movl       %esp,%ebp     //TODO  //  Complete  the  function  ret  
movl         8(%ebp),%ebx  
xorl         %edi    ,  %edi  

L1:    
                         cmpb                                $0,(ebx,%edi,1)  
    je         F1  
  incl     %edi  
  jmp     L1  
F1:   decl     %edi     //  edi  now  points  to  the  last  element  of  array  

  xorl     %esi,%esi  
L2:   movb     (%ebx,%esi,1),  %cl  
  movb     (%ebx,%edi,1),  %dl  
  movb     %dl,  (%ebx,%esi,1)  
  movb     %cl,  (%ebx,%esi,1)  
  incl     %esi  
  decl     %edi  
  cmpl     %esi,%edi  
  jg     L2  

popl     %ebp  
ret  
Question  5  (10  points)  :    
Assume  you  are  an  Intel  X86  processor  and  execute  the  following  program  segment.  
Before  the  program  is  started  your  registers  contain  the  following  values.    
%EAX  :  0x00000000    
%EBX  :  0xFFFFFFFF    
%ECX  :  0xFFFFFFFE    
%EDX  :  0x00000000    
%ESP  :  0xFF0B4400    
 

What  will  be  the  register  contents  upon  the  execution  of  the  each  program  segment  
given  below?    
 
PUSHL  %EBX    
PUSHL  %EAX    
MOVL  %EAX,  %EBX    
ADDL  $0XFFFFFFFF,  %EAX    
Answer  :    

EAX  :  0XFFFFFFFF  
EBX  :  0x00000000  
ECX  :  0xFFFFFFFE  
EDX  :  0x00000000  
ESP  :  0xFF0B43F8  
 

 
Question  6    (15  points  )  :  Answer  the  question  given  in  the  linked  image.  You  can  
write  your  answer  in  the  answer  box.  

 
 
 

 
 
Answer  :    
 

int  looper(int  n,  int  *a)    


{  
int  i;  
      int  x  =  0;  //  3  points  

      for(i  =  0;  i  <  n;  i++)    


{  //  3points  
            if  (a[i]  >  x)  or    if  (!(a[i]  <=  x))  //  3  points      

                 x  =  a[i];  //  3  points  


            x++;  //  3  points  
        }  
return  x;  

}  
 
 

 
Question  7  (  10  points)      
Given  the  following  C  function.  Write  the  assembly  equivalent  of  the  code.    

int    isGreater(int  number1,  int  number2)    


{    
if  (number1  >  number2  )    
return  (1);    

else    
return(0);    
}  

 
Answer:      
.globl  _isgreater  
_isgreater:  
  pushl  %ebp  
  movl     %esp,%ebp  
  xorl   %eax,%eax  
  movl   8(%ebp),%ebx            
  movl   12(%ebp),%edx        
  cmpl   %ebx,%edx                        
  jle   done  
  movl   $1,%eax  
done:  
  popl   %ebp  
  ret  
 
 
Question  8     (10  points)      
Given  the  following  C  function.  Write  the  assembly  equivalent  of  the  code.    

int  addNumbers(int  number)    


{  
                    int  sum  =  0;    
    int  i  =  0;    
                         while  (i  <  number)    
                                           {    
                                                   sum  =  sum  +  i;    
                                                   i    =  i  +  1;    
                                           }    
       return(sum);    
}  

 
Answer  :    

 
.globl  _addNumbers  
_addNumbers:  
          pushl     %ebp  
          movl       %esp,%ebp  
  movl       8(%ebp),%ebx  
          xorl       %esi,%esi  
          xorl         %eax,%eax  
 
bak:  
  addl       %esi,%eax  
  incl       %esi  
          cmpl   %esi,%ebx  
          jne   bak  
  popl       %ebp  
          ret  
 
 
 

You might also like