#include "dump.h" int main ( int argc, char* argv[] ) { __asm { mov eax, 1// init eax to 1...

23

Upload: cori-warren

Post on 18-Jan-2016

219 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: #include "dump.h" int main ( int argc, char* argv[] ) { __asm { mov eax, 1// init eax to 1 mov ebx, esp; keep a copy of esp mov ecx, 3/* init ecx to 3
Page 2: #include "dump.h" int main ( int argc, char* argv[] ) { __asm { mov eax, 1// init eax to 1 mov ebx, esp; keep a copy of esp mov ecx, 3/* init ecx to 3

#include "dump.h"

int main ( int argc, char* argv[] ) { __asm { mov eax, 1 // init eax to 1 mov ebx, esp ; keep a copy of esp mov ecx, 3 /* init ecx to 3 */ mov edx, 4 ; init edx to 4 } dump(); //show reg contents return 0;}

provided by me, your kind professor

Page 3: #include "dump.h" int main ( int argc, char* argv[] ) { __asm { mov eax, 1// init eax to 1 mov ebx, esp; keep a copy of esp mov ecx, 3/* init ecx to 3

The VC++ inline Assembler is only a small subset of MASM.

What's allowed:1. line labels

must always end with :

2. instruction opcodes and operands3. comments (both ; and C-style)

4. align and even simply inserts NOPs, if necessary

5. length, size, and type6. references to C variables & functions

Page 4: #include "dump.h" int main ( int argc, char* argv[] ) { __asm { mov eax, 1// init eax to 1 mov ebx, esp; keep a copy of esp mov ecx, 3/* init ecx to 3

The VC++ inline Assembler is only a small subset of MASM.

What's not allowed:1. data definitions (use C++ instead)2. macros (use C++ instead)3. conditional assembly (use C++

instead)4. local line labels5. and much, much more

Page 5: #include "dump.h" int main ( int argc, char* argv[] ) { __asm { mov eax, 1// init eax to 1 mov ebx, esp; keep a copy of esp mov ecx, 3/* init ecx to 3

While debugging . . .

We can view our inline Assembler!

We can also see the Assembler code generated by the compiler corresponding to our C code!

Page 6: #include "dump.h" int main ( int argc, char* argv[] ) { __asm { mov eax, 1// init eax to 1 mov ebx, esp; keep a copy of esp mov ecx, 3/* init ecx to 3
Page 7: #include "dump.h" int main ( int argc, char* argv[] ) { __asm { mov eax, 1// init eax to 1 mov ebx, esp; keep a copy of esp mov ecx, 3/* init ecx to 3
Page 8: #include "dump.h" int main ( int argc, char* argv[] ) { __asm { mov eax, 1// init eax to 1 mov ebx, esp; keep a copy of esp mov ecx, 3/* init ecx to 3
Page 9: #include "dump.h" int main ( int argc, char* argv[] ) { __asm { mov eax, 1// init eax to 1 mov ebx, esp; keep a copy of esp mov ecx, 3/* init ecx to 3

Assembler generated from the same C/C++ source code is different for Debug and Release versions!

Release version is optimized but doesn’t contain any debugging information.

Check size of debug and release .exe’s. (They are different!)

Turn on generation of Assembler listing (.cod) files and take a look.

Page 10: #include "dump.h" int main ( int argc, char* argv[] ) { __asm { mov eax, 1// init eax to 1 mov ebx, esp; keep a copy of esp mov ecx, 3/* init ecx to 3

This is very instructive:1. for learning Assembler2. as a starting point for improvement (optimization)

Page 11: #include "dump.h" int main ( int argc, char* argv[] ) { __asm { mov eax, 1// init eax to 1 mov ebx, esp; keep a copy of esp mov ecx, 3/* init ecx to 3

In Microsoft Visual C++ 2010 Express,1. Turn on expert mode.

Tools Settings Expert settings

2. Set a breakpoint in your program.3. Start debugging your program.4. View disassembly and registers.

Debug Windows Disassembly Debug Windows Registers

To display f.p. or XMM registers, right-click in Registers window.

Page 12: #include "dump.h" int main ( int argc, char* argv[] ) { __asm { mov eax, 1// init eax to 1 mov ebx, esp; keep a copy of esp mov ecx, 3/* init ecx to 3
Page 13: #include "dump.h" int main ( int argc, char* argv[] ) { __asm { mov eax, 1// init eax to 1 mov ebx, esp; keep a copy of esp mov ecx, 3/* init ecx to 3

#include <time.h>

clock_t start = clock();

for (int i=0; i<N; i++) { __asm { ; do something useful }}

clock_t delta = clock() - start;double elapsed = ((double)delta) / CLOCKS_PER_SEC;printf( "elapsed = %f sec (%d ticks) \n", elapsed, delta );printf( "<hit return> " );getchar();

Page 14: #include "dump.h" int main ( int argc, char* argv[] ) { __asm { mov eax, 1// init eax to 1 mov ebx, esp; keep a copy of esp mov ecx, 3/* init ecx to 3
Page 15: #include "dump.h" int main ( int argc, char* argv[] ) { __asm { mov eax, 1// init eax to 1 mov ebx, esp; keep a copy of esp mov ecx, 3/* init ecx to 3
Page 16: #include "dump.h" int main ( int argc, char* argv[] ) { __asm { mov eax, 1// init eax to 1 mov ebx, esp; keep a copy of esp mov ecx, 3/* init ecx to 3
Page 17: #include "dump.h" int main ( int argc, char* argv[] ) { __asm { mov eax, 1// init eax to 1 mov ebx, esp; keep a copy of esp mov ecx, 3/* init ecx to 3

AMD CodeAnalyst (http://developer.amd.com/CPU/CODEANALYST/Pages/default.aspx)

http://stackoverflow.com/questions/67554/whats-the-best-free-c-profiler-for-windows-if-there-are

Page 18: #include "dump.h" int main ( int argc, char* argv[] ) { __asm { mov eax, 1// init eax to 1 mov ebx, esp; keep a copy of esp mov ecx, 3/* init ecx to 3
Page 19: #include "dump.h" int main ( int argc, char* argv[] ) { __asm { mov eax, 1// init eax to 1 mov ebx, esp; keep a copy of esp mov ecx, 3/* init ecx to 3

javap (javap.exe)

JDK program used to disassemble .class files.

Steps:

1. Compile first (using javac) to produce .class file(s).

2. Then javap –verbose Test (to disassemble Test.class).

Page 20: #include "dump.h" int main ( int argc, char* argv[] ) { __asm { mov eax, 1// init eax to 1 mov ebx, esp; keep a copy of esp mov ecx, 3/* init ecx to 3

javap example

class Test { public static void main ( String[] args ) { for (int i=0; i<10; i++) { System.out.printf( "i = %d \n", i ); } }}

…public static void main(java.lang.String[]); Code: Stack=6, Locals=2, Args_size=1 0: iconst_0 1: istore_1 2: iload_1 3: bipush 10 5: if_icmpge 34 8: getstatic #2; //Field java/lang/System.out:Ljava/io/PrintStream; 11: ldc #3; //String i = %d \n 13: iconst_1 14: anewarray #4; //class java/lang/Object 17: dup 18: iconst_0 19: iload_1 20: invokestatic #5; //Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer; 23: aastore 24: invokevirtual #6; //Method java/io/PrintStream.printf:(Ljava/lang/String;[Ljava/lang/Object;)Ljava/io/PrintStream; 27: pop 28: iinc 1, 1 31: goto 2 34: return…

Page 21: #include "dump.h" int main ( int argc, char* argv[] ) { __asm { mov eax, 1// init eax to 1 mov ebx, esp; keep a copy of esp mov ecx, 3/* init ecx to 3

use System class for timing:

System.currentTimeMillis() returns (a long) the current time in milliseconds

System.nanoTime() returns (a long) the current value of the most

precise available system timer, in nanoseconds but may be wrong for timing longer algorithms

(Why?)

Page 22: #include "dump.h" int main ( int argc, char* argv[] ) { __asm { mov eax, 1// init eax to 1 mov ebx, esp; keep a copy of esp mov ecx, 3/* init ecx to 3
Page 23: #include "dump.h" int main ( int argc, char* argv[] ) { __asm { mov eax, 1// init eax to 1 mov ebx, esp; keep a copy of esp mov ecx, 3/* init ecx to 3

utilties.asm contains:call dump

shows the contents of registers

call rand8 returns a random number in eax in the range [0..255]

call resetTime sets elapsed time timer back to zero

call reportTime reports elapsed time in milliseconds