1 / 25

ECE291

ECE291. Lecture 5 Let’s get stacked. Lecture outline. Program stack Pushing and popping Program organization Debugging hints Assignments. Program stack. Key characteristics Stores temporary data during program execution One point of access – the top of the stack

kirk
Télécharger la présentation

ECE291

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. ECE291 Lecture 5 Let’s get stacked

  2. Lecture outline • Program stack • Pushing and popping • Program organization • Debugging hints • Assignments ECE 291 Lecture 5 Page 2 of 25

  3. Program stack Key characteristics • Stores temporary data during program execution • One point of access – the top of the stack • Last-in-first-out (LIFO) storage • Data is retrieved in the reverse order from which it was stored • Instructions that directly manipulate the stack • PUSH – places data on top of stack • POP – removes data from top of stack ECE 291 Lecture 5 Page 3 of 25

  4. Program stack Implementation in memory Original SP In use Stack grows in direction of decreasing memory addresses In use Direction of increasing memory addresses In use In use In use SS:SP In use Free Free Free SS Free ECE 291 Lecture 5 Page 4 of 25

  5. Program stack Implementation in memory • SS – Stack segment • SP – Stack pointer always points to the top of the stack • SP initially points to top of stack (high memory address) • SP decreases as data is PUSHed • PUSH AX  SUB SP, 2; MOV[SS:SP], AX • SP increases as data is POPed • POP AX  MOV AX, [SS:SP]; ADD SP, 2 • BP – Base pointer can point to any element on the stack ECE 291 Lecture 5 Page 5 of 25

  6. Push example To address 12FFF Register array 03800 AX 6A 037FF PUSH BX BX 6AB3 B3 037FE CX DX SP before push SP after push SP 0800 SS 0300 03000 Stack segment ECE 291 Lecture 5 Page 6 of 25

  7. Pop example To address 0FFFF Register array 01008 AX 39 01007 POP BX BX 392F 2F 01006 CX DX SP after pop SP before pop SP 1006 SS 0000 00000 Stack segment ECE 291 Lecture 5 Page 7 of 25

  8. Push and pop • PUSH and POP always store or retrieve words of data (never bytes) • 80386 and above allow words or double words to be transferred to and from the stack • Source of data for PUSH • Any internal 16-bit register, immediate data, any segment register, or any two bytes of memory • Pop places data into • Internal register, segment register (except CS), or a memory location ECE 291 Lecture 5 Page 8 of 25

  9. Pusha and popa • 80286 and later include PUSHA and POPA to store and retrieve the contents internal register set (AX, BX, CX, DX, SP, BP, SI, DI) ECE 291 Lecture 5 Page 9 of 25

  10. Stack initialization example • Assume that the stack segment resides in memory locations 10000h – 1FFFFh • The stack segment is loaded with 1000h • The SP is loaded with 0000h • This makes the stack 64KB • First PUSH does 0000h – 0002h = FFFEh, storing data in 1FFFEh and 1FFFFh ECE 291 Lecture 5 Page 10 of 25

  11. Stack use • To store • Registers • Return address information while procedures are executing • Local variables that procedures may require • Dynamically allocated memory • To pass • Parameters to procedures (function arguments) ECE 291 Lecture 5 Page 11 of 25

  12. Temporary register storage PUSH and POP registers to preserve their values PUSH AX ; Place AX on the stack PUSH BX ; Place BX on the stack … < modify contents of AX and BX > … POP BX ; Restore original value of BX POP AX ; Restore original value of AX ECE 291 Lecture 5 Page 12 of 25

  13. Temporary register storage • Why would you want to backup and restore registers? • Because registers are themselves temporary storage for instruction operands or memory addresses • You might need to do a task that modifies registers, but you might then need the original contents of those registers later • Any data that is an end result more than likely should go into a memory location (a variable) ECE 291 Lecture 5 Page 13 of 25

  14. The stack and procedures • call proc_name • Pushes the instruction pointer and sometimes the code segment register onto the stack • Performs an unconditional jump to the label proc_name • ret • Pops saved IP and if necessary saved CS from the stack and back into the IP and CS registers • This causes the instruction following the call statement to be executed next • More on all of this procedure stuff tomorrow ECE 291 Lecture 5 Page 14 of 25

  15. Program organization • Create block structure and/or pseudocode on paper to get a clear concept of program control flow and data structures • Break the total program into logical procedures/macros • Use jumps, loops, etc. where appropriate • Use descriptive names for variables • Noun_type for types • Nouns for variables • Verbs for procedures/macros ECE 291 Lecture 5 Page 15 of 25

  16. Program organization • Good program organization helps with debugging • Programs do not work the first time • Strategy to find problems • Use TD and set breakpoints to check program progress • Use comments to temporarily remove sections of code • Use “print” statements to announce milestones in the progrm • Test values and test cases • Try forcing registers or variables to test output of a procedure • Use “print” statements to display critical data • Double-check your logic • Try a different algorithm if all else fails ECE 291 Lecture 5 Page 16 of 25

  17. NASM directives • Includes %include “drive:\path\filename” • Definitions • DB/RESB define/reserve byte (8 bits) • DW/RESW define/reserve word (16 bits) • DD/RESD define/reserve doubleword (32 bits) • EQU names a constant (has no effect on memory) • Labels • “.” prefixed “local” to previous non-dotted label • “:” suffix not required and doesn’t change label • Global.local can access dotted local labels anywhere by prepending previous non-dotted label ECE 291 Lecture 5 Page 17 of 25

  18. NASM directives • Procedures • Labeled sections of code you can jump to and return from any point in your program • Procedures begin with merely a non-dotted label • Use dotted labels inside procedures to keep them local to the procedure • Helps keep labels unique program-wide ECE 291 Lecture 5 Page 18 of 25

  19. NASM directives • Macros • Procedures require some overhead in memory and execution time • NASM replaces macro call with the macro code itself • Advantages • Faster (no call instruction) • Readability – easier to understand program function • Drawbacks • Using the macro multiple times duplicates code • Tricky to debug sometimes (especially when you have nested macros) ECE 291 Lecture 5 Page 19 of 25

  20. NASM directives • References to procedures • EXTERN name – gives you access to procedures and variables in other files (such as library files) • GLOBAL name – makes your procedures and variables available to other files (as if you were creating a library) • Segment definition • SEGMENT name • Examples: SEGMENT STACK SEGMENT CODE ECE 291 Lecture 5 Page 20 of 25

  21. Example program structure ; ECE291:MPXXX ; In this MP you will develop a program which take input ; from the keyboard ;======Constants================================================= ;ASCII values for common characters CR EQU 13 ; EQU’s have no effect on memory LF EQU 10 ; They are preprocessor directives only ESCKEY EQU 27 ; LF gets replace with 10 when assembled ;====== Externals================================================= ; -- LIB291 Routines extern dspmsg, dspout, kbdin extern rsave, rrest, binasc ECE 291 Lecture 5 Page 21 of 25

  22. Example program structure ;==== LIBMPXXX Routines (Your code will replace calls to these ;functions) extern LibKbdHandler extern LibMouseHandler extern LibDisplayResult extern MPXXXXIT ;====== Stack ==================================================== stkseg segment STACK ; *** STACK SEGMENT *** resb 64*8 ; 64*8 = 512 Bytes of Stack stacktop: ;====== Begin Code/Data========================================== codeseg segmentCODE ; *** CODE SEGMENT *** ECE 291 Lecture 5 Page 22 of 25

  23. Example program structure ;====== Variables============================================== inputValid db 0 ; 0: InputBuffer is not ready ; 1: InputBuffer is ready ;-1: Esc key pressed operandsStr db 'Operands: ','$' OutputBuffer 16 times db 0 ; Contains formatted output db ‘$’ ; (Should be terminated with '$') MAXBUFLENGTH EQU 24 InputBuffer MAXBUFLENGTH times db 0 ; Contains one line of user input db ‘$’ graphData %include “graphData.dat” ; data GLOBAL outputBuffer, inputValid, operandsStr GLOBAL graphData ECE 291 Lecture 5 Page 23 of 25

  24. Example program structure ;====== Procedures=========================================== KbdHandler <Your code here> MouseHandler <Your code here> DisplayResult <Your code here> ;====== Program Initialization =============================== ..start: mov ax, cs ; Use common code & data segment mov ds, ax mov sp, stacktop ; Initialize top of stack ECE 291 Lecture 5 Page 24 of 25

  25. Example program structure ;====== Main Procedure ======================================== MAIN: MOV AX, 0B800h ;Use extra segment to access video MOV ES, AX <here comes your main procedure> CALL MPXXXXIT ; Exit to DOS ECE 291 Lecture 5 Page 25 of 25

More Related