Summer 2003 Lecture 15 07/03/03

Similar documents
Summer 2003 Lecture 14 07/02/03

1: /********************************************************** 2: * A

3: 4: 5: 9: ',0 10: ',0 11: ',0 12: 13:.CODE 14: INCLUDE

BLDEA S V.P. DR. P.G. HALAKATTI COLLEGE OF ENGINEERING & TECHNOLOGY, VIJAYAPURA

Parameter Passing. Procedure line. Calling procedure line. Most subroutines require parameters Can sometimes pass parameters via registers

Assembler Programming. Lecture 10

Summer 2003 Lecture 4 06/14/03

Topics Introduction to Microprocessors. Chapter 5 Macros and modules. What is macro? How to use macro? (I) How to use macro?

Copyright 2000 by Barry B. Brey The CPU Scheduling Processes

COE 205 Lab Manual Experiment N o 12. Experiment N o Using the Mouse

Week /8086 Microprocessor Programming

EE 314 Spring 2003 Microprocessor Systems

Week /8086 Microprocessor Programming II

Lecture (08) x86 programming 7

Segmentation in Assembly Language Programming

A4 Sample Solution Ch3

Computer Architecture and System Software Lecture 07: Assembly Language Programming

Lecture 16: Passing Parameters on the Stack. Push Examples. Pop Examples. CALL and RET

SPRING TERM BM 310E MICROPROCESSORS LABORATORY PRELIMINARY STUDY

SPRING TERM BM 310E MICROPROCESSORS LABORATORY PRELIMINARY STUDY

ELEC 242 Using Library Procedures

PESIT Bangalore South Campus

ORG ; TWO. Assembly Language Programming

EXPERIMENT TWELVE: USING DISK FILES

LABORATORY WORK NO. 8 WORKING WITH MACROS AND LIBRARIES

CS-202 Microprocessor and Assembly Language

EC 333 Microprocessor and Interfacing Techniques (3+1)

UNIVERSITY OF CALIFORNIA, RIVERSIDE

Module 3 Instruction Set Architecture (ISA)

Dr. D.M. Akbar Hussain

Objectives. ICT106 Fundamentals of Computer Systems Topic 8. Procedures, Calling and Exit conventions, Run-time Stack Ref: Irvine, Ch 5 & 8

UNIT 4. Modular Programming

Procedures and the Stack. Chapter 4 S. Dandamudi

PESIT Bangalore South Campus Hosur road, 1km before Electronic City, Bengaluru -100 Department of Electronics and Communication

PHY4635/5635 Spring Lecture 8: Program Control Instructions

INSTRUCTOR: ABDULMUTTALIB A. H. ALDOURI

Lecture (06) x86 programming 5

LABORATORY WORK NO. 7 FLOW CONTROL INSTRUCTIONS

Marking Scheme. Examination Paper Department of CE. Module: Microprocessors (630313)

PESIT Bangalore South Campus

CS401 Assembly Language Solved Subjective MAY 03,2012 From Midterm Papers. MC

Programs for Assembly Language Programming

SRI VENKATESWARA COLLEGE OF ENGINEERING AND TECHNOLOGY DEPARTMENT OF ECE EC6504 MICROPROCESSOR AND MICROCONTROLLER (REGULATION 2013)

EECE.3170: Microprocessor Systems Design I Summer 2017 Homework 4 Solution

db "Please enter up to 256 characters (press Enter Key to finish): ",0dh,0ah,'$'

Marking Scheme. Examination Paper. Module: Microprocessors (630313)

Summer 2003 Lecture 12 06/26/03

Assignment no:4 on chapter no :3 : Instruction set of 8086

The Stack. Lecture 15: The Stack. The Stack. Adding Elements. What is it? What is it used for?

Intel 8086: Instruction Set

Selection and Iteration. Chapter 7 S. Dandamudi

It is possible to define a number using a character or multiple numbers (see instruction DB) by using a string.

Chapter 3. Assembly Language Programming with 8086

Basic Assembly SYSC-3006

Ex: Write a piece of code that transfers a block of 256 bytes stored at locations starting at 34000H to locations starting at 36000H. Ans.

X86 Addressing Modes Chapter 3" Review: Instructions to Recognize"

Islamic University Gaza Engineering Faculty Department of Computer Engineering ECOM 2125: Assembly Language LAB. Lab # 7. Procedures and the Stack

8086 INSTRUCTION SET

CG2007 Microprocessor systems.

Assembly Language Lab #5

Memory Organization. 27 December 2016 Pramod Ghimire. Slide 1 of 21

16.317: Microprocessor Systems Design I Fall 2013

CS/ECE/EEE/INSTR F241 MICROPROCESSOR PROGRAMMING & INTERFACING MODULE 4: 80X86 INSTRUCTION SET QUESTIONS ANUPAMA KR BITS, PILANI KK BIRLA GOA CAMPUS

We will begin our study of computer architecture From this perspective. Machine Language Control Unit

Microprocessor. By Mrs. R.P.Chaudhari Mrs.P.S.Patil

ELEC 242 Time Delay Procedure

EEM336 Microprocessors I. Data Movement Instructions

Computer Architecture and System Software Lecture 06: Assembly Language Programming

CSCI516: Program 1 - October 11, 2010 The Program is due: October 25, 2010 in the beginning of the class

Lesson 4. Stack, Procedures and Macros

Experiment 3 3 Basic Input Output

SHEET-2 ANSWERS. [1] Rewrite Program 2-3 to transfer one word at a time instead of one byte.

Experiment 8 8 Subroutine Handling Instructions and Macros

16.317: Microprocessor Systems Design I Fall 2014

Q. State and Explain steps involved in program development. [w-08, w-10, s-12, w-11]

16.317: Microprocessor Systems Design I Fall 2015

Experiment #2. Addressing Modes and Data Transfer using TASM

Objectives. Saving Interrupt Vectors. Writing a Custom Interrupt Handler. Examples of use of System functions for Input-Output and Interrupts

16.317: Microprocessor Systems Design I Spring 2015

We will first study the basic instructions for doing multiplications and divisions

Chapter 3: Addressing Modes

complement) Multiply Unsigned: MUL (all operands are nonnegative) AX = BH * AL IMUL BH IMUL CX (DX,AX) = CX * AX Arithmetic MUL DWORD PTR [0x10]

Overview of Compiler. A. Introduction

CHAPTER SEVENTEEN Assemblers versus Compilers. Intel 80x86 Assembly Language

THE UNIVERSITY OF TRINIDAD & TOBAGO

ECOM Computer Organization and Assembly Language. Computer Engineering Department CHAPTER 7. Integer Arithmetic

CONTENTS. 1. Display a Message Display a one Digit Number Accept a Character from keyboard and display the character 4

Assembly Language. Dr. Esam Al_Qaralleh CE Department Princess Sumaya University for Technology. Overview of Assembly Language

Certified Ethical Hacker. Module 25 Writing Virus Codes

Logic Instructions. Basic Logic Instructions (AND, OR, XOR, TEST, NOT, NEG) Shift and Rotate instructions (SHL, SAL, SHR, SAR) Segment 4A

Q1: Define a character string named CO_NAME containing "Internet Services" as a constant?

Lab 3. The Art of Assembly Language (II)

reply db y prompt db Enter your favourite colour:, 0 colour db 80 dup(?) i db 20 k db? num dw 4000 large dd 50000

Computer Organization & Assembly Language Programming. CSE 2312 Lecture 15 Addressing and Subroutine

WINTER 12 EXAMINATION Subject Code : Model Answer Page No : / N. a) Describe the function of SID and SOD pins of 8085 microprocessor

.code. lea dx,msg2. Page 1/8. Problem 1: Programming in Assembly [25 Points]

EE 332 Real Time Systems Midterm Examination Solution Friday February 13, :30 pm to 4:30 pm

Programming in Module. Near Call


Constants and. Lecture 7: Assembly Language Programs. Expressions (cont.) Constants and. Statements. Expressions

Transcription:

Summer 2003 Lecture 15 07/03/03 Initialization of Variables In the C (or C++) programming language any variable definition can have an optional initializer for the variable. How and when the initialization occurs is determined by the storage class of the variable. The syntax is similar to the following: int foo = 10 The term storage class refers to how and when memory space (storage) for the variable is allocated. In C there are basically two options for storage class: static and automatic. The memory for static variables is allocated by the compiler at the time of compilation. In an x86 system, the compiler will reserve space in the program s data segment at compilation time to store the variable. The memory space for automatic variables is allocated at run time on the stack. The compiler will generate code to allocate space for automatic local variables on entry to a procedure and de-allocate the space on exit from the procedure. The storage class assigned to a variable is determined by how and where the variable is defined. All variables defined outside the scope of any procedure (i.e. outside the body of any subroutine or function) must be static. Thus, global variables are, and must be static. By default, variables defined within a procedure (local variables) will be of storage class automatic. It is possible to specify that a local variable within a procedure be given static storage class by using the keyword static at the beginning of the declaration, as follows: static int foo = 10 The assignment of initial values is determined by the variable s storage class. All static variables will be initialized once before execution of the program begins. Generally, this is done by the compiler initializing the memory allocated in the data segment for all initialized variables. Most C compilers for the x86 processors actually generate two data segments one for initialized variables and one for uninitialized variables, which will be combined by the linker into a single segment.

Automatic variables are initialized each time that the procedure where they are declared is executed. Because the memory is allocated on the stack automatically on entry to the procedure, the initialization must also be performed on each entry to the procedure.

For scalar variables, the initialization could look something like this: C Code: void subr(void) { int x = 1 int y = 2 } x = x+y return x ASM Code subr proc near push bp mov bp,sp sub sp,4 mov [bp-2],1 initialize x mov [bp-4],2 initialize y mov ax,[bp-2] add ax,[bp-4] mov sp,bp pop bp ret subr endp Notice that the compiler would generate code to perform the initialization that would explicitly assign a constant value to the variable. This is essentially the same code that would be generated by an assignment statement. In C, initializing a scalar variable via an initializer on the declaration and via an explicit assignment statement cause the compiler to generate essentially the same code.

When assigning initial values to elements of an array, the situation becomes more complex. If an array is: local to a procedure and not declared to be of static storage class, it will be of storage class automatic, and allocated on the stack the same as a scalar variable. If the array is given initializers, it must be initialized according to the same rules. This means that the initialization must be performed each time the subroutine is entered and the compiler must generate code to perform this initialization. If the array was very large, this would require the compiler to generate a large amount of code to perform the initialization on entry to the procedure. Most compilers handle initialization of arrays in a different manner. In most cases, a table of initial values will be created in the initialized data area of the data segment, and the compiler will generate code to copy this table of initial values from its location in the data segment to the appropriate location on entry to the procedure. This will reduce the total size of the program and slightly improve the performance of performing the initialization. Furthermore, in many compiler implementations, most of the work of copying the table of initializers will be performed by a compiler helper function in the run time library and the compiler will only: generate code to set up the parameters to the helper function and call the library function, further reducing the code size overhead of the initialization. Programming Example: The example program shown below illustrates several points. 1) Binary search algorithm 2) Use of table of pointers pointing to variable length data 3) Use of registers to pass parameters to subroutines 4) Using push and pop to preserve registers modified by a subroutine to preserve the register state of the calling procedure Binary Search: Binary search is a search algorithm that can be employed to search ordered (sorted) data. It depends upon the fact that the data is ordered to work. The algorithm works by dividing the data set into smaller and smaller regions until the target item is either found, or found not to be in the set. Two pointers are maintained into the data set. One points to the lower end of the range being considered, the other points to the upper end of the range.

At each iteration of the algorithm, the item at the middle of the range is compared to the target value. If the item matches, then the search terminates with success. If the item doesn t match then the range is adjusted by either moving the lower pointer up or the upper pointer down. The determination of which pointer to move is made by whether the item tested was greater than or less than the item being search for. This traps the item being searched for into smaller and smaller ranges until it is found, or the size of the range goes to 0, in which case the item isn t in the data set. Use of pointers for variable length data When maintaining an array of data items of variable length, it is generally less useful to maintain them directly in the table. There are several reasons for this. If items are being inserted and deleted from the table, it makes the insertion and deletion task more difficult if the items in the table are not all the same size. If reordering the items in the table is required (such as when sorting) the task or exchanging items is much more difficult if all items are not the same size. Generally when maintaining a table of large or variable sized items, an array of pointers to the items is maintained. Then, for example, when sorting the data, it is only necessary to exchange pointers to swap items rather than moving all of the data associated with the item.

FindStr -- This is an example program to illustrate a binary search of a table of pointers to strings. _DATA segment public byte 'DATA' str0 db "This is string 00",0 str1 db "This is string 01",0 str2 db "This is string 02",0 str3 db "This is string 03",0 str4 db "This is string 04",0 str5 db "This is string 05",0 str6 db "This is string 05",0 str7 db "This is string 07",0 str8 db "This is string 08",0 str9 db "This is string 09",0 str10 db "This is string 10",0 str11 db "This is string 11",0 str12 db "This is string 12",0 str13 db "This is string 13",0 str14 db "This is string 14",0 strtab dw str0,str1,str2,str3,str4,str5,str6,str7 dw str8,str9,str10,str11,str12,str13,str14 srch0 db "Not in table",0 srch1 db "This is string 03",0 srch2 db "This is string 11",0 srch3 db "This is string 00",0 srch4 db "This is string 14",0 srch5 db "Also not in table",0 _DATA _STCK ends segment stack dw 128 dup (?) _STCK ends

_TEXT segment public byte 'CODE' assume cs:_text start: mov ax,_data mov ds,ax mov es,ax assume ds:_data,es:_data mov bx,offset strtab mov cx,15 mov dx,offset srch0 mov dx,offset srch1 mov dx,offset srch2 mov dx,offset srch3 mov dx,offset srch4 mov dx,offset srch5 mov ax,4c00h int 21h

FindStr - Perform a binary search of a table of a sorted table of strings for a particular string. Input: BX - base address of the string pointer table CX - number of string pointers in the table DX - pointer to the search string Output: AX - returns the index of the search string CF - clear if string found, set if not found FindStr proc near push cx push dx push si push di mov di,dx keep target str ptr in DI mov dx,cx keep high pointer in DX xor cx,cx keep low pointer in CX Find the middle of the current range, and compare that string with the target string fnds20: cmp cx,dx if low ptr >= high ptr jae fnds80 then string isn't in table mov ax,cx add ax,dx shr ax,1 find middle of range mov si,ax add si,si mov si,[bx+si] get pointer to string at middle to SI call CmpStr compare the strings jz fnds90 if match, we found it This one wasn't the one. Adjust the low end up or the high end down. jc fnds30 if *si < *di adjust low end up mov dx,ax jmp fnds20 fnds30: mov cx,ax jmp fnds20

String not found in the table. Return CF set fnds80: stc All done fnds90: pop di pop si pop dx pop cx ret FindStr endp CmpStr - compare two zero terminated string Input: SI - pointer to first string DI - pointer to second string Output: CF set if *SI < *DI ZF set if *SI == *DI CmpStr proc near push si push di cpst20: cmpsb jnz cpst90 compare the next byte of the two strings cmp byte ptr [si-1],0 check for end of string jnz cpst20 and repeat if not end cpst90: pop di pop si ret CmpStr endp _TEXT ends end start