94
Computers Organization Computers Organization & & Assembly Language Assembly Language Chapter 3 Chapter 3 ARITHMETIC AND LOGIC ARITHMETIC AND LOGIC INSTRUCTIONS AND PROGRAMS INSTRUCTIONS AND PROGRAMS

Computers Organization & Assembly Language Chapter 3 ARITHMETIC AND LOGIC INSTRUCTIONS AND PROGRAMS

Embed Size (px)

Citation preview

Page 1: Computers Organization & Assembly Language Chapter 3 ARITHMETIC AND LOGIC INSTRUCTIONS AND PROGRAMS

Computers OrganizationComputers Organization&&

Assembly Language Assembly Language

Chapter 3Chapter 3

ARITHMETIC AND LOGIC ARITHMETIC AND LOGIC INSTRUCTIONS AND PROGRAMSINSTRUCTIONS AND PROGRAMS

Page 2: Computers Organization & Assembly Language Chapter 3 ARITHMETIC AND LOGIC INSTRUCTIONS AND PROGRAMS

Computers Organization & Assembly LanguageComputers Organization & Assembly Language 22

Arithmetic and Logic Arithmetic and Logic Instructions and Instructions and

ProgramsPrograms

Unsigned Addition and SubtractionUnsigned Addition and Subtraction

Subtraction of Unsigned NumbersSubtraction of Unsigned Numbers

Unsigned Multiplication and DivisionUnsigned Multiplication and Division

Logic InstructionsLogic Instructions

Page 3: Computers Organization & Assembly Language Chapter 3 ARITHMETIC AND LOGIC INSTRUCTIONS AND PROGRAMS

Computers Organization & Assembly LanguageComputers Organization & Assembly Language 33

Unsigned Addition and Unsigned Addition and SubtractionSubtraction

Addition of unsigned numbersAddition of unsigned numbersADD destination, sourceADD destination, source

sourcesource can be a general register, can be a general register, memory location, or constantmemory location, or constantdestinationdestination can be a register or can be a register or memory location except operands memory location except operands cannot both be memorycannot both be memoryThe instruction could change any ofThe instruction could change any of ZF, ZF, SF, AF, CF, or, PF bits of the flag SF, AF, CF, or, PF bits of the flag register.register.

Page 4: Computers Organization & Assembly Language Chapter 3 ARITHMETIC AND LOGIC INSTRUCTIONS AND PROGRAMS

Computers Organization & Assembly LanguageComputers Organization & Assembly Language 44

Example 3-1Example 3-1Show how the flag register is affected by Show how the flag register is affected by

MOV AL, 0F5HMOV AL, 0F5H ;AL = F5H;AL = F5HADD AL, 0BHADD AL, 0BH ;now AL = 0;now AL = 0

SolutionSolution

F5F5 11111111 01010101++ 0B0B 00000000 10111011 11 0000 11 00000000 00000000

CF = 1 since there is no carry beyond d7CF = 1 since there is no carry beyond d7PF = 1 since there is an odd number of Is in the resultPF = 1 since there is an odd number of Is in the resultAF = 1 since there is a carry from d3 to d4AF = 1 since there is a carry from d3 to d4ZF = 1 since the result is not zeroZF = 1 since the result is not zeroSF = 0 since d7 of the result is zeroSF = 0 since d7 of the result is zero

Page 5: Computers Organization & Assembly Language Chapter 3 ARITHMETIC AND LOGIC INSTRUCTIONS AND PROGRAMS

Computers Organization & Assembly LanguageComputers Organization & Assembly Language 55

Addition of individual byte and word Addition of individual byte and word datadata

Write a program to calculate the total sum of 5 bytes of data. Write a program to calculate the total sum of 5 bytes of data. Each byte represents the daily wages of a worker. This person Each byte represents the daily wages of a worker. This person does not make more than $255 (FFH) a day. The decimal data does not make more than $255 (FFH) a day. The decimal data is as follows: 125, 235, 197, 91, and 48.is as follows: 125, 235, 197, 91, and 48.

TITILTITIL PROG3-1A Adding 5 bytesPROG3-1A Adding 5 bytesPAGEPAGE 60, 13260, 132

.MODEL.MODEL SMALLSMALL

.STACK.STACK 6464; ----------------------------------------------------; ----------------------------------------------------

.DATA.DATACOUNTCOUNT EQU EQU 0505DATADATA DBDB 125, 235, 197, 91, 48125, 235, 197, 91, 48

ORGORG 0008H0008HSUMSUM DWDW ??; ----------------------------------------------------; ----------------------------------------------------

Page 6: Computers Organization & Assembly Language Chapter 3 ARITHMETIC AND LOGIC INSTRUCTIONS AND PROGRAMS

Computers Organization & Assembly LanguageComputers Organization & Assembly Language 66

Addition of individual byte and word Addition of individual byte and word datadata

.CODE.CODEMAINMAIN PROCPROC FARFAR

MOVMOV AX, @ DATAAX, @ DATAMOVMOV DS, AXDS, AXMOVMOV CX, COUNTCX, COUNT ; CX is the loop counter; CX is the loop counterMOVMOV SI, OFFSET DATASI, OFFSET DATA ; SI is the data pointer; SI is the data pointerMOVMOV AX, 00AX, 00 ; AX will hold the sum; AX will hold the sum

BACK:BACK: ADDADD AL, [SI]AL, [SI] ; add the next byte to AL; add the next byte to ALJNCJNC OVEROVER ; if no carry, continue; if no carry, continueINCINC AHAH ; else accumulate carry in AH; else accumulate carry in AH

OVEROVER INCINC SISI ; increment data pointer; increment data pointerDECDEC CXCX ; decrement loop counter; decrement loop counterJNZJNZ BACKBACK ; if not, go add next byte; if not, go add next byteMOVMOV SUM, AXSUM, AX ; store sum; store sumMOVMOV AH, 4CHAH, 4CHINTINT 21H21H ; go back to DOS; go back to DOS

MAINMAIN ENDPENDPENDEND MAINMAIN

Page 7: Computers Organization & Assembly Language Chapter 3 ARITHMETIC AND LOGIC INSTRUCTIONS AND PROGRAMS

Computers Organization & Assembly LanguageComputers Organization & Assembly Language 77

Addition of individual byte and word Addition of individual byte and word datadata

For Pipelining Replace these lines For Pipelining Replace these lines

BACK: ADDBACK: ADD AL, [SI]AL, [SI] ; add the next byte to AL; add the next byte to AL JNCJNC OVEROVER ; if no carry, continue; if no carry, continue INCINC AHAH ; else accumulate carry in AH; else accumulate carry in AH

OVEROVER : INC: INC SISI ; increment data pointer; increment data pointer

With these lines With these lines

BACK: ADDBACK: ADD AL, [SI]AL, [SI] ADC AH, 00ADC AH, 00 ; add 1 to AH if CF = 1; add 1 to AH if CF = 1 INCINC SISI

Page 8: Computers Organization & Assembly Language Chapter 3 ARITHMETIC AND LOGIC INSTRUCTIONS AND PROGRAMS

Computers Organization & Assembly LanguageComputers Organization & Assembly Language 88

Program 3-1bProgram 3-1bWrite a program to calculate the total sum of five words of data. Write a program to calculate the total sum of five words of data. Each data value represents the yearly wages of a worker. This Each data value represents the yearly wages of a worker. This person does not make more than $65,555 (FFFFH) a year. The person does not make more than $65,555 (FFFFH) a year. The decimal data is as follows: 27345, 28521, 29533, 30105, and 32375.decimal data is as follows: 27345, 28521, 29533, 30105, and 32375.

TITILTITIL PROG3-1B Adding 5 wordsPROG3-1B Adding 5 words.MODEL.MODEL SMALLSMALL.STACK.STACK 6464.DATA.DATA

COUNTCOUNT EQUEQU 0505DATADATA DWDW 27345, 28521, 29533, 30105, 3237527345, 28521, 29533, 30105, 32375

ORGORG 0010H0010HSUMSUM DWDW 2 DUP(?)2 DUP(?).CODE.CODEMAINMAIN PROCPROC FARFAR

MOVMOV AX, @DATAAX, @DATAMOVMOV DS, AXDS, AX

Page 9: Computers Organization & Assembly Language Chapter 3 ARITHMETIC AND LOGIC INSTRUCTIONS AND PROGRAMS

Computers Organization & Assembly LanguageComputers Organization & Assembly Language 99

Program 3-1bProgram 3-1bMOVMOV CX, COUNTCX, COUNT ; CX is the loop counter; CX is the loop counterMOVMOV SI, OFFSET DATASI, OFFSET DATA ; SI is the data pointer; SI is the data pointerMOVMOV AX, 00AX, 00 ; AX will hold the sum; AX will hold the sumMOVMOV BX, AXBX, AX ; BX will hold the carries; BX will hold the carries

BACK:BACK: ADDADD AX, [SI]AX, [SI] ; add the next word to AX; add the next word to AXADCADC BX, 00BX, 00 ; add carry to BX; add carry to BXINCINC SISI ; increment data pointer twice; increment data pointer twiceINCINC SISI ; to point to the next word; to point to the next wordDECDEC CXCX ; decrement loop counter; decrement loop counterJNZJNZ BACKBACK ; if not finished, continue adding; if not finished, continue addingMOVMOV SUM, AXSUM, AX ; store the sum; store the sumMOVMOV SUM + 2, BXSUM + 2, BX ; store the carries; store the carriesMOVMOV AH, 4CHAH, 4CHINTINT 21H21H ; go back to DOS; go back to DOS

MAINMAIN ENDPENDPENDEND MAINMAIN

Page 10: Computers Organization & Assembly Language Chapter 3 ARITHMETIC AND LOGIC INSTRUCTIONS AND PROGRAMS

Computers Organization & Assembly LanguageComputers Organization & Assembly Language 1010

Addition of multiword numbersAddition of multiword numbersWrite a program that adds the following two multi-word Write a program that adds the following two multi-word numbers and saves the result: DATA1 = 548FB9963CE7H and numbers and saves the result: DATA1 = 548FB9963CE7H and DATA2 = 3FCD4FA23B8DH.DATA2 = 3FCD4FA23B8DH.

TITILTITILPROG3-2 Multi-word AdditionPROG3-2 Multi-word Addition.MODEL.MODEL SMALLSMALL.STACK.STACK 6464.DATA.DATA

DATA1DATA1 DQDQ 548FB9963CE7H548FB9963CE7HORGORG 0010H0010H

DATA2DATA2 DQDQ 3FCD4FA23B8DH3FCD4FA23B8DHORGORG 0020H0020H

DATA3DATA3 DQDQ ??.CODE.CODE

MAINMAIN PROCPROC FARFARMOVMOV AX, @DATAAX, @DATAMOVMOV DS, AXDS, AX

Page 11: Computers Organization & Assembly Language Chapter 3 ARITHMETIC AND LOGIC INSTRUCTIONS AND PROGRAMS

Computers Organization & Assembly LanguageComputers Organization & Assembly Language 1111

Addition of multiword Addition of multiword numbersnumbersCLCCLC ; clear carry before first addition ; clear carry before first addition

MOVMOV SI, OFFSET DATA1SI, OFFSET DATA1 ; SI is pointer for operand1; SI is pointer for operand1MOVMOV DI, OFFSET DATA2DI, OFFSET DATA2 ; DI is pointer for operand2; DI is pointer for operand2MOVMOV BX, OFFSET DATA3BX, OFFSET DATA3 ; BX is pointer for sum; BX is pointer for sumMOVMOV CX, 04CX, 04 ; CX is the loop counter; CX is the loop counter

BACK:BACK: MOVMOV AX, [SI]AX, [SI] ; move the first operand to AX; move the first operand to AXADCADC AX, [DI]AX, [DI] ; add the second operand to AX; add the second operand to AXMOVMOV [BX], AX[BX], AX ; store the sum; store the sumINCINC SISI ; point to the next word ; point to the next word INCINC SISI ; of operand1; of operand1INCINC DIDI ; point to the next word; point to the next wordINCINC DIDI ; of operand2; of operand2INCINC BXBX ; point to the next word of sum; point to the next word of sumINCINC BXBXLOOPLOOP BACKBACK ; if not finished, continue adding; if not finished, continue addingMOVMOV AH, 4CHAH, 4CHINTINT 21H21H ; go back to DOS; go back to DOS

MAINMAIN ENDPENDPENDEND MAINMAIN

Page 12: Computers Organization & Assembly Language Chapter 3 ARITHMETIC AND LOGIC INSTRUCTIONS AND PROGRAMS

Computers Organization & Assembly LanguageComputers Organization & Assembly Language 1212

Analysis of program 3-2Analysis of program 3-2

LOOPLOOP xxxxxxxx ; ;

is equivalent to the following two instructionsis equivalent to the following two instructions

DECDEC CXCX

JNZJNZ xxxxxxxx

Page 13: Computers Organization & Assembly Language Chapter 3 ARITHMETIC AND LOGIC INSTRUCTIONS AND PROGRAMS

Computers Organization & Assembly LanguageComputers Organization & Assembly Language 1313

Subtraction of Unsigned NumbersSubtraction of Unsigned Numbers

SUB destination, sourceSUB destination, source

The CPU executed the SUB instruction as follows:The CPU executed the SUB instruction as follows:

Take the 2’s complement of the subtrahend (source Take the 2’s complement of the subtrahend (source operand)operand)

Add it to the minuend (destination operand)Add it to the minuend (destination operand)

Invert the carryInvert the carry

Page 14: Computers Organization & Assembly Language Chapter 3 ARITHMETIC AND LOGIC INSTRUCTIONS AND PROGRAMS

Computers Organization & Assembly LanguageComputers Organization & Assembly Language 1414

Example 3-2Example 3-2Show the steps involved in the following: Show the steps involved in the following:

MOV MOV AL, 3FHAL, 3FHMOV MOV BH, 23HBH, 23HSUB SUB AL, BHAL, BH

SolutionSolution

ALAL 3F3F 00110011 11111111 00110011 11111111-BH -BH -23-23 -0010 -0010 00110011 +1101+1101 1101 1101 (2’s (2’s comp.)comp.)

1C1C 1000110001 11001100

The flags would be set as follows: CF = 0, ZF = 0, AF = The flags would be set as follows: CF = 0, ZF = 0, AF = 0, PF = 0, and SF = 0.0, PF = 0, and SF = 0.

Page 15: Computers Organization & Assembly Language Chapter 3 ARITHMETIC AND LOGIC INSTRUCTIONS AND PROGRAMS

Computers Organization & Assembly LanguageComputers Organization & Assembly Language 1515

Example 3-3Example 3-3Analyze the following program:Analyze the following program:

;from the data segment:;from the data segment:DATA1DATA1 DBDB 4CH4CHDATA2DATA2 DBDB 6EH6EHDATA3DATA3 DBDB ??;from the code segment:;from the code segment:

MOV MOV DH, DATA1DH, DATA1SUB SUB DH, DATA2DH, DATA2JNCJNC NEXTNEXTNOTNOT DHDHINCINC DHDH

NEXT:NEXT: MOV MOV DATA3, DHDATA3, DHSolutionSolution

Following the three steps for SUB DH, DATA2Following the three steps for SUB DH, DATA24C 4C 01000100 1100 1100 01000100 11001100

- 6E- 6E 01100110 1110 1110 2's 2's comp comp +1001+1001 00100010- 22 - 22 01101 01101 1110 1110

CF= 1 (step 3) the result is negativeCF= 1 (step 3) the result is negative

Page 16: Computers Organization & Assembly Language Chapter 3 ARITHMETIC AND LOGIC INSTRUCTIONS AND PROGRAMS

Computers Organization & Assembly LanguageComputers Organization & Assembly Language 1616

SBB (Subtract with borrow)SBB (Subtract with borrow)

This instruction is used for multi-byte (multi-This instruction is used for multi-byte (multi-word) numbers and will take care of the word) numbers and will take care of the borrow of the lower operand.borrow of the lower operand.

If the carry flag is 0, SBB works like SUB.If the carry flag is 0, SBB works like SUB.

If the carry is 1, SBB subtracts 1 from the If the carry is 1, SBB subtracts 1 from the result.result.

The PTR is used to specify the size of the The PTR is used to specify the size of the operand when it differs from the defined size. operand when it differs from the defined size.

Page 17: Computers Organization & Assembly Language Chapter 3 ARITHMETIC AND LOGIC INSTRUCTIONS AND PROGRAMS

Computers Organization & Assembly LanguageComputers Organization & Assembly Language 1717

Example 3-4Example 3-4Analyze the following program:Analyze the following program:

DATA_ADATA_A DD 62562FAHDD 62562FAHDATA_BDATA_B DD 412963BHDD 412963BHRESULTRESULT DD ?DD ?

... ...... ...MOVMOV AX, WORD PTR DATA_AAX, WORD PTR DATA_A ;AX==62FA;AX==62FASUBSUB AX, WORD PTR DATA_BAX, WORD PTR DATA_B ;SUB 963B from AX;SUB 963B from AXMOVMOV AX, WORD PTR RESULT, AX AX, WORD PTR RESULT, AX ;save the result;save the resultMOVMOV AX, WOFW PTR DATA_A +2 AX, WOFW PTR DATA_A +2 ;AX=0625;AX=0625SBBSBB AX, WORD PTR DATA_B +2AX, WORD PTR DATA_B +2 ;SUB 0412 with ;SUB 0412 with

borrowborrowMOVMOV WORD PTR RESULT +2, AXWORD PTR RESULT +2, AX ;save the result.';save the result.'

Solution:Solution:

After the SUB, AX = 62FA – 963B = CCBF and the carry flag is set. Since CF = After the SUB, AX = 62FA – 963B = CCBF and the carry flag is set. Since CF = 1, when SBB is executed. AX = 625 - 412 - 1 = 212. Therefore, the value stored 1, when SBB is executed. AX = 625 - 412 - 1 = 212. Therefore, the value stored in RESULT is 0212CCBF.in RESULT is 0212CCBF.

Page 18: Computers Organization & Assembly Language Chapter 3 ARITHMETIC AND LOGIC INSTRUCTIONS AND PROGRAMS

Computers Organization & Assembly LanguageComputers Organization & Assembly Language 1818

Unsigned Multiplication and Unsigned Multiplication and DivisionDivision

Multiplication of unsigned numbersThe use of registers AX, AL, AH, and DX The use of registers AX, AL, AH, and DX is necessary in multiplication or division.is necessary in multiplication or division.

In discussing multiplication, the following cases will be examined:– (1) byte times byte, – (2) word times word, – (3) byte times word.

Page 19: Computers Organization & Assembly Language Chapter 3 ARITHMETIC AND LOGIC INSTRUCTIONS AND PROGRAMS

Computers Organization & Assembly LanguageComputers Organization & Assembly Language 1919

Byte Times ByteByte Times Byte operand1 in AL, operand2 in a register or memory, operand1 in AL, operand2 in a register or memory, and result in AXand result in AX

RESULT RESULT DWDW ?? ; result is defined ; result is defined ……MOV AL, 25HMOV AL, 25H ; a byte is moved to AL; a byte is moved to ALMOV BL, 65HMOV BL, 65H ; data must be in a ; data must be in a

registerregisterMUL BLMUL BL ; AX = 25 x 65H; AX = 25 x 65HMOV RESULT, AXMOV RESULT, AX ; the result is saved; the result is saved

Page 20: Computers Organization & Assembly Language Chapter 3 ARITHMETIC AND LOGIC INSTRUCTIONS AND PROGRAMS

Computers Organization & Assembly LanguageComputers Organization & Assembly Language 2020

Byte Times ByteByte Times Byte; from the data segment; from the data segmentDATA1DATA1 DBDB 25H25HDATA2DATA2 DBDB 65H65HRESULTRESULT DWDW ??; from the code segment; from the code segment

MOV AL, DATA1MOV AL, DATA1MOV BL, DATA2MOV BL, DATA2MUL BLMUL BL ; register addressing mode; register addressing modeMOV RESULT, AXMOV RESULT, AX

ororMOV AL, DATA1MOV AL, DATA1MUL DATA2MUL DATA2 ; direct addressing mode; direct addressing modeMOV RESULT, AXMOV RESULT, AX

ororMOV AL, DATA1MOV AL, DATA1MOV SI, OFFSET DATA2MOV SI, OFFSET DATA2MUL BYTE PTR [SI]MUL BYTE PTR [SI] ; register indirect ; register indirect MOV RESULT, AXMOV RESULT, AX

Page 21: Computers Organization & Assembly Language Chapter 3 ARITHMETIC AND LOGIC INSTRUCTIONS AND PROGRAMS

Computers Organization & Assembly LanguageComputers Organization & Assembly Language 2121

Word Times Word Word Times Word operand1 in AX, operand2 in a register or memory, and result in DX operand1 in AX, operand2 in a register or memory, and result in DX AXAX

DATA3DATA3 DWDW 2378H2378HDATA4DATA4 DWDW 2F79H2F79HRESULT1RESULT1 DWDW 2 DUP( ? )2 DUP( ? )

……MOV AX, DATA3MOV AX, DATA3 ; load first operand in ; load first operand in

AXAXMUL DATA4MUL DATA4 ; multiply ; multiply

MOV RESULT1, AXMOV RESULT1, AX ; store the ; store the lower resultlower result

MOV RESULT1+2, DXMOV RESULT1+2, DX ; store the higher ; store the higher resultresult

Page 22: Computers Organization & Assembly Language Chapter 3 ARITHMETIC AND LOGIC INSTRUCTIONS AND PROGRAMS

Computers Organization & Assembly LanguageComputers Organization & Assembly Language 2222

Byte Times WordByte Times Wordoperand1 in AL = byte, AH = 0, operand2 in a register or operand1 in AL = byte, AH = 0, operand2 in a register or memory, and result in DX AXmemory, and result in DX AX

; from the data segment; from the data segmentDATA5DATA5 DBDB 6BH6BHDATA6DATA6 DWDW 12C3H12C3HRESULT3RESULT3 DWDW 2 DUP( ? )2 DUP( ? )

; from the code segment; from the code segment……MOV AL, DATA5MOV AL, DATA5 ; AL holds byte operand ; AL holds byte operand SUB AH, AHSUB AH, AH ; AH must be cleared ; AH must be cleared MUL DATA6MUL DATA6 ; AL multiplied by word; AL multiplied by wordMOV BX, OFFSET RESULT3MOV BX, OFFSET RESULT3 ; BX points to product; BX points to productMOV [BX], AXMOV [BX], AX ; AX holds the lower result; AX holds the lower resultMOV [BX]+2, DXMOV [BX]+2, DX ; DX holds the higher ; DX holds the higher

resultresult

Page 23: Computers Organization & Assembly Language Chapter 3 ARITHMETIC AND LOGIC INSTRUCTIONS AND PROGRAMS

Computers Organization & Assembly LanguageComputers Organization & Assembly Language 2323

Unsigned Multiplication Unsigned Multiplication SummarySummary

MultiplicationMultiplication Operand 1Operand 1 Operand 2Operand 2 ResultResult

Byte X ByteByte X Byte ALAL Register or Register or MemoryMemory AXAX

Word X WordWord X Word AXAX Register or Register or MemoryMemory DX AXDX AX

Byte X WordByte X WordAL= Byte,AL= Byte,

AH = 0AH = 0Register or Register or

MemoryMemory DX AXDX AX

Page 24: Computers Organization & Assembly Language Chapter 3 ARITHMETIC AND LOGIC INSTRUCTIONS AND PROGRAMS

Computers Organization & Assembly LanguageComputers Organization & Assembly Language 2424

Division of unsigned Division of unsigned numbersnumbers

In the Division of unsigned numbers, the In the Division of unsigned numbers, the following cases are discussed:following cases are discussed:

– Byte over byteByte over byte– Word over wordWord over word– Word over byteWord over byte– Doubleword over wordDoubleword over word

There are two division exception There are two division exception – Dividing any number by zero.Dividing any number by zero.– The quotient is too large for the assigned The quotient is too large for the assigned

register.register.

Page 25: Computers Organization & Assembly Language Chapter 3 ARITHMETIC AND LOGIC INSTRUCTIONS AND PROGRAMS

Computers Organization & Assembly LanguageComputers Organization & Assembly Language 2525

Byte Over ByteByte Over Byte

In dividing a byte by a byte, the In dividing a byte by a byte, the numerator must be in the AL register numerator must be in the AL register an must be set to zero. an must be set to zero.

The denominator cannot be immediate The denominator cannot be immediate but can be in a register or memory.but can be in a register or memory.

After the DIV instruction is performed, After the DIV instruction is performed, the quotient is in AL and the remainder the quotient is in AL and the remainder is in AH. is in AH.

Page 26: Computers Organization & Assembly Language Chapter 3 ARITHMETIC AND LOGIC INSTRUCTIONS AND PROGRAMS

Computers Organization & Assembly LanguageComputers Organization & Assembly Language 2626

Byte Over ByteByte Over Byte

DATA7DATA7 DBDB 9595DATA8DATA8 DBDB 1010QOUT1QOUT1 DBDB ??REMAIN1REMAIN1 DBDB ? ? ; using immediate addressing mode will give an error; using immediate addressing mode will give an error

MOVMOV AL, DATA7AL, DATA7 ; move data into AL ; move data into AL SUB SUB AH, AHAH, AH ; AH must be cleared ; AH must be cleared DIV DIV 1010 ; immediate not allowed; immediate not allowed

Page 27: Computers Organization & Assembly Language Chapter 3 ARITHMETIC AND LOGIC INSTRUCTIONS AND PROGRAMS

Computers Organization & Assembly LanguageComputers Organization & Assembly Language 2727

Byte Over ByteByte Over Byte; using direct addressing mode; using direct addressing mode

MOV AL, DATA7MOV AL, DATA7 ; AL holds numerator; AL holds numeratorSUB AH, AHSUB AH, AH ; AH must be cleared ; AH must be cleared DIV DATA8DIV DATA8 ; divide AX by DATA8; divide AX by DATA8MOV QOUT1, ALMOV QOUT1, AL ; quotient = AL = 09; quotient = AL = 09MOV REMAIN1, AHMOV REMAIN1, AH ; remainder = AH = 05; remainder = AH = 05

; using register addressing mode; using register addressing modeMOV AL, DATA7MOV AL, DATA7 ; AL holds numerator; AL holds numeratorSUB AH, AHSUB AH, AH ; AH must be cleared ; AH must be cleared MOV BH, DATA8MOV BH, DATA8 ; move denominator to ; move denominator to

BHBHDIV BHDIV BH ; divide AX by BH; divide AX by BHMOV QOUT1, ALMOV QOUT1, AL ; quotient = AL = 09; quotient = AL = 09MOV REMAIN1, AHMOV REMAIN1, AH ; remainder = AH = 05; remainder = AH = 05

Page 28: Computers Organization & Assembly Language Chapter 3 ARITHMETIC AND LOGIC INSTRUCTIONS AND PROGRAMS

Computers Organization & Assembly LanguageComputers Organization & Assembly Language 2828

Byte Over ByteByte Over Byte

; using register indirect addressing mode; using register indirect addressing mode

MOVMOV AL, DATA7AL, DATA7 ; AL holds ; AL holds numeratornumerator

SUB SUB AH, AHAH, AH ; AH must be ; AH must be cleared cleared

MOV BX, OFFSET DATA8MOV BX, OFFSET DATA8 ; BX is offset ; BX is offset DIV DIV BYTE PTR [BX]BYTE PTR [BX] ; divide AX by ; divide AX by

DATA8DATA8MOV QOUT2, ALMOV QOUT2, ALMOV REMAIN2, AHMOV REMAIN2, AH

Page 29: Computers Organization & Assembly Language Chapter 3 ARITHMETIC AND LOGIC INSTRUCTIONS AND PROGRAMS

Computers Organization & Assembly LanguageComputers Organization & Assembly Language 2929

Word Over WordWord Over Word

In this case the numerator is in AX and DX In this case the numerator is in AX and DX must be cleared.must be cleared.The denominator can be in a register or The denominator can be in a register or memory. memory. After the DIV, AX will have the quotient and After the DIV, AX will have the quotient and the remainder will be in DX.the remainder will be in DX.

Page 30: Computers Organization & Assembly Language Chapter 3 ARITHMETIC AND LOGIC INSTRUCTIONS AND PROGRAMS

Computers Organization & Assembly LanguageComputers Organization & Assembly Language 3030

Word Over WordWord Over Word

The following program divides 10050 by 100The following program divides 10050 by 100

MOV MOV AX, 10050AX, 10050 ; AX holds numerator; AX holds numeratorSUB SUB DX, DXDX, DX ; DX must be cleared ; DX must be cleared MOV MOV BX, 100BX, 100 ; BX used for denominator; BX used for denominatorDIV DIV BXBX ; divide AX by BX; divide AX by BXMOV MOV QOUT2, AXQOUT2, AX ; quotient = AX = 64H = ; quotient = AX = 64H =

100100MOV MOV REMAIN2, DXREMAIN2, DX ; remainder = DX = 32H = ; remainder = DX = 32H =

5050

Page 31: Computers Organization & Assembly Language Chapter 3 ARITHMETIC AND LOGIC INSTRUCTIONS AND PROGRAMS

Computers Organization & Assembly LanguageComputers Organization & Assembly Language 3131

Word Over ByteWord Over Byte

Again, the numerator is in AX and the Again, the numerator is in AX and the denominator can be in a register or denominator can be in a register or memory.memory.

After the DIV instruction, AL will After the DIV instruction, AL will contain the quotient, and AH will contain the quotient, and AH will contain the remainder.contain the remainder.

The maximum quotient is FFH. The maximum quotient is FFH.

Page 32: Computers Organization & Assembly Language Chapter 3 ARITHMETIC AND LOGIC INSTRUCTIONS AND PROGRAMS

Computers Organization & Assembly LanguageComputers Organization & Assembly Language 3232

Word Over ByteWord Over Byte

The following program divides AX = 2055 by CL=100. The following program divides AX = 2055 by CL=100. Then AL= 14H (20 decimal) is the quotient AH = 37H (55 Then AL= 14H (20 decimal) is the quotient AH = 37H (55 decimal) is the remainder.decimal) is the remainder.

MOV AX, 2055MOV AX, 2055 ; AX holds numerator; AX holds numeratorMOV CL, 100MOV CL, 100 ; CX used for denominator; CX used for denominatorDIV CLDIV CL ; divide AX by BX; divide AX by BXMOV QOU, ALMOV QOU, AL ; AL holds quotient; AL holds quotientMOV REMI, AHMOV REMI, AH ; AH holds remainder ; AH holds remainder

Page 33: Computers Organization & Assembly Language Chapter 3 ARITHMETIC AND LOGIC INSTRUCTIONS AND PROGRAMS

Computers Organization & Assembly LanguageComputers Organization & Assembly Language 3333

Doubleword Over WordDoubleword Over Word

The numerator is in AX and DX with the most significant word lf1DX and the least significant word in AX.

The denominator can be in a register or in memory.

After the DIV instruction, the quotient will be in AX, the remainder in DX.

The maximum quotient is FFFFH.

Page 34: Computers Organization & Assembly Language Chapter 3 ARITHMETIC AND LOGIC INSTRUCTIONS AND PROGRAMS

Computers Organization & Assembly LanguageComputers Organization & Assembly Language 3434

Doubleword Over WordDoubleword Over Word

; from the data segment; from the data segmentDATA1DATA1 DDDD 105432105432DATA2DATA2 DWDW 1000010000QUOTQUOT DWDW ??REMAINREMAIN DWDW ? ? ; from the code segment; from the code segment

MOVMOV AX, WORD PTR DATA1AX, WORD PTR DATA1 MOV DX, WORD PTR DATA1 + 2MOV DX, WORD PTR DATA1 + 2 DIV DIV DATA2DATA2 ; divide AX by BX; divide AX by BXMOV QUOT, AXMOV QUOT, AX ; AX holds quotient; AX holds quotientMOV REMAIN, DXMOV REMAIN, DX ; DX holds ; DX holds

remainder remainder

Page 35: Computers Organization & Assembly Language Chapter 3 ARITHMETIC AND LOGIC INSTRUCTIONS AND PROGRAMS

Computers Organization & Assembly LanguageComputers Organization & Assembly Language 3535

Unsigned Division Unsigned Division SummarySummary

Division Numerator Denominator Quo. Rem.

Byte / ByteAL=Byte,

AH=0Register or

MemoryAL AH

Word / WordAX=Word,

DX=0Register or

MemoryAX DX

Word / Byte AX=WordRegister or

MemoryAL AH

DWord / word DX:AX=DWRegister or

MemoryAX DX

Page 36: Computers Organization & Assembly Language Chapter 3 ARITHMETIC AND LOGIC INSTRUCTIONS AND PROGRAMS

Translating Arithmetic ExpressionsTranslating Arithmetic Expressions

Some good reasons to translate arithmetic expressionsSome good reasons to translate arithmetic expressions

– Learn how compilers do itLearn how compilers do it

– Test your understanding of MUL, IMUL, DIV, and IDIVTest your understanding of MUL, IMUL, DIV, and IDIV

– Check for Carry and Overflow flagsCheck for Carry and Overflow flags

Two Types of Arithmetic ExpressionsTwo Types of Arithmetic Expressions

– Unsigned arithmetic expressionsUnsigned arithmetic expressions

Unsigned variables and values are used onlyUnsigned variables and values are used only

Use MUL and DIV for unsigned multiplication and divisionUse MUL and DIV for unsigned multiplication and division

– Signed arithmetic expressionsSigned arithmetic expressions

Signed variables and valuesSigned variables and values

Use IMUL and IDIV for signed multiplication and divisionUse IMUL and IDIV for signed multiplication and division

Computers Organization & Assembly LanguageComputers Organization & Assembly Language 3636

Page 37: Computers Organization & Assembly Language Chapter 3 ARITHMETIC AND LOGIC INSTRUCTIONS AND PROGRAMS

Unsigned Arithmetic ExpressionsUnsigned Arithmetic Expressions

Example: Example: var4 = (var1 + var2) * var3var4 = (var1 + var2) * var3

All variables are 32-bit unsigned integersAll variables are 32-bit unsigned integers

Translation:Translation:

mov eax, var1add eax, var2 ; EAX = var1 + var2jc tooBig ; check for carrymul var3 ; EAX = EAX * var3jc tooBig ; check for carrymov var4, eax ; save resultjmp next

tooBig:. . . ; display error message

next:

Computers Organization & Assembly LanguageComputers Organization & Assembly Language 3737

Page 38: Computers Organization & Assembly Language Chapter 3 ARITHMETIC AND LOGIC INSTRUCTIONS AND PROGRAMS

Signed Arithmetic ExpressionsSigned Arithmetic Expressions

Example: var4 = (-var1 * var2) + var3mov eax, var1neg eaximul var2 ; signed multiplicationjo tooBig ; check for overflowadd eax, var3jo tooBig ; check for overflowmov var4, eax ; save result

Example: var4 = (var1 * 5) / (var2 – 3)mov eax, var1

mov ebx, 5imul ebx ; EDX:EAX = productmov ebx, var2 ; right sidesub ebx, 3idiv ebx ; EAX = quotientmov var4, eax

Computers Organization & Assembly LanguageComputers Organization & Assembly Language 3838

Page 39: Computers Organization & Assembly Language Chapter 3 ARITHMETIC AND LOGIC INSTRUCTIONS AND PROGRAMS

Computers Organization & Assembly LanguageComputers Organization & Assembly Language 3939

Logic InstructionsLogic Instructions

In this section we discuss the logic In this section we discuss the logic instructions: instructions:

– AND, AND, – OR, OR, – XOR, XOR, – SHIFT, SHIFT, – COMPARE.COMPARE.

Page 40: Computers Organization & Assembly Language Chapter 3 ARITHMETIC AND LOGIC INSTRUCTIONS AND PROGRAMS

Computers Organization & Assembly LanguageComputers Organization & Assembly Language 4040

AND InstructionAND Instruction

The AND InstructionThe AND InstructionAND destination, sourceAND destination, source

This instruction will perform a logical AND on the This instruction will perform a logical AND on the operands and place the result in the destination. operands and place the result in the destination. The destination can be a register or in memory.The destination can be a register or in memory.The source can be a register, in memory, or immediate. The source can be a register, in memory, or immediate. AND is used to mask certain bits of the operand.AND is used to mask certain bits of the operand.AND can be used to test for a zero operand.AND can be used to test for a zero operand.

; AND DH with itself and set ZF = 1 if the result is zero.; AND DH with itself and set ZF = 1 if the result is zero.ANDAND DH, DH DH, DH JZJZ L1L1……

L1: L1: ……

Page 41: Computers Organization & Assembly Language Chapter 3 ARITHMETIC AND LOGIC INSTRUCTIONS AND PROGRAMS

Computers Organization & Assembly LanguageComputers Organization & Assembly Language 4141

Example 3-5Example 3-5

Show the result of the following:Show the result of the following:MOVMOV BL, 35HBL, 35HANDAND BL,0FHBL,0FH ; AND BL with 0FH. ; AND BL with 0FH.

SolutionSolution

35H35H 00110011 010101010FH0FH 00000000 1111111105H05H 00000000 01010101

Flag settings will be: SF = 0, ZF = 0, PF = 1 (according Flag settings will be: SF = 0, ZF = 0, PF = 1 (according to the result), CF = OF = 0 (automatically).to the result), CF = OF = 0 (automatically).

Page 42: Computers Organization & Assembly Language Chapter 3 ARITHMETIC AND LOGIC INSTRUCTIONS AND PROGRAMS

Computers Organization & Assembly LanguageComputers Organization & Assembly Language 4242

OR InstructionOR Instruction

The OR InstructionThe OR InstructionOROR destination, source destination, source

This instruction will perform a logical OR on the This instruction will perform a logical OR on the operands and place the result in the destination. operands and place the result in the destination. The destination operand can be a register or in The destination operand can be a register or in memory.memory.The source operand can be a register, in memory, The source operand can be a register, in memory, or immediate. or immediate.

OR can be used to test for a zero operand.OR can be used to test for a zero operand.OR BL, BLOR BL, BL

; OR BL with itself and set ZF = 1 if the result is zero.; OR BL with itself and set ZF = 1 if the result is zero.

Page 43: Computers Organization & Assembly Language Chapter 3 ARITHMETIC AND LOGIC INSTRUCTIONS AND PROGRAMS

Computers Organization & Assembly LanguageComputers Organization & Assembly Language 4343

Example 3-6Example 3-6Show the result of the following:Show the result of the following:

MOVMOV AX, 0504HAX, 0504H ; AX = 0504; AX = 0504OROR AX,0DA68HAX,0DA68H ; AX = DF6C; AX = DF6C

Solution:Solution:

0504H0504H 0000 0101 0000 01000000 0101 0000 0100DA68HDA68H 1101 1010 0110 10001101 1010 0110 1000DF6CHDF6CH 1101 1111 0110 11001101 1111 0110 1100

Flag settings will be: SF = 1, ZF = 0, PF = 1 Flag settings will be: SF = 1, ZF = 0, PF = 1 (according to the result), CF = OF = 0 (automatically). (according to the result), CF = OF = 0 (automatically). The parity is checked for the lower 8 bit only.The parity is checked for the lower 8 bit only.

Page 44: Computers Organization & Assembly Language Chapter 3 ARITHMETIC AND LOGIC INSTRUCTIONS AND PROGRAMS

Computers Organization & Assembly LanguageComputers Organization & Assembly Language 4444

XOR InstructionXOR InstructionThe XOR InstructionThe XOR Instruction

XORXOR destination, source destination, sourceThis instruction will perform a logical XOR on the This instruction will perform a logical XOR on the operands and place the result in the destination. operands and place the result in the destination. The destination can be a register or in memory.The destination can be a register or in memory.The source can be a register, in memory, or immediate. The source can be a register, in memory, or immediate. XOR can be used to see if two registers have the same XOR can be used to see if two registers have the same values.values.

XOR BX, CXXOR BX, CX ; will make ZF = 1 if both ; will make ZF = 1 if both registers registers have the same value and have the same value and

BX = BX = result = 0000H.result = 0000H.XOR can be used to toggle bits of an operand.XOR can be used to toggle bits of an operand.

XOR AL, 04HXOR AL, 04H ; will toggle bit 2 in AL.; will toggle bit 2 in AL.

Page 45: Computers Organization & Assembly Language Chapter 3 ARITHMETIC AND LOGIC INSTRUCTIONS AND PROGRAMS

Computers Organization & Assembly LanguageComputers Organization & Assembly Language 4545

Example 3-7Example 3-7Show the result of the following:Show the result of the following:

MOVMOV DH, 54HDH, 54HXORXOR DH, 78HDH, 78H

Solution:Solution:

54H54H 0101 01000101 010078H78H 0111 10000111 10002CH2CH 0010 11000010 1100

Flag settings will be: SF = 0, ZF = 0, PF = 0 Flag settings will be: SF = 0, ZF = 0, PF = 0 (according to the result), CF = OF = 0 (according to the result), CF = OF = 0 (automatically).(automatically).

Page 46: Computers Organization & Assembly Language Chapter 3 ARITHMETIC AND LOGIC INSTRUCTIONS AND PROGRAMS

Computers Organization & Assembly LanguageComputers Organization & Assembly Language 4646

SHIFT InstructionsSHIFT Instructions

There are logical (for unsigned operands) There are logical (for unsigned operands) and arithmetic (for signed operands) shift and arithmetic (for signed operands) shift instructions. We consider logical ones.instructions. We consider logical ones.Shift instructions shift the contents of a Shift instructions shift the contents of a register or memory location right or left.register or memory location right or left.In logical shift, the number of times or bits In logical shift, the number of times or bits that operand is shifted can be specified that operand is shifted can be specified directly if it is once or through CL register if directly if it is once or through CL register if it is more than once.it is more than once.In logical right shift; SHR: 0 to MSB and In logical right shift; SHR: 0 to MSB and LSB to CFLSB to CF

Page 47: Computers Organization & Assembly Language Chapter 3 ARITHMETIC AND LOGIC INSTRUCTIONS AND PROGRAMS

Computers Organization & Assembly LanguageComputers Organization & Assembly Language 4747

SHR InstructionSHR Instruction

This is the logical shift right.This is the logical shift right.

The operand is shifted right bit by bit, and for every The operand is shifted right bit by bit, and for every shift the LSB (least significant bit) will go to the shift the LSB (least significant bit) will go to the carry flag (CF) and the MSB (most significant bit) is carry flag (CF) and the MSB (most significant bit) is filled with 0. filled with 0.

MSB LSB0 CF

Page 48: Computers Organization & Assembly Language Chapter 3 ARITHMETIC AND LOGIC INSTRUCTIONS AND PROGRAMS

Computers Organization & Assembly LanguageComputers Organization & Assembly Language 4848

Example 3-8Example 3-8Show the result of the SHR in the following:Show the result of the SHR in the following:

MOVMOV AL, 9AHAL, 9AHMOVMOV CL, 03HCL, 03H ; set number of times to shift; set number of times to shiftSHRSHR AL, CLAL, CL

Solution:Solution:9AH =9AH = 1001 10101001 1010

00100 1101100 1101 CF = 0 (shifted once)CF = 0 (shifted once)000010 011010 0110 CF = 1 (shifted twice)CF = 1 (shifted twice)

0000001 00111 0011 CF = 0 (shifted three CF = 0 (shifted three times)times)After three times of shifting right, AL = 13H and CF = 0After three times of shifting right, AL = 13H and CF = 0

Page 49: Computers Organization & Assembly Language Chapter 3 ARITHMETIC AND LOGIC INSTRUCTIONS AND PROGRAMS

Computers Organization & Assembly LanguageComputers Organization & Assembly Language 4949

Example 3-9Show the results of SHR in the following:;from the data segment:DATA1 DW 7777H;from the code segment:TIMES EQU 4MOV CL,TIMES ;CL=04SHR DATA1,CL ;shift DATA1 CL times

Solution:After the four shifts, the word at memory location DATA1 will contain 0777. The four LSBs are lost through the carry, one by one, and 0s fill the four MSBs.

Page 50: Computers Organization & Assembly Language Chapter 3 ARITHMETIC AND LOGIC INSTRUCTIONS AND PROGRAMS

Computers Organization & Assembly LanguageComputers Organization & Assembly Language 5050

SHL InstructionSHL Instruction

Shift left is also a logical shift.

It is the reverse of SHR.

After every shift, the LSB is filled with 0 and the MSB goes to CF.

All the rules are the same as SHR.

MSB LSB 0CF

Page 51: Computers Organization & Assembly Language Chapter 3 ARITHMETIC AND LOGIC INSTRUCTIONS AND PROGRAMS

Computers Organization & Assembly LanguageComputers Organization & Assembly Language 5151

Example 3-10Example 3-10Show the effect of SHL in the following:

MOVMOV DH, 06HDH, 06HMOVMOV CL, 04HCL, 04H ; set number of times to ; set number of times to

shiftshiftSHLSHL DH, CLDH, CL

SolutionSolution::06H06H = = 0000 01100000 0110

CF = 0 CF = 0 0000 1100000 11000 (shifted left once) (shifted left once)CF = 0 CF = 0 0001 100001 100000 (shifted left twice) (shifted left twice)CF = 0 CF = 0 0011 00011 0000000 (shifted left 3 times) (shifted left 3 times)CF = 0 CF = 0 0110 0110 00000000 (shifted left 4 times) (shifted left 4 times)

After the four times of shifting left, the DH = 60H and CF = 0After the four times of shifting left, the DH = 60H and CF = 0

Page 52: Computers Organization & Assembly Language Chapter 3 ARITHMETIC AND LOGIC INSTRUCTIONS AND PROGRAMS

Computers Organization & Assembly LanguageComputers Organization & Assembly Language 5252

COMPARE of Unsigned COMPARE of Unsigned NumbersNumbers

CMPCMP destination, source destination, sourceThe CMP instruction compares two operands and The CMP instruction compares two operands and changes the flags according to the result of the changes the flags according to the result of the comparison.comparison.The operands of this instruction remain unchanged.The operands of this instruction remain unchanged.The destination can be a register or in memory.The destination can be a register or in memory.The source can be a register, memory, or immediate.The source can be a register, memory, or immediate.All the CF, AF, SF, PF, ZF, and OF flags reflect the result All the CF, AF, SF, PF, ZF, and OF flags reflect the result of the comparison, only the CF and ZF are used.of the comparison, only the CF and ZF are used.

Compare Operands CF ZF

Destination > Source 0 0

Destination = Source 0 1

Destination < Source 1 0

Page 53: Computers Organization & Assembly Language Chapter 3 ARITHMETIC AND LOGIC INSTRUCTIONS AND PROGRAMS

Computers Organization & Assembly LanguageComputers Organization & Assembly Language 5353

Example 3-11Example 3-11

DATA1DATA1 DWDW 235FH235FH……MOVMOV AX, 0CCCCHAX, 0CCCCHCMPCMP AX, DATA1AX, DATA1 ; compare CCCC with ; compare CCCC with

235F235FJNCJNC OVER OVER ; jump if CF = 0; jump if CF = 0SUBSUB AX, AXAX, AX

OVER:OVER: INCINC DATA1 DATA1

Page 54: Computers Organization & Assembly Language Chapter 3 ARITHMETIC AND LOGIC INSTRUCTIONS AND PROGRAMS

Computers Organization & Assembly LanguageComputers Organization & Assembly Language 5454

Example 3-12Example 3-12

MOVMOV BX, 7888HBX, 7888H

MOVMOV CX, 9FFFHCX, 9FFFH

CMPCMP BX, CXBX, CX ;compare 7888 with ;compare 7888 with 9FFF9FFF

JNCJNC NEXT NEXT

ADDADD BX, 4000HBX, 4000H

NEXT:NEXT:ADDADD CX, 250H CX, 250H

Page 55: Computers Organization & Assembly Language Chapter 3 ARITHMETIC AND LOGIC INSTRUCTIONS AND PROGRAMS

Computers Organization & Assembly LanguageComputers Organization & Assembly Language 5555

Example 3-13Example 3-13Assume that there is a class of five people with the following Assume that there is a class of five people with the following grades: 69, 87, 96, 45, and 75. Find the highest grade.grades: 69, 87, 96, 45, and 75. Find the highest grade.

.MODEL.MODEL SMALLSMALL

.STACK.STACK 6464

.DATA.DATAGRADESGRADES DBDB 69, 87, 96, 45, 7569, 87, 96, 45, 75

ORGORG 00080008HIGHESTHIGHEST DBDB ??

Page 56: Computers Organization & Assembly Language Chapter 3 ARITHMETIC AND LOGIC INSTRUCTIONS AND PROGRAMS

Computers Organization & Assembly LanguageComputers Organization & Assembly Language 5656

.CODE.CODEMAINMAIN PROCPROC FARFAR

MOVMOV AX, @DATAAX, @DATAMOVMOV DS, AXDS, AXMOVMOV CX, 05CX, 05 ; set up the loop counter ; set up the loop counter

MOVMOV BX, OFFSET GRADESBX, OFFSET GRADES ; BX points to GRADE data; BX points to GRADE data

SUBSUB AL, ALAL, AL ; AL holds highest grade found so far; AL holds highest grade found so far

AGAIN:AGAIN: CMPCMP AL, [BX]AL, [BX] ; compare next grade to highest; compare next grade to highest

JAJA NEXT NEXT ; jump if AL still highest; jump if AL still highest

MOVMOV AL, [BX]AL, [BX] ; else AL holds new highest; else AL holds new highest

NEXT:NEXT: INCINC BXBX ; point to the next grade ; point to the next grade

LOOPLOOP AGAINAGAIN ; continue search; continue search

MOVMOV HIGHEST, ALHIGHEST, AL ; store highest grade; store highest grade

MOVMOV AH, 4CHAH, 4CHINTINT 21H21H ; go back to DOS; go back to DOS

MAINMAIN ENDPENDPENDEND MAINMAIN

Page 57: Computers Organization & Assembly Language Chapter 3 ARITHMETIC AND LOGIC INSTRUCTIONS AND PROGRAMS

Computers Organization & Assembly LanguageComputers Organization & Assembly Language 5757

Example 3-14Example 3-14Another complete program that uses the CMP instruction to Another complete program that uses the CMP instruction to determine if an ASCII character is uppercase or lowercase.determine if an ASCII character is uppercase or lowercase.

TITILTITIL PROG3-4 Lowercase to uppercase conversionPROG3-4 Lowercase to uppercase conversion.MODEL.MODEL SMALLSMALL.STACK.STACK 6464.DATA.DATA

DATA1DATA1 DBDB ‘my name is Ali’‘my name is Ali’ORGORG 0020H0020H

DATA2DATA2 DBDB 14 DUP(?)14 DUP(?)

Page 58: Computers Organization & Assembly Language Chapter 3 ARITHMETIC AND LOGIC INSTRUCTIONS AND PROGRAMS

Computers Organization & Assembly LanguageComputers Organization & Assembly Language 5858

.CODE.CODEMAINMAIN PROCPROC FARFAR

MOVMOV AX, @DATAAX, @DATAMOVMOV DS, AXDS, AXMOVMOV SI, OFFSET DATA1SI, OFFSET DATA1 ; SI points to original data; SI points to original dataMOVMOV BX, OFFSET DATA2BX, OFFSET DATA2 ; BX points to uppercase data; BX points to uppercase dataMOVMOV CX, 14CX, 14 ; CX is the loop counter; CX is the loop counter

BACK:BACK: MOVMOV AL, [SI]AL, [SI] ; get the next character; get the next characterCMPCMP AL, 61HAL, 61H ; if less than ‘a’; if less than ‘a’JBJB OVEROVER ; than no need to convert; than no need to convertCMPCMP AL, 7AHAL, 7AH ; if greater than ‘z’; if greater than ‘z’JAJA OVEROVER ; than no need to convert; than no need to convertANDAND AL, 11011111BAL, 11011111B ; mask d5 to convert to uppercase; mask d5 to convert to uppercase

OVER:OVER: MOVMOV [BX], AL[BX], AL ; store uppercase character; store uppercase characterINCINC SISI ; increment pointer to original ; increment pointer to original INCINC BXBX ; inc pointer to uppercase data ; inc pointer to uppercase data LOOPLOOP BACKBACK ; continue looping if CX > 0; continue looping if CX > 0MOVMOV AH, 4CHAH, 4CHINTINT 21H21H ; go back to DOS; go back to DOS

MAINMAIN ENDPENDPENDEND MAINMAIN

Page 59: Computers Organization & Assembly Language Chapter 3 ARITHMETIC AND LOGIC INSTRUCTIONS AND PROGRAMS

Block-Structured IF StatementsBlock-Structured IF StatementsIF statement in high-level languages (such as C or Java)IF statement in high-level languages (such as C or Java)

– Boolean expression (evaluates to true or false)Boolean expression (evaluates to true or false)

– List of statements performed when the expression is trueList of statements performed when the expression is true

– Optional list of statements performed when expression is Optional list of statements performed when expression is falsefalse

Task: Translate IF statements into assembly languageTask: Translate IF statements into assembly language

Example:Example:mov eax,var1cmp eax,var2jne elsepartmov X,1jmp next

elsepart:mov X,2

next:

if( var1 == var2 ) X = 1;else X = 2;

Computers Organization & Assembly LanguageComputers Organization & Assembly Language 5959

Page 60: Computers Organization & Assembly Language Chapter 3 ARITHMETIC AND LOGIC INSTRUCTIONS AND PROGRAMS

Your Turn . . .Your Turn . . .Implement the following IF in assembly language Implement the following IF in assembly language

All variables are All variables are 32-bit signed32-bit signed integers integers

mov eax,var1cmp eax,var2jle ifpartmov var3,6mov var4,7jmp next

ifpart:

mov var3,10next:

if (var1 <= var2) { var3 = 10;}else { var3 = 6; var4 = 7;}

There can be multiple correct solutionsComputers Organization & Assembly LanguageComputers Organization & Assembly Language 6060

Page 61: Computers Organization & Assembly Language Chapter 3 ARITHMETIC AND LOGIC INSTRUCTIONS AND PROGRAMS

Compound Expression with Compound Expression with ANDANDHLLs use HLLs use short-circuit evaluationshort-circuit evaluation for logical AND for logical AND

If first expression is If first expression is falsefalse, second expression is , second expression is skippedskipped

if ((al > bl) && (bl > cl)) {X = 1;}

; One Possible Implementation ...

cmp al, bl ; first expression ...

ja L1 ; unsigned comparison

jmp next

L1: cmp bl,cl ; second expression ...

ja L2 ; unsigned comparison

jmp next

L2: mov X,1 ; both are true

next:Computers Organization & Assembly LanguageComputers Organization & Assembly Language 6161

Page 62: Computers Organization & Assembly Language Chapter 3 ARITHMETIC AND LOGIC INSTRUCTIONS AND PROGRAMS

Your Turn . . .Your Turn . . .Implement the following IF in assembly languageImplement the following IF in assembly language

All values are All values are unsignedunsigned

cmp ebx,ecxja nextcmp ecx,edxjbe nextmov eax,5mov edx,6

next:

if ((ebx <= ecx) && (ecx > edx))

{ eax = 5; edx = 6;}

Computers Organization & Assembly LanguageComputers Organization & Assembly Language 6262

Page 63: Computers Organization & Assembly Language Chapter 3 ARITHMETIC AND LOGIC INSTRUCTIONS AND PROGRAMS

Application: IsDigit ProcedureApplication: IsDigit Procedure

IsDigit PROC cmp al,'0' ; AL < '0' ? jb L1 ; yes? ZF=0, return cmp al,'9' ; AL > '9' ? ja L1 ; yes? ZF=0, return test al, 0 ; ZF = 1

L1: retIsDigit ENDP

Receives a character in AL

Sets the Zero flag if the character is a decimal digitif (al >= '0' && al <= '9') {ZF = 1;}

Computers Organization & Assembly LanguageComputers Organization & Assembly Language 6363

Page 64: Computers Organization & Assembly Language Chapter 3 ARITHMETIC AND LOGIC INSTRUCTIONS AND PROGRAMS

Compound Expression with ORCompound Expression with OR HLLs use short-circuit evaluation for logical OR

If first expression is true, second expression is skipped

Use fall-through to keep the code as short as possible

if ((al > bl) || (bl > cl)) {X = 1;}

cmp al,bl ; is AL > BL?ja L1 ; yes, execute if partcmp bl,cl ; no: is BL > CL?jbe next ; no: skip if part

L1: mov X,1 ; set X to 1next:

Computers Organization & Assembly LanguageComputers Organization & Assembly Language 6464

Page 65: Computers Organization & Assembly Language Chapter 3 ARITHMETIC AND LOGIC INSTRUCTIONS AND PROGRAMS

Computers Organization & Assembly LanguageComputers Organization & Assembly Language 6565

BCD and ASCII Operands and BCD and ASCII Operands and InstructionsInstructions

This section covers instructions that This section covers instructions that handle ASCII and BCD numbers. handle ASCII and BCD numbers.

BCD stands for binary coded decimal.

BCD is needed because human beings use the digits 0 to 9 for numbers.

Binary representation of 0 to 9 is called BCD

In computer literature one encounters two terms for BCD numbers:– (1) unpacked BCD – (2) packed BCD

Digit BCD

0 0000

1 0001

2 0010

3 0011

4 0100

5 0101

6 0110

7 0111

8 1000

9 1001

Page 66: Computers Organization & Assembly Language Chapter 3 ARITHMETIC AND LOGIC INSTRUCTIONS AND PROGRAMS

Computers Organization & Assembly LanguageComputers Organization & Assembly Language 6666

BCD and ASCIIBCD and ASCIIUnpacked BCD– In unpacked BCD, the lower 4 bits of the number

represent the BCD number and the rest of the bits are 0.

– Example: "0000 1001" and "0000 0101" are unpacked BCD for 9 and 5, respectively.

Packed BCD– In the case of packed BCD, a single byte has two

BCD numbers in it, one in the lower 4 bits and one in the upper 4 bits.

– For example, “0101 1001” is packed BCD for 59. ASCII numbers– In ASCII keyboards, when key "0" is activated, for

example, "011 0000“ (30H) is provided to the computer

Page 67: Computers Organization & Assembly Language Chapter 3 ARITHMETIC AND LOGIC INSTRUCTIONS AND PROGRAMS

Computers Organization & Assembly LanguageComputers Organization & Assembly Language 6767

ASCII to unpacked BCD Conversion

To convert ASCII data to BCD, the programmer To convert ASCII data to BCD, the programmer must get rid of the tagged "011" in the higher 4 must get rid of the tagged "011" in the higher 4 bits of the ASCII.bits of the ASCII.To do that, each ASCII number is ANDed with To do that, each ASCII number is ANDed with "0000 1111" (0FH), as shown in next example. "0000 1111" (0FH), as shown in next example. This example is written in three different ways This example is written in three different ways using different addressing modes. using different addressing modes. The following three programs show three The following three programs show three different methods for converting the 10 ASCII different methods for converting the 10 ASCII digits to unpacked BCD. digits to unpacked BCD.

Page 68: Computers Organization & Assembly Language Chapter 3 ARITHMETIC AND LOGIC INSTRUCTIONS AND PROGRAMS

Computers Organization & Assembly LanguageComputers Organization & Assembly Language 6868

Example 1Example 1

ASCASC DBDB ‘9562481273’‘9562481273’ORGORG 0010H0010H

UNPACKUNPACK DBDB 10 DUP( ? )10 DUP( ? )

MOVMOV CX, 05CX, 05 ; CX is the loop counter; CX is the loop counterMOVMOV BX, OFFSET ASCBX, OFFSET ASC ; BX points to ASCII data; BX points to ASCII dataMOVMOV DI, OFFSET UNPACKDI, OFFSET UNPACK ; DI points to Unpacked BCD data; DI points to Unpacked BCD data

AGAIN:AGAIN: MOVMOV AX, [BX]AX, [BX] ; move next 2 ASCII numbers to AX; move next 2 ASCII numbers to AXANDAND AX, 0F0FHAX, 0F0FH ; remove ASCII 3s; remove ASCII 3sMOVMOV [DI], AX[DI], AX ; store unpacked BCD; store unpacked BCDADD ADD DI, 2DI, 2 ; point to next unpacked BCD data; point to next unpacked BCD dataADD ADD BX, 2BX, 2 ; point to next ASCII data; point to next ASCII dataLOOPLOOP AGAINAGAIN ; continue looping if CX > 0; continue looping if CX > 0

Page 69: Computers Organization & Assembly Language Chapter 3 ARITHMETIC AND LOGIC INSTRUCTIONS AND PROGRAMS

Computers Organization & Assembly LanguageComputers Organization & Assembly Language 6969

Example 2Example 2ASCASC DBDB ‘9562481273’‘9562481273’

ORGORG 0010H0010HUNPACKUNPACK DBDB 10 DUP( ? )10 DUP( ? )

MOVMOV CX, 05CX, 05 ; CX is the loop counter; CX is the loop counterMOVMOV BX, OFFSET ASCBX, OFFSET ASC ; BX points to ASCII data; BX points to ASCII dataMOVMOV DI, OFFSET UNPACKDI, OFFSET UNPACK ; DI points to Unpacked BCD ; DI points to Unpacked BCD

datadataAGAIN:AGAIN: MOVMOV AX, WORD PTR [BX]AX, WORD PTR [BX] ; move next 2 ASCII numbers to ; move next 2 ASCII numbers to

AXAXANDAND AX, 0F0FHAX, 0F0FH ; remove ASCII 3s; remove ASCII 3sMOVMOV WORD PTR [DI], AXWORD PTR [DI], AX ; store unpacked BCD; store unpacked BCDADD ADD DI, 2DI, 2 ; point to next unpacked BCD ; point to next unpacked BCD

datadataADD ADD BX, 2BX, 2 ; point to next ASCII data; point to next ASCII dataLOOPLOOP AGAINAGAIN ; continue looping if CX > 0; continue looping if CX > 0

Page 70: Computers Organization & Assembly Language Chapter 3 ARITHMETIC AND LOGIC INSTRUCTIONS AND PROGRAMS

Computers Organization & Assembly LanguageComputers Organization & Assembly Language 7070

Example 3Example 3ASCASC DBDB ‘9562481273’‘9562481273’

ORGORG 0010H0010HUNPACKUNPACK DBDB 10 DUP( ? )10 DUP( ? )

MOVMOV CX, 10CX, 10 ; load the counter; load the counterSUB SUB BX, BXBX, BX ; clear BX; clear BX

AGAIN:AGAIN: MOVMOV AL, ASC [BX]AL, ASC [BX] ; move [BX + ASC] to AL ; move [BX + ASC] to AL ANDAND AL, 0FHAL, 0FH ; mask the upper nibble; mask the upper nibbleMOVMOV UNPACK [DI], AL UNPACK [DI], AL ; move AL to [BX + UNPACK] ; move AL to [BX + UNPACK] INC INC BXBX ; make the pointer to next ; make the pointer to next

bytebyteLOOPLOOP AGAINAGAIN ; loop until it is finished; loop until it is finished

Page 71: Computers Organization & Assembly Language Chapter 3 ARITHMETIC AND LOGIC INSTRUCTIONS AND PROGRAMS

Computers Organization & Assembly LanguageComputers Organization & Assembly Language 7171

ASCII to packed BCD ASCII to packed BCD ConversionConversion

To convert ASCII to packed BCD, it is first converted to To convert ASCII to packed BCD, it is first converted to unpacked BCD (to get rid of the 3) and then combined unpacked BCD (to get rid of the 3) and then combined to make packed BCD. to make packed BCD. For example, for 9 and 5 the keyboard gives 39 and 35, For example, for 9 and 5 the keyboard gives 39 and 35, respectively. respectively. The goal is to produce 95H or“1001 0101”, which is The goal is to produce 95H or“1001 0101”, which is called packed BCD. called packed BCD. This process is illustrated in detail below. .This process is illustrated in detail below. .

Key ASCII Unpacked BCD Packed BCD

4 34 00000100

7 37 00000111 01000111 or 47H

Page 72: Computers Organization & Assembly Language Chapter 3 ARITHMETIC AND LOGIC INSTRUCTIONS AND PROGRAMS

Computers Organization & Assembly LanguageComputers Organization & Assembly Language 7272

ExampleExample

ORGORG 0010H0010HVAL_ASCVAL_ASC DBDB ‘47’‘47’VAL_BCDVAL_BCD DBDB ??

; the DB will put 34 in 0010H location and 37 in 0011H location; the DB will put 34 in 0010H location and 37 in 0011H location

MOVMOV AX, WORD PTR VAL_ASCAX, WORD PTR VAL_ASC ; AH = 37, AL = 34; AH = 37, AL = 34ANDAND AX, 0F0FHAX, 0F0FH ; mask 3 to get unpacked ; mask 3 to get unpacked

BCDBCDXCHGXCHG AH, ALAH, AL ; swap AH and AL; swap AH and ALMOVMOV CL, 04CL, 04 ; CL = 4 to shift 4 times; CL = 4 to shift 4 timesSHL, SHL, AH, CLAH, CL ; shift left AH to get AH = 40H; shift left AH to get AH = 40HOR OR AL, AHAL, AH ; OR them to get packed BCD; OR them to get packed BCDMOVMOV VAL_BCD, ALVAL_BCD, AL ; save the result; save the result

Page 73: Computers Organization & Assembly Language Chapter 3 ARITHMETIC AND LOGIC INSTRUCTIONS AND PROGRAMS

Computers Organization & Assembly LanguageComputers Organization & Assembly Language 7373

Packed BCD to ASCII Conversion

To convert packed BCD to ASCII, it must first be converted to unpacked and then the unpacked BCD is tagged with all 0000 (30H). The following shows the process of converting from packed BCD to ASCII.

Packed BCD Unpacked BCD ASCII

29H0010 1001

02H & 09H

0000 0010 & 0000 1001

32H & 39H 011 0010 & 011 1001

Page 74: Computers Organization & Assembly Language Chapter 3 ARITHMETIC AND LOGIC INSTRUCTIONS AND PROGRAMS

Computers Organization & Assembly LanguageComputers Organization & Assembly Language 7474

ExampleExampleORGORG 0010H0010H

VAL1_BCDVAL1_BCD DBDB 29H29HVAL3_ASCVAL3_ASC DWDW ??

……MOVMOV AL, VAL1_BCDAL, VAL1_BCDMOV MOV AH, ALAH, AL ; copy AL to AH, AH = AL = 29H; copy AL to AH, AH = AL = 29HANDAND AX, 0F00FHAX, 0F00FH ; mask 9 from AH and 2 from AL; mask 9 from AH and 2 from ALMOVMOV CL, 04CL, 04 ; CL = 4 to shift 4 times; CL = 4 to shift 4 timesSHRSHR AH, CLAH, CL ; shift right AH to get unpacked BCD; shift right AH to get unpacked BCDOR OR AX, 3030HAX, 3030H ; combine with 30 to get ASCII; combine with 30 to get ASCIIXCHG AH, ALXCHG AH, AL ; swap for ASCII storage convention; swap for ASCII storage conventionMOVMOV VAL3_ASC, AXVAL3_ASC, AX ; save the ASCII; save the ASCII

Page 75: Computers Organization & Assembly Language Chapter 3 ARITHMETIC AND LOGIC INSTRUCTIONS AND PROGRAMS

Computers Organization & Assembly LanguageComputers Organization & Assembly Language 7575

BCD Addition and Subtraction

BCD addition and correctionThere is a problem with adding BCD numbers, which must be corrected by DAA instruction.The problem is that after adding packed BCD numbers, the result is no longer BCD.Look at this example:

MOV AL, 17HADD AL, 28H

Adding them gives 0011 1111B (3FH), which is not BCD! A BCD number can only have digits from 0000 to 1001 (or 0 to 9). In other words, adding two BCD numbers must give a BCD result. The result above should have been 17 + 28 = 45 (0100 0101). To correct this problem, the programmer must add 6 (0110) to the low digit: 3F + 06 = 45H. The same problem could have happened in the upper digit (for example, in 52H + 87H = D9H).Again, 6 must be added to the upper digit (D9H + 60H =139H)

Page 76: Computers Organization & Assembly Language Chapter 3 ARITHMETIC AND LOGIC INSTRUCTIONS AND PROGRAMS

Computers Organization & Assembly LanguageComputers Organization & Assembly Language 7676

DAA InstructionDAA InstructionThe DAA (decimal adjust for addition) instruction in 80x86 microprocessors is provided exactly for the purpose of correcting the problem associated with BCD addition. DAA will add 6 to the lower nibble or higher nibble if needed; otherwise, it will leave the result alone.

DATA1DATA1 DBDB 47H47HDATA2DATA2 DBDB 25H25HDATA3DATA3 DBDB ??

MOVMOV AL, DATA1AL, DATA1 ; AL holds the first BCD operand; AL holds the first BCD operandMOV MOV BL, DATA2BL, DATA2 ; BL holds the second BCD ; BL holds the second BCD

operandoperandADD ADD AL, BLAL, BL ; BCD addition; BCD additionDAADAA ; adjust for BCD addition; adjust for BCD additionMOV MOV DATA3, ALDATA3, AL ; store result in correct BCD form; store result in correct BCD form

Page 77: Computers Organization & Assembly Language Chapter 3 ARITHMETIC AND LOGIC INSTRUCTIONS AND PROGRAMS

Computers Organization & Assembly LanguageComputers Organization & Assembly Language 7777

Summary of DAA actionDAA works only on ALIf after an ADD or ADC instruction the lower nibble is greater than 9, or if AF = 1, add 0110 to the lower 4 bits.If the upper nibble is greater than 9, or if CF = 1, add 0110 to the upper 4 bits.For example, adding 29H and 18H will result in 41 H, which is incorrect as far as BCD is concerned.

Hex BCD29 0010 1001

+ 18 + 0001 100041 0100 0001 AF = 1

+ 6 + 0110 because AF =1 DAA will add 6 to the lower nibble47 0100 0111 The final result is BCD.

Page 78: Computers Organization & Assembly Language Chapter 3 ARITHMETIC AND LOGIC INSTRUCTIONS AND PROGRAMS

Computers Organization & Assembly LanguageComputers Organization & Assembly Language 7878

BCD subtraction and correction

The problem associated with the addition of packed BCD numbers also shows up in subtraction. Again, there is an instruction (DAS) specifically designed to solve the problem.Therefore, when subtracting packed BCD (single-byte or multibyte) operands, the DAS instruction is put after the SUB or SBB instruction.AL must be used as the destination register to make DAS work.

Page 79: Computers Organization & Assembly Language Chapter 3 ARITHMETIC AND LOGIC INSTRUCTIONS AND PROGRAMS

Computers Organization & Assembly LanguageComputers Organization & Assembly Language 7979

Summary of DAS actionIf after a SUB or SBB instruction the lower nibble is greater than 9, or if AF = 1, subtract 0110 from the lower 4 bits.If the upper nibble is greater than 9, or CF =1, subtract 0110 from the upper nibble.

BUDGETBUDGET DTDT 8796514101287965141012EXPENSESEXPENSES DTDT 3161064039231610640392BALANCEBALANCEDTDT ??

MOV MOV CX, 10CX, 10 ; counter = 0; counter = 0MOV MOV BX, 00BX, 00 ; pointer = 0; pointer = 0CLCCLC ; clear carry ; clear carry

BACK:BACK: MOVMOV AL, BYTE PTR BUDGET [BX]AL, BYTE PTR BUDGET [BX] ; get a byte of the budget; get a byte of the budgetSBBSBB AL, BYTE PTR EXPENSES [BX] ; subtract a byte from itAL, BYTE PTR EXPENSES [BX] ; subtract a byte from itDASDAS ; correct the result ; correct the result MOV MOV BYTE PTR BALANCE [BX], ALBYTE PTR BALANCE [BX], AL; save it in balance; save it in balanceINC INC BXBX ; increment for next byte; increment for next byteLOOP BACKLOOP BACK ; continue until CX = 0; continue until CX = 0

Page 80: Computers Organization & Assembly Language Chapter 3 ARITHMETIC AND LOGIC INSTRUCTIONS AND PROGRAMS

Computers Organization & Assembly LanguageComputers Organization & Assembly Language 8080

ASCII addition and subtraction

ASCII numbers can be used as operands in add and subtract instructions the way they are, without masking the tagged 011, using instructions AAA and AAS.

MOVMOV AL, ‘5’AL, ‘5’ ; AL = 35; AL = 35

ADD ADD AL, ‘2’AL, ‘2’ ; add 32 to AL the ASCII for ; add 32 to AL the ASCII for 22

AAAAAA ; changes 67H to 07H; changes 67H to 07H

OR OR AL, 30HAL, 30H ; OR AL with 30H to get ASCII; OR AL with 30H to get ASCII

Page 81: Computers Organization & Assembly Language Chapter 3 ARITHMETIC AND LOGIC INSTRUCTIONS AND PROGRAMS

Computers Organization & Assembly LanguageComputers Organization & Assembly Language 8181

ASCII addition and subtraction

If the addition results in a value of more than 9, AAA will correct it and pass the extra bit to carry and add 1 to AH.

SUB SUB AH, AHAH, AH ; AH = 00; AH = 00MOVMOV AL, ‘7’AL, ‘7’ ; AL = 37H; AL = 37HMOVMOV BL, ‘5’BL, ‘5’ ; BL = 35H; BL = 35HADD ADD AL, BLAL, BL ; add 37H + 35H = 6CH, AL=6C.; add 37H + 35H = 6CH, AL=6C.AAAAAA ; changes 6CH to 02H in AL and ; changes 6CH to 02H in AL and

; AH = CF = 1; AH = CF = 1OR OR AX, 3030HAX, 3030H ; AX = 3132 which is ASCII for ; AX = 3132 which is ASCII for

12H12H

Page 82: Computers Organization & Assembly Language Chapter 3 ARITHMETIC AND LOGIC INSTRUCTIONS AND PROGRAMS

Computers Organization & Assembly LanguageComputers Organization & Assembly Language 8282

ASCII addition and subtraction

Two facts must be noted. First, AAA and AAS work only on the AL register, and second, the data added can be unpacked BCD rather than ASCII, and AAA and AAS will work fine.

MOV AX, 105H ;AX==0105H unpacked BCD for 15

MOV CL, 06 ;CL==06H

SUB AL, CL ;5 - 6 = -1 (FFH)

AAS ;FFH in AL is adjusted to 09, and

;AH is decremented, AX = 0009

Page 83: Computers Organization & Assembly Language Chapter 3 ARITHMETIC AND LOGIC INSTRUCTIONS AND PROGRAMS

Computers Organization & Assembly LanguageComputers Organization & Assembly Language 8383

Unpacked BCD multiplication and division

There AAM and AAD designed for multiplication and division of unpacked BCD operands. They convert the result of the multiplication and division to unpacked BCD.AAM stands for "ASCII adjust multiplication,“ but it really is unpacked multiplication correction. If two unpacked BCD numbers are multiplied, the result can be converted back to BCD by AAM.MOV AL, ‘7’ ;AL=37HAND AL, 0FH ;AL=07 unpacked BCDMOV DL, ‘6’ ;DL=36HAND DL, 0FH ;DL=06 unpacked BCDMUL DL ;AX=ALxDL =07x06=002AH=42AAM ;AX=0402 (7x6=42 unpacked BCD)OR AX, 3030H ;AX=3432 result in ASCII

Page 84: Computers Organization & Assembly Language Chapter 3 ARITHMETIC AND LOGIC INSTRUCTIONS AND PROGRAMS

Computers Organization & Assembly LanguageComputers Organization & Assembly Language 8484

Unpacked BCD multiplication and division

AAD represents "ASCII adjust for division," but that can be misleading since the data must be unpacked BCD for this instruction to work. Before dividing the unpacked BCD by another unpacked BCD,AAD is used to convert it to HEX. By doing that the quotient and remainder are both in unpacked BCD.

MOV AX,3539H ;AX=3539. ASCII for 59AND AX,0F0FH ;AH=05,AL=09 unpacked BCD dataAAD ;AX=003BH hex equivalent of 59MOV 8H,08H ;divide by 08DIV 8H ;3B / 08 gives AL=07 ,AH=03OR AX,3030H ;AL=37H (quotient) AH=33H (rem)

Page 85: Computers Organization & Assembly Language Chapter 3 ARITHMETIC AND LOGIC INSTRUCTIONS AND PROGRAMS

Computers Organization & Assembly LanguageComputers Organization & Assembly Language 8585

Rotate InstructionsIn many applications there is a need to perform a bitwise rotation of an operand. The rotation instructions ROR, ROL and RCR, RCL are designed specifically for that purpose. They allow a program to rotate an operand right or left. In this section we explore the rotate instructions, which frequently have highly specialized applications. In rotate instructions, the operand can be in a register or memory. If the number of times an operand is to be rotated is more than 1, this is indicated by CL. This is similar to the shift instructions. There are two type of rotations. One is a simple rotation of the bits of the operand, and the other is a rotation through the carry.

Page 86: Computers Organization & Assembly Language Chapter 3 ARITHMETIC AND LOGIC INSTRUCTIONS AND PROGRAMS

Computers Organization & Assembly LanguageComputers Organization & Assembly Language 8686

ROR rotate rightIn rotate right, as bits are shifted from left to right they exit from the right end (LSB) and enter the left end (MSB).

In addition, as each bit exits the LSB, a copy of it is given to the carry flag.

In other words, in ROR the LSB is moved to the MSB and is also copied to CF.

MSB LSB CF

CF

CF

Page 87: Computers Organization & Assembly Language Chapter 3 ARITHMETIC AND LOGIC INSTRUCTIONS AND PROGRAMS

Computers Organization & Assembly LanguageComputers Organization & Assembly Language 8787

ExampleExample

MOVMOV AL, 36HAL, 36H ; AL = 0011 0110; AL = 0011 0110ROR ROR AL, 1AL, 1 ; AL = 0001 1011; AL = 0001 1011 CF = 0 CF = 0 ROR ROR AL, 1AL, 1 ; AL = 1000 1101; AL = 1000 1101 CF = 1CF = 1ROR ROR AL, 1AL, 1 ; AL = 1100 0110; AL = 1100 0110 CF = 1CF = 1

or:or:MOVMOV AL, 36HAL, 36H ; AL = 0011 0110; AL = 0011 0110MOV MOV CL, 3CL, 3 ; CL = 3 number of times to rotate ; CL = 3 number of times to rotate ROR ROR AL, CLAL, CL ; AL = 1100 0110; AL = 1100 0110 CF = 1CF = 1

;the operand can be a word:;the operand can be a word:

MOVMOV BX, 0C7E5HBX, 0C7E5H ; BX = 1100 0111 1110 0101; BX = 1100 0111 1110 0101MOV MOV CL, 6CL, 6 ; CL = 6 number of times to rotate ; CL = 6 number of times to rotate ROR BX, CLROR BX, CL ; BX = 1001 0111 0001 1111 CF = 1; BX = 1001 0111 0001 1111 CF = 1

Page 88: Computers Organization & Assembly Language Chapter 3 ARITHMETIC AND LOGIC INSTRUCTIONS AND PROGRAMS

Computers Organization & Assembly LanguageComputers Organization & Assembly Language 8888

ROL Rotate LeftROL Rotate Left

In rotate left, as bits are shifted from

right to left they exit the left end (MSB) and enter the right end (LSB).

In addition, every bit that leaves the MSB is copied to the carry flag. In other words, in ROL the MSB is moved to the LSB and is also copied to CF.

MSB LSBCF

CF

CF

Page 89: Computers Organization & Assembly Language Chapter 3 ARITHMETIC AND LOGIC INSTRUCTIONS AND PROGRAMS

Computers Organization & Assembly LanguageComputers Organization & Assembly Language 8989

ExampleExampleMOVMOV BH, 72HBH, 72H ; BH = 0111 0010; BH = 0111 0010ROL ROL BH, 1BH, 1 ; BH = 1110 0100; BH = 1110 0100 CF = 0 CF = 0 ROL ROL BH, 1BH, 1 ; BH = 1100 1001; BH = 1100 1001 CF = 1 CF = 1 ROL ROL BH, 1BH, 1 ; BH = 1001 0011; BH = 1001 0011 CF = 1 CF = 1 ROL ROL BH, 1BH, 1 ; BH = 0010 0111; BH = 0010 0111 CF = 1 CF = 1

or:or:MOVMOV BH, 72HBH, 72H ; BH = 0111 0010; BH = 0111 0010MOV CL, 4MOV CL, 4 ; CL = 4 number of times to rotate ; CL = 4 number of times to rotate ROL BH, CLROL BH, CL ; BH = 0010 0111; BH = 0010 0111 CF = 1CF = 1

;the operand can be a word:;the operand can be a word:

MOVMOV DX, 672AHDX, 672AH ; BX = 0110 0111 0010 1010; BX = 0110 0111 0010 1010MOV CL, 3MOV CL, 3 ; CL = 3 number of times to rotate ; CL = 3 number of times to rotate ROL DX, CLROL DX, CL ; BX = 0011 1001 0101 0011 CF = 1; BX = 0011 1001 0101 0011 CF = 1

Page 90: Computers Organization & Assembly Language Chapter 3 ARITHMETIC AND LOGIC INSTRUCTIONS AND PROGRAMS

Computers Organization & Assembly LanguageComputers Organization & Assembly Language 9090

ExampleExampleWrite a program that finds the number of 1s in a byte.Write a program that finds the number of 1s in a byte.

From the data segment:From the data segment:DATA1DATA1 DB DB 97H97HCOUNT COUNT DB DB ??

From the code segment:From the code segment:SUB SUB BL, BLBL, BL ;clear BL to keep the number of 1s;clear BL to keep the number of 1sMOV MOV DL, 8DL, 8 ;rotate total of S times;rotate total of S timesMOV MOV AL, DATA1AL, DATA1

AGAIN: AGAIN: ROL ROL AL, 1AL, 1 ;rotate it once;rotate it onceJNC JNC NEXTNEXT ;check for 1;check for 1INC INC BLBL ;if CF=1 then add one to count;if CF=1 then add one to count

NEXT:NEXT: DEC DEC DLDL ;go through this 8 times;go through this 8 timesJNZ JNZ AGAINAGAIN ;if not finished go back;if not finished go backMOV MOV COUNT, BLCOUNT, BL ;save the number of 1s;save the number of 1s

Page 91: Computers Organization & Assembly Language Chapter 3 ARITHMETIC AND LOGIC INSTRUCTIONS AND PROGRAMS

Computers Organization & Assembly LanguageComputers Organization & Assembly Language 9191

RCR rotate right through carry

In RCR, as bits are shifted from left to right, they exit the right end (LSB) to the carry flag, and the carry flag enters the left end (MSB). In other words, in RCR the LSB is moved to CF and CF is moved to the MSB. In reality, CF acts as if it is part of the operand.

MSB LSB

CF

CF

CF

Page 92: Computers Organization & Assembly Language Chapter 3 ARITHMETIC AND LOGIC INSTRUCTIONS AND PROGRAMS

Computers Organization & Assembly LanguageComputers Organization & Assembly Language 9292

ExampleExampleCLCCLC ; make CF = 0; make CF = 0MOVMOV AL, 26HAL, 26H ; AL = 0010 0110; AL = 0010 0110RCR RCR AL, 1AL, 1 ; AL = 0001 0011; AL = 0001 0011 CF = 0 CF = 0 RCR RCR AL, 1AL, 1 ; AL = 0000 1001; AL = 0000 1001 CF = 1 CF = 1 RCR RCR AL, 1AL, 1 ; AL = 1000 0100; AL = 1000 0100 CF = 1 CF = 1

or:or:CLCCLC ; make CF = 0; make CF = 0MOVMOV AL, 26HAL, 26H ; AL = 0010 0110; AL = 0010 0110MOV CL, 3MOV CL, 3 ; CL = 3 times to rotate ; CL = 3 times to rotate RCR RCR AL, CLAL, CL ; AL = 1000 0100; AL = 1000 0100 CF = 1CF = 1

;the operand can be a word:;the operand can be a word:

STCSTC ; make CF = 1; make CF = 1MOVMOV BX, 37F1HBX, 37F1H ; BX = 0011 0111 1111 0001; BX = 0011 0111 1111 0001MOV CL, 5MOV CL, 5 ; CL = 5 times to rotate ; CL = 5 times to rotate RCR BX, CLRCR BX, CL ; BX = 0001 1001 1011 1111 CF = 0; BX = 0001 1001 1011 1111 CF = 0

Page 93: Computers Organization & Assembly Language Chapter 3 ARITHMETIC AND LOGIC INSTRUCTIONS AND PROGRAMS

Computers Organization & Assembly LanguageComputers Organization & Assembly Language 9393

RCL rotate left through carry

In RCL, as bits are shifted from right to left theyIn RCL, as bits are shifted from right to left they

exit the left end (MSB) and enter the carry flag, and the exit the left end (MSB) and enter the carry flag, and the carry flag enters the right end (LSB). carry flag enters the right end (LSB).

In other words, in RCL the \f5B is moved to CF and CF In other words, in RCL the \f5B is moved to CF and CF is moved to the LSB. In reality, CF acts as if it is part of is moved to the LSB. In reality, CF acts as if it is part of the operand. the operand.

MSB LSB

CF

CF

CF

Page 94: Computers Organization & Assembly Language Chapter 3 ARITHMETIC AND LOGIC INSTRUCTIONS AND PROGRAMS

Computers Organization & Assembly LanguageComputers Organization & Assembly Language 9494

ExampleExampleSTCSTC ; make CF = 1; make CF = 1MOVMOV BL, 15HBL, 15H ; BL = 0001 0101; BL = 0001 0101RCL RCL BL, 1BL, 1 ; BL = 0010 1011; BL = 0010 1011 CF = 0 CF = 0 RCL RCL BL, 1BL, 1 ; BL = 0101 0110; BL = 0101 0110 CF = 0 CF = 0

or:or:STCSTC ; make CF = 1; make CF = 1MOVMOV BL, 15HBL, 15H ; BL = 0001 0101; BL = 0001 0101MOV MOV CL, 2CL, 2 ; CL = 2 number of times to rotate ; CL = 2 number of times to rotate RCL RCL BL, CLBL, CL ; BL = 0101 0110; BL = 0101 0110 CF = 0 CF = 0

;the operand can be a word:;the operand can be a word:

CLCCLC ; make CF = 0; make CF = 0MOVMOV AX, 191CHAX, 191CH ; BX = 0001 1001 0001 1100; BX = 0001 1001 0001 1100MOV MOV CL, 5CL, 5 ; CL = 5 number of times to rotate ; CL = 5 number of times to rotate RCL RCL AX, CLAX, CL ; BX = 0010 0011 1000 0001 CF =1; BX = 0010 0011 1000 0001 CF =1