Overview of Assembly Language Chapter 4 S. Dandamudi.

Slides:



Advertisements
Similar presentations
There are two types of addressing schemes:
Advertisements

ITCS 3181 Logic and Computer Systems 2015 B. Wilkinson slides3.ppt Modification date: March 16, Addressing Modes The methods used in machine instructions.
The Assembly Language Level
ICS312 Set 6 Operands. Basic Operand Types (1) Register Operands. An operand that refers to a register. MOV AX, BX ; moves contents of register BX to.
Assembly Language Fundamentals
CEN 226: Computer Organization & Assembly Language :CSC 225 (Lec#4)
The 8086 Assembly Programming Data Allocation & Addressing Modes
Chapter 3 Assembly Language: Part 1. Machine language program (in hex notation) from Chapter 2.
TK 2633 Microprocessor & Interfacing Lecture 3: Introduction to 8085 Assembly Language Programming (2) 1 Prepared By: Associate Prof. Dr Masri Ayob.
Room: E-3-31 Phone: Dr Masri Ayob TK 2633 Microprocessor & Interfacing Lecture 1: Introduction to 8085 Assembly Language.
CEN 226: Computer Organization & Assembly Language :CSC 225 (Lec#8) By Dr. Syed Noman.
INTRODUCTION TO IBM PC ASSEMBLY LANGUAGE
INTRODUCTION TO IBM PC ASSEMBLY LANGUAGE
Chapter 2 Software Tools and Assembly Language Syntax.
Addressing Modes Chapter 11 S. Dandamudi To be used with S. Dandamudi, “Fundamentals of Computer Organization and Design,” Springer,  S.
Fundamentals of Assembly language
ICS312 Set 4 Program Structure. Outline for a SMALL Model Program Note the quiz at the next lecture will be to reproduce this slide.MODEL SMALL.586 ;
CEG 320/520: Computer Organization and Assembly Language ProgrammingIntel Assembly 1 Intel IA-32 vs Motorola
Dr. José M. Reyes Álamo 1.  The 80x86 memory addressing modes provide flexible access to memory, allowing you to easily access ◦ Variables ◦ Arrays ◦
Overview of Assembly Language Chapter 9 S. Dandamudi.
A Simple Two-Pass Assembler
1/2002JNM1 Basic Elements of Assembly Language Integer Constants –If no radix is given, the integer is assumed to be decimal. Int 21h  Int 21 –A hexadecimal.
Chapter 3 Elements of Assembly Language. 3.1 Assembly Language Statements.
ASSEMBLY LANGUAGE. Assembler and Compiler Pascal A Program Compiler Version A Assembly Language Versiion A Machine Code Actual version that will be executed.
1/2002JNM1 Positional Notation (Hex Digits). 1/2002JNM2 Problem The 8086 has a 20-bit address bus. Therefore, it can access 1,048,576 bytes of memory.
Debug and Assembler By, B.R.Chandavarkar Lect. COMP Department NITK, Surathkal.
Introduction to 8086 Assembly Language Assembly Language Programming University of Akron Dr. Tim Margush.
Faculty of Engineering, Electrical Department,
Overview of Assembly Language Chapter 4 S. Dandamudi.
Overview of Assembly Language Chapter 4 S. Dandamudi.
Execution of an instruction
Chapter Five–80x86 Assembly program development Principles of Microcomputers 2015年11月15日 2015年11月15日 2015年11月15日 2015年11月15日 2015年11月15日 2015年11月15日 1.
Addressing Modes Chapter 6 S. Dandamudi To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,
Click to add Title Comunicación y Gerencia Click To add Subtitle Click to add Text Fundamentals of Assembly Language.
Assembly Language. Symbol Table Variables.DATA var DW 0 sum DD 0 array TIMES 10 DW 0 message DB ’ Welcome ’,0 char1 DB ? Symbol Table Name Offset var.
Overview of Assembly Language Chapter 4 S. Dandamudi.
Microprocessors used in Personal Computers. The Memory Map of a Personal Computers Transient Program Area (TPA): Holds the operating system (interrupt.
Review of Assembly language. Recalling main concepts.
Arrays. Outline 1.(Introduction) Arrays An array is a contiguous block of list of data in memory. Each element of the list must be the same type and use.
Computer Organization & Assembly Language University of Sargodha, Lahore Campus Prepared by Ali Saeed.
CC410: System Programming Dr. Manal Helal – Fall 2014 – Lecture 4 - Assembler 1.
Assembly language programming
Instruction set Architecture
Introduction to assembly programmıng language
Format of Assembly language
Data Transfers, Addressing, and Arithmetic
Assembly Language programming
Additional Assembly Programming Concepts
Microprocessor Systems Design I
Microprocessor and Assembly Language
INTRODUCTION ABOUT ASSEMBLY
Morgan Kaufmann Publishers Computer Organization and Assembly Language
Computer Organization and Assembly Language (COAL)
Defining Types of data expression Dn [name] expression Dn [name]
Symbolic Instruction and Addressing
Introduction to Assembly Language
BIC 10503: COMPUTER ARCHITECTURE
Morgan Kaufmann Publishers Computer Organization and Assembly Language
Symbolic Instruction and Addressing
(Array and Addressing Modes)
Symbolic Instruction and Addressing
(Array and Addressing Modes)
CNET 315 Microprocessor & Assembly Language
A Simple Two-Pass Assembler
Requirements for coding in Assembly Language
Assembler Directives end label end of program, label is entry point
Chapter 6 –Symbolic Instruction and Addressing
Introduction to 8086 Assembly Language
Computer Architecture and System Programming Laboratory
(Array and Addressing Modes)
Presentation transcript:

Overview of Assembly Language Chapter 4 S. Dandamudi

2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,  S. Dandamudi Chapter 4: Page 2 Outline Assembly language statements Data allocation Where are the operands?  Addressing modes »Register »Immediate »Direct »Indirect Data transfer instructions  mov, xchg, and xlat  Ambiguous moves Overview of assembly language instructions  Arithmetic  Conditional  Iteration  Logical  Shift/Rotate Defining constants  EQU, %assign, %define Macros Illustrative examples Performance: When to use the xlat instruction

2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,  S. Dandamudi Chapter 4: Page 3 Assembly Language Statements Three different classes  Instructions »Tell CPU what to do »Executable instructions with an op-code  Directives (or pseudo-ops) »Provide information to assembler on various aspects of the assembly process »Non-executable –Do not generate machine language instructions  Macros »A shorthand notation for a group of statements »A sophisticated text substitution mechanism with parameters

2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,  S. Dandamudi Chapter 4: Page 4 Assembly Language Statements (cont’d) Assembly language statement format: [label] mnemonic [operands] [;comment]  Typically one statement per line  Fields in [ ] are optional  label serves two distinct purposes: »To label an instruction –Can transfer program execution to the labeled instruction »To label an identifier or constant  mnemonic identifies the operation (e.g. add, or )  operands specify the data required by the operation »Executable instructions can have zero to three operands

2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,  S. Dandamudi Chapter 4: Page 5 Assembly Language Statements (cont’d)  comments »Begin with a semicolon (;) and extend to the end of the line Examples repeat: inc [result] ; increment result CR EQU 0DH ; carriage return character White space can be used to improve readability repeat: inc [result] ; increment result

2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,  S. Dandamudi Chapter 4: Page 6 Data Allocation Variable declaration in a high-level language such as C char response int value float total double average_value specifies »Amount storage required (1 byte, 2 bytes, …) »Label to identify the storage allocated (response, value, …) »Interpretation of the bits stored (signed, floating point, …) –Bit pattern is interpreted as   29,255 as a signed number  36,281 as an unsigned number

2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,  S. Dandamudi Chapter 4: Page 7 Data Allocation (cont’d) In assembly language, we use the define directive  Define directive can be used »To reserve storage space »To label the storage space »To initialize »But no interpretation is attached to the bits stored –Interpretation is up to the program code  Define directive goes into the.DATA part of the assembly language program Define directive format for initialized data [var-name] D? init-value [,init-value],...

2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,  S. Dandamudi Chapter 4: Page 8 Data Allocation (cont’d) Five define directives for initialized data DB Define Byte ;allocates 1 byte DW Define Word ;allocates 2 bytes DD Define Doubleword ;allocates 4 bytes DQ Define Quadword ;allocates 8 bytes DT Define Ten bytes ;allocates 10 bytes Examples sorted DB ’y’ value DW Total DD float1 DD 1.234

2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,  S. Dandamudi Chapter 4: Page 9 Data Allocation (cont’d) Directives for uninitialized data Five reserve directives RESB Reserve a Byte ;allocates 1 byte RESW Reserve a Word ;allocates 2 bytes RESD Reserve a Doubleword ;allocates 4 bytes RESQ Reserve a Quadword ;allocates 8 bytes REST Reserve a Ten bytes ;allocates 10 bytes Examples response resb 1 buffer resw 100 Total resd 1

2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,  S. Dandamudi Chapter 4: Page 10 Data Allocation (cont’d) Multiple definitions can be abbreviated Example message DB ’B’ DB ’y’ DB ’e’ DB 0DH DB 0AH can be written as message DB ’B’,’y’,’e’,0DH,0AH More compactly as message DB ’Bye’,0DH,0AH

2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,  S. Dandamudi Chapter 4: Page 11 Data Allocation (cont’d) Multiple definitions can be cumbersome to initialize data structures such as arrays Example To declare and initialize an integer array of 8 elements marks DW 0,0,0,0,0,0,0,0 What if we want to declare and initialize to zero an array of 200 elements?  There is a better way of doing this than repeating zero 200 times in the above statement »Assembler provides a directive to do this (DUP directive) marks DW 200 DUP (0)

2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,  S. Dandamudi Chapter 4: Page 12 Data Allocation (cont’d) Multiple initializations  The TIMES assembler directive allows multiple initializations to the same value  Examples »Previous marks array marks DW 0,0,0,0,0,0,0,0 can be compactly declared as marks TIMES 8 DW 0

2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,  S. Dandamudi Chapter 4: Page 13 Data Allocation (cont’d) Symbol Table  Assembler builds a symbol table so we can refer to the allocated storage space by the associated label Example.DATA name offset value DW 0value 0 sum DD 0sum 2 marks DW 10 DUP (?)marks 6 message DB ‘The grade is:’,0message 26 char1 DB ?char1 40

2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,  S. Dandamudi Chapter 4: Page 14 Data Allocation (cont’d) Correspondence to C Data Types DirectiveC data type DBchar DWint, unsigned DDfloat, long DQdouble DTinternal intermediate float value

2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,  S. Dandamudi Chapter 4: Page 15 Data Allocation (cont’d) Example: Assembly Program ; Assembly program to print on screen.586 ; allow to use Pentium’s code option segment:use16 ; it’s a mixed mode.model small ; near code and data models.data ; start of the data segment x db 5 ; declare x = 5 y db 4 ; declare y = 4

2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,  S. Dandamudi Chapter 4: Page 16 Data Allocation (cont’d) Example: Assembly Program.code ; start of the code segment start: mov mov ds,ax ; points DS to the data segment ; or you can type.startup mov dl,x ; dl = x add dl,y ; dl = dl + y add dl,'0' ; convert the result from hex. ; to ASCII code mov ah,2h ; DOS print character function int 21h ; print the character mov ah,4ch ; DOS end program function int 21h ; end the program end start

2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,  S. Dandamudi Chapter 4: Page 17 Where Are the Operands? Operands required by an operation can be specified in a variety of ways A few basic ways are:  operand is in a register –register addressing mode  operand is in the instruction itself –immediate addressing mode  operand is in the memory –variety of addressing modes  direct and indirect addressing modes  operand is at an I/O port

2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,  S. Dandamudi Chapter 4: Page 18 Where Are the Operands? (cont’d) Register Addressing Mode  Most efficient way of specifying an operand »operand is in an internal register Examples mov EAX,EBX mov BX,CX  The mov instruction mov destination,source copies data from source to destination

The register addressing mode is the most efficient way of specifying operands for two reasons: The operands are in the registers and no memory access is required. Instructions using the register mode tend to be shorter, as only 3 bits are needed to identify a register. In contrast, we need at least 16 bits to identify a memory location. As a consequence, good compilers attempt to place frequently accessed data items in registers To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,  S. Dandamudi Chapter 4: Page 19 Register Addressing Mode

2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,  S. Dandamudi Chapter 4: Page 20 As an example, consider the following pseudo code: total := 0 for (i = 1 to 400) total = total + marks[i] end for Even if the compiler allocates memory for variables i and total, it should move these variables to registers for the duration of the for loop. At the end of the loop, we can copy the values of these registers to memory. This arrangement is more efficient than accessing variables i and total directly from memory during each iteration. Register Addressing Mode

2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,  S. Dandamudi Chapter 4: Page 21 Where Are the Operands? (cont’d) Immediate Addressing Mode  Data is part of the instruction »operand is located in the code segment along with the instruction »Efficient as no separate operand fetch is needed »Typically used to specify a constant Example mov AL,75 This instruction uses register addressing mode for specifying the destination and immediate addressing mode to specify the source. Such instructions are said to use mixed mode addressing.

2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,  S. Dandamudi Chapter 4: Page 22 Where Are the Operands? (cont’d) Direct Addressing Mode  Data is in the data segment »Need a logical address to access data –Two components: segment:offset »Various addressing modes to specify the offset component –offset part is referred to as the effective address  The offset is specified directly as part of the instruction  We write assembly language programs using memory labels (e.g., declared using DB, DW,...) »Assembler computes the offset value for the label –Uses symbol table to compute the offset of a label

2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,  S. Dandamudi Chapter 4: Page 23 Where Are the Operands? (cont’d) Direct Addressing Mode (cont’d) Examples mov AL,[response] »Assembler replaces response by its effective address (i.e., its offset value from the symbol table) mov [table1],56 »table1 is declared as table1 TIMES 20 DW 0 »Since the assembler replaces table1 by its effective address, this instruction refers to the first element of table1 –In C, it is equivalent to table1[0] = 56

2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,  S. Dandamudi Chapter 4: Page 24 Where Are the Operands? (cont’d) Direct Addressing Mode (cont’d) Problem with direct addressing  Useful only to specify simple variables  Causes serious problems in addressing data types such as arrays »As an example, consider adding elements of an array –Direct addressing does not facilitate using a loop structure to iterate through the array –We have to write an instruction to add each element of the array Indirect addressing mode remedies this problem

2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,  S. Dandamudi Chapter 4: Page 25 Where Are the Operands? (cont’d) Indirect Addressing Mode The offset is specified indirectly via a register  Sometimes called register indirect addressing mode  For 16-bit addressing, the offset value can be in one of the three registers: BX, SI, or DI  For 32-bit addressing, all 32-bit registers can be used Example mov AX,[BX]  Square brackets [ ] are used to indicate that BX is holding an offset value »BX contains a pointer to the operand, not the operand itself

2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,  S. Dandamudi Chapter 4: Page 26 Where Are the Operands? (cont’d) Using indirect addressing mode, we can process arrays using loops Example: Summing array elements  Load the starting address (i.e., offset) of the array into BX  Loop for each element in the array »Get the value using the offset in BX –Use indirect addressing »Add the value to the running total »Update the offset in BX to point to the next element of the array

2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,  S. Dandamudi Chapter 4: Page 27 Where Are the Operands? (cont’d) Loading offset value into a register Suppose we want to load EBX with the offset value of table1 We can simply write mov EBX,table1  It resolves offset at the assembly time Another way of loading offset value »Using the lea instruction –This is a processor instruction –Resolves offset at run time

2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,  S. Dandamudi Chapter 4: Page 28 Where Are the Operands? (cont’d) Loading offset value into a register Using lea (load effective address) instruction  The format of lea instruction is lea register,source  The previous example can be written as lea BX,[table1]  May have to use lea in some instances »When the needed data is available at run time only –An index passed as a parameter to a procedure »We can write lea BX,[table1+SI] to load BX with the address of an element of table1 whose index is in the SI register –We cannot use the mov instruction to do this

2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,  S. Dandamudi Chapter 4: Page 29 Data Transfer Instructions We will look at three instructions  mov (move) »Actually copy  xchg (exchange) »Exchanges two operands  xlat (translate) »Translates byte values using a translation table Other data transfer instructions such as  movsx (move sign extended)  movzx (move zero extended) are discussed in Chapter 7

2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,  S. Dandamudi Chapter 4: Page 30 Data Transfer Instructions (cont’d) The mov instruction  The format is mov destination,source »Copies the value from source to destination »source is not altered as a result of copying »Both operands should be of same size »source and destination cannot both be in memory –Most Pentium instructions do not allow both operands to be located in memory –Pentium provides special instructions to facilitate memory-to-memory block copying of data

2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,  S. Dandamudi Chapter 4: Page 31 Data Transfer Instructions (cont’d) The mov instruction  Five types of operand combinations are allowed: Instruction type Example mov register,register mov DX,CX mov register,immediate mov BX,100 mov register,memory mov EBX,[count] mov memory,register mov [count],ESI mov memory,immediate mov [count],23  The operand combinations are valid for all instructions that require two operands

2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,  S. Dandamudi Chapter 4: Page 32 Assembly Example (1) ; Assembly program to print on screen ; using immediate and register addressing modes.586 ; allow to use Pentium’s code option segment:use16 ; it’s a mixed mode.model small ; near code and data models.code ; start of the code segment start: mov bl,3h ; bl = 3 immediate mode add bl,8h ; bl = bl + 8 immediate

2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,  S. Dandamudi Chapter 4: Page 33 Assembly Example (1) sub bl,6h ; bl = bl - 6 add bl,30h ; convert hex. to decimal number mov dl,bl ; dl = bl register mode mov ah,2h ; DOS print character function int 21h ; print the character mov ah,4ch ; DOS end program function int 21h ; end the program end start

2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,  S. Dandamudi Chapter 4: Page 34 Assembly Example (2) ; Assembly program to print on screen ; using direct addressing modes.586 ; allow to use Pentium’s code option segment:use16 ; it’s a mixed mode.model small ; near code and data models.data numbers db 3,8,6

2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,  S. Dandamudi Chapter 4: Page 35 Assembly Example (2).code ; start of the code segment start:.startup mov dl,[numbers] ; dl = numbers[0] add dl,[numbers+1] ; dl = dl + numbers[1] sub dl,[numbers+2] ; dl = dl - numbers[2] add dl,'0' ; convert result mov ah,2h ; DOS print character function int 21h ; print the character mov ah,4ch ; DOS end program function int 21h ; end the program end start

2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,  S. Dandamudi Chapter 4: Page 36 Assembly Example (3) ; Assembly program to print on screen ; using indirect addressing modes.586 ; allow to use Pentium’s code option segment:use16 ; it’s a mixed mode.model small ; near code and data models.data numbers db 3,8,6.code ; start of the code segment start:.startup mov bx, offset numbers ;point to numbers[0] address

2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,  S. Dandamudi Chapter 4: Page 37 Assembly Example (3) mov dl,[bx] ; dl = numbers[0] inc bx ;point to next location in numbers-add 1 to bx add dl,[bx] ; dl = dl + numbers[1] inc bx ;point to next location in numbers sub dl,[bx] ; dl = dl - numbers[2] add dl,'0' ; convert result mov ah,2h ; DOS print character function int 21h ; print the character mov ah,4ch ; DOS end program function int 21h ; end the program end start

2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,  S. Dandamudi Chapter 4: Page 38 Assembly Example (4) ; Assembly program to print a text in reverse order ; using lea instruction instead of mov.586 ; allow to use Pentium’s code option segment:use16 ; it’s a mixed mode.model small ; near code and data models.data text db 'HELLO‘ ; declare text variable.code ; start of the code segment start: mov mov ds,ax ; point to DS base address

2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,  S. Dandamudi Chapter 4: Page 39 Assembly Example (4) mov si,4 ; store the offset 4 in si register lea bx,[text+si] ;point to text[4] address mov dl,[bx] ; dl = text[4] mov ah,2h ; DOS print character function int 21h ; print the character dec bx ; bx point to previous location in text (bx = bx-1) mov dl,[bx] ; dl = text[3] mov ah,2h ; DOS print character function int 21h ; print the character dec bx ; bx point to previous location in text mov dl,[bx] ; dl = text[2] mov ah,2h ; DOS print character function int 21h ; print the character

2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,  S. Dandamudi Chapter 4: Page 40 Assembly Example (4) dec bx ; bx point to previous location in text mov dl,[bx] ; dl = text[1] mov ah,2h ; DOS print character function int 21h ; print the character dec bx ; bx now point to first location in text mov dl,[bx] ; dl = text[0] mov ah,2h ; DOS print character function int 21h ; print the character mov ah,4ch ; DOS end program function int 21h ; end the program end start