ECE291 Computer Engineering II Lecture 8 Josh Potts University of Illinois at Urbana- Champaign.

Slides:



Advertisements
Similar presentations
Programming 8086 – Part IV Stacks, Macros
Advertisements

The University of Adelaide, School of Computer Science
Procedures in more detail. CMPE12cGabriel Hugh Elkaim 2 Why use procedures? –Code reuse –More readable code –Less code Microprocessors (and assembly languages)
CPS3340 COMPUTER ARCHITECTURE Fall Semester, /17/2013 Lecture 12: Procedures Instructor: Ashraf Yaseen DEPARTMENT OF MATH & COMPUTER SCIENCE CENTRAL.
CEN 226: Computer Organization & Assembly Language :CSC 225 (Lec#3) By Dr. Syed Noman.
EENG 4005 Microprocessors.
Procedures and Stacks. Outline Stack organization PUSH and POP instructions Defining and Calling procedures.
Subroutines: Passing Arguments Using the Stack. Passing Arguments via the Stack Arguments to a subroutine are pushed onto the stack. The subroutine accesses.
More about procedures and Video Processing. Lesson plan Review existing concepts More about procedures and boolean expression Video processing.
8086 Assembly Language Programming I
CEN 226: Computer Organization & Assembly Language :CSC 225 (Lec#4)
IP high IP low IP high IP low BP high BP low IP high IP low BP high BP low FL high FL low CS high CS low IP high IP low _TEXTsegment byte public ‘CODE’
Chapter 12: High-Level Language Interface. Chapter Overview Introduction Inline Assembly Code C calls assembly procedures Assembly calls C procedures.
Run time vs. Compile time
1 Homework Reading –PAL, pp , Machine Projects –Finish mp2warmup Questions? –Start mp2 as soon as possible Labs –Continue labs with your.
Semantics of Calls and Returns
ICS312 Set 11 Introduction to Subroutines. All the combinations in which a subroutine can be written 1. The subroutine may be: a. Internal or b. External.
High-Level Language Interface Chapter 13 S. Dandamudi.
ICS312 Set 4 Program Structure. Outline for a SMALL Model Program Note the quiz at the next lecture will be to reproduce this slide.MODEL SMALL.586 ;
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.
Lecture 11 Last notes on interrupts and exam review Dr. Dimitrios S. Nikolopoulos CSL/UIUC.
Stack Operations LIFO structure (last-in,first-out) –The last value put into the stack is the first value taken out Runtime stack –A memory array that.
Today’s topics Parameter passing on the system stack Parameter passing on the system stack Register indirect and base-indexed addressing modes Register.
Objective At the conclusion of this chapter you will be able to:
Strings, Procedures and Macros
Lecture 7 A closer look at procedures Dr. Dimitrios S. Nikolopoulos CSL/UIUC.
Module R3 Process Scheduling. Module R3 involves the creation of a simple “Round Robin” dispatcher. The successful completion of this module will require.
UHD:CS2401: A. Berrached1 The Intel x86 Hardware Organization.
4-Oct Fall 2001: copyright ©T. Pearce, D. Hutchinson, L. Marshall Sept  direct mode: OK for static addresses  indirect register mode:
Video systems. Lesson plan Review the code for the previous exercise Video systems Review for midterm exam.
ECE291 Lecture 6 Procedures and macros. ECE 291 Lecture 6Page 2 of 36 Lecture outline Procedures Procedure call mechanism Passing parameters Local variable.
Microprocessor Microprocessor (cont..) It is a 16 bit μp has a 20 bit address bus can access upto 220 memory locations ( 1 MB). It can support.
Subroutines: Passing Arguments Using the Stack. Passing Arguments via the Stack Arguments to a subroutine are pushed onto the stack. The subroutine accesses.
CSC 8505 Compiler Construction Runtime Environments.
EEL 3801 Part IV The Assembler. OFFSET Operator Returns address of variable used as operand. Actually, it represents the offset from the beginning of.
Lecture 9 (The Stack and Procedures). 1 Lecture Outline Introduction The Stack The PUSH Instruction The POP Instruction Terminology of Procedures INDEC.
4-1 Embedded Systems C Programming Language Review and Dissection II Lecture 4.
Multi-module programming. Requirements of an assembly language module when it is linked with another module PUBLIC directive - it exports to other modules.
1 CS 201 Computer Systems Programming Chapter 12 x86 Call & Return Herbert G. Mayer, PSU Status 6/28/2015.
Calling Procedures C calling conventions. Outline Procedures Procedure call mechanism Passing parameters Local variable storage C-Style procedures Recursion.
ICS312 Set 12 Subroutines: Passing Arguments Using the Stack.
Preocedures A closer look at procedures. Outline Procedures Procedure call mechanism Passing parameters Local variable storage C-Style procedures Recursion.
Week 6 Dr. Muhammad Ayaz Intro. to Assembly Language.
ECE291 Computer Engineering II Lecture 12 Josh Potts University of Illinois at Urbana- Champaign.
7-Nov Fall 2001: copyright ©T. Pearce, D. Hutchinson, L. Marshall Oct lecture23-24-hll-interrupts 1 High Level Language vs. Assembly.
I NTEL 8086 M icroprocessor بسم الله الرحمن الرحيم 1.
Storage Allocation Mechanisms
Assembly language programming
Instruction set Architecture
Format of Assembly language
COURSE OUTCOMES OF Microprocessor and programming
© Craig Zilles (adapted from slides by Howard Huang)
Microprocessor and Assembly Language
Introduction to Compilers Tim Teitelbaum
(The Stack and Procedures)
Stack and Subroutines Module M17.1 Section 11.2.
Programming 8086 – Part IV Stacks, Macros
8086 Registers Module M14.2 Sections 9.2, 10.1.
Assembly Language Programming II: C Compiler Calling Sequences
Microprocessor and Assembly Language
EECE.3170 Microprocessor Systems Design I
(The Stack and Procedures)
Morgan Kaufmann Publishers Computer Organization and Assembly Language
Multi-modules programming
EECE.3170 Microprocessor Systems Design I
Unit-I 80386DX Architecture
Where is all the knowledge we lost with information? T. S. Eliot
(The Stack and Procedures)
© Craig Zilles (adapted from slides by Howard Huang)
Procedures and Macros.
Presentation transcript:

ECE291 Computer Engineering II Lecture 8 Josh Potts University of Illinois at Urbana- Champaign

Z. KalbarczykECE291 Outline Recursion Local variable storage Programming with high and low-level languages

Z. KalbarczykECE291 Recursion Recursion: procedure calls itself RecursiveProc DECAX JZ.QuitRecursion CALLRecursiveProc.QuitRecursion: RET Requires a termination condition in order to stop infinite recursion Many recursively implemented algorithms are more efficient than their iterative counterparts

Z. KalbarczykECE291 Recursion (cont.) Example: Factorial ; Input AX = CX = Value ; Output AX = Value ! DEC CX CMP CX,0 ;Test for base case JE.FactDone IMUL CX Call Factorial ; Recurs.FactDone: RET RETURN IP iteration 1 RETURN IP iteration 2 RETURN IP iteration 3 Assume: AX = CX = 4 CX = 4; AX = 4 CX = 3; AX = 12 CX = 2; AX = 24 RETURN IP iteration 4 CX = 1; AX = 24

Z. KalbarczykECE291 Recursion (cont.) Recursion must maintain separate copies of all pertinent information (parameter value, return address, local variables) for each active call Recursive routines can consume a considerable stack space Remember to allocate sufficient memory in your stack segment In general you will not know the depth to which recursion will take you –allocate a large block of memory for the stack

Z. KalbarczykECE291 Local Variable Storage Using Stack Consider a procedure that takes three input integers i, j, k and computes: i = i + 2 j = i * k + j n = j - i m = i + j + n Assumptions: –parameters i, j, k are on the stack before the proc is called –procedure is using stack as temporary storage, that is no longer required when the procedure returns

Z. KalbarczykECE291 Local Variable Storage Using Stack (cont.) OurProcWithLocalVariables PUSHBP MOVBP, SP PUSHAX SUBSP, 6 ;allocate local var MOVAX, [bp+8]; [bp+8]=[i] ADDAX, 2 MOV[bp+8], AX MULword [bp+4]; [bp+4]=[k] ADDAX, [bp+6]; [bp+6]=[j] MOV[bp+6], AX SUBAX, [bp+8] MOV[bp-8], AX; [bp-8]=[n] ADDAX, [bp+8] ADDAX, [bp+6] MOV[bp-6], AX ; [bp-6]=[m] ;deallocate local storage ADDSP, 6 POPAX POPBP RET6 OLD BP RETURN IP AX SP BP i j k l n m i = i + 2 j = i * k + j n = j - i m = i + j + n

Z. KalbarczykECE291 Programming with High and Low Level Languages Why to write in assembly? Speed: assembly language programs are generally the fastest programs –experienced assembly programmers can speed up many programs by a factor of five or ten over their HLL counterparts Space: assembly language programs are often the smallest –sometimes one-half the size of comparable HLL program Capability: one can implement things in assembly which are difficult or impossible in HLLs (the opposite is of course also true) –e.g., direct access of certain I/O devices on the computer Knowledge: knowing assembly will help you will write better programs, even when using HLLs

Z. KalbarczykECE291 Programming with High and Low Level Languages Why to write in high-level languages –easier to write (much) –portable across machines (kind of) We want to have the best of both assembly and high-level languages –ultimately, all code becomes machine language code – both HLL and Assembly –we need a mechanism to call our code directly

Z. KalbarczykECE291 Interaction of C and Assembly Introductory Example Suppose a HLL program calls a procedure named proc1 written in assembly language Proc1 requires three arguments a, b, c The HLL statement might be: proc1(a, b, c) Assume that –the size of arguments is no bigger than one word –the high-level compiler generates code to push the values of a, b, and c onto stack, save the return address, and transfer control to the first instruction in proc1 The assembly language module must be assembled with the correct ret (near or far) and stack handling of its procedures matching the corresponding values in the high-level module

Z. KalbarczykECE291 Interaction of C and Assembly Introductory Example Where will proc1 find its arguments on the stack? In generating code for a procedure (such as the proc1(a, b, c)) call, the C language pushes the arguments on the stack in the order first c, then b, then a - a right-pusher OLD BP RETURN IP SP c b a RETURN CS The stack form when compiler is generating code for the far call of proc1 BP=SP BP+ 10 BP+ 8 BP+ 6 BP+ 4 BP+ 2

Z. KalbarczykECE291 Interaction of C and Assembly Introductory Example How procedures return values to the calling program? If the returned value needs four or fewer bytes, it is by default returned in registers –one or two bytes - returned in AX –three or four bytes - returned in AX (low word) and in DX (high byte or word), (in EAX in 32-bit mode) –more than four bytes - the call procedure stores data in some address (e.g., in data segment) and returns the offset and segment parts of that address in AX and DX, respectively Caller is responsible for clearing the arguments from the stack as soon as it regains control after the call –this done by the compiler that generates the appropriate code –a far procedure called from C should end with RETF

Z. KalbarczykECE291 Calling Assembly from C The call from C Consider a function that allows the user to select row and column coordinates on the screen and prints a string at that location # include extern void placeStr (char *, unsigned, unsigned); voidmain (void) { intn; for (n = 10; n < 20; ++n) placeStr (“This is the string”, n, 45); }

Z. KalbarczykECE291 Calling Assembly from C The assembly procedure GLOBAL_placeStr SEGMENT code _placeStr ; setup stack frame and save state PUSHBP MOVBP, SP PUSHAX PUSHBX PUSHDX ; get current page - returns in BH MOVAH, 0fh INT10h ; read unsigned args 2 and 3 MOVDL, [BP+8] MOVDH, [BP+6] ;set cursor position MOV AH, 02h INT 10h ;point to string MOV BX, [BP+4] ;call outAsc to disp string call outAsc ;restore state POP DX POP BX POP AX POP BP RETF OLD BP RETURN IP SP 45 (column number) Value of n (row number) OFFSET to the string BP=SP BP+ 8 BP+ 6 BP+ 4 AX BX DX

Z. KalbarczykECE291 Calling Assembly from C Putting things together The C module must be compiled The assembly language module assembled The pair must be linked together Extern in C is exactly the same as EXTERN in assembly programs Notice that the procedure is named _placeStr, because C compilers preface all external variables with an underscore It’s possible to write macros to make C-callable functions easier to write in NASM. Later in the course, we’ll use macros to help write C-callable functions in an MP.

Z. KalbarczykECE291 Interfacing Assembly with C Using NASM Macros proc _placeStr.Stringarg 2.Rowarg 2.Colarg 2 ;get current page - returns in BH MOVAH, 0fh INT10h ;set cursor position MOVDL, [bp+.Col] MOVDH, [bp+.Row] MOVAH, 02h INT10h ;display string MOVBX, [bp+.String] CALLoutAsc RETF endproc the procedure receives three arguments on the stack of the size stated, e.g., the NASM macro will generate:.String equ 4.Row equ 6.Col equ 8 Note that it’s still necessary to offset from bp, as in [bp+.Col]. the procedure receives three arguments on the stack of the size stated, e.g., the NASM macro will generate:.String equ 4.Row equ 6.Col equ 8 Note that it’s still necessary to offset from bp, as in [bp+.Col].

Z. KalbarczykECE291 Interfacing Assembly with C Using NASM Macros The NASM “proc” macro generates: PUSHBP MOVBP, SP …. POPBP GLOBAL declarations are generated for all procedure names The assembly language module need only contain RET/RETF

Z. KalbarczykECE291 Calling Assembly from C Example ;Assembly routine Power2 proc _Power2.factor arg 2.power arg 2 movax, [bp+.factor] movcx, [bp+.power] cwde shleax, cl retf endproc /* C++ program calling assembly Power2 */ #include extern “C” int Power2(int, int); void main() { int factor, power; printf ("\nInput Factor = "); scanf("%d", &factor); printf ("\nInput Power = "); scanf("%d", &power); printf("\n --->> (%d * (2^%d)) = %d\n\n", factor, power, Power2(factor, power)); }

Z. KalbarczykECE291 Complete Procedure Call Mechanism (Summary) Program writes function parameters to stack (C is right-pusher) CALL saves program’s return address on the stack [PUSH CS (Far Proc); PUSH IP] Routine marks stack frame (PUSH BP; MOV BP, SP) Routine allocates stack memory for local variables (SUB SP, n) Routine saves registers it modifies (PUSH SI, PUSH DI, PUSH DS, PUSH SS) Subroutine Code Additional CALLs, PUSHs, POPs) Routine restores registers it modifies (POP SS, POP DS, POP DI, POP SI) Routine deallocates stack memory for local variables (ADD SP, n) Routine restores original value of BP (POP BP) Subroutine Returns (RET) Program clears parameters from (ADD SP,p)