Introduction to Embedded Assembler in C

Download Report

Transcript Introduction to Embedded Assembler in C

#include "dump.h"
provided by me, your kind
professor
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;
}
The VC++ inline Assembler is only a small subset of
MASM.
What's allowed:
1.
line labels

must always end with :
3.
instruction opcodes and operands
comments (both ; and C-style)
4.
align and even
2.

5.
6.
simply inserts NOPs, if necessary
length, size, and type
references to C variables & functions
The VC++ inline Assembler is only a small subset of
MASM.
What's not allowed:
1.
2.
3.
4.
5.
data definitions
macros
conditional assembly
local line labels
and much, much more
(use C++ instead)
(use C++ instead)
(use C++ instead)
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 Assembler
2. as a starting point for improvement (optimization)
 In
Microsoft Visual C++ 2010 Express,
Turn on expert mode.
1.

Tools  Settings  Expert settings
Set a breakpoint in your program.
Start debugging your program.
View disassembly and registers.
2.
3.
4.


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/wh
ats-the-best-free-c-profiler-for-windows-ifthere-are
 javap
(javap.exe)

JDK program used to disassemble .class files.

Steps:
1.
2.
Compile first (using javac) to produce .class file(s).
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.currentTimeMillis()


System class for timing:
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

call dump


returns a random number in eax in the range [0..255]
call resetTime


shows the contents of registers
call rand8


contains:
sets elapsed time timer back to zero
call reportTime

reports elapsed time in milliseconds