Presentation is loading. Please wait.

Presentation is loading. Please wait.

Data Structures Covers Chapter 5, pages 144 – 160 and Chapter 6, pages 198 – 203.

Similar presentations


Presentation on theme: "Data Structures Covers Chapter 5, pages 144 – 160 and Chapter 6, pages 198 – 203."— 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: simpleArray1DB‘ ‘ simpleArray2DD0, 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: movax, 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: movax, 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: movax, [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: movax, [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: movax, [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: movax, [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 twentyZerosDB20 DUP (0) thirty32sDW20 DUP (32) DW5 DUP (32, 32) bigListDB14 DUP (“empty, ”) DB“empty”, 0 tonsOfSpaceDD8000 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: scratchSpaceDQ? string255DB255 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: doubleQuoteDB “This text isn’t in single quotes.” singleQuoteDB ‘This text is in “single” quotes.’ insaneSingleDB ‘This text isn‘’t in ‘’double‘’ quotes.’ insaneDoubleDB “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: STRUCDate dayDB1 monthDB? yearDW1991 ENDSDate

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 Examples: birthDayDate<>; 1-0-1991 todayDate ; 5-10-1991 dayInDayOutDate ; 11-12-1912 anotherDayDate ; 1-8-1991

31 Addressing Fields To address fields in a structure, use the. (period) operator – like member access in Java or C++ Example: STRUCAD0 STRUCTGarbage trashDB? refuseDB? ENDSTGarbage DATASEG canTGarbage CODESEG moval, [can.trash]

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: movax, [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]


Download ppt "Data Structures Covers Chapter 5, pages 144 – 160 and Chapter 6, pages 198 – 203."

Similar presentations


Ads by Google