Ch 3 Objectives CMPE328 Microprocessors (Spring 2007-08) ALU Instructions and Programs By Dr. Mehmet Bodur The 8-bit and 16-bit, signed and unsigned addition, and subtraction operations. Conversion between ASCII, packed BCD, unpacked BCD and binary number formats Effect of arithmetic instruction on the flag register Coding of Arithmetic Instructions NEG, ADD, ADC, SUB, SBB, MUL and DIV, and CMP; decimal arithmetic corrections DAA, DAS; ASCII arithmetic corrections AAA, AAS, AAM, and AAD. Coding of Logic Instructions AND, OR, XOR; shift and rotates SHR, SHL, ROR, ROL, RCR, RCL. Bitwise manipulations using C language. CMPE328 Spring 2007-08 Dr.Mehmet Bodur, EMU-CMPE Section 3.1 Section 3.1 Signed and Unsigned Numbers Sign Extension 8-bit signed number can describe numbers from –128 to + 127. (80 81 … FF 00 01 … 7F) 16-bit signed number can describe from –32768 to +32767. (8000 … FFFF 0000 0001 … 7FFF ) Signed addition may yield overflow error CMPE328 Spring 2007-08 8-bit unsigned number can describe numbers from 0 to + 255. (00 …. FF) 16-bit unsigned number can describe from 0 to +65535 (0000 0001 … FFFF) Unsigned addition may yield carry flag as an extra bit. Dr.Mehmet Bodur, EMU-CMPE 2 3 CBW (8086/80386 instruction) Extends the sign of AL over AX. Example: AL= 24h (=36) ; after CBW, AX= 0024h (=36) AL= B0h (= – 80); after CBW, AX=FFB0h (= – 80) CWD (8086/80386 instruction) Extends the sign of AX over DX. AX=9208h, after CWD DX=FFFFh, AX=9208h MOVSX dst, src (80386 instruction) Moves from source to destination with sign extension. dst is 16-bit, src is 8-bit. (for signed extension) MOVZX dst, src (80386 instruction) Moves from source to destination with zero extension. dst is 16-bit, src is 8-bit. (for unsigned extension) CMPE328 Spring 2007-08 Dr.Mehmet Bodur, EMU-CMPE 4 Section 3.1 Section 3.1 ADD Instruction add d8, s8 s8 : al,bl,cl,dl,ah,bh,ch,dh or constant d8 : al,bl,cl,dl,ah,bh,ch,dh add d16, s16 s16 : ax,bx,cx,dx,si,di,sp,bp or constant d16 : ax,bx,cx,dx,si,di,sp,bp, cs,ds,es,ss (cs,ds,es,ss cannot be used with a constant s16) add d, [const] ; dest ←dest+ mem[DS:const] add d, [r] ; dest ←dest + mem[DS:r] r : bx,di,si ; d : d8 or d16 Restrictions: d cannot be a memory location. Flags affected are: CSPAZO; Operands can be both signed or unsigned. Dr.Mehmet Bodur, EMU-CMPE CMPE328 Spring 2007-08 Adding Multiple Words 5 LSB words added by ADD, all other words added by ADC, to propagate the carry. CMPE328 Spring 2007-08 .MODEL SMALL .STACK 64 .DATA num1 dd 12345678h num2 dd 13579BDFh SUM dd ? .CODE MAIN PROC FAR ;program entry point MOV AX,@DATA ;data segment MOV DS,AX ; goes into DS MOV AH,4CH ;Exit to DOS INT 21H MAIN ENDP END MAIN Dr.Mehmet Bodur, EMU-CMPE TITLE PROG3-2 (EXE) MULTIWORD ADDITION .MODEL SMALL .STACK 64 .DATA DATA1 DQ 548B9963CE7H ORG 0010H DATA2 DQ 3FCD4FA23B8DH ORG 0020H DATA3 DQ ? MAIN BACK: each word is 2 bytes long. MAIN CMPE328 Spring 2007-08 .CODE PROC MOV MOV CLC MOV MOV MOV MOV SI,OFFSET DATA1 DI,OFFSET DATA2 bx,OFFSET DATA3 CX,04 ;SI is pointer for operand1 ;DI is pointer for operand2 ;BX is pointer for the sum MOV ADC MOV INC INC INC INC INC INC AX,[SI] AX,[DI] [BX],AX SI SI DI DI BX BX ;move the first operand to AX ;add the second operand to Ax ;store the sum ;point to next word of operad1 LOOP BACK ; CONTUNUE ADDUNG for 4-words MOV INT ENDP END AH,4CH 21H ;go back to DOS FAR AX,@DATA DS,AX 6 Section 3.1 Multiple Word Addition – 2 write a program that adds two quadword numbers and saves the result: ds:0004 78 56 34 12 DF 9B 57 13 MOV AX,word ptr num1 ;first number LSW ADD AX,word ptr num2 ; add second number LSW MOV word ptr SUM,AX ; save LSW to SUM MOV AX,word ptr num1+2 ;first number MSW ADC AX,word ptr num2+2 ; add second number MSW with carry MOV word ptr SUM+2,AX ; save MSW to SUM+2 Section 3.1 Example ds:0000 Multi Word Addition Exercise Write a program to find the sum of signed numbers -37, 126, 112, 15, 35, 41, 90. Write a program to find the sum of unsigned words 27354, 28521, 29553, 41280, 60606. Result may be multipleword. Clear Carry for the LSW addition ;point to next word of operad2 ;point to next word of sum MAIN Dr.Mehmet Bodur, EMU-CMPE 7 CMPE328 Spring 2007-08 Dr.Mehmet Bodur, EMU-CMPE 8 Section 3.1 Section 3.1 Example – 1 8086 SUB Instruction Write a program to find the sum of signed bytes -37, 126, 112, 15, 35, 41, 90. .data count equ 7 snum db -37,126,112 15,35,41,90 sum dw ? .code mov ax,@data mov ds,ax mov cx,count mov bx,0 clc mov si,offset snum add_next: mov al,[si] cbw adc bx,ax inc si dec cx jnz add_next mov sum,bx CMPE328 Spring 2007-08 destination and source are similar with the add instruction. Flags affected are: C S P A Z O; Operands can be both signed or unsigned. CF=1 means that a borrow taken from the next digit. ; alternate coding is .data count equ 7 snum db -37,126,112 15,35,41,90 sum dw ? .code mov ax,@data mov ds,ax mov cx,count mov bx,0 clc mov si,offset snum add_next: movsx bx,[si] adc ax,bx inc si loop add_next mov sum,bx Dr.Mehmet Bodur, EMU-CMPE sub dest, source subtraction requires borrow propagation. Carry flag is employed for Borrow. Multi-word subtraction requires subtraction with borrow. sbb dest, source 9 after execution dest = dest – source – CF if CF=1 then dest = dest – source –1 ; CMPE328 Spring 2007-08 Dr.Mehmet Bodur, EMU-CMPE 10 Section 3.1 Section 3.2 Multi-Word Subtraction Example: Double-Word subtraction: data_a data_b result . mov sub mov mov sbb mov . . . dd 62562FAh dd 412963Bh dd ? . . ax, word ptr data_a ax, word ptr data_b word ptr result, ax ax, word ptr data_a+2 ax, word ptr data_b+2 word ptr result+2, ax 62FA h – 963B h = CCBF h with CF=1 ; ax Å LSW of data_a ; subtract LSW of data_b ; ax Æ LSW of result. ; ax Å MSW of data_a ; subtract with borrow ; ax Æ MSW of result. 625 h – 412 h – 1 = 212 h CMPE328 Spring 2007-08 Dr.Mehmet Bodur, EMU-CMPE Unsigned Multiplication 11 Multiplication generates longer result than operands: mul op Multiplies a byte by a byte, or a word by a word. Multiplication Oper.1 Oper.2. Byte x byte AL reg. or mem AX Result Word x word AX reg. or mem DX,AX In word x word case, double-word result goes to dx, ax. dx returns MSW, and ax returns LSW of the result. Example: multiply 50 by 20 Example: multiply 300 by 5 300>255 Î word x word required both operands are byte. mov al, 50 mov ax, 300 mov bx, 5 mov bl, 20 mul bl ; result is in ax mul bx ; result is in dx.ax CMPE328 Spring 2007-08 Dr.Mehmet Bodur, EMU-CMPE 12 Section 3.2 Section 3.2 Unsigned Division Division generates a quotient and a reminder: Unsigned Division Example Example for dw/w. ; from the data segment data1 dd 105432 data2 dw 10000 quotnt dw ? remain dw ? ; from mov mov div mov mov Example: 325 /6 = 54 + 1/6 Æ Quotient=54, Reminder=1 div denum Divides a word by a byte, or a double-word by a word. div gives divide error interrupt when quotient exceeds AL (for word/byte) or AX (for double-word/word case). division numer. denum. word / byte AX Quotient Reminder reg. or mem AL AH double word / word DX AX reg. or mem AX DX CMPE328 Spring 2007-08 Dr.Mehmet Bodur, EMU-CMPE 13 the code segment: ax, word ptr data1 dx, word ptr data1+2 data2 quotnt, ax remain, dx Dr.Mehmet Bodur, EMU-CMPE CMPE328 Spring 2007-08 14 Section 3.3 Section 3.3 Logic Instructions AND, OR, XOR instructions clear CF and OF flags, and change PF, ZF, SF according to the ALU result. AND dst,src Bitwise “or” operations. “1” sets, “0” selects the related bit of the other. Bitwise “exclusive-or” operations. “1” toggles, “0” does not toggle the related bit of the other operand. CMPE328 Spring 2007-08 Dr.Mehmet Bodur, EMU-CMPE 15 op 0 0 op CF SHL op,1 and, SHR op,1 instructions shifts op one bit right or left., and change C, P, Z, S, O according to the ALU result. SHL op,cl and, SHR op,cl make repetitive shifts as many as the contents of cl. XOR dst, src CF Bitwise “and” operations. “1” selects, “0” clears the related bit of the other operand (it is called masking the unwanted bits). OR dst,src Shift Instructions Example: SHR AL,3 ; is not allowed. Example MOV cl,3 SHR al, cl ; shifts al 3 bits right. ; Two least significant bits become lost, ; third bit becomes shifted into CF. CMPE328 Spring 2007-08 Dr.Mehmet Bodur, EMU-CMPE 16 Section 3.3 Compare Instruction Upper Case Conversion – 1 -0 -1 -2 -3 -4 -5 -6 -7 -8 -9 -A -B -C -D -E -F CMP op1, op2 234567- Compares signed or unsigned numbers. It changes flags C,S,Z,O,A,P according to the result of op1 – op2. But it does not change the register contents. For unsigned numbers use JE, JA, JB, etc. For signed numbers use JE, JL, JG, etc. CMPE328 Spring 2007-08 Dr.Mehmet Bodur, EMU-CMPE 17 0 @ P ` p ! 1 A Q a q " 2 B R b r # 3 C S c s $ 4 D T d t % 5 E U e u & 6 F V f v ' 7 G W g w ( 8 H X h x ) 9 I Y i y CMPE328 Spring 2007-08 MOV INT MAIN END AH,4CH 21H ENDP MAIN CMPE328 Spring 2007-08 Dr.Mehmet Bodur, EMU-CMPE AL, “a” OVER AL, “z” OVER AL, ‘a’ – ’A’ 19 l | . > N ^ n → / ? O o ← Dr.Mehmet Bodur, EMU-CMPE ASCII strings are not little endian, but unpacked-BCD is little-endian. ASCII to unpacked- BCD conversion: Alternate Conversion ;go back to DOS \ = M ] m } 18 BCD and ASCII operations MOV SI,OFFSET DATA1 ;SI points to original data MOV BX,OFFSET DATA2 ;BX points to uppercase data MOV CX,14 ;CX is loop counter BACK: MOV AL,[SI] ;get next char CMP AL,61H ;if less than 'a' JB NONEED ;then no need to convert CMP AL,7AH ;if greater than 'z' JA NONEED ;than no need to convert AND AL,11011111B ;masj d5 to convert to uppercase NONEED: MOV [BX],AL ;store uppercase character INC SI ;increment pointer to original INC BX ;increment pointer to uppercase data LOOP BACK ;continue looping if CX>0 , < L Section 3.4 Upper Case Conversion – 2 CMP JB CMP JA SUB + ; K [ k { Convert a text message to all upper case letters. for each character ch, if ch>60h and ch<7Bh then clear bit-5 of ch. Both comparison “c” = 0110 0011 Æ “C” = 0100 0011. are unsigned. “u” = 0111 0101 Æ “U” = 0101 0101. Section 3.3 .MODEL SMALL .STACK 64 .DATA DATA1 DB 'mY NAME is jOe' ORG 0020H DATA2 DB 14 DUP(?) .CODE MAIN PROC FAR MOV AX,@DATA MOV DS,AX * : J Z j z Delete the upper nibble of the ASCII coded digit. Correct the byte-ordering. -0 -1 -2 -3 -4 -5 -6 -7 -8 -9 -A -B -C -D -E -F 2- ! " # $ % & ' ( ) * + , - . / 3- 0 1 2 3 4 5 6 7 8 9 : ; < = > ? 4- @ A B C D E F G H I J K L M N O 5- P Q R S T U V W X Y Z [ \ ] ^ - 6- ` a b c d e f g h i j k l m n o 7- p q r s t u v w x y z { | } → ← unpacked to packed- BCD conversion: Write two unpacked BCD digit in one packed BCD digit. Examples: Number 5 Æ ASCII 35h Æ unpacked-BCD 05 Number 7 Æ ASCII 37h Æ unpacked-BCD 07 Number 0 Æ ASCII 30h Æ unpacked-BCD 00 Number 1 Æ ASCII 31h Æ unpacked-BCD 01 packed in little unpacked BCD 05 07 00 01 00 … Æ endian convention Packed-BCD 75 10 00 … CMPE328 Spring 2007-08 Dr.Mehmet Bodur, EMU-CMPE 20 Section 3.4 Section 3.4 Packed-BCD to ASCII BCD and ASCII add/sub packed-BCD Each Packed-BCD byte expands to two unpackedexpanded in little BCD and ASCII code. endian convention Example: p-BCD 29h Æ u-BCD 09h 02h ASCII strings are in reverse direction. Æ ASCII 32h 39h mov al, bcd mov ah,al and ax,0F00Fh mov cl,4 shr ah,cl or ax,3030h xchg ah,al mov asc, ax CMPE328 Spring 2007-08 unpacked BCD unpacked BCD ASCII numbers ;p-bcd taken into al ;copy it into ah ;clear LS-nib of AH, MS in AL ;shift MS-nib 4-bit right ; and get both digits in LS-nib. ;combine with 30h to get ASCII ;swap for ASCII storage convent. ;store ASCII to memory location Dr.Mehmet Bodur, EMU-CMPE 21 SUB dst, src DAS SUB AX, src AAS DIV src AAD SUB AX, src AAS OR AX,3030h correction of multiplication and division work only with unpacked BCD. packed BCD decimal adjust is available for both dst=AL, and dst=AX. AAA clears the higher nibbles of AH and AL. ADD dst, src DAA ADD AX, src AAA MUL src AAM ADD AX, src AAA OR AX,3030h ASCII addition needs OR AX,3030h to convert the higher nibbles to 30h DAA-AAA/DAS-AAS shall only be used after a BCD ADD/SUB operation. AAM and AAD shall only be used after an unpacked BCD multiplication and division. Dr.Mehmet Bodur, EMU-CMPE CMPE328 Spring 2007-08 22 Section 3.4 Section 3.4 ROL, ROR, RCL, RCR Instructions ROL operand,1 ROR operand,1 RCL operand,1 RCR operand,1 CF operand operand CF xor ax,ax mov cx, 16 mov bx, wdata bitloop: ror bx,1 adc al,0 aaa loop bitloop CF Example: swap the nibbles of bl mov cl,4 ror bl,cl (bl=4Ch becomes bl=C4h). Dr.Mehmet Bodur, EMU-CMPE Write a code to count the number of “1”s in a list of data-words, and display it on the screen. .model small .data wdata dw 97F4h cnt1 db ?,?, ‘$’ .code mov ax,@data mov ds,ax CF operand operand Rxx op,cl rotates op by cl bits. CMPE328 Spring 2007-08 Example: nr. of 1’s in a Word 23 CMPE328 Spring 2007-08 or ax, 3030h ; ascii conv mov cnt1, ah ; MSB first, mov cnt1+1, al ; LSB next. mov dx, offset cnt1 mov ah, 09h int 21h ; display a msg ; clear ax ; for 16 bits\ ; ls-bit shifts in CF ; if CF=1, alÅal+1 ; unpacked-BCD adjust ; for 16 bits. mov ah, 4ch int 21h ; exit to DOS .stack 64 end Dr.Mehmet Bodur, EMU-CMPE 24 Section 3.5 Section 3.5 Bitwise Operations in C Embedded System programming industry 20% Assembler Coding 60% C coding with inline assembly codes 20% other languages (C++, Basic, Forth etc.) AND: bitwise OR: bitwise invert: shift left 2 bits, shift right 2 bits, CMPE328 Spring 2007-08 data1 & 0x0F data1 | 0xF0 ~data1 data1<<2 data1>>2 Æ 0x0A Æ 0xFA Æ 0xD5 Æ 0xE8 Æ 0x0E Dr.Mehmet Bodur, EMU-CMPE 25 What is the Next? We have completed Mov, Jump, Call, and basic ALU-instructions. Next, we have to see some services of BIOS and DOS programming. Please solve the following problems (page 117 in Mazidi-Mazidi) 1, 2, 7 (a, b, c, g, h, I, j, k, l, n, o), 8 (a, b, c, e), 9 (a, b, c), 14, 16, 19, 20, 25 CMPE328 Spring 2007-08 Dr.Mehmet Bodur, EMU-CMPE Program to display nr of “1”s in a word #include <stdio.h> void main(void) { unsigned int w=0x97F4, ; unsigned char ones, bits; bits=16; ones = 0; do{ if( w & 0x0001) ++ones; w >>1; } while(--bits); printf( “%i”, ones); } Assume data1= 0x3A = 0b00111010 , bitwise Programming in C 27 CMPE328 Spring 2007-08 Dr.Mehmet Bodur, EMU-CMPE 26