Test 3 Arrays/Interrupts/Recursion

Use the Data Segment definitions below in answering the following questions 1 and 2.

1a. (5) Determine the following data segment offsets. Use hexadecimal base.

Offset    Data      Segment
______         L    Dw   ?
______         X    Dw   9, -5, 74, 28
______         Cnt  Dw   6
______         Src  Db   ' b bd f',0         ;; Zero byte terminated string
______         Dst  Db   7 dup(?)
          Data      Ends

1b. (12) What are the values in each of the listed registers after execution of ALL the following instructions? Indicate number base used.
          Mov  Ax, Seg Data
          Mov  Ds, Ax
          Mov  Es, Ax
          Lea  Bx, X
          Mov  Si, 4
          Mov  Cx, [Si]
          Mov  Dx, X[Si]
          Add  Si, 6
          Lea  Di, [Bx][Si]
          Mov  Al, [Di]
          Mov  Ah, [Di]+1

          Ax   ____________        Bx   ______________

          Cx   ____________        Dx   ______________

          Si   ____________        Di   ______________

2. The PROCEDURE named Compress accepts a zero byte terminated character string as input. It returns as output the input string with all blanks removed and a zero terminating byte. An example of a zero byte terminated string definition and the hexadecimal values stored in memory is:
                    Src  Db   ' b bd f',0         
               
                    20 62 20 62 64 20 66 00
                       b     b  d     f

Procedure parameters are defined as follows:

     Input - Offset of the first character of input string.

     Output - Offset of the first character of output string
On return to the calling procedure, the output string is to have all blanks removed. The C++ function prototype definition would be:
void Compress( char Input[], char Output[]);
2a. (5) Write the corresponding assembly instructions to call Compress using the C++ statement of:
Compress( Src, Dst );
 
 Invoke Compress, Addr Src, Addr Dst


2b. (15) Write the Compress function.
void Compress( char Input[], 
               char Output[]) {  
    while (*Input != '\0') {
        if (*Input != ' ') {
            *Output = *Input;
            Output++;
        }
        Input++;
    }
    *Output='\0';
}
Compress	proc	near C, input  : near ptr byte, 
                                output : near ptr byte
		Mov	eSi, input
		Mov	eDi, output
					 	
    	@@while:			 	
        	Cmp	byte ptr [eSi], 0 	
            	Jne	@@do		 	
            	Jmp	@@endwhile	 	
        @@do:				 	
          @@if:	Cmp	byte ptr [eSi], ' ' 	
    		Jne	@@then			
    		Jmp	@@endif			
	  @@then:			
  		Mov	Al, [eSi]
		Mov	[eDi], Al
		Inc	eDi
	  @@endif:
		Inc	eSi
		Jmp	@@while
	@@endwhile:
		Mov	byte ptr [eDi], 0
		Ret
Compress	Endp


4a. (5) Complete diagram of recursive calls and result in computing S(6) given the definition S(n) for a sequence of:
     S(1) = 1                                          S(6)
     S(2) = 2                                         /    \
     S(n) = n + S(n-1) * S(n-2)  for n > 2         S(5)    S(4)
                                                  /   \    /   \









b. (20) For S(4), accurately show VALUES (return address offsets, etc.)
on the stack for the DEEPEST recursion. Start at IP=2001 with SP=50F0.
                                               Stack
Offset                               Values    Label         
2001      Mov  Bp, 5                 _________________      Offset
2003      Push 4                    |        |        |     50D2 
2006      Call S                    |________|________|
2009      Add  Sp, 2                |        |        |     50D4
             :                      |________|________|
                                    |        |        |     50D6
     N    Equ  word ptr [Bp+4]      |________|________|
                                    |        |        |     50D8
200A S    Proc Near                 |________|________|
200A      Push Bp                   |        |        |     50DA
200D      Mov  Bp, Sp               |________|________|
200E  If: Cmp  N, 2                 |        |        |     50DC
2010      Jle  Then                 |________|________|
2013      Jmp  Else                 |        |        |     50DE
2015   Then:                        |________|________|
2015      Mov  Ax, N                |        |        |     50E0
2017      Jmp  EndIf                |________|________|
2019   Else:                        |        |        |     50E2
2019      Mov  Dx, N                |________|________|
201A      Sub  Dx, 1                |        |        |     50E4
201D      Push Dx                   |________|________|
201F      Call S                    |        |        |     50E6
2020      Add  Sp, 2                |________|________|
2021      Push Ax                   |        |        |     50E8
2022      Mov  Dx, N                |________|________|
2024      Sub  Dx, 2                |        |        |     50EA
2027      Push Dx                   |________|________|
2029      Call S                    |        |        |     50EC
202A      Add  Sp, 2                |________|________|
202B      Pop  Dx                   |        |        |     50EE
202C      Mul  Dx                   |________|________|
202E      Add  Ax, N                |        |        |     50F0 
2030 EndIf:                         |________|________|
2030      Pop  Bp                   
2031      Ret                       
     S    Endp

5a. (15) Given the recursive definition of A and the corresponding C++ function, write the Assembly language FUNCTION.

A(m,n) = n                    int A( int m, int n) {
          if m = 0            {
A(m,n) = A(m-1,1)                  if (m == 0)
          if n = 0                    return n; 
A(m,n) = A(A(m,n-1), m-1)          if (n == 0)
                                      return A(m-1, 1);
                                   return A(A(m,n-1),m-1); 
                              }
5b. (5) Give the corresponding Assembly instructions to do:

              X= A(6, 3);
 
 
 

5c. (5) Briefly describe the role the BP register plays in recursive functions:
 
 
 
 
 


6a. (5) Assume that the procedure OverFlow is to be invoked by a Type 6 interrupt. Give instructions necessary to initialize the interrupt vector to invoke OverFlow on the execution of the instruction:

                    Int 6

6b. (15) Write the complete INTERRUPT procedure named OverFlow that prints the message of 'Overflow error' on a type 6 interrupt. Remember that interrupt procedures must not alter registers. Use the DOS function 9 to output the character string. Its definition is:

          Int 21H,  Function 09H, Output character string
          _________________________________________________________________
          Call with:     AH        = 09
                         DS:DX     = segment:offset of string
          Returns:       Nothing
          Notes:         The string must be terminated with the character
                         '$', which is not output.
          _________________________________________________________________
          Example:       Output the string 'Hello'.

                         Mov  Ah, 9
                         Mov  Dx, Seg String
                         Mov  Ds, Dx
                         Mov  Dx, Offset String
                         Int  21h
                              :
               String    db   'Hello$'

7. (25) Given the following description of the Keyboard BIOS or DOS functions, write a function named ReadString to read keyboard characters and store the characters into a string array implementing the following:
  1. Repeat until either 80 characters or a carriage return character has been read. The ASCII code for a carriage return character is 1310.
  2. Do not store the carriage return character in the string array.
  3. The string should be terminated by a zero byte.
  4. The prototype is:    void ReadString( char s[] );

void ReadString(char s[]) {
    char Al;
    int Cx=80;
    do {
            cin >> Al;
            *s = Al;
            s++;
            Cx--;
    while (Cx != 0 && Al != 13);
    s--;
    *s = '\0';
}

 Keyboard BIOS interrupt description
 _________________________________________________________________________ 
|Type 16h      AH | Function            Procedure Outputs                 |
|_________________|_______________________________________________________|
|              0  | Read Character      Ah-register contains scan code    |
|                 |                     for next keyboard character       |
|                 |                     Al-register contains ASCII code   |
|                 |                     for next keyboard character       |
|                 |                     Character is deleted from buffer  |
|                 |                                                       |
|              1  | Read buffer status  ZF = 0 implies input buffer is    | 
|                 |                     not empty. Ah-register contains   |
|                 |                     scan code for next keyboard       |
|                 |                     character Al-register contains    |
|                 |                     ASCII code for next keyboard      |
|                 |                     character. Character is not       |
|                 |                     deleted from buffer.              |
|                 |                                                       |
|_________________|_______________________________________________________|
 DOS input description
 _________________________________________________________________________ 
|Type 21h      AH | Function            Procedure Outputs                 |
|_________________|_______________________________________________________|
|              0  | Read Character      Al-register contains ASCII code   |
|                 |                     for next keyboard character.      |
|_________________|_______________________________________________________|