Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Irvine, Kip R. Assembly Language for Intel-Based Computers. Chapter 7: Integer Arithmetic Slides to Accompany Assembly Language for Intel-Based Computers,"— Presentation transcript:

1 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

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

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

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

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

6 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

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

8 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

9 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 ax,@data 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

10 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

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

12 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,00001111b ; clear high 4 bits mov month,dl ; save in variable Isolating the Month

13 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

14 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

15 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 080108700A64938D2h result dd 3 dup(?).code main proc mov ax,@data 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

16 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

17 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

18 Irvine, Kip R. Assembly Language for Intel-Based Computers..data op1 dq 20403004362047A1h op2 dq 055210304A2630B2h 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

19 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

20 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

21 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 = 00050000h ; CF=1, OF=1 mov ax,500h mov bx,10h mul bx; DX:AX = 00005000h ; CF=0, OF=0

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

23 Irvine, Kip R. Assembly Language for Intel-Based Computers. MUL Examples (2).data productQ DQ ?.code mov eax,12345678h mov ebx,10000h mul ebx; EDX:EAX = 0000123456780000h 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:

24 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

25 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

26 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

27 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

28 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

29 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

30 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

31 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

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

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

34 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

35 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

36 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

37 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

38 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

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

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


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

Similar presentations


Ads by Google