CS-220, Practice Exam 2
Answer Key


1. Suppose we have defined the following data segment:

data    segment
x       db       10
y       dw       4
z       db       3,5,2,7,8,6,1,4,9
data    ends

and that the following code segment program fragent executes:

        mov      ax,data
        mov      ds,ax
        mov      es,ax
        mov      si,y                si = 0004
        lea      bx,z                bx = 0003
        mov      al,z[si]            al = 08
        mov      ah,z[si][bx]        ah = 04
        mov      di,y[bx]            di = 0205
        mov      dl,[bx+5]           dl = 06

What values will be stored in each of the general purpose registers
affected by this program fragment after it executes?


2. Assuming that low memory of an 8086-based computer system is as shown
below, to what physical address (a 5-digit hex number) would control be
transferred when the instruction INT 4 executes? (All numbers are
expressed in hex; in each case the first number is the address and the
second number is the byte that is stored at that address.)

00000 4f    00004 32    00008 e5     0000c a1    00010 37    00014 cc
00001 7b    00005 78    00009 3f     0000d 4b    00011 5d    00015 66
00002 bb    00006 80    0000a 27     0000e 99    00012 0f    00016 91
00003 65    00007 7a    0000b 4f     0000f dd    00013 10    00017 bb
                                                       ^
 4 * 4 = 16 = 10h.            Vector = 100f:5d37 ______|

                              Physical address = 100f0 + 5d37 = 15e27h


Write some code that will cause control to be transferred to the
instruction stored at physical address 3af6bh whenever any subsequent
INT 4 is executed.

           mov     cx,0            ;get start address (0) of vector table
           mov     es,cx
           mov     si,16           ;point to int 4 vector offset in table
           mov     es:[si],0bh     ;offset of new ISR to table
           mov     es:2[si],3af6h  ;base address of new ISR to table

  [This could also be done using DOS int 21h, function 25h]


3. The program fragment given below outputs a pulse (a 1 then a 0) to
a device connected to one of the ports of a certain 8086-based
microcomputer.

(A) Under what conditions is the pulse output?  When Bit-1 of port 513h = 1

(B) To what port is the pulse sent?  57

(C) Which bit is used for sending out the pulse?  Bit-6

(D) Assuming a 5 MHz system clock, what is the duration of the pulse?

  Duration D = (# of clks/iteration) * (# iterations) * Tclk
                      17                    10


  D = 17 * 10 * Tclk

  But Tclk = 1/f

  So  D = 170/f

      D = 170 / 5000000

      D = 34 microseconds


(E) Explain in words how the pulse is produced.

    Whenever Bit-1 of port 513h is set to 1, the program sends a 1 out to
    Bit-6 of port 57, waits for 34 microseconds, and sends out a 0 to the
    same bit of the same port.


(Recall that in Intel nomenclature, the least significant bit of an 8-bit
register is called bit-0 and the most significant bit is bit-7).

On an 8086 processor, each iteration of the LOOP instruction requires
17 system clock pulses.

                 AND    AL,0BFH  ;clear bit-6 of port 57
                 OUT    57,AL
                 MOV    DX,513H
    XXX:         IN     AL,DX    ;polling loop to check bit-1 of port 513h
                 TEST   AL,2
                 JZ     XXX
                 MOV    CX,10    ;set up delay loop
                 OR     AL,40H   ;set bit-6 to 1
                 OUT    57,AL    ;output the 1 to start the pulse
    YYY:         LOOP   YYY      ;wait (delay loop)
                 XOR    AL,40H   ;toggle bit-6 to 0
                 OUT    57,AL    ;output the 0 to end the pulse
                 JMP    XXX      ;repeat process


4. Write some IBM-PC assembly language code that will display a horizontal
line composed of blinking magenta asterisks on a yellow background across
the middle of the screen. The line should go from the left edge of the
screen to its right edge. Assume that the video mode is the default mode
3. You should not use int 10h.

; Start of middle-of-screen row offset: 12*80*2 = 1920

        mov     ax,0b800h   ;Start of text video RAM
        mov     es,ax
        mov     si,1920     ;si points to next VRAM character cell address
        mov     al '*'      ;ASCII code for asterisk
        mov     ah,11100101 ;display attribute (1==>blink, 
                            ;110==>yellow bkgnd (RG), 0101==>magenta (RB))
        mov     cx,80       ;80 characters in the row
doit:   mov     es:[si],ax  ;display next character
        add     si,2        ;point to next character cell
        loop    doit        ;again 
        

5. On an IBM-PC compatible computer you are to write a routine that will 
output an unending 500 Hz tone, regardless of the processor and system clock 
being used. Write an assembly language routine that will produce that tone.

const      dd      1193180  ;this would be in a data segment

           ; compute timer count (1193180/500)
           mov       ax,word ptr const
           mov       dx,word ptr const+2
           mov       bx,500
           div       bx
           mov       dx,ax         ;store count in dx

           mov       al,10110110b  ;command word to timer
           out       43h,al
           mov       ax,dx         ;program timer count
           out       42h,al
           mov       al,ah
           out       42h,al
           in        al,61h
           or        al,00000011b  ;turn on timer (start tone)
           out       61h,al

   
6. A certain IBM-PC compatible computer is using as its "drive B" a system
in which the diskette is double-sided, 80 tracks per side, 18 sectors per
track, with 512 bytes of information stored on a sector. (A) What is the total 
number of bytes of information that can be stored on the diskette? (B) Write
some assembly language code that will fill track 3, sector 4 on side 0 of the 
diskette with the letter 'A'.

(A) # bytes = 2*80*18*512 =  1,470,560 bytes
    (Single sided would be half that amount)

(B) 
data    segment
s       db       512 dup ('A')
data    ends

code    segment
;       mov      ax,data
        mov      ds,ax  ; es and ds point to our data segment
        mov      es,ax
        mov      ch,3  ; track 3
        mov      cl,4  ; sector 4
        mov      dh,0  ; side 0
        mov      al,1  ; one sector
        lea      bx,s  ; address of write buffer
        mov      ah,3  ; write sector service  
        int      13h   ; write the sector 
;       rest of program
code    ends

7. The following is a C program that will do what is required:

// 2D Array
#include <stdio.h>

int avg(int arr[5][3]);

int main()
{
   int i,j,x;
   int data[3][5] = { 11, 12, 13, 14, 15, 21, 22, 23, 24, 25, 31, 32, 33, 34, 35 };

   printf("      ");
   for (i=0; i<5; i++)
      printf("%d   ", i);
   printf("\n");
   for (j=0; j<3; j++)
   {
      printf("%d    ", j);
      for (i=0; i<5; i++)
         printf("%d  ", data[j][i]);
      printf("\n");
   }

   x = avg(data);
   printf("The average of all data is %d\n", x);
   return (0);
}

int avg(int arr[3][5])
{
   int sum=0;
   int i,j; 
   for (j=0; j<3; j++)
      for (i=0; i<5; i++)
         sum += arr[j][i];
   return (sum/15);
}