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
|
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
|
______________________________
.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: |
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, 00100000Bb. Rotate Si register 5 bits to the left through the carry flag.
Mov Cl, 5c. Position the Carry Flag to bit 4 of BL.
RCL Si, Cl
Mov Cl, 4d. Branch to label ODD if the LEAST significant bit of AL is 1.
RCR Bl, Cl
Test Al, 00000001Be. Branch to label READY if bit 7 = 1 and bit 5 = 0 of Al, ignore the rest.
Jnz ODD
And Al, 10100000B6. (15) In the following indicate the value in the AL register and the designated flag after execution of instructions.
Cmp Al, 10000000B
Je READY
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 = _0 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