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

Post on 18-Jan-2016

218 views 1 download

Tags:

transcript

#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

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

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

While debugging . . .

We can view our inline Assembler!

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

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.

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

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.

#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();

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

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).

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…

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?)

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