Exercise 8        Name _________________ Points  _    /22

Use PROTO and INVOKE for defining Assembler function call corresponding to the C++.
 
Example
C++ Prototype and Call
Assembler
int f(int x, int &y);
int A = 5, int B = 6;
B = f(A, B);
f	proto	near C, x : dword, y : near ptr dword
	...
	invoke	f, A, addr B
	Mov  	B, eAx
1. (2)          void f1(int x, int &y, int &z);
                int A = 8, int B = 2;
                f1(A, B, A);
2. (2)          int f2(int x, int &y, int z);
                int A = 8, int B = 2;
                A = f2(A, B, A);
The function definition also changes somewhat for the 32 bit processor under Visual C++. Give the Assembler corresponding to the C++ functions.
 
Example
C++ Function
Assembler
Stack from invoke
int f(int x, int &y)
{ int sum;

  y = x;

  sum = x + 1;
  return sum;
}
f Proc  Near C, x : dword, y : near ptr dword
  local sum : dword

  Mov   eBx, y  	; y = x
  Mov   eAx, x
  Mov   [eBx], eAx

  Mov	sum, eAx
  Inc   sum           	;  sum = x + 1

  Mov   eAx, sum      	; return
  Ret
f Endp
     ____________
    |  Offset B  | eBp+12
    |      5     | eBp+8
    | Return Addr| eBp+4
    |Caller's eBp| eBp=eSp
    |  sum       | eBp-4

    invoke	A, addr B

3.  (3)  void f3(int x, int &y, int &z) {
                z = x / y;
            }
 
 
 
 
 
 
 

4. (3)    int f4(int x, int &y, int z) {
                x = y / z;
                return y;
            }
 
 
 
 
 
 
 
 
 
 
 
 
 

Give the Assembler to implement the following using both Array indexing and String operations. Only the fragments relevant to the array indexing and string operations are needed (see the itialized parts in the examples below).
 
 
 
Two Examples: Square all the 32 bit values in a 10 entry array using String Operations and Array indexing. 
.data
    src dd 24,8,0,13,5,-4,13,6,13,18
.code
main Proc  Far
    Mov    eCx, 10
    Mov    eSi, Offset src ; eSi = src
    Mov    eDi, Offset src ; eDi = src
    Cld
                           ; do {
do: LodsD                  ;   eAx = *eSi++
    Mul    eAx             ;   eAx = eAx * eAx
    StosD                  ;   *eDi++ = eAx
while:                     ; }while (--eCx!=0);
    loop   do

    invoke ExitProcess, 0
main endp
     End   main
.data
    src dd 24,8,0,13,5,-4,13,6,13,18

.code
main Proc  near
    Mov    eCx, 10
    Mov    eSi, Offset src; eSi = src
                         ; do {
do: Mov    eAx, [eSi]    ;   eAx = *eSi
    Mul    eAx           ;   eAx = eAx * eAx
    Mov    [eSi], eAx    ;   *eSi = eAx
    Add    eSi, 4        ;   eSi++
while:                   ; } while(--eCx!=0);
    loop   do

    invoke ExitProcess, 0
main endp
     End   main

5. (6)    Find the greatest 16 bit value in the src array and store in e Ax. Hint: Use ScasD for string operation.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

6. (6)    Change all the entries that are 13 in array src to 10.