52.223 Low Level Programming Lecturer: Duncan Smeed The Interface Between High-Level and Low-Level Languages.

Slides:



Advertisements
Similar presentations
Machine-Level Programming III: Procedures Feb. 8, 2000 Topics IA32 stack Stack-based languages Stack frames Register saving conventions Creating pointers.
Advertisements

Calling sequence ESP.
CS 4284 Systems Capstone Godmar Back Processes and Threads.
University of Washington Procedures and Stacks II The Hardware/Software Interface CSE351 Winter 2013.
University of Washington Last Time For loops  for loop → while loop → do-while loop → goto version  for loop → while loop → goto “jump to middle” version.
Machine-Level Programming III: Procedures Apr. 17, 2006 Topics IA32 stack discipline Register saving conventions Creating pointers to local variables CS213.
PC hardware and x86 3/3/08 Frans Kaashoek MIT
1 Lecture 5: Procedures Assembly Language for Intel-Based Computers, 4th edition Kip R. Irvine.
1 Function Calls Professor Jennifer Rexford COS 217 Reading: Chapter 4 of “Programming From the Ground Up” (available online from the course Web site)
Accessing parameters from the stack and calling functions.
– 1 – , F’02 ICS05 Instructor: Peter A. Dinda TA: Bin Lin Recitation 4.
Chapter 12: High-Level Language Interface. Chapter Overview Introduction Inline Assembly Code C calls assembly procedures Assembly calls C procedures.
1 Homework Reading –PAL, pp , Machine Projects –Finish mp2warmup Questions? –Start mp2 as soon as possible Labs –Continue labs with your.
Machine-Level Programming III: Procedures Jan 30, 2003
Semantics of Calls and Returns
Assembly תרגול 8 פונקציות והתקפת buffer.. Procedures (Functions) A procedure call involves passing both data and control from one part of the code to.
September 22, 2014 Pengju (Jimmy) Jin Section E
Compiler Construction
Stack Activation Records Topics IA32 stack discipline Register saving conventions Creating pointers to local variables February 6, 2003 CSCE 212H Computer.
CS2422 Assembly Language & System Programming November 7, 2006.
Stacks and Frames Demystified CSCI 3753 Operating Systems Spring 2005 Prof. Rick Han.
6.828: PC hardware and x86 Frans Kaashoek
Computer Architecture and Operating Systems CS 3230 :Assembly Section Lecture 7 Department of Computer Science and Software Engineering University of Wisconsin-Platteville.
Introduction to Subroutines. All the combinations in which a subroutine can be written 1. The subroutine may be: a. Internal or b. External 2. The type.
1 Carnegie Mellon Stacks : Introduction to Computer Systems Recitation 5: September 24, 2012 Joon-Sup Han Section F.
Ithaca College Machine-Level Programming IV: IA32 Procedures Comp 21000: Introduction to Computer Systems & Assembly Lang Spring 2013 * Modified slides.
Runtime Environments Compiler Construction Chapter 7.
University of Washington Today More on procedures, stack etc. Lab 2 due today!  We hope it was fun! What is a stack?  And how about a stack frame? 1.
CSc 453 Runtime Environments Saumya Debray The University of Arizona Tucson.
Fabián E. Bustamante, Spring 2007 Machine-Level Programming III - Procedures Today IA32 stack discipline Register saving conventions Creating pointers.
Assembly Language for x86 Processors 7th Edition Chapter 13: High-Level Language Interface (c) Pearson Education, All rights reserved. You may modify.
CSC 8505 Compiler Construction Runtime Environments.
Machine-level Programming III: Procedures Topics –IA32 stack discipline –Register saving conventions –Creating pointers to local variables.
Functions/Methods in Assembly
University of Amsterdam Computer Systems – the instruction set architecture Arnoud Visser 1 Computer Systems The instruction set architecture.
Introduction to Assembly II Abed Asi Extended System Programming Laboratory (ESPL) CS BGU Fall 2014/2015.
Assembly Language Co-Routines
Introduction to Intel IA-32 and IA-64 Instruction Set Architectures.
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.
LECTURE 19 Subroutines and Parameter Passing. ABSTRACTION Recall: Abstraction is the process by which we can hide larger or more complex code fragments.
ICS51 Introductory Computer Organization Accessing parameters from the stack and calling functions.
Subroutines reasons for subroutines −repeat same code, or similar code with slightly different parameters −hide design decisions or design complexity −partition.
Section 5: Procedures & Stacks
CS 177 Computer Security Lecture 9
Assembly function call convention
Reading Condition Codes (Cont.)
Computer Science 210 Computer Organization
C function call conventions and the stack
143A: Principles of Operating Systems Lecture 4: Calling conventions
Homework In-line Assembly Code Machine Language
Introduction to Compilers Tim Teitelbaum
High-Level Language Interface
Assembly Language Programming V: In-line Assembly Code
Chapter 3 Machine-Level Representation of Programs
Machine-Level Programming 4 Procedures
Chapter 9 :: Subroutines and Control Abstraction
Condition Codes Single Bit Registers
Application Binary Interface (ABI)
Stack Frames and Advanced Procedures
Introduction to Intel IA-32 and IA-64 Instruction Set Architectures
Assembly Language Programming II: C Compiler Calling Sequences
Machine-Level Programming III: Procedures Sept 18, 2001
Multi-modules programming
Chapter 3 Machine-Level Representation of Programs
Where is all the knowledge we lost with information? T. S. Eliot
“Way easier than when we were students”
Presentation transcript:

Low Level Programming Lecturer: Duncan Smeed The Interface Between High-Level and Low-Level Languages

52223_10/2 High Level Linking  Programming in assembly language (AL) - also known as low- level language (LLL) - is now seldom used to develop complete applications.  Generally speaking, where convenience and development time are more important than speed or code size, applications can be effectively written in a high-level language. Then such programs can be optimised using AL if necessary.  AL may need to be used to control high-speed hardware, access low-level DOS services, and so on.  What we will be concerned with in this section is the interface, the connection, between high-level languages and assembly language.

The Interface Between High-Level and Low-Level Languages 52223_10/3 General Conventions  The following general considerations need to be addressed when calling AL subroutines form high- level languages: Naming Conventions Calling Conventions

The Interface Between High-Level and Low-Level Languages 52223_10/4 Naming Conventions  When calling an AL subroutine from another language, any identifiers that are shared between the two languages must be compatible with both.  These are known as external identifiers.  The linker resolves references to external identifiers, but can only do so if the naming conventions are consistent.  C does not change the case of names, and it is case sensitive. C (compilers) normally also prefix the beginning of external identifiers with an underscore (_) character.

The Interface Between High-Level and Low-Level Languages 52223_10/5 Calling (HLL to LL) Conventions  The calling conventions used by a program refer to the low-level details about how procedures/functions are called. This is often referred to as a low-level protocol.  We need to know the following: Which registers must be restored The parameter (argument) passing order Whether arguments are passed by value or reference How the stack pointer will be restored How function results will be returned

The Interface Between High-Level and Low-Level Languages 52223_10/6...Calling (HLL to LL) Conventions  It is often difficult for the same subroutine to be called by different languages.  A subroutine called by Pascal, for instance, expects its parameters to be in a different order from the subroutine called by C.  One principle seems to be universal: when a HLL calls a subroutine, it pushes its arguments on the stack before executing a CALL instruction.  But beyond this basic principle, languages vary in their calling conventions.

The Interface Between High-Level and Low-Level Languages 52223_10/7...Calling (HLL to LL) Conventions  The calling program (caller) needs to know how arguments are to be passed to the subroutine.  The caller also needs to know if it is responsible for restoring the original value of the stack pointer after the call.  The called subroutine (callee) uses a calling convention to decide how parameters are to be received.  The callee also needs to know whether or not it is responsible for restoring the stack pointer before returning.  If the subroutine is a function, i.e. one that returns a result, then the convention for returning the function result also needs to be known.

The Interface Between High-Level and Low-Level Languages 52223_10/8 Stack Structure

The Interface Between High-Level and Low-Level Languages 52223_10/9 Procedure Linking  An IA-32 processor provides two pointers for linking of procedures: the stack-frame base pointer and the return instruction pointer.  When used in conjunction with a standard software procedure-call technique, these pointers permit reliable and coherent linking of procedures.

The Interface Between High-Level and Low-Level Languages 52223_10/10 Stack-Frame Base Pointer  The stack is typically divided into frames. Each stack frame can then contain local variables, parameters to be passed to another procedure, and procedure linking information.  The stack-frame base pointer (contained in the EBP register) identifies a fixed reference point within the stack frame for the called procedure.  To use the stack-frame base pointer, the called procedure typically copies the contents of the ESP register into the EBP register prior to pushing any local variables on the stack.  The stack-frame base pointer then permits easy access to data structures passed on the stack, to the return instruction pointer, and to local variables added to the stack by the called procedure.

The Interface Between High-Level and Low-Level Languages 52223_10/11 Return Instruction Pointer  Prior to branching to the first instruction of the called procedure, the CALL instruction pushes the address in the EIP register onto the current stack.  This address is then called the return instruction pointer and it points to the instruction where execution of the calling procedure should resume following a return from the called procedure.  Upon returning from a called procedure, the RET instruction pops the return-instruction pointer from the stack back into the EIP register. Execution of the calling procedure then resumes.

The Interface Between High-Level and Low-Level Languages 52223_10/12 Parameter Passing  Parameters can be passed between procedures in any of three ways: through general-purpose registers, on the stack, or in an argument list

The Interface Between High-Level and Low-Level Languages 52223_10/13 Passing Parameters Through the General Purpose Registers  The processor does not save the state of the general- purpose registers on procedure calls.  A calling procedure can thus pass up to six parameters to the called procedure by copying the parameters into any of these registers (except the ESP and EBP registers) prior to executing the CALL instruction.  The called procedure can likewise pass parameters back to the calling procedure through general-purpose registers.

The Interface Between High-Level and Low-Level Languages 52223_10/14 Passing Parameters on the Stack  To pass a large number of parameters to the called procedure, the parameters can be placed on the stack, in the stack frame for the calling procedure.  Here, it is useful to use the stack-frame base pointer (in the EBP register) to make a frame boundary for easy access to the parameters.  The stack can also be used to pass parameters back from the called procedure to the calling procedure.

The Interface Between High-Level and Low-Level Languages 52223_10/15 Passing Parameters in an Argument List  An alternate method of passing a larger number of parameters (or a data structure) to the called procedure is to place the parameters in an argument list in one of the data segments in memory.  A pointer to the argument list can then be passed to the called procedure through a general-purpose register or the stack.  Parameters can also be passed back to the calling procedure in this same manner.

The Interface Between High-Level and Low-Level Languages 52223_10/16 Some examples...

The Interface Between High-Level and Low-Level Languages 52223_10/17 Block Scope #include #define PutDigit(c) {putchar((c)+'0');\ putchar('\n');} void Scope(void) { int level = 1; PutDigit(level); { int level = 2; PutDigit(level); { int level = 3; PutDigit(level); } PutDigit(level); } PutDigit(level); } int main( ) { Scope();return 0; }

The Interface Between High-Level and Low-Level Languages 52223_10/18 Recursive Functions  How does the following program work? #include #define PrintDigit(c) (putchar((c)+'0')) void PrintNumber(unsigned int number) { if (number >= 10) PrintNumber(number/10); PrintDigit(number%10); } int main( ) { PrintNumber(1984); putchar('\n'); return 0; }

The Interface Between High-Level and Low-Level Languages 52223_10/19...Recursive Functions main: pushl %ebp movl %esp, %ebp subl $8, %esp andl $-16, %esp movl $1984, (%esp) call PrintNumber movl stdout, %eax movl %eax, 4(%esp) movl $10, (%esp) call _IO_putc movl $0, %eax movl %ebp, %esp popl %ebp ret

The Interface Between High-Level and Low-Level Languages 52223_10/20...Recursive Functions PrintNumber: pushl %ebp movl %esp, %ebp subl $24, %esp movl %ebx, -4(%ebp) movl 8(%ebp), %ebx cmpl $9, %ebx jbe.L2 movl $ , %edx movl %ebx, %eax mull %edx shrl $3, %edx movl %edx, (%esp) call PrintNumber.L2:

The Interface Between High-Level and Low-Level Languages 52223_10/21...Recursive Functions.L2: movl $ , %edx movl %ebx, %eax mull %edx shrl $3, %edx leal (%edx,%edx,4), %eax addl %eax, %eax movl %ebx, %edx subl %eax, %edx addl $48, %edx movl stdout, %eax movl %eax, 4(%esp) movl %edx, (%esp) call _IO_putc movl -4(%ebp), %ebx movl %ebp, %esp popl %ebp ret