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

Slides:



Advertisements
Similar presentations
Assembly Language for x86 Processors 6th Edition Chapter 5: Procedures (c) Pearson Education, All rights reserved. You may modify and copy this slide.
Advertisements

Intel Computer Architecture Presented By Jessica Graziano.
C Programming and Assembly Language Janakiraman V – NITK Surathkal 2 nd August 2014.
Assembly Language for Intel-Based Computers Chapter 5: Procedures Kip R. Irvine.
Assembly Language for Intel-Based Computers
Practical Session 3. The Stack The stack is an area in memory that its purpose is to provide a space for temporary storage of addresses and data items.
Accessing parameters from the stack and calling functions.
Practical Session 3. The Stack The stack is an area in memory that its purpose is to provide a space for temporary storage of addresses and data items.
– 1 – , F’02 ICS05 Instructor: Peter A. Dinda TA: Bin Lin Recitation 4.
Quiz #2 Topics Character codes Intel IA-32 architecture Mostly MASM
CEG 320/520: Computer Organization and Assembly Language ProgrammingIntel Assembly 1 Intel IA-32 vs Motorola
Assembly Language for Intel-Based Computers, 6 th Edition Chapter 8: Advanced Procedures (c) Pearson Education, All rights reserved. You may.
The x86 Architecture Lecture 15 Fri, Mar 4, 2005.
IA32 (Pentium) Processor Architecture. Processor modes: 1.Protected (mode we will study) – 32-bit mode – 32-bit (4GB) address space 2.Virtual 8086 modes.
Assembly Language for Intel-Based Computers, 6 th Edition Chapter 6: Conditional Processing (c) Pearson Education, All rights reserved. You may modify.
1 ICS 51 Introductory Computer Organization Fall 2009.
Sahar Mosleh California State University San MarcosPage 1 Stack operations, Applications and defining procedures.
Microprocessors The ia32 User Instruction Set Jan 31st, 2002.
Assembly Language. Symbol Table Variables.DATA var DW 0 sum DD 0 array TIMES 10 DW 0 message DB ’ Welcome ’,0 char1 DB ? Symbol Table Name Offset var.
Machine-level Programming III: Procedures Topics –IA32 stack discipline –Register saving conventions –Creating pointers to local variables.
Introduction to Assembly II Abed Asi Extended System Programming Laboratory (ESPL) CS BGU Fall 2013/2014.
Compiler Construction Code Generation Activation Records
COMP 2003: Assembly Language and Digital Logic Chapter 2: Flags and Instructions Notes by Neil Dickson.
Practical Session 4. GNU Linker Links object files together Used as the last step in the compilation We will use ld to link together compiled assembly.
Arrays. Outline 1.(Introduction) Arrays An array is a contiguous block of list of data in memory. Each element of the list must be the same type and use.
1 Assembly Language: Function Calls Jennifer Rexford.
Calling Procedures C calling conventions. Outline Procedures Procedure call mechanism Passing parameters Local variable storage C-Style procedures Recursion.
Assembly Language Wei Gao. Assembler language Instructions.
Practical Session 4. GNU Linker Links object files together Used as the last step in the compilation We will use ld to link together compiled assembly.
Assembly Language Addressing Modes. Introduction CISC processors usually supports more addressing modes than RISC processors. –RISC processors use the.
ICS51 Introductory Computer Organization Accessing parameters from the stack and calling functions.
Chapter 8 String Operations. 8.1 Using String Instructions.
CS-401 Computer Architecture & Assembly Language Programming
Practical Session 2 Computer Architecture and Assembly Language.
Precept 7: Introduction to IA-32 Assembly Language Programming
Computer Architecture and Assembly Language
Practical Session 3.
CSC 221 Computer Organization and Assembly Language
Stack Operations Dr. Hadi AL Saadi.
Assembly function call convention
Debugging Survival Guide (x86-DSG) Divyanand Kutagulla
Reading Condition Codes (Cont.)
Computer Architecture and Assembly Language
C function call conventions and the stack
Computer Architecture and Assembly Language
Homework Reading Labs PAL, pp
Practical Session 2.
Instruksi Set Prosesor 8088
Aaron Miller David Cohen Spring 2011
Chapter 4 Data Movement Instructions
Assembly IA-32.
INSTRUCTION SET.
Assembly Language Programming Part 2
CS 301 Fall 2002 Assembly Instructions
Y86 Processor State Program Registers
Computer Organization and Assembly Language
Machine-Level Programming 4 Procedures
Machine-Level Programming III: Procedures Sept 18, 2001
Practical Session 4.
Homework Reading Machine Projects Labs PAL, pp
Multi-modules programming
X86 Assembly Review.
Credits and Disclaimers
Computer Architecture and Assembly Language
Computer Architecture and System Programming Laboratory
Computer Architecture and System Programming Laboratory
Computer Architecture and System Programming Laboratory
Presentation transcript:

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

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

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

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 ……

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

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

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

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:

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

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

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:... }

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:

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.

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

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

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.

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.

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

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

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.

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

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

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

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

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

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!

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.

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

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

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

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

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