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

Similar documents
CS401 - Computer Architecture and Assembly Language Programming Glossary By

Code segment Stack segment

Intel 8086: Instruction Set

Computer Architecture 1 ح 303

PESIT Bangalore South Campus

Addressing Modes on the x86

CS401 Assembly Language Solved MCQS From Midterm Papers

SPRING TERM BM 310E MICROPROCESSORS LABORATORY PRELIMINARY STUDY

ORG ; TWO. Assembly Language Programming

VARDHAMAN COLLEGE OF ENGINEERING (AUTONOMOUS) Shamshabad, Hyderabad

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

Chapter 3: Addressing Modes

6/20/2011. Introduction. Chapter Objectives Upon completion of this chapter, you will be able to:

EC-333 Microprocessor and Interfacing Techniques

9/25/ Software & Hardware Architecture

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

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

INSTRUCTOR: ABDULMUTTALIB A. H. ALDOURI

EEM336 Microprocessors I. Addressing Modes

EC 333 Microprocessor and Interfacing Techniques (3+1)

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

Basic characteristics & features of 8086 Microprocessor Dr. M. Hebaishy

Lecture 15 Intel Manual, Vol. 1, Chapter 3. Fri, Mar 6, Hampden-Sydney College. The x86 Architecture. Robb T. Koether. Overview of the x86

BAHAR DÖNEMİ MİKROİŞLEMCİLER LAB3 FÖYÜ

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

LABORATORY WORK NO. 7 FLOW CONTROL INSTRUCTIONS

Summer 2003 Lecture 4 06/14/03

ADVANCE MICROPROCESSOR & INTERFACING


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

Chapter Three Addressing Mode MOV AX, BX

Experiment N o 8. String Handling Instructions

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.

16-Bit Intel Processor Architecture

CC411: Introduction To Microprocessors

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

Real instruction set architectures. Part 2: a representative sample

Assembling, Linking and Executing 1) Assembling: .obj obj .obj.lst .crf Assembler Types: a) One pass assembler:

8086 programming Control Flow Instructions and Program Structures

Scott M. Lewandowski CS295-2: Advanced Topics in Debugging September 21, 1998

if 2 16bit operands multiplied the result will be

Microprocessor and Assembly Language Week-5. System Programming, BCS 6th, IBMS (2017)

Homework 2. Lecture 6: Machine Code. Instruction Formats for HW2. Two parts: How to do Homework 2!!!!

Introduction to IA-32. Jo, Heeseung

UNIT 2 PROCESSORS ORGANIZATION CONT.

SPRING TERM BM 310E MICROPROCESSORS LABORATORY PRELIMINARY STUDY

INTRODUCTION TO IA-32. Jo, Heeseung

Assembler Programming. Lecture 10

Program controlled semiconductor device (IC) which fetches (from memory), decodes and executes instructions.

EXPERIMENT WRITE UP. LEARNING OBJECTIVES: 1. Get hands on experience with Assembly Language Programming 2. Write and debug programs in TASM/MASM

The x86 Architecture

Segmentation in Assembly Language Programming

UNIT-II. Part-2: CENTRAL PROCESSING UNIT

Hardware and Software Architecture. Chapter 2

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

3.1 DATA MOVEMENT INSTRUCTIONS 45

CSC 2400: Computer Systems. Towards the Hardware: Machine-Level Representation of Programs

CSE351 Spring 2018, Midterm Exam April 27, 2018

Computer Processors. Part 2. Components of a Processor. Execution Unit The ALU. Execution Unit. The Brains of the Box. Processors. Execution Unit (EU)

EEM336 Microprocessors I. Data Movement Instructions

UNIT 4. Modular Programming

Program Control Instructions

Assembler Programming. Lecture 2

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

INSTRUCTOR: ABDULMUTTALIB A. H. ALDOURI

Ethical Hacking. Assembly Language Tutorial

1. Introduction to Assembly Language

Intel 8086 MICROPROCESSOR. By Y V S Murthy

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2018 Lecture 4

Microcomputer Architecture..Second Year (Sem.2).Lecture(2) مدرس المادة : م. سندس العزاوي... قسم / الحاسبات

Section 001. Read this before starting!

Basic Execution Environment

UMBC. contain new IP while 4th and 5th bytes contain CS. CALL BX and CALL [BX] versions also exist. contain displacement added to IP.

x86 architecture et similia

CSIS1120A. 10. Instruction Set & Addressing Mode. CSIS1120A 10. Instruction Set & Addressing Mode 1

Intel 8086 MICROPROCESSOR ARCHITECTURE

Complex Instruction Set Computer (CISC)

EC 333 Microprocessor and Interfacing Techniques (3+1)

Lab 3. The Art of Assembly Language (II)

Read this before starting!

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

CSC 8400: Computer Systems. Machine-Level Representation of Programs

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

8086 INTERNAL ARCHITECTURE

MICROPROCESSOR TECHNOLOGY

16.317: Microprocessor Systems Design I Fall 2013

Northern India Engineering College, Delhi (GGSIP University) PAPER I

3- ADDRESSING MODES in 8086: In this section we use the MOV instruction to describe the data-addressing modes. Figure 3-1 shows the MOV instruction.

icroprocessor istory of Microprocessor ntel 8086:

administrivia today start assembly probably won t finish all these slides Assignment 4 due tomorrow any questions?

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

Module 3 Instruction Set Architecture (ISA)

Come and join us at WebLyceum

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

Introduction to Microprocessor

Summary: Direct Code Generation

Lecture 5:8086 Outline: 1. introduction 2. execution unit 3. bus interface unit

EC-333 Microprocessor and Interfacing Techniques

Experiment 3 3 Basic Input Output

CSE2421 FINAL EXAM SPRING Name KEY. Instructions: Signature

Transcription:

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

Sections in 8088 Code TEXT section, for the processor instructions. DATA section for the initialization of the memory in the data segment, which is known at the start of the process. BSS (Block Started by Symbol), section, for the reservation of memory in the data segment that is not initialized (i.e., initialized to 0). Each of these sections has its own location counter. At run time, the TEXT section is stored in the text segment and the data and BSS sections are stored (consecutively) in the data segment. 2

Label Label An instruction or data word in the assembly language program can begin with a label. A label may also appear all by itself on a line, in which case it is as though it appeared on the next instruction or data word. For example, L is a label that refers to the instruction of data word following it. Different Kinds of Labels Global labels: alphanumeric identifiers followed by a colon (:). These must all be unique, and cannot match any keyword or instruction mnemonic. Local labels: in the TEXT section only, we can have local labels, each of which consists of a single digit followed by a colon (:). A local label may occur multiple times, such as JE 2f; JNE 4b 3

Constants Constants The assembler allows constants to be given a symbolic name using the syntax identifier expression identifier = expression The identifier is an alphanumeric string, as in BLOCKSIZE = 1024 Like all identifiers in this assembly language, only the first eight characters are significant, so BLOCKSIZE and BLOCKSIZZ are the same symbol, namely, BLOCKSIZ. Expressions can be constructed from constants, numerical values, and operators. Labels are considered to be constants because at the end of the first pass their numerical values are known. 4

Pseudoinstructions 5

Pseudoinstructions (cont d) The SPACE Pseudoinstructions The SPACE pseudoinstruction simply requires the location pointer to be incremented by the number of bytes given in the arguments. This keyword is especially useful following a label in the BSS segment to reserve memory for a variable. The ALIGN Pseudoinstructions The ALIGN keyword is used to advance the location pointer to the first 2-, 4-, or 8-byte boundary in memory to facilitate the assembly of words, longs, etc. at a suitable memory location. The EXTERN Pseudoinstructions EXTERN announces that the routine or memory location mentioned will be made available to the linker for external references. The definition need not be in the current file; it can also be somewhere else, as long as the linker can handle the reference. 6

8088 Memory Memory Organization The 8088 has a somewhat ungainly memory organization due to its combination of a 1-MB memory and 16-bit registers. With a 1-MB memory, it takes 20 bits to represent a memory address. However, it is impossible to store a pointer to memory in any of the 16-bit registers. To get around this problem, memory is organized as segments, each of them 64 KB, so an address within a segment can be represented in 16 bits. Memory Segment The memory of the 8088, which consists simply of an array of addressable 8-bit bytes, is used for the storage of instructions as well as for the storage of data and for the stack. In order to separate the parts of the memory which are used for these different purposes, the 8088 uses segments which are chunks of the memory set apart for a certain use. In the 8088, such a segment consists of 65,536 consecutive bytes. 7

Memory Segments Four Segments The code segment The data segment The stack segment The extra segment Code Segment The code segment contains the program instructions. The contents of the PC register are always interpreted as a memory address in the code segment. A PC value of 0 refers to the lowest address in the code segment, not absolute memory address zero. Data Segment The data segment contains the initialized and uninitialized data for the program. When BX contains a pointer, it points to this data segment. Extra Segment The extra segment is a spare segment register that can be placed anywhere in memory that it is needed. 8

Stack Segment Stack Segment The stack segment contains local variables and intermediate results pushed on the stack. Addresses in SP and BP are always in this stack segment. POP and PUSH The stack segment is made up of 2-byte words and so the stack pointer, SP, should always contain an even number. The stack is filled up from high addresses to low addresses. Thus, the PUSH instruction decreases the stack pointer by 2 and then stores the operand in the memory address computed from SS and SP. The POP command retrieves the value, and increments SP by 2. Addresses in the stack segment which are lower than those indicated by SP are considered free. Stack cleanup is thus achieved by merely increasing SP. In practice, DS and SS are always the same, so a 16-bit pointer can be used to refer to a variable in the shared data/stack segment. If DS and SS were different, a 17th bit would be needed on each pointer to distinguish pointers into the data segment from pointers into the stack segment. 9

Segment Registers Address For each of the segments, there exists a corresponding segment register: the 16-bit registers CS, DS, SS, and ES. The starting address of a segment is the 20-bit unsigned integer. It is constructed by shifting the segment register by 4 bits to the left, and putting zero s in the four rightmost positions. This means that segment registers always indicate multiples of 16, in a 20-bit address space. The segment register points to the base of the segment. Addresses within the segment can be constructed by converting the 16- bit segment register value to its true 20-bit address by appending four zero bits to the end and adding the offset to that. In effect, an absolute memory address is computed by multiplying the segment register by 16 and then adding the offset to it. For example, if DS is equal to 7, and BX is 12, then the address indicated by BX is 7 16 + 12 =124. In other words, the 20-bit binary address implied by DS = 7 is 00000000000001110000. Adding the 16-bit offset 0000000000001100 (decimal 12) to the segment s origin gives the 20-bit address 00000000000001111100 (decimal 124). 10

Memory Reference Absolute Address For every memory reference, one of the segment registers is used to construct the actual memory address. If some instruction contains a direct address without reference to a register, then this address is automatically in the data segment, and DS is used to determine the base of the segment. The physical address is found by adding this base to the address in the instruction. The physical address in memory of the next instruction code is obtained by shifting the contents of CS by four binary places and adding the value of the program counter. In other words, the true 20-bit address implied by the 16-bit CS register is first computed, then the 16-bit PC is added to it to form a 20-bit absolute memory address. 11

Addressing: Instruction Destination and Source Almost every instruction needs data, either from memory or from the registers. To name this data, the 8088 has a reasonably versatile collection of addressing modes. Many instructions contain two operands, usually called destination and source. For Instance The copy instruction, MOV AX, BX; The add instruction: ADD CX, 20 In them, the first operand is destination and the second is the source. The destination must be a left value that is, it must be a place where something can be stored. This means that constants can be sources, but not destinations. 12

Addressing: 8088 Design Original design In its original design, the 8088 required that at least one operand in a two operand instruction be a register. This was done so that the difference between word instructions, and byte instructions could be seen by checking whether the addressed register was a word register or a byte register. In the first release of the processor, this idea was so strictly enforced that it was even impossible to push a constant, because neither the source nor the destination was a register in that instruction. Later Version Later versions were less strict, but the idea influenced the design anyway. In some cases, one of the operands is not mentioned. For example, in the MULB instruction, only the AX register is powerful enough to act as a destination. 13

Addressing Mode One Operand There are also a number of one-operand instructions, such as increments, shifts, negates, etc. In these cases, there is no register requirement, and the difference between the word and byte operations has to be inferred from the opcodes (i.e., instruction types) only. Basic Data Types The 8088 supports four basic data types: 1-byte byte, the 2-byte word, the 4-byte long, and binary coded decimal. A memory address always refers to a byte, but in case of a word or a long, the memory locations directly following the indicated byte are implicitly referred to as well. The word at 20 is in the memory locations 20 and 21. The long at address 24 occupies the addresses 24, 25, 26 and 27. In the stack segment, words should be placed at even addresses. 14

Operand Addressing Mode (1) The table gives an overview of the 8088 addressing modes. The topmost horizontal block of the table lists the registers. They can be used as operands in nearly all instructions, both as sources and as destinations. There are eight word registers and eight byte registers. 15

Operand Addressing Mode (2) Operand addressing modes. The symbol # indicates a numerical value or label. 16

Data Segment Addressing (1) Addressing Modes Direct Addressing Register Indirect Addressing Register Displacement Addressing Register with Index Register with Index and Displacement 17

Data Segment Addressing (2) Direct Addressing The easiest addressing mode of this type is direct addressing, in which the data address of the operand is in the instruction itself. Example: ADD CX, (20), in which the contents of the memory word at address 20 and 21 is added to CX. Memory locations are usually represented by labels instead of by numerical values in the assembly language, and the conversion is made at assembly time. The parentheses around the labels are essential (for the assembler we are using) because ADD CX,20 is also a valid instruction, only it means add the constant 20 to CX, not the contents of memory word 20. Register Indirect Addressing In register indirect addressing, the address of the operand is stored in one of the registers BX, SI, or DI. In all three cases the operand is found in the data segment. 18

Data Segment Addressing (3) Register Displacement Addressing Put a constant in front of the register. Then the address is found by adding the register to the constant. It is called register displacement, is convenient for arrays. For example, SI contains 5, then the fifth character of the string at the label FORMAT can be loaded inal by MOVB AL, FORMAT(SI). Register with Index The entire string can be scanned by incrementing or decrementing the register in each step. It is also possible to put the base (i.e., lowest numerical address) of the array in the BX register, and keep the SI or DI register for counting. This is called register with index addressing. For example PUSH (BX)(DI) fetches the contents of the data segment location whose address is given by the sum of the BX and DI registers. This value is then pushed onto the stack. Register with Index and Displacement We can combine the Register Displacement Addressing mode and the Register Displacement Addressing mode together to get register with index and displacement addressing. For example, NOT 20(BX)(DI), which logistically complements the memory word at BX + DI + 20 and BX + DI + 21. 19

Stack Segment Addressing Stack Segment All the indirect addressing modes in the data segment also exist for the stack segment. In this case, the base pointer BP is used instead of the base register BX. In this way (BP) is the only register indirect stack addressing mode, but more involved modes also exist, up to base pointer indirect with index and displacement 1(BP)(SI). These modes are valuable for addressing local variables and function parameters, which are stored in stack addresses in subroutines. 20

Effective&Immediate&Implied Addressing Effective Addressing All the addresses which comply with the addressing modes discussed up to now can be used as sources and as destinations for operations. Together they are defined to be effective addresses. The addressing mode in the following cannot be used as destinations and are not referred to as effective addresses. They can only be used as sources. Immediate Addressing The addressing mode in which the operand is a constant byte or word value in the instruction itself is called immediate addressing. For example, CMP AX, 50 compares AX to the constant 50 and sets bits in the flag register, depending on the results. Implied Addressing In this case, the operand or operands are implicit in the instruction itself. The instruction PUSH AX pushes the contents of AX onto the stack by decrementing SP and then copying AX to the location now pointed to by SP. SP is not named in the instruction itself, however; the fact that it is a PUSH instruction implies that SP is used. Flag related instructions implicitly use status flags register without naming it. 21

Automatic Increment and Decrement Auto Increment/Decrement Mode The 8088 has special instructions for moving (MOVS), comparing (CMPS), and scanning (SCAS) strings. With these string instructions, the index registers SI and DI are automatically changed after the operation. This behavior is called auto increment or auto decrement mode. Whether SI and DI are incremented or decremented depends on the direction flag in the status flags register. For Example A direction flag value of 0 increments, whereas a value of 1 decrements. The change is 1 for byte instructions and 2 for word instructions. The stack pointer is also auto increment and auto decrement: it is decremented by 2 at the start of a PUSH and incremented by 2 at the end of a POP. 22

Subroutine Calls Subroutine The 8088 has an instruction used to call procedures, usually known in assembly language as subroutines. The destination is either a label or can be found at an effective address. Parameters have to be pushed onto the stack in reverse order first. In assembly language, parameters are usually called arguments, but the terms are interchangeable. Following these pushes the CALL instruction is executed. The instruction starts by pushing the current program counter onto the stack. In this way the return address is saved. The return address is the address at which the execution of the calling routine has to be resumed when the subroutine returns. Next the new program counter is loaded either from the label, or from the effective address. The return instruction, RET, just pops the return address from the stack, stores it in the program counter and the program continues at the instruction immediately after the CALL instruction. Sometimes the RET instruction contains a positive number as immediate data. This number is assumed to be the number of bytes of arguments that were pushed onto the stack before the call; it is added to SP to clean up the stack. 23

Subroutine Calls An example stack. 24

Subroutine Example Example ( vecprod.s pp. 732) Compute the inner product of two vector vecmul(count, vec1, vec2) 25

The program vecprod.s.

Figure C-14. The program vecprod.s (cont d)

Jump Instructions JMP JMP is an unconditional jump to a label that is usually within the same procedure. Syntax: JMP target Logic: EIP target Example: top:.. jmp top A jump outside the current procedure must be to a special type of label called a global label 28

Conditional Jumps 29

Program: MY_MAX _EXIT = 1! 1 _PRINTF = 127! 2.SECT.TEXT! 3 start:! 4 ===== Bubble sort on AH, AL, BH, BL ====== MOV AX, 514! 5 AX = 514 MOV BX, 1025! 6 BX = 1025 PUSH BX! 7 2nd parameter of MY_MAX: b PUSH AX! 8 1st parameter of MY_MAX: a CALL MY_MAX! 9 call (AX, BX)=MY_MAX(a, b); // use MY_MAX function to find the min and max ADD SP, 4! 10 Clean up stack output2:! 11 output2 is the same as output1 PUSH BX! 12 push BX for printf, param 3 PUSH AX! 13 push AX for printf, param 2 PUSH pfmf2! 14 push format for printf, param 1 PUSH _PRINTF! 15 push system call ID of printf SYS! 16 make system call ADD SP, 4! 17 clean up 2 word of stack POP AX! 18 store AX POP BX! 19 store BX exit:! 20 Exit PUSH 0! 20 Just return success(0) PUSH _EXIT! 21 system call id exit SYS! 22 make system call MY_MAX:! ret max store in AX! 24 function (AX, BX) = MY_MAX(a, b) PUSH BP! 25 save old value of BP on stack MOV BP, SP! 26 BP = SP, means start a new frame for the function! process! 27 MOV AX, 4(BP)! 28 AX=a MOV BX, 6(BP)! 29 BX=b CMP BX, AX! 30 Compare a and b JL RETURN! 31 if (b<a) goto RETURN; XCHG AX, BX! 32 else swap(a, b); RETURN:! 33 POP BP! 34 restore BP to previous value RET! 35 AX = max, BX = min.sect.data! 36 pfmf2:! 37.ASCIZ "AX:%d, BX:%d\n"! 38.SECT.BSS! 39 30