Computer Architecture and System Software Lecture 04: Floating Points & Intro to Assembly

Similar documents
Intel 8086: Instruction Set

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

INSTRUCTOR: ABDULMUTTALIB A. H. ALDOURI

VARDHAMAN COLLEGE OF ENGINEERING (AUTONOMOUS) Shamshabad, Hyderabad

ADVANCE MICROPROCESSOR & INTERFACING

Introduction to Microprocessor

EC 333 Microprocessor and Interfacing Techniques (3+1)

Chapter 3: Addressing Modes

Defining and Using Simple Data Types

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

Arithmetic Instructions

Code segment Stack segment

ORG ; TWO. Assembly Language Programming

Intel 8086 MICROPROCESSOR ARCHITECTURE

IBM PC Hardware CPU 8088, Pentium... ALU (Arithmetic and Logic Unit) Registers. CU (Control Unit) IP.

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

Intel 8086 MICROPROCESSOR. By Y V S Murthy

Lecture 9. INC and DEC. INC/DEC Examples ADD. Arithmetic Operations Overflow Multiply and Divide

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

Signed number Arithmetic. Negative number is represented as

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

8088/8086 Programming Integer Instructions and Computations

EC-333 Microprocessor and Interfacing Techniques

CS-202 Microprocessor and Assembly Language

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

8086 INTERNAL ARCHITECTURE

Computer Architecture and System Software Lecture 06: Assembly Language Programming

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

17. Instruction Sets: Characteristics and Functions

Basic Assembly SYSC-3006

EEM336 Microprocessors I. Data Movement Instructions

US06CCSC04: Introduction to Microprocessors and Assembly Language UNIT 1: Assembly Language Terms & Directives

Microprocessors & Assembly Language Lab 1 (Introduction to 8086 Programming)

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

CC411: Introduction To Microprocessors

UNIT 2 PROCESSORS ORGANIZATION CONT.

EEM336 Microprocessors I. The Microprocessor and Its Architecture

Computer Architecture 1 ح 303

Islamic University Gaza Engineering Faculty Department of Computer Engineering ECOM 2125: Assembly Language LAB

Computer Architecture and System Software Lecture 07: Assembly Language Programming

9/25/ Software & Hardware Architecture

SPRING TERM BM 310E MICROPROCESSORS LABORATORY PRELIMINARY STUDY

Instructions moving data

CS401 Assembly Language Solved MCQS From Midterm Papers

Experiment 3 3 Basic Input Output

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

Lecture 5: Computer Organization Instruction Execution. Computer Organization Block Diagram. Components. General Purpose Registers.

Addressing Modes on the x86

We can study computer architectures by starting with the basic building blocks. Adders, decoders, multiplexors, flip-flops, registers,...

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

Internal architecture of 8086


Module 3 Instruction Set Architecture (ISA)

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

Basic Execution Environment

ECOM 2325 Computer Organization and Assembly Language. Instructor: Ruba A.Salamah INTRODUCTION

Mr. Sapan Naik 1. Babu Madhav Institute of Information Technology, UTU

EEM336 Microprocessors I. Arithmetic and Logic Instructions

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

SPRING TERM BM 310E MICROPROCESSORS LABORATORY PRELIMINARY STUDY

EEM336 Microprocessors I. Addressing Modes

MICROPROCESSOR PROGRAMMING AND SYSTEM DESIGN

1-Operand instruction types 1 INC/ DEC/ NOT/NEG R/M. 2 PUSH/ POP R16/M16/SR/F 2 x ( ) = 74 opcodes 3 MUL/ IMUL/ DIV/ DIV R/M

Advanced Microprocessors

INTRODUCTION TO MICROPROCESSORS

Experiment #2. Addressing Modes and Data Transfer using TASM

Q1: Multiple choice / 20 Q2: Memory addressing / 40 Q3: Assembly language / 40 TOTAL SCORE / 100

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

Inline Assembler. Willi-Hans Steeb and Yorick Hardy. International School for Scientific Computing

PESIT Bangalore South Campus

icroprocessor istory of Microprocessor ntel 8086:

The x86 Microprocessors. Introduction. The 80x86 Microprocessors. 1.1 Assembly Language

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

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

Tutorial Letter 103/3/2012 Computer Organization COS2621 Semesters 1 & 2

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

Summer 2003 Lecture 4 06/14/03

Mnem. Meaning Format Operation Flags affected ADD Addition ADD D,S (D) (S)+(D) (CF) Carry ADC Add with ADC D,C (D) (S)+(D)+(CF) O,S,Z,A,P,C

Computer Organization and Assembly Language CSC-210

ALT-Assembly Language Tutorial

CS401 - Computer Architecture and Assembly Language Programming Glossary By

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.

Week /8086 Microprocessor Programming I

Assembly Language Each statement in an assembly language program consists of four parts or fields.

Experiment N o 1. 1 Introduction to Assembly Language Programming

1. Introduction to Assembly Language

Arithmetic and Logic Instructions And Programs

if 2 16bit operands multiplied the result will be

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

The x86 Architecture

Lecture (02) The Microprocessor and Its Architecture By: Dr. Ahmed ElShafee

Q1: Multiple choice / 20 Q2: Protected mode memory accesses

16-Bit Intel Processor Architecture

EC-333 Microprocessor and Interfacing Techniques

An 8-Bit Scientific Calculator Based Intel 8086 Virtual Machine Emulator

A Presentation created By Ramesh.K Press Ctrl+l for full screen view

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

Introduction to IA-32. Jo, Heeseung

Computer Architecture and System Software Lecture 12: Review. Instructor: Rob Bergen Applied Computer Science University of Winnipeg

Lecture (07) x86 programming 6

Transcription:

Computer Architecture and System Software Lecture 04: Floating Points & Intro to Assembly Instructor: Rob Bergen Applied Computer Science University of Winnipeg

Decimal Addition Review decimal addition in scientific notation: Step 1: Align decimal points Step 2: Add Step 3: Normalize result (if needed) Examples: Without normalization: (3.25 10 3 )+(2.63 10-1 ) = (3.25 + 0.000263) x 10 3 = 3.250263 x 10 3 With normalization: (9.25 10^3)+(8.41 10^2) = (9.25 + 0.841) x 10 3 = 10.091x 10 3 = 1.091 x 10 4

Floating Point Addition Before proceeding note: Shifting frac left by 1 bit decreases exp by 1 Shifting frac right by 1 bit increases exp by 1 The idea behind adding FP numbers is to make exp of the smaller number equal to exp of the larger Then, the steps are the same as decimal: Convert numbers to binary FP (if necessary) Align binary points Add Normalize result

Example: FP Addition 1. 100 + 0.25 2. 100 + 80

Floating Point Subtraction Like addition in terms of aligning radix points But, requires a few more rules: Signs If the signs are the same, then do subtraction If signs are different, then change the problem to addition Magnitudes Compare magnitudes, then subtract smaller from larger If the order is switched, then switch sign of result. Don t forget to normalize!

Examples: FP Subtraction 100 80 Signs are the same, so perform subtraction 100 (-80) Signs are different so change to addition (i.e. 100 + 80) 80 100 Magnitude of 80 is less than 100, so perform 100 80 and change the sign bit of the result -100 (-80) Magnitude of 100 is larger than 80, so perform subtraction as is Note: same as 100 80 except sign bit is 1 for both numbers -80 100 Signs are different so change to addition (-80 + -100)

Floating Point Multiplication Multiply significands and add exponents (add E not exp) Decimal (3 10^1) (5 10^2) = (1.5 10^4)

Floating Point Division Perform unsigned division on significands Subtract exponent (subtract E not exp) Decimal (5 10^2) / (2 10^1) = (2.5 10^1)

Final Thoughts on Ints, Floats When do we want to use ints vs floats, or vice versa? Ints require less space (bits) but have a much smaller range Can overflow Floats have larger range per bit, but require more space Convenient for very small or very large numbers Can represent fractions and infinite/nan May produce rounding/precision errors

Assembly Language Background

Assembly Language Programming We will be working with the Intel 32-bit architecture For work at home you will need an emulator, DOSBOX http://www.dosbox.com/ Other software packages available, but solutions given in class will only be guaranteed to compile using DOSBOX

High-Level Language High-level language (HLL) shield us from machinelevel complexity HLL programs are Easier to write Less error prone Readable Portable Efficient

High-Level Languages HLL relieves programmer from Writing in machine-level instructions Managing memory Rewriting programs to run on different machines

Compiler Main work is done by compiler Translate HLL program to assembly code (hardest part) Assemble assembly code to machine code (object code) Link object code with runtime library Yields an executable Assembly code: VERY close to machine code Human readable encoding of machine code Each instruction corresponds to a machine instruction Not readable by machine (needs to be assembled)

Motivation for Assembly Code Important: Understand how computers execute code Understand what optimizations the compiler performs Reverse engineer a piece of software Improve efficiency of a program Write programs that have direct control over system hardware Example: writing a device driver to a new scanner If you are developing system software, you will need to know assembly language

Instruction Set The set of instructions (and their encodings) that a processor can execute We will use the IA32 (aka i386/i486/pentium) instruction set Used by majority of computers Instruction set for Intel s most commercially successful microprocessors Easily accessible Large # of instructions and modes Gives a good view of how computers work

Program Encoding Recall the compilation process Editor hello.cpp Compiler hello.asm Assembler hello.obj Linker Source program (text) Assembly program (text) Relocatable object programs (binary) Executable Hello.exe program (binary) hello.asm: assembly (human readable, not executable) hello.obj: machine code (binary, executable) Assembly and machine code are different encodings of the same instructions

Machine-Level (ML) Code Both assembly and machine code are ML code How does ML differ from High Level (HL)? HL view: Objects of different types are declared and allocated memory Memory is accessed by accessing the objects Local variables used to store temporary values CPU state is hidden from programmer Object type defines what operations can be performed Single statement can perform a complex task e.g. if (a+b < c) somefunc(d);

Machine-Level (ML) Code ML view: CPU state is visible and accessible to the program All CPU state is stored in its register file, i.e. the memory on the CPU CPU has: Registers ALU Control Unit Execution Unit etc

x86 and IA-32 Instruction Set Architectures

Source (UofW electronic resource): Chapter 3 in S. Dandamudi, Introduction to Assembly Language Programming for Pentium and RISC Processors, Springer, 2005. History Intel first introduced microprocessors in 1969 Work on early processors led to the development of the Intel Architecture (IA) First processor in the IA family was the 8086 (1979) 20-bit address bus and 16-bit data bus First 32-bit IA processor was the 80386 (1985) 32-bit address bus and 32-bit data bus Pentium introduced in 1993 Not named 80586 because numbers can t be trademarked 32-bit address bus and 64-bit data bus

History Source (UofW electronic resource): Chapter 3 in S. Dandamudi, Introduction to Assembly Language Programming for Pentium and RISC Processors, Springer, 2005.

8086 / 8088 Architecture 8086/8088 is the first CPU in the X86-series (1979) 8088 is identical to 8086 except it has a 8-bit data bus instead of a 16-bit data bus (more economical) Later processors in this series have similar but extended architectures Need to start at the beginning to understand a processor like Pentium-IV

8086 / 8088 Architecture Two main units Bus interface unit (BIU) Execution unit (EU) 8086 Data bus size: 16 bit Instruction queue: 6 bytes 20 bit address bus

General Purpose Registers Used for arithmetic, logical, and other operations Can be configured as: Four 16-bit registers Eight 8-bit registers

Index and Pointer Registers BP, SI, DI, SP Can only be used as 16 bit registers SI, DI used for addressing, and string op. BP, SP are used for maintaining the stack

Segment Registers 16 bit registers labelled DS, CS, SS, ES Used to indicate the start of different segments in memory

Flag Register 16-bit register containing 9 1-bit flags Gives status of the last instruction and the processor

Flag Register Overflow flag: Set if a signed overflow occurs Direction flag: Used to indicate direction of processing for string manipulations (0 = forward, 1 = backward) Interrupt enable flag: Setting this enables interrupts Trace (trap) flag: Used for debugging. When set processor executes only 1 instruction, then interrupts to call debugger Sign flag: Set to the MSB of the result of an operation Zero flag: Set if the result is zero Auxiliary carry flag: Set if there was a carry from or borrow to bits 0-3 in the AL register Parity flag: Set if the number of1 s in result is even Carry flag: Set if there was a carry from or borrow to the MSB during last calculation Image source: http://www.electronics.dit.ie/staff/tscarff/8086_registers/8086_registers.html

Programming Tools

DOSBox Required for those of you that have 64-bit machines and want to work on assignments at home Available at: http://www.dosbox.com/

8086 Emulator for Mac DosBox Works on a mac Other options: http://i8086emu.sourceforge.net/ http://wiki.qemu.org/main_page Again, assignments that you hand in MUST COMPILE in DOSBOX. Solutions posted in the future will only compile for DOSBOX

Using DOSBOX Run DOSBOX Choose your working folder by mounting your virtual C:\ Example: mount c C:\Program Files\Assembly Directory in quotations is where you will be storing your source code Once the C:\ is mounted, type C: to switch to your directory in DOSBOX

DOS commands You will need to know simple DOS commands to navigate your folders in the command line dir /p display contents of current directory (folders and files), one page at a time cd FolderName change directory to the folder called FolderName in your current directory Or type full directory path in place of folder name (C:\Windows\etc...)

DOS commands cd.. Go up a level in the folder hierarchy Prog.exe run program Prog.exe (must be in your current directory) help displays a list of usable commands

Shortcuts Up arrow previous command Tab Scrolls through available files/directories that begin with whatever you have started typing Use to avoid wasting time typing long directory pathways

Creating an Assembly file Create a text file Paste any program template you have into the file Change file name extension to.asm You may have to turn on the option to show file extensions on your computer Google how to do this if necessary

Programming Tools Course website has link to websites and tools you will need (these will also be e-mailed) After you have created an.asm file, you ll need to compile and link your program before you can run it This is done within DOSBOX from your mounted C:\

Programming Tools Compile, link, and run program masm programname Include the file extension above Hit enter 4 times link programname DO NOT include the file extension above Hit enter 4 times Execute program To be demonstrated in class

Debugging Debug programname.exe Type? for command listing Commands that may be useful dump go Dumps a section of memory Jumps to a line of code in the program Is like running until a break point in a program Can be used to skip over interrupt routines

Debugging trace Allows you to step through the code one line at a time, allowing you to see the CPU state at any point unassemble Displays the disassembly starting from the next instruction to be executed Here is a guide that has examples which I found more useful than the help tool: http://kipirvine.com/asm/debug/debug_tutorial.pdf

Notepad++ Recommend notepad++ as assembly editor for this course Highlights keywords Good organization/formatting Does not require an installer Available at: http://notepad-plus-plus.org/

Addressing Modes

x86 Addressing Modes Specify the method for finding the memory address of an operand e.g. using information held in registers and/or constants contained within a machine instruction Specifies operands in an assembly language program and is completely architecture dependent

Example MOV AH, 08h h stands for hexadecimal MOV copies operand 2 into operand 1 Source (data value 08h) is copied into destination (register AH) Data 08h is provided in the instruction Called Immediate addressing mode

x86 Addressing Modes There are three basic types of operands: Immediate Constant integer (8, 16, or 32 bits) Constant value is stored within the instruction Register Name of a register is specified Register number is encoded within the instruction Memory Reference to a location in memory Memory address is encoded within the instruction, or Register holds the address of a memory location

x86 Addressing Modes The Intel 8086 has about 9 data addressing modes: Immediate, register, direct, register indirect, based, indexed, based indexed, string, and port addressing Definition names vary across books Register and immediate modes do not need to access the external bus (faster) The syntax of the operand field determines the addressing mode

Immediate Addressing Mode MOV CX, 10 Immediate data is coded directly in the instruction s machine code Note: CX contains 0xA because there is no h after 10 The data is put in the operand field The constant in the operand field may be of byte or word length Some assemblers need a # before the constant

Immediate Addressing Mode Cont. The constant may be in hex., dec., binary, or text Default format is decimal Examples MOV CX, 65 MOV CX, 41h MOV CX, 01000001b MOV CX, A Advantage: No memory reference Disadvantage: Size of number is restricted to size of address field

Direct Addressing Mode MOV DS:[1234h], AL Copies AL using offset 1234h The operand is stored in a MEMORY location DS specifies data segment register An offset address is coded directly in the instruction The offset combined with DS forms the address where the operand is located

Direct Addressing Mode Cont. Ordinarily, DS: is assumed unless explicitly overridden using a colon (e.g. ES:[001h]) Advantage: Single memory reference to access data Disadvantage: Limited address space

Direct Addressing Mode Cont. Examples: Assume DS=10000h, ES=20000h, FRED=4567h MOV AX, [20h]; load the contents at address 10020h and 10021h into AL and AH respectively MOV DS:[1234h], AL; copy AL to address 11234h MOV ES:[1234h], AL; copy AL to address 21234h MOV FRED, AL; copy AL to address 14567h

Register Addressing Mode Instruction gets its source data from a register Data can be either 8/16/32 bits in length Data resulting from the operation is stored in another register Advantages: Only a small address field is needed in instruction No memory references are required Disadvantage: address space is very limited

Register Addressing Mode Cont. Examples: MOV AX, BX; copy the 16 bit content of BX to AX MOV AL, BL; copy the 8-bit content of BL to AL MOV SI, DI; copy DI into SI MOV DS, AX; copy AX into DS The following register to register transfers are not permitted. MOV BL, BX; mixed sizes MOV CS, AX; CS cannot be the destination MOV ES, DS; Segment register to segment register forbidden

Register Indirect Addressing Mode MOV CX, [BX] Copies a word from memory with location specified by BX Uses a register instead of a constant to specify the 16-bit offset address of the operand Offset address can be in any of the following registers: BP, BX, DI, SI. The [ ] is needed to denote register indirect addressing mode The DS register is the default segment address register (except BP, which uses SS as the default register)

Register Indirect Addressing Mode In cases of ambiguity: assemblers need the presence of BYTE PTR or WORD PTR directives to indicate the size of the data addressed by the memory pointer e.g. MOV [DI], 10h is ambiguous since assembler does not know whether to save 10h to memory as a byte or a word Instead, use the following: MOV BYTE PTR [DI], 10h; save 10h to memory MOV WORD PTR [DI], 10h; save 0010h to memory

Register Indirect Addressing Mode Register indirect addressing is commonly used to access a table of data in memory Examples: Assume BX=0222h, DS=10000h, SS=20000h, BP=0111h MOV CX, [BX]; Copy a byte from address 10222h and 10223h to CX MOV [BP], DL; Copy a byte from register DL to address 20111h Remember: Indirect addressing using BP defaults to SS

Based Addressing Mode Operand is located at the address given by adding 8 or 16 bit displacement to either BX or BP and combing the result with a segment register Displacement must be specified in the operand field Interpreted as signed 2 s complement value Examples Assume DS=1000h, SS=2000h, BP=0222h, BX=0111h MOV AX, [BP-2]; Copy the content of 20220h and 20221h to AX MOV [BX+777h], AX; Copy AL to 10888h and AH to 10889h

Indexed Addressing Mode Similar to based addressing except the index registers (SI or DI) must be used instead Operand is located at the address given by adding 8 or 16 bit displacement to either SI or DI and combing the result with a segment register Examples Assume DS=10000h, SI=222h, DI=111h MOV [DI-1], BL; store the content of BL to 10110h MOV BX, [SI+1000h]; Load BL with the contents of 11222h and BH with the contents of 11223h

Indexed Addressing Mode Cont. Based and indexed addressing are aka REGISTER RELATIVE addressing Other syntax may be permitted to indicate the displacement e.g. the following examples are all equivalent And they all result in the same assembled binary code Examples Assume FRED is a constant defined in the assembly code MOV [DI+FRED], BL MOV [DI]+FRED, BL MOV FRED[DI], BL

Based-Index-Relative Addressing The base and index registers are added to give the segment offset of where the operand is located The base register (either BX or BP) is added to an index register(di or SI) as positive integers only Each register lies in the range 0 to 65535 By default, the segment address is derived from DS except the BP register, which is derived from SS A signed displacement may also be included to calculate the offset

Based-Index-Relative Addressing Examples Assume SS=10000h, SI=3333h, BP=2222h MOV AX, [SI+BP]; Load the content of 15555h and 15556h to AL and AH respectively MOV AX, [SI+BP+1111h]; Load the contents of 16666h and 16667h to AL and AH respectively

String Addressing Mode A string is a series of bytes or words in sequential memory locations String instructions do not use any of the previous address modes Strings may be up to 64KB in length String addressing modes uses SI, DI, DS, and ES registers String instructions assume SI points to first byte of string to be processed, and DI points to first byte of destination string The use of a register my be implicit in the instruction

String Addressing Mode Example Assume DS=10000h, ES=20000h, SI=10h, DI=20h MOVSB; Move string byte from 10010h to 20020h

Port Addressing Mode 8086 has separate input/output space Up to 65536 I/O ports are available The I/O ports may be addressed by a byte sized constant Limited to I/O ports in the range 0 to 255 IN AL, 40h; Put the content of I/O port 40h into AL OUT 80h, AL; Send the contents of AL to I/O port 80h The I/O ports may be addressed using a register Full range of 65536 ports are accessible IN AL, DX; Load AL with the byte from port address given by DX OUT DX, AX; Send the word in AX to port address given by DX

Summary of Addressing Modes Operand needed for an instruction may be located: Immediately in the operand field, e.g. MOV AX, 1234h In a register (register addressing), e.g. MOV DS, AX In memory at a an offset specified by one of the following (disp is a constant): The segment address is in DS (by default, except when BP is used) In memory locations given implicitly by string instructions At input/output ports specified by a register or a constant

Instruction Set

Assembly Language Statements Three types of statements Directives or Pseudo-ops Instructions Macros

Assembly Language Statements Three types of statements Directives or Pseudo-ops Direct the assembler during the assembly process Used for variable declaration and storage allocation Non-executable and do not generate any machine language e.g. segment, db, dw Instructions Macros

Assembly Language Statements Three types of statements Directives of Pseudo-ops Instructions Instruct the processor to perform a task Contains operation code (opcode) Cause the assembler to generate machine language e.g. mov, add Macros

Assembly Language Statements Three types of statements Directives of Pseudo-ops Instructions Macros Permit the assembly language programmer to name a group of statements and refer to the group by the macro name During the assembly process, each macro is replaced by the group of statements that it represents and assembled in place

Directives: Data Allocation [var-name] define-directive initial-value,[initial-value] The define directive takes one of the five basic forms: 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: var1 DB y var2 DB 79h var3 DB 11110010B

Directives: Data Allocation Another example: total DD 542803535 This would allocated four contiguous bytes of memory and initialize it to 542803535 (205A864Fh): Address: x x+1 x+2 x+3 Contents: 4F 86 5A 20

Directives: Data Allocation Or: total DB 20h,5Ah,86h,4Fh This would allocated four contiguous bytes of memory and initialize it to 205A864Fh: Address: x x+1 x+2 x+3 Contents: 20 5A 86 4F

Directives: Data Allocation Range of numeric operands DB: -2 7 to 2 8 1 DW: -2 15 to 2 16 1 DD: -2 31 to 2 32 1 or a short floating-point number (32 bits) DQ: -2 63 to 2 64 1or a long floating-point number Ranges depend on context (signed or unsigned)

Directives: Data Allocation Uninitialized Data and multiple initializations Use DUP command Number after DB indicates the number of bytes, and DUP(X) will fill the number of bytes with X Examples: text DB 10 DUP ( W ) ;initializes 10 bytes to W test DB 1 DUP(?) ;Reserve 1 uninitialized byte test2 DB 20 DUP(?) ;Reserve 20 uninitialized bytes

Directives: Data Allocation Multiple definitions: sort DB y ; ASCII of y = 79h value DW 25159 ; 25159 = 6247h total DD 542803535 ; 542803535 = 205A864Fh Memory allocation Address: x x+1 x+2 x+3 x+4 x+5 x+6 Contents: 79 47 62 4F 86 5A 20 More examples: message DB BYE,0Dh,0Ah OR message DB B DB Y DB E DB 0Dh DB 0Ah

Directives: Defining Constants name EQU expression Assigns the result of the expression to name. Example: NUM_OF_STUDENTS EQU 90 MOV AX, NUM_OF_STUDENTS

8086 Instructions Instructions can be of various types Data moving instructions Arithmetic add, subtract, increment, decrement, etc. Control transfer conditional, unconditional, call subroutine Logic AND, OR, XOR, shift/rotate and test String manipulation load, store, move, compare Input/Output instructions Other setting/clearing flag bits, stack operations, etc.

MOV mov destination, source mov register, register mov register, immediate mov memory, immediate mov register, memory mov memory, register Memory to memory forbidden. Examples mov [response], bh mov dx, [table1] mov [name1+4], k

Data transfer instructions lea register, memory_address Examples lea DX, mystring

Data Transfer Instructions XCHG exchanges 8, or 16-bit source and destination operands Examples xchg ax, dx xchg [response], cl xchg [total], dx As in the mov instruction, both operands cannot be located in memory. Note that this restriction is applicable to most instructions

Data Transfer Instructions XLATB instruction can be used to perform character translation To use this instruction, the BX register must be loaded with the starting address of the translation table and AL must contain an index value into the table The xlatb instruction adds contents of AL to BX and reads the byte at the resulting address. This byte replaces the index value in the AL register. Since the 8-bit AL register provides the index into the translation table, the number of entries in the table is limited to 256.

Arithmetic Instructions INC and DEC used to either increment or decrement the operands by one inc destination dec destination Examples inc bx ; increment bx register by one dec dl ; decrement 8-bit register

Arithmetic Instructions ADD used to add two 8 or 16-bit operands As with the mov instruction, add can also take the five basic forms depending on how the two operands are specified. The semantics of the add instruction are add destination, source => destination = destination + source Examples:

Arithmetic Instructions Examples:

Arithmetic Instructions Examples:

Arithmetic Instructions Examples:

Arithmetic Instructions Examples:

Arithmetic Instructions Examples:

Arithmetic Instructions SUB used to subtract two 8, 16-bit operands sub destination, source => destination = destination - source Examples:

Arithmetic Instructions CMP used to compare two 8, 16, or 32-bit numbers cmp operand1, operand2 => operand1-operand2 The cmp instruction performs the same operation as the sub instruction except that the result of subtraction is not saved Thus, cmp does not disturb the source and destination operands After the subtraction operation, flags are set accordingly

Arithmetic Instructions MUL used to perform unsigned multiplication of a number with AL register mul source The source operand can be in a general-purpose register or in memory. Immediate operand specification is not allowed Example: mov al, 10 mov dl, 25 mul dl

Arithmetic Instructions MUL is used to perform unsigned multiplication of a number with AL register Example: mov al, 2 mov bl, 80h ; 128 mul bl ;Ax = 0100h (256)

Arithmetic Instructions IMUL used to perform signed multiplication of a number with AL register imul source The behaviour of the imul instruction is similar to that of mul The only difference to note is that the carry and overflow flags are set if the upper half of the result is not the sign extension of the lower half mov al, 2 mov bl, f6h ; -10 imul bl ;ax = FFECh (-20) Note, here the carry and overflow flags are cleared, because AH contains the sign extension of the AL value

Arithmetic Instructions DIV, IDIV used to perform division of AX register div source (unsigned) idiv source (signed) Division generates two result components: a quotient and a remainder In multiplication, by using double-length registers, overflow never occurs. In division, divide overflow is a real possibility. The processor generates a special software interrupt when a divide overflow occurs

Arithmetic Instructions Example mov ax, 251 mov cl, 12 div cl Leaves 20 (14h) in the al register and 11 (0bh) in the ah register Therefore, ax contains 0b14h after division

Arithmetic Instructions Extension to larger operands: Both the multiplication and division instructions can be performed using 32-bit and 16-bit operands Or, 64-bit and 32-bit operands on 32-bit system Example:

Arithmetic Instructions Need for sign extension Example: Perform signed division 8000h/400h Here the divisor is larger than a byte, so dividend must be stored in DX AX However, 8000h fits entirely within AX So, AX needs to be sign extended to DX in order to maintain sign for division To aid sign extension instructions such as idiv, several instructions are provided: cbw cwd cdq ; Convert byte to word ; Convert word to doubleword ; Convert doubleword to quadword

Lab 04 Practice integer addition/subtraction/multiplication/division Know how to do this in signed (two s complement) form as well as unsigned Practice converting back and forth between fractional decimal and fractional binary numbers