Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "ECE291 Computer Engineering II Lecture 8 Josh Potts University of Illinois at Urbana- Champaign."— Presentation transcript:

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

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

3 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

4 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

5 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

6 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

7 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

8 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

9 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

10 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

11 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

12 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

13 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); }

14 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

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

16 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].

17 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

18 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)); }

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


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

Similar presentations


Ads by Google