You are on page 1of 7

COMPE 271 - FALL 2013

Midterm 1
Absolutely No Books Notes Calculators- Computers
Question 1 : (Knowledge assessed Assembly language , program flow) (20 points)
Given the following function compareSET that accepts two integer arguments: If the second argument
is larger than the first argument, function compare set exchanges the values of these variables and
returns 1. Otherwise returns 0.
.global
_compareSET
int compareSET(int *a, int *b)
_compareSET:
{
int temp;
If ( *b > *a )
pushl %ebp
{ temp = *b;
movl %esp,%ebp
*b = *a;
*a = temp;
movl 8(%ebp),%ebx
return (1);
}
movl 12(%ebp),%ecx
else return(0);
movl (%ebx),%edx
}
_____________________________________________
The main function calling the function
CompareSET is given below. Write the function

compareSET in assembly language starting with the


skeleton code given on the right.
#include <stdio.h>
#include <stdlib.h>
extern

int compareSET();

int main(void)
{
int a, b;
a=10;
b=7;
if (compareSET(&a,&b) == 0)
{
puts("No change was needed");
}
else puts("Values have been changed
");
}

The function is called as given in the following program.

movl $0,%eax
cmpl %edx,(%ecx)
JLE done
movl (%ecx),%eax
movl %eax,(%ebx)
movl %edx,(%ecx)
movl $1,%eax
done:
movl %ebp,&esp
popl %ebp
ret

Question 2 : (Knowledge assessed Assembly language , program flow) (20 points)


Given the following function written in C programming language and the partially completed equivalent
Assembly code given on the answer page. Complete the assembly language code given on answer page.
Please use only the answer sheet and write in the space provided.

int Threshold(int *numbers, int count, int threshold)


{
int i;
for (i=count-1 ; i >= 0 ; i--)
{
if (numbers[i] > threshold) numbers[i] = threshold;
}
}
Solution : Please use only the space provided for your answer.

.globl _Threshold
_Threshold:
pushl %ebp
movl %esp,%ebp
movl 8(%ebp), %ebx
movl 12(%ebp), %ecx
movl 16(%ebp), %edx
dec %ecx
loopstart:
cmpl $0,%ecx
JL done
cmpl %edx,(%ebx,%ecx,4)
JLE nochange
movl %edx,(%ebx,%ecx,4)
nochange:
dec %ecx
JMP loopstarrt
done:
popl
ret

%ebp

Question 3: (12 points) (Floating Point Number System)


Please use space provided for your answer.
a- (5 points) Consider a 10 bit floating point number system. The sign bit is the most significant bit.
4 bits are reserved for the exponent and remaining 5 bits are the mantissa. A bias of 7 is to be
used. What is the floating point representation of number 22.5

0 |1 0 1 1|0 1 1 0 1

b- (5 points) Consider a 10 bit floating point number system. The sign bit is the most significant bit.
4 bits are reserved for the exponent and remaining 5 bits are the mantissa. A bias of 7 is to be
used. What is the floating point representation of number 12.5

0 |1 0 1 0 |1 0 0 1 0
c- (10 points) Using the rules for floating point addition , add the two floating point numbers in (a)
and (b) and present the result as a binary floating point number. Please note that it is not allowed
to add the numbers in decimal number system and represent the number in floating point.

0 |1 1 0 0|0 0 0 1 1

Question 4 : ( 8 points)
a- Consider a 16 bit binary number system. Write the Hexadecimal equivalent of the most negative number.
Answer

0 x 8000

b- Consider a 16 bit binary number system. Write the number -127 in binary.
Answer

1111 1111 1000 0001

c- Compute two's complement of the number 0x56FF and write the result in Hexadecimal.
Answer

56FF- 0101 0110 1111 1111


Twos complement- 1010 1001 0000 0001
Result= 0 x A901

d- Consider a 16 bit binary number system. What is the hexadecimal equivalent of the signed binary number
0101011101110011.
Answer

0 x 5773

Question 5 : (10 points)


a- Write the assembly language command to add register %ebx to the contents of memory location
addressed by %ecx register. Store the result in the memory location pointed by %ecx register.

addl %ebx, (%ecx)

b- Write an assembly language instruction equivalent of the C statement Numbers[i] = 11; Assume in
assembly the base Numbers is mapped to register %ebx and index i is mapped to %esi register. Also
assume integer size variables.

movl $11, (%ebx,%esi,4)

c- Is the following statement correct ? If not what is wrong with it ?

movl (%ebx),(%edx)- Not correct, memory to memory operation

d- What is wrong with the following instruction?

addl %ebx, $4- Not correct, destination is a constant

Question 6 : (Assembly Language Programming ) (10 points)


Consider the following C function. Write the corresponding assembly language function to perform the
same operation. (25)
int myFunction (int a)
{
return (a + 30);
}
Assembly language function:
.global _myFunction
_myFunction:
pushl %ebp
movl %esp,%ebp
movl 8(%ebp),%eax
addl $30,%eax
popl %ebp
ret

Extra Credit : ( 10 points)


Given the following C functions, implement equivalent assembly language function in the given space.
int addUS(int *num, int count)
{
int i;
i=1;
do {
num[i] +=num[i-1];
i++;
} while ( i < count);
}

.global addUS
_addUS:
pushl %ebp
movl %esp,%ebp
movl 8(%ebp),%ebx
movl 12(%ebp),%ecx
movl $1,%esi
continue:
movl %esi,%edi
dec %edi
movl (%ebx,%edi,4),%eax
addl %eax,(%ebx,%esi,4)
inc %esi
cmpl %esi,%ecx
JG continue
popl %ebp
ret

You might also like