Presentation is loading. Please wait.

Presentation is loading. Please wait.

Copyright © 2000, Daniel W. Lewis. All Rights Reserved. CHAPTER 5 MIXING C AND ASSEMBLY.

Similar presentations


Presentation on theme: "Copyright © 2000, Daniel W. Lewis. All Rights Reserved. CHAPTER 5 MIXING C AND ASSEMBLY."— Presentation transcript:

1 Copyright © 2000, Daniel W. Lewis. All Rights Reserved. CHAPTER 5 MIXING C AND ASSEMBLY

2 Copyright © 2000, Daniel W. Lewis. All Rights Reserved. L1:MOVEAX,[RESULT+2]; load selected table element The Four Fields of a Line of Code in Assembly Language Label Field Operation Field Operation Field Operand Fields Comment Field

3 Copyright © 2000, Daniel W. Lewis. All Rights Reserved. Use of “[…]” in NASM Assembler ORG1234h xyzzy:DD5678h; the address of this word is 1234 (hex)... MOVEAX,[xyzzy]; loads 5678 (hex) into register EAX … MOVEAX,xyzzy; loads 1234 (hex) into register EAX

4 Copyright © 2000, Daniel W. Lewis. All Rights Reserved. Two Passes of an Assembler... A &x+2 1B27 3F3A... A B27 3F3A 3F3C... MOVAL,[X+2]... XDB 5,7,3... Assembler Pass 1 Assembler Pass 2 Symbol Table 3F3A X ……

5 Copyright © 2000, Daniel W. Lewis. All Rights Reserved. Instruction Sequencing

6 Copyright © 2000, Daniel W. Lewis. All Rights Reserved. for (;;)top_of_for:... {... if (...) break ;JMPend_of_for... }JMPtop_of_for end_of_for:... Code Generated by Compiler for Break and End of Loop

7 Copyright © 2000, Daniel W. Lewis. All Rights Reserved. Commonly-Used Conditional Jump Instructions CompareMnemonic(s)Jump if...Determined by... equality JE (JZ)Equal (Zero)ZF==1 JNE (JNZ)Not Equal (Not Zero)ZF==0 unsigned JB (JNAE)Below (Not Above or Equal)CF==1 JBE (JNA)Below or Equal (Not Above)CF==1 || ZF==1 JAE (JNB)Above or Equal (Not Below)CF==0 JA (JNBE)Above (Not Below or Equal)CF==0 && ZF==0 signed JL (JNGE)Less than (Not Greater than or Equal)SF!=OF JLE (JNG)Less than or Equal (Not Greater than)SF!=OF || ZF==1 JGE (JNL)Greater than or Equal (Not Less than)SF==OF JG (JNLE)Greater than (Not Less than or Equal)SF==OF && ZF==0

8 Copyright © 2000, Daniel W. Lewis. All Rights Reserved. Conditional Jump Preceded by a CMP Instruction while (x < 1000)top_of_while:CMPDWORD [x],1000 {JNLend_of_while... }JMPtop_of_while end_of_while:

9 Copyright © 2000, Daniel W. Lewis. All Rights Reserved. Compound Conditionals if (lower_limit <= x && x <= upper_limit) y = x ; if (x < lower_limit) goto L1 if (x > upper_limit) goto L1 y = x ; L1: if (x upper_limit) goto L1 y = x ; L1: if (!(lower_limit <= x && x <= upper_limit)) goto L1 y = x ; L1: MOVEAX,[x] CMPEAX,[lower_limit] JLL1 CMPEAX,[upper_limit] JGL1 MOV[y],EAX L1:... Convert “then” clause to a goto Convert AND to OR so if can be split Split into two if’s

10 Copyright © 2000, Daniel W. Lewis. All Rights Reserved. Compound Conditionals if (x < lower_limit || upper_limit < x) y = x ; if (x < lower_limit) goto L1 if (x > upper_limit) goto L1 goto L2 ; L1: y = x ; L2: if (x < lower_limit) goto L1 if (!(x > upper_limit)) goto L2 L1: y = x ; L2: MOVEAX,[x] CMPEAX,[lower_limit] JLL1 CMPEAX,[upper_limit] JNGL2 L1:MOV[y],EAX L2:... if (x < lower_limit || upper_limit < x) goto L1 ; goto L2 ; L1: y = x ; L2: Convert “then” clause to a goto Split into two if’s Reverse the sense to eliminate extra goto

11 Copyright © 2000, Daniel W. Lewis. All Rights Reserved. If-Then-Else Statements if (x > y)MOVEAX,[x] ; x > y ? {CMPEAX,[y] x = 0 ;JNGL1 }MOVDWORD [x],0 ; then: x = 0 ; elseJMPL2 ; skip over else {L1:MOVDWORD [y],0 ; else: y = 0 ; y = 0 ;L2:... }

12 Copyright © 2000, Daniel W. Lewis. All Rights Reserved. Building a Loop With the JECXZ and LOOP Instructions MOVECX,[iteration_count] JECXZloop_exit; jump if ECX is zero. top_of_loop: LOOPtop_of_loop; decrement ECX & jump if NZ loop_exit:

13 Copyright © 2000, Daniel W. Lewis. All Rights Reserved. Building a Loop With an Increasing Loop Index XORECX,ECX; Set ECX to 0 top_of_loop: INCECX; Add 1 to ECX CMPECX,[iteration_count]; ECX < count? JBtop_of_loop; Stop if not.

14 Copyright © 2000, Daniel W. Lewis. All Rights Reserved. Application of the Repeated String Instructions Initialize MemoryScan MemoryCopy MemoryCompare Memory MOVECX,[bytes] MOVAL,[value] MOVEDI,[dadrs] CLD REPSTOSB MOVECX,[bytes] MOVAL,[value] MOVEDI,[dadrs] CLD REPSCASB JEfound MOVECX,[bytes] MOVESI,[sadrs] MOVEDI,[dadrs] CLD REPMOVSB MOVECX,[bytes] MOVESI,[sadrs] MOVEDI,[dadrs] CLD REPCMPSB JEidentical

15 Copyright © 2000, Daniel W. Lewis. All Rights Reserved. Interfacing to C

16 Copyright © 2000, Daniel W. Lewis. All Rights Reserved. Register Usage Conventions Register(s)Usage in C functions EAX Functions return all pointers and integer values up to 32 ‑ bits in this register. EDX and EAX Functions return 64 ‑ bit values (long long ints) in this register pair. (Note: EDX holds bits 63-32, EAX holds bits 31-0). EBP Used to access: (1) The arguments that were passed to a function when it was called, and (2) any automatic variables allocated by the function. EBX, ESI, EDI, EBP, DS, ES, and SS. These registers must be preserved by functions written in assembly language. Any of these registers that the function modifies should be saved (PUSH) on entry to the function and restored (POP) on exit. EAX, ECX, EDX, FS and GS "Scratch" registers. These registers may be used without preserving their current content.

17 Copyright © 2000, Daniel W. Lewis. All Rights Reserved. Function Call and Return CALL instruction used by caller to invoke the function –Pushes the return address onto the stack. RET instruction used in function to return to caller. –Pops the return address off the stack.

18 Copyright © 2000, Daniel W. Lewis. All Rights Reserved. No Parameters and No Return Value.

19 Copyright © 2000, Daniel W. Lewis. All Rights Reserved. No Parameters and 8-bit Return Value.

20 Copyright © 2000, Daniel W. Lewis. All Rights Reserved. Parameter Passing Parameters are pushed onto stack prior to CALL. –gcc pushes parameters in reverse order. –8/16-bit parameters are extended to 32-bits Caller removes parameters after function returns.

21 Copyright © 2000, Daniel W. Lewis. All Rights Reserved. Passing Parameters to a C Function Function call w/parameters:Byte2Port(0x3BC, data) ; Code generated by the compiler: PUSHDWORD [_data]; Push 2 nd param PUSHDWORD 03BCh; Push 1 st param CALL_Byte2Port; Call the function. ADDESP,8; Remove params

22 Copyright © 2000, Daniel W. Lewis. All Rights Reserved. Passing an 8 ‑ bit Unsigned Integer CAssembly unsigned char data ;... Do_Something(data) ;... MOVZX EAX,[_data] PUSH EAX CALL_Do_Something ADDESP,4

23 Copyright © 2000, Daniel W. Lewis. All Rights Reserved. Passing an 8 ‑ bit Signed Integer CAssembly signed char data ;... Do_Something(data) ;... MOVSX EAX,[_data] PUSH EAX CALL_Do_Something ADDESP,4

24 Copyright © 2000, Daniel W. Lewis. All Rights Reserved. Passing a 64 ‑ bit Integer CAssembly /* signed or unsigned */ long long data ;... Do_Something(data) ;... PUSH DWORD [_data+4] PUSH DWORD [_data] CALL_Do_Something ADDESP,8

25 Copyright © 2000, Daniel W. Lewis. All Rights Reserved. Retrieving Parameters Stack immediately after the CALL PUSHDWORD [_data]; Push 2 nd parameter PUSHDWORD 03BCh; Push 1 st parameter CALL_Byte2Port; Call the function

26 Copyright © 2000, Daniel W. Lewis. All Rights Reserved. Retrieving Parameters Can’t use POP instructions to access parameters. –Parameters expect to be removed from the stack later by the caller. –RET instruction expects return address to be on top of the stack. Need a way to access parameters without actually removing them from the stack!

27 Copyright © 2000, Daniel W. Lewis. All Rights Reserved. Retrieving Parameters _Byte2Port: PUSHEBP; Preserve current contents of EBP on stack MOVEBP,ESP; Establish a reference point in the stack MOVDX,[EBP+8]; Copy 1 st parameter to DX (the I/O port address) MOVAL,[EBP+12]; Copy 2 nd parameter to AL (discard bits 15-8) OUTDX,AL; Write the data to the I/O port POPEBP; Restore old contents of EBP from stack RET; Return to caller _Byte2Port: MOVDX,[ESP+4]; Copy 1 st parameter to DX (the I/O port adrs). MOVAL,[ESP+8]; Copy 2 nd parameter to AL (discard bits 31-8). OUTDX,AL; Write the data to the I/O port. RET; Return to caller.

28 Copyright © 2000, Daniel W. Lewis. All Rights Reserved. Everything is Pass By Value Emulating pass-by-reference in C

29 Copyright © 2000, Daniel W. Lewis. All Rights Reserved. Temporary Variables Use automatic allocation: –Temporaries rarely need persistence –Allocate temporaries on the stack –Guarantees that function is reentrant Only available space is beyond top of stack. –Must be allocated before it can be used (stack pointer must be adjusted and later restored when temporaries are no longer needed).

30 Copyright © 2000, Daniel W. Lewis. All Rights Reserved. _Swap:PUSHEBP; Preserve original EBP contents MOVEBP,ESP; Establish stack frame reference in EBP SUBESP,4; Allocate temporary in automatic memory MOVESP,EBP; Release the temporary automatic int POPEBP; Restore original EBP RET; Return from this function

31 Copyright © 2000, Daniel W. Lewis. All Rights Reserved. _Swap:PUSHEBP; Preserve original EBP contents MOVEBP,ESP; Establish stack frame reference in EBP SUBESP,4; Allocate a temporary in automatic memory MOVECX,[EBP+8] ; temp = *p1: (1) Get 1 st parameter (p1) MOVEAX,[ECX] ; (2) Use it to get *p1 into EAX MOV[EBP-4],EAX; (3) Then store EAX into temp. MOVECX,[EBP+12] ; *p1 = *p2: (1) Get 2 nd parameter (p2) MOVEAX,[ECX] ; (2) Use it to get *p2 into EAX MOVECX,[EBP+8] ; (3) Get 1 st parameter (p1) again MOV[ECX],EAX; (4) Use it to store EAX into *p1 MOVEAX,[EBP-4] ; *p2 = temp: (1) Get the temp into EAX MOVECX,[EBP+12] ; (2) Get 2 nd parameter (p2) again MOV[ECX],EAX; (3) Use it to store EAX into *p2 MOVESP,EBP; Release the temporary int POPEBP; Restore original EBP RET; Return from this function

32 Copyright © 2000, Daniel W. Lewis. All Rights Reserved. Optimized Implementation of the Swap Function in Assembly _Swap: MOVECX,[ESP+4]; Copy parameter p1 to ECX MOVEDX,[ESP+8]; Copy parameter p2 to EDX MOVEAX,[ECX]; Copy *p1 into EAX XCHGEAX,[EDX]; Exchange EAX with *p2 MOV[ECX],EAX; Copy EAX into *p1 RET; Return from this function


Download ppt "Copyright © 2000, Daniel W. Lewis. All Rights Reserved. CHAPTER 5 MIXING C AND ASSEMBLY."

Similar presentations


Ads by Google