Call-by-value/Call-by-reference/Dynamic Variables/Bit Operations/Multi-byte Arithmetic/Macros


1. (25) Euclid's algorithm for finding the greatest common divisor (GCD) of two positive integers, A and B, in C++ is:
int GCD( int A, int B ) {
 int T;
     
 while (A != 0 && B != 0) {   
           A = A % B;
           T = A;
           A = B;
           B = T;
 };
          return A;
}
 
 
 
 
 
 
 
Answer

GCD   proc   C, A:dword, B:dword
         .WHILE A!=0 && B!=0
               mov     eDx, 0
               mov     eAx, A
               div       B
               mov     A, eDx
               mov     eCx, A
               mov     eBx, B
               mov     A, eBx
               mov     B, eCx
          .ENDWHILE
               mov     eAx, A
               ret
GCD    endp 
                

 

 

 

 

Answer

A       Equ     word ptr[eBp+8]
B       Equ     word ptr[eBp+12]

GCD     proc    near
        Push    eBp
        Mov     eBp, eSp
 while: Cmp     A, 0            ; while( A!=0 && B!=0) {
        Jne     And
        Jmp     Endwhile
   And: Cmp     B, 0
        Jne     Do
        Jmp     Endwhile
    Do: Mov     eDx, 0
        Mov     eAx, A
        Div     B
        Mov     A, eDx           ;   A = A%B;
        Mov     eCx, A           ;   T = A;
        Mov     eBx, B
        Mov     A, eBx           ;   A = B;
        Mov     B, eCx           ;   B = T;
        Jmp     While            ; }
Endwhile:
        Mov     eAx, A           ; return A;
        Pop     Bp
        Ret
GCD     endp
Write a COMPLETE assembly procedure to implement the above GCD. It is NOT necessary to give the complete program, only the FUNCTION. Parameters and results are passed using C++ conventions. Use stack or register for local variable T.


2. (30) The following is a brute force method for calculating prime numbers from 3 through 10,000 using the GCD function from Question 2. Implement the following C++ code FRAGMENT in Assembly language using the stack call-by-value or call- by-reference parameter passing conventions and procedures PutDec/PutStrng for input/output. Use data segment definitions of:
______________________________ 
.data                         |         for (I = 3; I <= 10000; I++) 
     I     dd  ?              |         {
     P     dd  ?              |              Prime = 1;
     G     dd  ?              |              G = 2;
     Prime dd  ?              |              while (G <= I && Prime == 1) 
     PStr  db  'Prime',0      |              {
     NPStr db  'not Prime',0  |                   P = GCD( I, G);
                              |                   if (P != 1 && P != I)
______________________________|                        Prime = 0;
                                                  G = G + 1;
                                             }; 
                                             if (Prime == 1)
                                                  cout << I << "Prime";
                                             else cout << I << "not Prime";
                                        };
Answer

        Mov    I, 3
for:    Cmp    I, 10000          
        Jbe    do
        Jmp    endfor
 do:    Mov    Prime, 1
        Mov    G, 2
 while: Mov    eAx, G
        Cmp    eAx, I
        Jbe    And1
        Jmp    endwhile
  And1: Cmp    Prime, 1
        Je     dowhile
        Jmp    endwhile
   dowhile:
        Push   G
        Push   I
        Call   GCD
        Add    eSp, 8   
        Mov    P, eAx
    if: Cmp    P, 1
        Jne    And2
        Jmp    endif
  And2: Mov    eAx, P
        Cmp    eAx, I
        Jne    then
        Jmp    endif
  then: Mov    Prime, 0
  endif:
        Inc    G
        Jmp    while
 endwhile:
   if2: Cmp    Prime, 1       
        Je     then2
        Jmp    else2
    then2:
        Lea    eDi, PStr
        Call   WriteString
        Jmp    endif2
    else2:
        Lea    eDi, NPStr
        Call   WriteString
        Jmp    endif2
    endif2:
        Inc    I
        Jmp    for
endfor:
Answer
G	equ    eBx
I	equ    eCx

        Mov    I, 3
@for:   Cmp    I, 10000          
        Jbe    @do
        Jmp    @endfor
 @do:   Mov    Prime, 1
        Mov    G, 2
 .WHILE G <= I && Prime == 1
        invoke GCD, I, G
        Mov    P, eAx
    .IF P != 1 && P != I
  	  Mov    Prime, 0
    .ENDIF
        Inc    G
 .ENDWHILE
 .IF	Prime == 1
        Lea    eDx, PStr
        Call   WriteString
 .ELSE
        Lea    eDx, NPStr
        Call   WriteString
 .ENDIF
        Inc    I
        Jmp    @for
@endfor:
 
 
 
 
 
 
 
 
 

3. (25) Trace the execution of the following to answer questions below:
Offset                             Offset 

       .data                            Subp   Proc     
00010     Q    dd      9           000B        Push   eBp   
00014     R    dd      26          000C        Mov    eBp, eSp    
                                   000D        Sub    eSp, 4     
                                   000E        Mov    eAx, [eBp+12]
                                   000F        Mov    [eBp-4], eAx
                                   0010        Mov    eBx, [eBp+8]
.code                              0011        Mov    [eBx], eAx
                                   0012        Mov    eAx, eBx
       Main  Proc                  0013        Mov    [eBp+8], 789Ah
                                   0014        Mov    eSp, eBp 
                                   0015        Pop    eBp       
0000     Push   Q                  0016        Ret              
0003     Push   offset R                Subp   Endp
0004     Call   Subp 
0005     Add    eSp, 8       
0006     Call   WriteDec      
0007     Ret                                          
       Main     Endp        


A. Fill in values on STACK BEFORE       Assume initial values of:
execution of the instruction at                   eIP = 0000      
offset 0014 hexadecimal.                          eSP = 021E                  
                                                  eAX = 6534
          _________________                       eBX = 0054      
 021E    |                 |                      eBP = 1234      
         |_________________|                            
 021A    |                 |                                     
         |___9_______Q_____| eBp+12     B. Fill in final REGISTER values and variables
 0216    |   0014  offset R|               immediately BEFORE the execution
         |___789A          | eBp+8         of instruction at offset 0007
 0212    |                 |                                    
         |___0005__ret addr| eBp+4                eIP = 0007
 020E    |                 |                      eSP = _021E_____ 
         |___1234__________| eBp                  eAX = _0014_____             
 020A    |                 |                      eBX = _0014_____             
         |____9 ___________| eBp-4                eBP = __1234____       
 0206    |                 |                      Q   = __9      _
         |_________________|                      R   =   9       
 0202    |                 |
         |_________________|

 Offset     Stack
4. (15) Short Coding

a. Using either the AND or the OR operation, convert an 'A'-'Z' character (41h-5Ah) in the AL register to 'a'-'z' (61h-7Ah).

Or    Al, 00100000B
b. Rotate Si register 5 bits to the left through the carry flag.
Mov    Cl, 5
RCL    Si, Cl
c. Position the Carry Flag to bit 4 of BL.
Mov    Cl, 4
RCR    Bl, Cl
d. Branch to label ODD if the LEAST significant bit of AL is 1.
Test    Al, 00000001B
Jnz      ODD
e. Branch to label READY if bit 7 = 1 and bit 5 = 0 of Al, ignore the rest.
And    Al, 10100000B
Cmp   Al, 10000000B
Je       READY
6. (15) In the following indicate the value in the AL register and the designated flag after execution of instructions.
        
     a.   MOV  AL,01001010B                   ZF = _0_
          AND  AL,0AH                         AL = _00001010_

     b.   MOV  AL,41H                         ZF = _1_
          AND  AL,20H                         AL = _00000000_

     c.   MOV  AL,00011100B                   ZF = _0_
          STC                                 CF = _0_
          RCR  AL,1                           AL = _10001110_

     d.   MOV  CL,5
          CLC                                 ZF = _0_
          MOV  AL,0EEH                        CF = _1
          SAL  AL,CL                          AL = __11000000_

     e.   MOV  CL, 4                          ZF = _          MOV  AL,10100000B                   CF = _0_
          SAR  AL, CL                         AL = __11111010_

     f.   MOV  AL, 10010001B                  ZF = _0_
          TEST AL, 10000001B                  AL = _10010001_
g.   Mov  Al, 4
     Mov  Cl, 64
     Mul  Cl

     Ah 00000001  Al 00000000  Cl __64______   OF _1_   


h.   Mov  Al, -4
     Mov  Cl, 32
     Imul Cl

     Ah 11111111  Al 10000000  Cl ___32_____   OF _0_


i.   Mov  Al, 19
     Mov  Ah, 0
     Mov  Cl, 4
     Div  Cl

     Ah 3  Al 4  Cl 4   


 j.  Mov  Al, -1
     Cbw

     Ah 11111111  Al 11111111  


 k.  Mov  Al, -19
     Cbw
     Mov  Cl, 4
     Idiv Cl

     Ah -3  Al -4  Cl 4