Presentation is loading. Please wait.

Presentation is loading. Please wait.

Computer Organization and Assembly Language

Similar presentations


Presentation on theme: "Computer Organization and Assembly Language"— Presentation transcript:

1 Computer Organization and Assembly Language
Prepared by: Muhammad Faisal

2 Prepared by: Muhammad Faisal
Key Objectives Structure Defining, Declaring and Referencing Structure Structure within Structure Macros Defining Macros Parameters in Macros Difference between Macros and Procedure Prepared by: Muhammad Faisal

3 Prepared by: Muhammad Faisal
Structures A structure is a template or pattern given to a logically related group of variables. The variables in a structure are called fields. Program statements can access the structure as a single entity, or they can access individual fields. Structures often contain fields of different types. For example: You want to store some information about a person: his/her name, citizenship number and salary. You can easily create different variables name, citNo, salary to store these information separately. What if you would want to store information about multiple persons? . Prepared by: Muhammad Faisal

4 Prepared by: Muhammad Faisal
Structures contd. Structures provide an easy way to cluster data and pass it from one procedure to another. You could place all of the input data in a structure and pass the address of the structure to the procedure. Minimal stack space would be used (one address), and the called procedure could modify the contents of the structure. Structures in assembly language are essentially the same as structures in C and C++. Prepared by: Muhammad Faisal

5 Prepared by: Muhammad Faisal
Defining Structures Using a structure involves three sequential steps: 1. Define the structure. 2. Declare one or more variables of the structure type, called structure variables. 3. Write runtime instructions that access the structure fields. A structure is defined using the STRUCT and ENDS directives. Inside the structure, fields are defined using the same syntax as for ordinary variables. Structures can contain virtually any number of fields: Prepared by: Muhammad Faisal

6 Prepared by: Muhammad Faisal
Defining Structures name STRUCT field-declarations name ENDS Field Initializers: Undefined: The ? operator leaves the field contents undefined. String literals: Character strings enclosed in quotation marks. Integers: Integer constants and integer expressions. Arrays: The DUP operator can initialize array elements. Initialization is the process of locating and using the defined values for variable data that is used by a computer program. Prepared by: Muhammad Faisal

7 Prepared by: Muhammad Faisal
Example The following Employee structure describes employee information, with fields such as ID number, last name, years of service, and an array of salary history values. Employee STRUCT IdNum BYTE " " LastName BYTE 30 DUP Years WORD 0 SalaryHistory DWORD 0,0,0,0 Employee ENDS This is a linear representation of the structure’s memory layout: Prepared by: Muhammad Faisal

8 Aligning Structure Field
For best memory I/O performance, structure members should be aligned to addresses matching their data types. Otherwise, the CPU will require more time to access the members. For example, a doubleword member should be aligned on a doubleword boundary. In assembly language, the ALIGN directive sets the address alignment of the next field or variable: ALIGN datatype For example, aligns myVar to a doubleword boundary: .data ALIGN DWORD myVar DWORD ? Prepared by: Muhammad Faisal

9 Declaring Structure Variables
Structure variables can be declared and optionally initialized with specific values. This is the syntax, in which structureType has been defined using the STRUCT directive: identifier structureType < initializer-list > The initializer-list is optional, but if used, is a comma-separated list of assembly-time constants that match the data types of specific structure fields: initializer [, initializer] . . . Prepared by: Muhammad Faisal

10 Declaring Structure Variables
Empty angle brackets < > cause the structure to contain the default field values from the structure definition. Alternatively, you can insert new values in selected fields. The values are inserted into the structure fields in order from left to right, matching the order of the fields in the structure declaration. .data point1 COORD <5,10> ; X = 5, Y = 10 point2 COORD <20> ; X = 20, Y = ? point3 COORD <> ; X = ?, Y = ? worker Employee <> ; (default initializers) Prepared by: Muhammad Faisal

11 Declaring Structure Variables
When the initializer for a string field is shorter than the field, the remaining positions are padded with spaces. You can skip over structure fields by inserting commas as place markers. For example, the following statement skips the IdNum field and initializes the LastName field: person3 Employee <,"dJones"> For an array field, use the DUP operator to initialize some or all of the array elements. If the initializer is shorter than the field, the remaining positions are filled with zeros. person4 Employee <,,,2 DUP(20000)> In the following, we initialize the first two SalaryHistory values and set the rest to zero: person4 Employee <,,,2 DUP(20000)> Prepared by: Muhammad Faisal

12 Prepared by: Muhammad Faisal
Indirect Operands Indirect operands permit the use of a register (such as ESI) to address structure members. Indirect addressing provides flexibility, particularly when passing a structure address to a procedure or when using an array of structures. The PTR operator is required when referencing indirect operands: mov esi,OFFSET worker mov ax,(Employee PTR [esi]).Years An indirect operand is a register that contains the (offset) memory address, which means the register is acting as a pointer. to a variable (or procedure). 16-bit registers for used indirect addressing: SI, DI, BX, and BP. Prepared by: Muhammad Faisal

13 Prepared by: Muhammad Faisal
Indexed Operands We can use indexed operands to access arrays of structures. Suppose department is an array of five Employee objects. The following statements access the Years field of the employee in index position 1: .data department Employee 5 DUP(<>) .code mov esi,TYPE Employee ; index = 1 mov department[esi].Years, 4 Looping through an Array A loop can be used with indirect or indexed addressing to manipulate an array of structures. Prepared by: Muhammad Faisal

14 Referencing Structure Variables
References to structure variables and structure names can be made using the TYPE and SIZEOF operators. For example: Employee STRUCT IdNum BYTE " " ; 9 LastName BYTE 30 DUP(0) ; 30 ALIGN WORD ; 1 byte added Years WORD 0 ; 2 ALIGN DWORD ; 2 bytes added SalaryHistory DWORD 0,0,0,0 ; 16 Employee ENDS ; 60 total Prepared by: Muhammad Faisal

15 Example Displaying the System Time
MS-Windows provides console functions that set the screen cursor position and get the system time. To use these functions, create instances of two predefined structures—COORD and SYSTEMTIME: COORD STRUCT X WORD ? Y WORD ? COORD ENDS SYSTEMTIME STRUCT wYear WORD ? wMonth WORD ? wDayOfWeek WORD ? Prepared by: Muhammad Faisal

16 Example Displaying the System Time
wDay WORD ? wHour WORD ? wMinute WORD ? wSecond WORD ? wMilliseconds WORD ? SYSTEMTIME ENDS To get the system time (adjusted for your local time zone), call the MS-Windows GetLocalTime function and pass it the address of a SYSTEMTIME structure: .data sysTime SYSTEMTIME <> .code INVOKE GetLocalTime, ADDR sysTime Prepared by: Muhammad Faisal

17 Structures containing Structures
Structures can contain instances of other structures. For example, a Rectangle can be defined in terms of its upper-left and lower-right corners, both COORD structures: Rectangle STRUCT UpperLeft COORD <> LowerRight COORD <> Rectangle ENDS Prepared by: Muhammad Faisal

18 Structures containing Structures
Rectangle variables can be declared without overrides or by overriding individual COORD fields. Alternative notational forms are shown: rect1 Rectangle < > rect2 Rectangle { } rect3 Rectangle { {10,10}, {50,20} } rect4 Rectangle < <10,10>, <50,20> > The following is a direct reference to a structure field: mov rect1.UpperLeft.X, 10 Prepared by: Muhammad Faisal

19 Declaring and Using Unions
Whereas each field in a structure has an offset relative to the first byte of the structure, all the fields in a union start at the same offset. The storage size of a union is equal to the length of its longest field. When not part of a structure, a union is declared using the UNION and ENDS directives: unionname UNION union-fields unionname ENDS Prepared by: Muhammad Faisal

20 Declaring and Using Unions
If the union is nested inside a structure, the syntax is slightly different: structname STRUCT structure-fields UNION unionname union-fields ENDS structname ENDS The field declarations in a union follow the same rules as for structures, except that each field can have only a single initializer. Prepared by: Muhammad Faisal

21 Declaring and Using Unions
For example, the Integer union has three different size attributes for the same data and initializes all fields to zero: Integer UNION D DWORD 0 W WORD 0 B BYTE 0 Integer ENDS Prepared by: Muhammad Faisal

22 Prepared by: Muhammad Faisal
MACROS A macro procedure is a named block of assembly language statements. Once defined, it can be invoked (called) as many times in a program as you wish. When you invoke a macro procedure, a copy of its code is inserted directly into the program at the location where it was invoked. This type of automatic code insertion is also known as inline expansion. Prepared by: Muhammad Faisal

23 Prepared by: Muhammad Faisal
MACROS Macro definitions usually appear at the beginning of a program’s source code. They are placed in a separate file and copied into a program by an INCLUDE directive. Macros are expanded during the assembler’s preprocessing step. If a program defines a macro but never calls it, the macro code does not appear in the compiled program. In the following example, a macro named PrintX calls the WriteChar procedure from the library. Prepared by: Muhammad Faisal

24 Prepared by: Muhammad Faisal
Example PrintX MACRO mov al,'X' call WriteChar ENDM Next, in the code segment, we call the macro: .code PrintX When the preprocessor scans this program and discovers the call to PrintX, it replaces the macro invocation with the following statements: mov al,'X' call WriteChar Prepared by: Muhammad Faisal

25 Prepared by: Muhammad Faisal
Defining Macros A macro is defined using the MACRO and ENDM directives. The syntax is macroname MACRO parameter-1, parameter statement-list ENDM There can be any number of parameters in the macro definition, separated by commas. Macro parameters are named placeholders for text arguments passed to the caller. The arguments may in fact be integers, variable names, or other values, but the preprocessor treats them as text. Parameters contain no type information, so the preprocessor does not check argument types to see whether they are correct. Prepared by: Muhammad Faisal

26 Difference between Macros and Procedure
Procedures Accessed during assembly when name given to macro is written as an instruction in the assembly program. Accessed by CALL and RET instructions during program execution. Machine code is generated for instructions each time a macro is called. Machine code for instructions is put only once in the memory. This due to repeated generation of machine code requires more memory. This as all machine code is defined only once so less memory is required. Parameters are passed as a part of the statement in which macro is called. The macro is a "skeleton" a "pattern" from which you generate program parts. Parameters can be passed in register memory location or stack. Procedure is a program part, what you can "call". Prepared by: Muhammad Faisal

27 Prepared by: Muhammad Faisal
Thank You Prepared by: Muhammad Faisal


Download ppt "Computer Organization and Assembly Language"

Similar presentations


Ads by Google