SOLUTIONS
CS-220 Practice Exam

1. The following assembly language program has been written for the simple
computer we talked about during the second week of class (the one you used 
the simulator with in Lab 1:

address  contents    Assembly Language
--------------------------------------
   9                     ORG   9
 -----
   09      003       X:  DW    3
 -----    -----
   10      004       Y:  DW    4
 -----    -----
   00                    ORG   0
 -----         
   00      109           LDA   X
 -----    -----
   01      500           MBA
 -----    -----
   02      110           LDA   Y
 -----    -----
   03      400       BB: SUB
 -----    -----
   04      706           JN    AA
 -----    -----
   05      603           JMP   BB
 -----    -----
   06      209       AA: STA   X
 -----    -----
   07      800           HLT
 -----    -----
                         END

(A) Fill in the blanks with the decimal values that would be placed in the 
address and contents fields when this program is assembled into machine 
language.

(B) Assume the machine code for this program has been loaded into memory.
Complete the following table of values that would be stored in the various
registers and on the buses, as well as the active control signals during 
each of the first ten clock pulses of the fetch-decode-execute cycle when 
the computer's clock starts. 

Clock  PC  MAR  MDR  ACC  ALU  NF  B  IR   Ring   Active  
Pulse                                      Pulse  Signals
----------------------------------------------------------
0      0    0    0    0    0   0   0   0     0    EP,LM    
1               109                          1    R
2      1                              109    2    ED,LI,IP
3           9                                3    EI,LM
4               003                          4    R
5                    003                     5    ED,LA
6           1                                0    EP,LM
7               500                          1    R
8      2                              500    2    ED,LI,IP
9                                 003        3    EA,LB
10

(C) The figure below is a diagram of the simple computer's hard-wired control 
unit. You are to highlight all signal lines, AND gates, OR gates, and flip-flops 
that are active (are high), as well as write in the values that are contained in 
the Instruction Register, the Negative Flag, and the "Active Control Signals" 
boxes in one of these diagrams at each of the following moments in the execution 
of the example program:

Ring Pulse 0 of the "fetch" of the second LDA instruction in the program
Ring Pulse 2 of the "fetch" of the SUB instruction
Ring pulse 5 of the second LDA instruction
Ring pulse 3 of the SUB instruction the first time it is executed
Ring pulse 3 of the JN instruction the first time it is executed
Ring pulse 3 of the JN instruction the second time it is executed
Ring pulse 0 of the next instruction fetched
Ring pulse 4 of the STA instruction
Ring pulse 5 of the STA instruction

Run the simulator to see the results

(D) After the program is done executing, some value was stored at some address
in memory. What is the value stored and what is the address?

The program causes a -2 to be stored at memory address 09 

2. (A) Convert -58.40625 (decimal) to the 32-bit IEEE short real
floating point format discussed in class. Your result should be
expressed in hex.

(A) -58.40625 (decimal) to the 32-bit IEEE short real floating point format
discussed in class. Your result should be expressed in hex.

   C269A000h
   ---------

(B) The number 43572000h is a hexadecimal representation of a 32-
bit IEEE short real number. Determine the decimal value of this
number.

(B) 43572000h IEEE short real floating point = 215.125 decimal

    ----------------------------------------------------------

3. Assume that the 8086 registers contain the following data:

AX = 19 (decimal),  CX = 2,  BH = 10 (decimal),  BL = -3

What will be the contents of any register changed by each of the
instructions given below? Express your answers in hex.

If the zero flag, the carry flag, the parity flag, the sign flag, and/or
the overflow flag are affected by the instruction, give the new status (1
or 0) after execution. Each instruction is independent. In other words, the
registers always contain the same initial values given above before
execution of each instruction.

(A) ADD AX,127
             AX = 92h,  CF=0(NC), PF=0(PO), ZF=0(NZ), SF=0(PL), OF=0(NV)
             -----------------------------------------------------------

(B) CMP CX,3
             CX = 2,  CF=1(CY), PF=1(PE), ZF=0(NZ), SF=1(NG), OF=0(NV)
 (result=-1) --------------------------------------------------------

(C) SHR BH,CL
             BH = 2,  CF=1(CY), PF=0(PO), ZF=0(NZ), SF=0(PL), OF=0(NV)
             ---------------------------------------------------------

(D) SUB BH,10
             BH = 0,  CF=0(NC), PF=1(PE), ZF=1(ZR), SF=0(PL), OF=0(NV)
             ---------------------------------------------------------

(E) TEST AL,0Fh
(Result=3)   AL = 13h,  PF=1(PE), ZF=0(NZ), SF=0(PL)
             ---------------------------------------

(F) XOR BH,AL
             BH = 19H,  PF=0(PO), ZF=0(NZ), SF=0(PL)
             ---------------------------------------

(G) MUL CL
             AX = 26h (38),  CF=0(NC), OF=0(NV)
             -----------------------------

(H) IDIV BL
             AL = 0FAh (-6),  AH=01,  NO FLAGS AFFECTED
             ------------------------------------------

4A. (memory)
3B89:0000  03 03 03 2E A3 3F 00 E8-03 48 45 4C 4C 4F B0 72   .....?...HELLO.r
3B89:0010  36 FA FE FF 04 02 3A F0-17 5F 00 00 00 00 00 00   6.....:.._......

4B. (registers)
AX=A32E  BX=723A  CX=00C1  DX=00FE  SP=007C  BP=0000  SI=4F4C  DI=4C4C
DS=3B89  ES=3B71  SS=3B81  CS=3B8B  IP=0020   NV UP EI PL ZR NA PE NC


5. The following is output from the -r and -d DEBUG commands after a
certain 8086 program has run with a breakpoint set.

-r
AX=200F  BX=0002  CX=0049  DX=0001  SP=0020  BP=0000  SI=0000  DI=0000
DS=3B49  ES=3B37  SS=3B47  CS=3B4A  IP=000D   NV UP EI PL NZ NA PO NC
3B4A:000D F6F3          DIV     BL

-d0,f
3B49:0000  0F 20 06 5B 66 1F 15 2A-37 6B CC 5D 00 21 3D 18   . .[f...........

The answer to each of the following questions should be expressed in hex:

(A) What is the offset of the next instruction to be executed?  000Dh
                                                                -----

(B) What is the physical address (a 5-digit hex number) of the next
instruction to be executed?  3B4ADh
                             ------

(C) What is the machine language code for the next instruction to be
executed?   F6F3h
            -----

(D) What is currently stored in BH register?   00
                                               --

(E) What byte value is stored at offset 9 in the data segment?  6Bh
                                                                ---

(F) What is the physical address of that byte?  3B499h
                                                ------

(G) What is the status (0 or 1) off the Carry Flag, the Zero Flag, and the
Parity Flag?   CF=0, ZF=0, PF=0
               ----------------

(H) At what physical address does the stack segment begin?   3B470H
                                                             ------

(I) If the next instruction to be executed were a CALL to a near procedure
at offset 005Eh instead of the DIV instruction, what would be the new
contents of any registers changed by execution of that instruction. (Assume
that the registers are as indicated above before the CALL is executed.)

     SP=001Eh, IP=005Eh


6. A variable called 'flag' is defined with a db. Write an 8088 assembly
program fragment that will do one of the following, depending upon the
contents of 'flag':

If 'flag' contains the ASCII code for an 'M' it should multiply the
contents of bl and cl registers, store the result in the word-size variable
called 'mul_result', and store a zero in the byte-size variable called
'error'.

If 'flag' contains the ASCII code for a 'D' it should divide the contents
of si register by the contents of ch register, store the integer part of
the quotient in the byte-size variable called 'div_result', and store a 0
in 'error'.

If 'flag' contains anything else, it should store the ASCII code for an 'E'
in 'error'.

        cmp     flag,'M'   ;if it's an 'M'
        je      multip
        cmp     flag,'D'   ;if it's a 'D'
        je      divide
        mov     error,'E'  ;it's neither, so store an 'E'
        jmp     done
multip: mov     al,bl      ;multiply the numbers
        mul     cl
        mov     mul_result,ax
        mov     error,0
        jmp     done
divide: mov     ax,si      ;divide the numbers
        div     ch
        mov     div_result,al
        mov     error,0
done:


7. Write a C language program that will compute the value of either the expression
a * 2^n  or the expression  a/(2^n), where a and n are positive integers entered 
by the user. The former should be computed if a is greater than n; the latter if 
a is greater than n. In either case, the result should be output to the screen. If 
a and n are equal nothing should be computed.  The following are sample program 
runs (underlined text is entered by the user):

Enter two integers:
56 3
56 divided by 2 to the power 3 is 7

Enter two integers:
7 10
7 times 2 to the power 10 is 7168

(Hint: use integer arithmetic and bit shifting.)
#include <stdio.h>

int main()
{
   int a,n,x;          /* the variables */
   printf("Enter a two integers\n");
   scanf("%d %d", &a, &n);
   if (a<n)
      x=a<<n;
   else
      x=a>>n;
   printf ("The result is %d\n", x);

   return (0);
}


8. Write a C language program that will prompt the user to enter a two-digit 
hexadecimal integer, determine whether it is the ASCII code for a hexadecimal 
digit, and,if so, output that digit to the screen. If the integer entered is 
not the ASCII code for a hexadecimal digit, the program should display the word 
"ERROR" on the screen.The following is what should be observed on the screen 
during a couple of runs of the program (underlined text is entered by the user): 

Enter a two-digit number
42
The digit is B

Enter a two-digit number
37
The digit is 7

Enter a two-digit number
1F
ERROR

Enter a two-digit number
99
ERROR 
#include <stdio.h>

int main()
{
   int a;          /* the variables */
   char b;
   printf("Enter a two-digit number\n");
   scanf("%x", &a);
   if ( (a<0x30) || ((a>0x39)&&(a<0x41)) || (a>0x46) )
      printf("ERROR\n");
   else
   {
//****** The following could be done if we did not have a %c format string specifier:
//      a = a-0x30;
//      if (a<=10)
//        b = '0'+a;
//      else
//      {
//        a=a-7;
//        b=a;
//      }
//      b=a; 
//          b would then be printed out as an integer ******/

      printf("The digit is %c \n" , b);
   }
   return (0);
}