1 / 35

Data Structures

Data Structures. Covers Chapter 5, pages 144 – 160 and Chapter 6, pages 198 – 203. Instruction du Jour. mov ax, [bx + TPoint.x]. Agenda. Simple Arrays Addressing modes Duplicating data Uninitialized data String variables Local labels STRUCtures. Simple Arrays.

lenka
Download Presentation

Data Structures

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. Data Structures Covers Chapter 5, pages 144 – 160 and Chapter 6, pages 198 – 203

  2. Instruction du Jour mov ax, [bx + TPoint.x]

  3. Agenda • Simple Arrays • Addressing modes • Duplicating data • Uninitialized data • String variables • Local labels • STRUCtures

  4. Simple Arrays • In assembly language, simple arrays can be created on the data segment like this: simpleArray1 DB ‘ ‘ simpleArray2 DD 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 • There are better (and shorter) ways to declare arrays that will be discussed later

  5. Addressing Modes • Informal definition: address, v – to specify the location of something, usually data (can be code) • Nine addressing modes: • Immediate • Register • Direct • Register-indirect • Base • Indexed • Base-indexed • String • I/O port

  6. Immediate Addressing • Example: mov ax, 2 • The “2” is called an immediate value • Is not usually regarded as a type of addressing – but the immediate value is in memory, in the code segment

  7. Register Addressing • Example: mov ax, bx • Like immediate, usually not regarded as a type of addressing – but the register has a physical location in the CPU

  8. Direct Addressing • Syntax: [label] • Example: mov ax, [numBodies] • The label numBodies is assembled into a number – an offset into the data segment • Assuming numBodies is at offset 244, the instruction assembles into something like mov cx, [244]

  9. Register-indirect Addressing • Syntax: [base-or-index-register] • Example: mov ax, [bx] • The register bx is used as an offset into the data segment • Usually used for addressing arrays

  10. Register-indirect Addressing (cont.) • Register used for addressing can be bx, si, or di • The register should contain some meaningful value first • 8086 string instructions use this type of addressing, although the implementation is hidden

  11. Base Addressing • Syntax: [base-register + displacement] • Example: mov ax, [bx + partNumArray] • The contents of bx and the offset of the label are added together to create an offset into the data segment

  12. Base Addressing (cont.) • Usually used for addressing data structures or more than one array of the same size • Register used for addressing can be bx or bp (which uses the stack segment by default) • Again, the register should contain some meaningful value first

  13. Indexed Addressing • Syntax: [index-register + displacement] • Example: mov ax, [si + partNumArray] • The contents of si and the offset of the label are added together to create an offset into the data segment

  14. Indexed Addressing (cont.) • Usually used for addressing data structures or more than one array of the same size • Register used for addressing can be si or di • And again, the register should contain some meaningful value • Strikingly similar (actually almost identical) to base addressing

  15. Base-indexed Addressing • Syntax: [base-register + index-register + displacement] • Example: mov ax, [bx + si + fifthField] • The contents of si and bx, and the offset of the label fifthField are added together to create an offset into the data segment

  16. Base-indexed Addressing (cont.) • Usually used for addressing very complex data structures (e.g. bx = array address, si = array index, displacement = field in record) • Base register can be bx or bp (which uses the stack segment by default), index register can be si or di

  17. String Addressing • Example: movsb • movsb is shorthand for movs [byte es:di], [byte ds:si] • String instructions use register-indirect addressing – but always use di in conjunction with es

  18. Nifty Rules • The types of addressing available for an operand depend on the opcode • Every opcode that allows memory addressing (meaning not immediate or register) allows direct, register-indirect, base, indexed, and base-indexed addressing • Size override directives are necessary when the assembler is unable to decide how much data to address

  19. Duplicating Data • What if you wanted to create a byte string of 10 NULL characters? • Methods: lotsNulls DB 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 lotsNulls DB 10 DUP (0) • The DUP directive duplicates almost any type (or size) of data a specified number of times

  20. DUP, DUP, DUP • Syntax: [label] directive count DUP (expression [, expression]…) • Examples • twentyZeros DB 20 DUP (0) • thirty32s DW 20 DUP (32) • DW 5 DUP (32, 32) • bigList DB 14 DUP (“empty, ”) • DB “empty”, 0 • tonsOfSpace DD 8000 DUP (?)

  21. Uninitialized Data • So far, we’ve worked only with initialized data • Pro – initialized data doesn’t have to be initialized in the code • Con – initialized data takes up space in the executable • The ? expression stands for uninitialized data

  22. ? • ? used with DUP can create very large blocks of uninitialized data that would otherwise take up space in the executable • Examples: scratchSpace DQ ? string255 DB 255 DUP(?) • Single rule: all uninitialized data should be placed at the end of the data segment, or be prefaced with the UDATASEG directive

  23. String Variables • Only the DB directive can set aside space for strings • Use single or double quotes • Rules for using single and double quotes: • ‘ can exist when enclosed by “ • “ can exist when enclosed by ‘ • ‘’ means one ‘ when within two ‘ • “” means one “ when within two “

  24. String Variables (cont.) • Examples: doubleQuote DB “This text isn’t in single quotes.” singleQuote DB ‘This text is in “single” quotes.’ insaneSingle DB ‘This text isn‘’t in ‘’double‘’ quotes.’ insaneDouble DB “This text is in ““double”” quotes.”

  25. Local Labels • Labels so far have been global • Big, gigantic, enormous drawback – it’s hard to come up with new label names for every place in code that needs to be jumped to • Possible solution: • Preface every label with the name of its procedure • Problem: you get big, long, ugly labels that are hard to read

  26. Local Labels (cont.) • Actual solution: • Use a @@ before the label name • Local labels are not visible before or beyond global labels • A good practice is to make every label in a PROC local, and every label between Start and End local

  27. STRUCtures • Structure: a named variable that contains other named variables called fields • Example: STRUC Date day DB 1 month DB ? year DW 1991 ENDS Date

  28. So What? • Using structures • groups information that belongs together • enhances readability • makes for a slick way to create arrays of records

  29. STRUC Syntax • Begins with STRUC label, ends with ENDS label • Fields are declared between, as if they were setting aside space for data • The ? means the data has no default value • A STRUC directive really creates a template, or blueprint, or new data type

  30. Declaring Structures • Syntax: [label] struc-name <initializers> • Examples: birthDay Date <> ; 1-0-1991 today Date <5,10> ; 5-10-1991 dayInDayOut Date <11,12,1912> ; 11-12-1912 anotherDay Date <,8,> ; 1-8-1991

  31. Addressing Fields STRUC TGarbage trash DB ? refuse DB ? ENDS TGarbage DATASEG can TGarbage <24,32> CODESEG mov al, [can.trash] • To address fields in a structure, use the . (period) operator – like member access in Java or C++ • Example: STRUCAD0

  32. Base Addressing • Idea: to set bx to the beginning of the structure and add displacements to address fields • Implementation: first, set bx to the offset of the structure, and access the members like this: mov ax, [bx + TGarbage.trash] • Using the STRUC name (TGarbage) instead of the structure itself gives the displacement of the field in any structure • Example: STRUCAD1

  33. Array of Structures • The structure array is declared using the DUP directive • Idea: to set bx to the beginning of the structure array, add displacements to address fields, and add the size of the structure to bx to address the next structure in the array • The SIZE [structure-name] directive expands to the size of the structure

  34. Array of Structures (cont.) • Example: STRUCAD2 • Example: STRUCAD3 • Question of the Day: assume each structure in the array contains an array of words, and you want to perform some operation on each word. Which memory addressing mode would you use?

  35. Instruction du Jour (revisited) mov ax, [bx + TPoint.x]

More Related