Irvine, Kip R. Assembly Language for Intel-Based Computers. Chapter 7: Integer Arithmetic Slides to Accompany Assembly Language for Intel-Based Computers,

Slides:



Advertisements
Similar presentations
NEG Instruction Change operand content into two’s complement (negative value) and stored back into its operand mov bl, b neg bl; bl = mov.
Advertisements

Department of Computer Science and Software Engineering
ACOE2511 Assembly Language Arithmetic and Logic Instructions.
Computer Organization & Assembly Language
80x86 Instruction Set Dr. Qiang Lin.
Web siteWeb site ExamplesExamples Irvine, Kip R. Assembly Language for Intel-Based Computers, MUL Instruction The MUL (unsigned multiply) instruction.
1 Lecture 7 Integer Arithmetic Assembly Language for Intel-Based Computers, 4th edition Kip R. Irvine.
Gursharan Singh Tatla 21-Nov-20101www.eazynotes.com.
8086 : INSTRUCTION SET By, Pramod Sunagar Assistant Professor
Assembly Language for Intel-Based Computers Chapter 15: BIOS-Level Programming (c) Pearson Education, All rights reserved. You may modify and.
Assembly Language for Intel-Based Computers, 4 th Edition Chapter 7: Integer Arithmetic (c) Pearson Education, All rights reserved. You may modify.
Assembly Language for Intel-Based Computers Chapter 5: Procedures Kip R. Irvine.
Assembly Language for Intel-Based Computers, 5 th Edition Chapter 7: Integer Arithmetic (c) Pearson Education, All rights reserved. You may.
Kip Irvine: Assembly Language for Intel-Based Computers
Irvine, Kip R. Assembly Language For Intel-Based Computers.data string db "This is a string." COUNT = ($–string) ; calculate string length.code mov cx,COUNT.
Assembly Language for Intel-Based Computers, 5 th Edition Chapter 7: Integer Arithmetic (c) Pearson Education, All rights reserved. You may.
Shift and Rotate Instructions
Assembly Language for Intel-Based Computers, 4 th Edition Chapter 7: Integer Arithmetic (c) Pearson Education, All rights reserved. You may modify.
Irvine, Kip R. Assembly Language For Intel-Based Computers XADD Instruction.code mov ax,1000h mov bx,2000h ; AX = 1000h, BX = 2000h xadd ax,bx ; AX = 3000h,
Microcomputer & Interfacing Lecture 3
Chapter 4 Basic Instructions. 4.1 Copying Data mov Instructions mov (“move”) instructions are really copy instructions, like simple assignment statements.
Assembly Language for Intel-Based Computers, 4 th Edition Chapter 7: Integer Arithmetic (c) Pearson Education, All rights reserved. You may modify.
Assembly Language for Intel-Based Computers
Integer Arithmetic Computer Organization & Assembly Language Programming Dr Adnan Gutub aagutub ‘at’ uqu.edu.sa [Adapted from slides of Dr. Kip Irvine:
Integer Arithmetic COE 205 Computer Organization and Assembly Language
Assembly Language – Lab 5
Sahar Mosleh California State University San MarcosPage 1 Applications of Shift and Rotate Instructions.
Multiplication and Division Instructions & the 0Ah function.
Arithmetic Flags and Instructions
Computer Architecture and Operating Systems CS 3230 :Assembly Section Lecture 4 Department of Computer Science and Software Engineering University of Wisconsin-Platteville.
Integer Arithmetic Computer Organization and Assembly Languages Yung-Yu Chuang 2007/12/24 with slides by Kip Irvine.
Assembly Language for Intel-Based Computers Chapter 7: Integer Arithmetic (c) Pearson Education, All rights reserved. You may modify and copy.
CT215: Assembly Language Programming
1 Logic, Shift, and Rotate Instructions Read Sections 6.2, 7.2 and 7.3 of textbook.
Assembly Language for Intel-Based Computers, 4 th Edition Unpacked and Packed Integers (c) Pearson Education, All rights reserved. You may modify.
Assembly Language for x86 Processors 6th Edition Chapter 7: Integer Arithmetic (c) Pearson Education, All rights reserved. You may modify and copy.
Chapter 7: Integer Arithmetic. 2 Chapter Overview Shift and Rotate Instructions Shift and Rotate Applications Multiplication and Division Instructions.
Lecture 12 Integer Arithmetic Assembly Language for Intel-Based Computers, 4th edition Kip R. Irvine.
Assembly Language for Intel-Based Computers, 5 th Edition Chapter 7: Integer Arithmetic (c) Pearson Education, All rights reserved. You may.
Assembly Language for x86 Processors 7th Edition
ICS 312 SET 10 Multiplication & Division & input using function 0Ah.
Chapter 7 Bit Manipulation. 7.1 Logical Operations.
Microprocessor & Assembly Language Arithmetic and logical Instructions.
Computer and Information Sciences College / Computer Science Department CS 206 D Computer Organization and Assembly Language.
Multiplication and Division instructions Dr.Hadi AL Saadi.
Computer Architecture and Assembly Language
Data Transfers, Addressing, and Arithmetic
Assembly Language for Intel-Based Computers, 5th Edition
Assembly Language for x86 Processors 7th Edition
Multiplication and Division Instructions
INSTRUCTION SET OF 8086 PAWAN KUMAR SINGH.
CS 301 Fall 2002 Assembly Instructions
X86’s instruction sets.
Chapter 4: Instructions
Computer Organization and Assembly Languages Yung-Yu Chuang 2005/11/17
Shift & Rotate Instructions)
Multiplication and Division Instructions
ADDITION Register Addition. ADD AX,BX AX=AX+BX 2. Immediate Addition.
Assembly Language for Intel-Based Computers, 4th Edition
Assembly Language for Intel-Based Computers, 4th Edition
Assembly Language for Intel-Based Computers, 5th Edition
Shift, Multiply, and Divide
Computer Architecture and System Programming Laboratory
X86 Assembly Review.
Chapter 5 Arithmetic and Logic Instructions
Multiplication and Division Instructions
Multiplication and Division Instructions
Division instruction.
Shift and Rotate Instructions.
Computer Architecture and System Programming Laboratory
Presentation transcript:

Irvine, Kip R. Assembly Language for Intel-Based Computers. Chapter 7: Integer Arithmetic Slides to Accompany Assembly Language for Intel-Based Computers, Third Edition Updated 10/26/01

Irvine, Kip R. Assembly Language for Intel-Based Computers. SHL instruction SHR instruction SHL and SHR

Irvine, Kip R. Assembly Language for Intel-Based Computers. SAR instruction SAL instruction SAR and SAL

Irvine, Kip R. Assembly Language for Intel-Based Computers. ROR instruction ROL instruction ROL and ROR

Irvine, Kip R. Assembly Language for Intel-Based Computers. RCR instruction RCL instruction RCL and RCR

Irvine, Kip R. Assembly Language for Intel-Based Computers..286.data byteval db 0Fh wordval dw 1234h.code byte_values: mov al,26h rol al,4 ; AL = 62h rol byteval,4 ; byteval = F0h word_values: mov ax,0203h rol ax,8 ; AX = 0302h rol wordval,8 ; wordval = 3412h Using ROL to Exchange Register Halves

Irvine, Kip R. Assembly Language for Intel-Based Computers. mov al,2 ; AL = Shl al,1 ; AL = Shl al,1 ; AL = Shl al,1 ; AL = Shl al,1 ; AL = Using SHL to multiply mov al,32 ; AL = Shr al,1 ; AL = Shr al,1 ; AL = Shr al,1 ; AL = Shr al,1 ; AL = Using SHR to divide Fast Multiplication & Division

Irvine, Kip R. Assembly Language for Intel-Based Computers..data byte1 db 3Bh ; after: 03h byte2 db 46h ; after: B4h byte3 db 0FFh ; after: 6Fh.code mov cx,4 ; repeat the shift four times L1: shr byte1,1 ; highest byte rcr byte2,1 ; middle byte, include Carry flag rcr byte3,1 ; low byte, include Carry flag loop L1 Shifting Multiple Bytes with RCR

Irvine, Kip R. Assembly Language for Intel-Based Computers..data prompt db "Enter a decimal integer: ",0.code extrn Clrscr:proc, Crlf:proc, Readint:proc, \ Writestring:proc main proc mov mov ds,ax ; Prompt for an integer: call Clrscr mov dx,offset prompt call Writestring call Readint ;read integer into AX call Crlf mov cx,16 ; number of bits in AX Binary Number Display Program

Irvine, Kip R. Assembly Language for Intel-Based Computers. L1: shl ax,1 ; shift AX left into Carry flag mov dl,'0' ; choose '0' as default digit jnc L2 ; if no carry, then jump to L2 mov dl,'1' ; else move '1' to DL L2: push ax ; save AX mov ah,2 ; display DL int 21h pop ax ; restore AX loop L1 ; shift another bit to left mov ax,4C00h ; exit program int 21h main endp end main Binary Number Display Program

Irvine, Kip R. Assembly Language for Intel-Based Computers. Date Example: DOS Date Format

Irvine, Kip R. Assembly Language for Intel-Based Computers. mov ax,dx ; make copy of date shr dx,5 ; move month into low position and dl, b ; clear high 4 bits mov month,dl ; save in variable Isolating the Month

Irvine, Kip R. Assembly Language for Intel-Based Computers. ; January 1, 1980: date_record RECORD year:7=0, month:4=1, day:5=1.data birthDate date_record <>.code mov ax, width year ; width = 7 aov bx, width month ; width = 4 mov ax, width day ; width = 5 Using the WIDTH Operator

Irvine, Kip R. Assembly Language for Intel-Based Computers. ADC dest,source ; add (source + CF) to destination SBB dest,source ; subtract (source + CF) from destination STC; set carry flag CLC; clear carry flag ADC and SBB Instructions

Irvine, Kip R. Assembly Language for Intel-Based Computers. title QuadWord Addition (QWADD.ASM) ; This program adds two quadword operands..model small.stack 100h.386; enable 32-bit registers.data op1 dq 0A2B2A406B7C62938h op2 dq A64938D2h result dd 3 dup(?).code main proc mov mov ds,ax mov si,offset op1 mov di,offset op2 mov bx,offset result mov cx,2 ; number of dwords call Multi32_Add mov ax,4C00h int 21h main endp Adding Two 64-bit Quadwords

Irvine, Kip R. Assembly Language for Intel-Based Computers. ; Multi32_Add ; Add two integers, consisting of multiple doublewords. Input ; parameters: SI and DI point to the two operands, BX points to ; the destination operand and CX contains the number of ; doublewords to be added..code Multi32_Add proc pusha clc ; clear the Carry flag L1: mov eax,[si] ; get the first operand adc eax,[di] ; add the second operand pushf ; save the Carry flag mov [bx],eax ; store the result add si,4 ; advance all 3 pointers add di,4 add bx,4 popf ; restore the Carry flag loop L1 ; repeat loop Adding Two 64-bit Quadwords

Irvine, Kip R. Assembly Language for Intel-Based Computers. ; store the high-order doubleword of the sum mov dword ptr [bx],0 adc dword ptr [bx],0 ; add leftover carry popa ret Multi32_Add endp end main Adding Two 64-bit Quadwords

Irvine, Kip R. Assembly Language for Intel-Based Computers..data op1 dq A1h op2 dq A2630B2h result dq 0 ; result = 1A EE 1F D3 EB FA 16 EF.code mov cx,8 ; loop counter: 8 bytes mov si,0 ; set index to 0 clc ; clear Carry flag L1: mov al,byte ptr op1[si] sbb al,byte ptr op2[si] mov byte ptr result[si],al inc si loop L1 Subtracting QuadWord Values

Irvine, Kip R. Assembly Language for Intel-Based Computers. Multiplication AL, AX, EAX are implied destination operands Source operand can be a register or variable 16-bit output is AX 32-bit output is DX:AX 64-bit output is EDX:EAX

Irvine, Kip R. Assembly Language for Intel-Based Computers. MUL Instruction 8-bit multiplication: MUL BL; product = AX 16-bit multiplication: MUL DX; product = DX:AX 32-bit multiplication: MUL ECX; product = EDX:EAX

Irvine, Kip R. Assembly Language for Intel-Based Computers. MUL (Flags) Carry and Overflow flags are set when the product extends into its high register: mov ax,5000h mov bx,10h mul bx; DX:AX = h ; CF=1, OF=1 mov ax,500h mov bx,10h mul bx; DX:AX = h ; CF=0, OF=0

Irvine, Kip R. Assembly Language for Intel-Based Computers. MUL Examples mov al,5 mov bl,10h mul bl; AX = 0050h mov eax, h mov ebx,10000h mul ebx; EDX:EAX = h mov ax,500h mov bx,100h mul bx; DX:AX = h

Irvine, Kip R. Assembly Language for Intel-Based Computers. MUL Examples (2).data productQ DQ ?.code mov eax, h mov ebx,10000h mul ebx; EDX:EAX = h mov dword ptr productQ, eax mov dword ptr productQ+4, edx You can save the 64-bit product in a Quadword variable by storing each doubleword separately:

Irvine, Kip R. Assembly Language for Intel-Based Computers. IMUL Instruction Use with signed operands Sign-extends the result into the high register CF=1 and OF=1 if the sign of the high register is different from the sign of the low register

Irvine, Kip R. Assembly Language for Intel-Based Computers. IMUL Examples mov al,-4 mov bl,4 imul bl; AX = FFF0h (-16), CF=0, OF = 0 mov al,48 mov bl,4 imul bl; AX = 00C0h (+192), CF=1, OF = 1

Irvine, Kip R. Assembly Language for Intel-Based Computers. IMUL Examples (2) mov ax,-128 mov bx,4 imul bx; DX:AX = FFFFh,FFE0h (-512) ; CF=0, OF = 0

Irvine, Kip R. Assembly Language for Intel-Based Computers. DIV Instruction Dividiend is AX, DX:AX, or EDX:EAX Divisor can be 8-bit, 16-bit, 32-bit register or variable Quotient is AL, AX, or EAX Remainder is AH, DX, or EDX Status flag values are undefined

Irvine, Kip R. Assembly Language for Intel-Based Computers. DIV Examples mov ax,0083h; dividend mov bl,2; divisor div bl; AL = 41h, AH = 01h mov dx,0; dividend, high mov ax,8003h; dividend, low mov cx,100h; divisor div cx; AX = 0080h, DX = 0003h

Irvine, Kip R. Assembly Language for Intel-Based Computers. Divide Overflow Happens when the quotient is too large to fit in the destination register. Causes a processor interrupt. mov dx,0050h; dividend, high mov ax,0000h; dividend, low mov cx,10h; divisor div cx; quotient= 50000h, cannot ; fit in AX register

Irvine, Kip R. Assembly Language for Intel-Based Computers. IDIV Instruction Use for signed division Dividend must be sign-extended before executing the IDIV instruction: CBW extends AL into AH CWD extends AX into DX CDQ extends EAX into EDX Status flag values are undefined

Irvine, Kip R. Assembly Language for Intel-Based Computers. IDIV Examples mov ax,-257; dividend mov bl,2; divisor idiv bl; AL = -128, AH = -1 mov ax,-5000; dividend cwd; extend into DX mov bx,256; divisor idiv bx; AX = -19, DX = -136

Irvine, Kip R. Assembly Language for Intel-Based Computers. IDIV Examples (2) mov eax, ; low part of dividend cdq; extend into EDX mov ebx,100; divisor idiv ebx; EAX = -5000, EDX = -2

Irvine, Kip R. Assembly Language for Intel-Based Computers. ABC screen Video Display Memory Mapping

Irvine, Kip R. Assembly Language for Intel-Based Computers. ; Set_videoseg ; ; Set the current video segment address (the ; default is B800h). Input parameter: AX ; contains the address value. ; Set_videoseg proc mov videoSegment,ax ret Set_videoseg endp Setting the Video Segment Address

Irvine, Kip R. Assembly Language for Intel-Based Computers. ;Writechar_direct ; ; Write a character directly to VRAM. Input ; parameters: AL = char, AH = attribute, ; DH/DL = row, column on screen (0-24, 0-79). ; Writechar_direct proc push ax push dx push di push es mov di,videoSegment mov es,di Writechar_direct Procedure

Irvine, Kip R. Assembly Language for Intel-Based Computers. ; multiply the row by 160 push ax mov ax,160 mul dh mov di,ax ; multiply the column by 2, add to DI shl dl,1 mov dh,0 add di,dx pop ax mov es:[di],ax ; store char/attribute pop es pop di pop dx pop ax ret Writechar_direct endp Writechar_direct Procedure

Irvine, Kip R. Assembly Language for Intel-Based Computers. ; Writestring_direct ; ; Write a string directly to video RAM. Input ; parameters: DS:SI points to a null-terminated ; string, AH = attribute, DH/DL = row/column on ; the screen. ; Writestring_direct proc push ax push dx push si Writechar_direct Procedure

Irvine, Kip R. Assembly Language for Intel-Based Computers. L1: mov al,[si] ; get character cmp al,0 ; check for null byte je L2 ; quit if found call Writechar_direct inc si ; next character inc dl ; next column jmp L1 L2: pop si pop dx pop ax ret Writestring_direct endp Writechar_direct Procedure

Irvine, Kip R. Assembly Language for Intel-Based Computers. ASCII Decimal Formats

Irvine, Kip R. Assembly Language for Intel-Based Computers. The End