1 / 19

ECE291 Computer Engineering II Lecture 8

ECE291 Computer Engineering II Lecture 8. Josh Potts University of Illinois at Urbana- Champaign. Outline. Recursion Local variable storage Programming with high and low-level languages. Recursion. Recursion: procedure calls itself RecursiveProc DEC AX JZ .QuitRecursion

gaille
Download Presentation

ECE291 Computer Engineering II Lecture 8

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. ECE291Computer Engineering IILecture 8 Josh Potts University of Illinois at Urbana- Champaign

  2. Outline • Recursion • Local variable storage • Programming with high and low-level languages ECE291

  3. Recursion • Recursion: procedure calls itself RecursiveProc DEC AX 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 ECE291

  4. 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 Assume: AX = CX = 4 RETURN IP iteration 1 CX = 4; AX = 4 RETURN IP iteration 2 CX = 3; AX = 12 RETURN IP iteration 3 CX = 2; AX = 24 RETURN IP iteration 4 CX = 1; AX = 24 ECE291

  5. 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 ECE291

  6. 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 procis called procedure is using stack as temporary storage, that is no longer required when the procedure returns Local Variable StorageUsing Stack ECE291

  7. OurProcWithLocalVariables PUSH BP MOV BP, SP PUSH AX SUB SP, 6 ;allocate local var MOV AX, [bp+8] ; [bp+8]=[i] ADD AX, 2 MOV [bp+8], AX MUL word [bp+4] ; [bp+4]=[k] ADD AX, [bp+6] ; [bp+6]=[j] MOV [bp+6], AX SUB AX, [bp+8] MOV [bp-8], AX ; [bp-8]=[n] ADD AX, [bp+8] ADD AX, [bp+6] MOV [bp-6], AX ; [bp-6]=[m] ;deallocate local storage ADD SP, 6 POP AX POP BP RET 6 i j k RETURN IP BP OLD BP AX l m n SP Local Variable Storage Using Stack (cont.) i = i + 2 j = i * k + j n = j - i m = i + j + n ECE291

  8. 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 ECE291

  9. 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 ECE291

  10. 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 ECE291

  11. BP+ 10 BP+ 8 BP+ 6 BP+ 4 BP+ 2 Interaction of C and AssemblyIntroductory 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 The stack form when compiler is generating code for the far call of proc1 c b a RETURN CS RETURN IP BP=SP OLD BP SP ECE291

  12. 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 ECE291

  13. Calling Assembly from CThe 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 <stdio.h> extern void placeStr (char *, unsigned, unsigned); void main (void) { int n; for (n = 10; n < 20; ++n) placeStr (“This is the string”, n, 45); } ECE291

  14. GLOBAL _placeStr SEGMENT code _placeStr ; setup stack frame and save state PUSH BPMOV BP, SPPUSH AXPUSH BXPUSH DX ; get current page - returns in BH MOV AH, 0fhINT 10h ; read unsigned args 2 and 3 MOV DL, [BP+8]MOV DH, [BP+6] ;set cursor position MOV AH, 02hINT 10h ;point to string MOV BX, [BP+4] ;call outAsc to disp string call outAsc ;restore state POP DXPOP BXPOP AXPOP BP RETF 45 (column number) BP+ 8 BP+ 6 Value of n (row number) OFFSET to the string BP+ 4 RETURN IP BP=SP OLD BP AX BX SP DX Calling Assembly from CThe assembly procedure ECE291

  15. Calling Assembly from CPutting 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. ECE291

  16. Interfacing Assembly with C Using NASM Macros • 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]. proc _placeStr .String arg 2.Row arg 2.Col arg 2 ;get current page - returns in BH MOV AH, 0fhINT 10h ;set cursor position MOV DL, [bp+.Col]MOV DH, [bp+.Row]MOV AH, 02hINT 10h ;display string MOV BX, [bp+.String]CALL outAsc RETF endproc ECE291

  17. Interfacing Assembly with C Using NASM Macros • The NASM “proc” macro generates: PUSH BP MOV BP, SP …. POP BP • GLOBAL declarations are generated for all procedure names • The assembly language module need only contain RET/RETF ECE291

  18. ;Assembly routine Power2 proc _Power2 .factor arg 2 .power arg 2 mov ax, [bp+.factor] mov cx, [bp+.power] cwde shl eax, cl retf endproc /* C++ program calling assembly Power2 */ #include<stdio.h> 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)); } Calling Assembly from CExample ECE291

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

More Related