Integer Arithmetic COE 205 Computer Organization and Assembly
Integer Arithmetic COE 205 Computer Organization and Assembly Language Computer Engineering Department King Fahd University of Petroleum and Minerals
Presentation Outline v Shift and Rotate Instructions v Shift and Rotate Applications v Multiplication and Division Instructions v Translating Arithmetic Expressions v Decimal String to Number Conversions Integer Arithmetic COE 205 – KFUPM © Muhamed Mudawar – slide 2
SHL Instruction v SHL is the Shift Left instruction ² Performs a logical left shift on the destination operand ² Fills the lowest bit with zero ² The last bit shifted out from the left becomes the Carry Flag v Operand types for SHL: SHL SHL Integer Arithmetic reg, imm 8 mem, imm 8 reg, CL mem, CL The shift count is either: 8 -bit immediate imm 8, or stored in register CL COE 205 – KFUPM © Muhamed Mudawar – slide 3
Fast Multiplication Shifting left 1 bit multiplies a number by 2 mov dl, 5 shl dl, 1 Shifting left n bits multiplies the operand by 2 n For example, 5 * 22 = 20 mov dl, 5 shl dl, 2 Integer Arithmetic ; DL = 00000101 b ; DL = 00010100 b = 20, CF = 0 COE 205 – KFUPM © Muhamed Mudawar – slide 4
SHR Instruction v SHR is the Shift Right instruction ² Performs a logical right shift on the destination operand ² The highest bit position is filled with a zero ² The last bit shifted out from the right becomes the Carry Flag ² SHR uses the same instruction format as SHL v Shifting right n bits divides the operand by 2 n mov dl, 80 shr dl, 1 shr dl, 2 Integer Arithmetic ; DL = 01010000 b ; DL = 00101000 b = 40, CF = 0 ; DL = 00001010 b = 10, CF = 0 COE 205 – KFUPM © Muhamed Mudawar – slide 5
Logical versus Arithmetic Shifts v Logical Shift ² Fills the newly created bit position with zero v Arithmetic Shift ² Fills the newly created bit position with a copy of the sign bit ² Applies only to Shift Arithmetic Right (SAR) Integer Arithmetic COE 205 – KFUPM © Muhamed Mudawar – slide 6
SAL and SAR Instructions v SAL: Shift Arithmetic Left is identical to SHL v SAR: Shift Arithmetic Right ² Performs a right arithmetic shift on the destination operand v SAR preserves the number's sign mov dl, -80 sar dl, 1 sar dl, 2 Integer Arithmetic ; DL = 10110000 b ; DL = 11011000 b = -40, CF = 0 ; DL = 11110110 b = -10, CF = 0 COE 205 – KFUPM © Muhamed Mudawar – slide 7
Your Turn. . . Indicate the value of AL and CF after each shift mov al, 6 Bh ; al = 01101011 b shr al, 1 ; al = 00110101 b = 35 h, CF = 1 shl al, 3 ; al = 10101000 b = A 8 h, CF = 1 mov al, 8 Ch ; al = 10001100 b sar al, 1 ; al = 11000110 b = C 6 h, CF = 0 sar al, 3 ; al = 11111000 b = F 8 h, CF = 1 Integer Arithmetic COE 205 – KFUPM © Muhamed Mudawar – slide 8
ROL Instruction v ROL is the Rotate Left instruction ² Rotates each bit to the left, according to the count operand ² Highest bit is copied into the Carry Flag and into the Lowest Bit v No bits are lost mov rol Integer Arithmetic al, 11110000 b al, 1 dl, 3 Fh dl, 4 ; AL = 11100001 b, CF = 1 ; DL = 00111111 b ; DL = 11110011 b = F 3 h, CF = 1 COE 205 – KFUPM © Muhamed Mudawar – slide 9
ROR Instruction v ROR is the Rotate Right instruction ² Rotates each bit to the right, according to the count operand ² Lowest bit is copied into the Carry flag and into the highest bit v No bits are lost mov ror Integer Arithmetic al, 11110000 b al, 1 dl, 3 Fh dl, 4 ; AL = 01111000 b, CF = 0 ; DL = 00111111 b ; DL = F 3 h, CF = 1 COE 205 – KFUPM © Muhamed Mudawar – slide 10
RCL Instruction v RCL is the Rotate Carry Left instruction ² Rotates each bit to the left, according to the count operand ² Copies the Carry flag to the least significant bit ² Copies the most significant bit to the Carry flag v As if the carry flag is part of the destination operand clc mov bl, 88 h rcl bl, 1 rcl bl, 2 Integer Arithmetic ; ; clear carry, CF = 0 BL = 1000 b CF = 1, BL = 00010000 b CF = 0, BL = 01000010 b COE 205 – KFUPM © Muhamed Mudawar – slide 11
RCR Instruction v RCR is the Rotate Carry Right instruction ² Rotates each bit to the right, according to the count operand ² Copies the Carry flag to the most significant bit ² Copies the least significant bit to the Carry flag v As if the carry flag is part of the destination operand stc mov ah, 11 h rcr ah, 1 rcr ah, 3 Integer Arithmetic ; ; set carry, CF = 1 AH = 0001 b CF = 1, AH = 1000 b CF = 0, AH = 00110001 b COE 205 – KFUPM © Muhamed Mudawar – slide 12
SHLD Instruction v SHLD is the Shift Left Double instruction v Syntax: SHLD destination, source, count ² Shifts a destination operand a given count of bits to the left v The rightmost bits of destination are filled by the leftmost bits of the source operand v The source operand is not modified v Operand types: SHLD reg 16/32, imm 8/CL SHLD mem 16/32, reg 16/32, imm 8/CL Integer Arithmetic COE 205 – KFUPM © Muhamed Mudawar – slide 13
SHLD Example Shift variable var 1 4 bits to the left Replace the lowest 4 bits of var 1 with the high 4 bits of AX. data var 1 WORD 9 BA 6 h. code mov ax, 0 AC 36 h shld var 1, ax, 4 destination source var 1 AX Before: 9 BA 6 AC 36 After: BA 6 A AC 36 count destination Only the destination is modified, not the source Integer Arithmetic COE 205 – KFUPM © Muhamed Mudawar – slide 14
SHRD Instruction v SHRD is the Shift Right Double instruction v Syntax: SHRD destination, source, count ² Shifts a destination operand a given count of bits to the left v The leftmost bits of destination are filled by the rightmost bits of the source operand v The source operand is not modified v Operand types: SHLD reg 16/32, imm 8/CL SHLD mem 16/32, reg 16/32, imm 8/CL Integer Arithmetic COE 205 – KFUPM © Muhamed Mudawar – slide 15
SHRD Example Shift AX 4 bits to the right Replace the highest 4 bits of AX with the low 4 bits of DX mov ax, 234 Bh mov dx, 7654 h shrd ax, dx, 4 destination source DX AX Before: 7654 234 B After: 7654 4234 count destination Only the destination is modified, not the source Integer Arithmetic COE 205 – KFUPM © Muhamed Mudawar – slide 16
Your Turn. . . Indicate the values (in hex) of each destination operand mov ax, 7 C 36 h mov dx, 9 FA 6 h shld dx, ax, 4 ; DX = FA 67 h shrd ax, dx, 8 ; AX = 677 Ch Integer Arithmetic COE 205 – KFUPM © Muhamed Mudawar – slide 17
Next. . . v Shift and Rotate Instructions v Shift and Rotate Applications v Multiplication and Division Instructions v Translating Arithmetic Expressions v Decimal String to Number Conversions Integer Arithmetic COE 205 – KFUPM © Muhamed Mudawar – slide 18
Shifting Bits within an Array v Sometimes, we need to shift all bits within an array ² Example: moving a bitmapped image from one screen to another v Task: shift an array of bytes 1 bit right, starting a first byte. data Array. Size EQU 100 array BYTE Array. Size DUP(9 Bh) [0] [1] [2] [99]. code 9 B array before 9 B 9 B 9 B. . . mov ecx, Array. Size 4 D CD CD CD array after. . . mov esi, 0 clc ; clear carry flag L 1: rcr array[esi], 1 ; propagate the carry flag inc esi ; does not modify carry loop L 1 ; does not modify carry Integer Arithmetic COE 205 – KFUPM © Muhamed Mudawar – slide 19
Binary Multiplication v You know that SHL performs multiplication efficiently ² When the multiplier is a power of 2 v You can factor any binary number into powers of 2 ² Example: multiply EAX by 36 § Factor 36 into (4 + 32) and use distributive property of multiplication ² EAX * 36 = EAX * (4 + 32) = EAX * 4 + EAX * 32 mov shl add Integer Arithmetic ebx, eax, eax 2 5 ebx ; ; EBX EAX COE 205 – KFUPM = = number * 4 number * 32 number * 36 © Muhamed Mudawar – slide 20
Your Turn. . . Multiply EAX by 26, using shifting and addition instructions Hint: 26 = 2 + 8 + 16 mov shl add ebx, eax, eax 1 3 ebx 1 ebx ; ; ; EBX EAX = = = number number * * * 2 8 10 16 26 Multiply EAX by 31, Hint: 31 = 32 – 1 mov shl sub Integer Arithmetic ebx, eax, 5 eax, ebx ; EBX = number ; EAX = number * 32 ; EAX = number * 31 COE 205 – KFUPM © Muhamed Mudawar – slide 21
Convert Number to Binary String Task: Convert Number in EAX to an ASCII Binary String Receives: ESI EAX = Number = Address of binary string Returns: '1' String is filled with binary characters '0' and Conv. To. Bin. Str PROC mov ecx, 32 L 1: rol eax, 1 mov BYTE PTR jnc L 2 mov BYTE PTR L 2: inc esi loop L 1 mov BYTE PTR ret Conv. To. Bin. Str ENDP Integer Arithmetic USES ecx esi [esi], '0' [esi], '1' [esi], 0 COE 205 – KFUPM Rotate left most significant bit of EAX into the Carry flag; If CF = 0, append a '0' character to a string; otherwise, append a '1'; Repeat in a loop 32 times for all bits of EAX. © Muhamed Mudawar – slide 22
Convert Number to Hex String Task: Convert EAX to a Hexadecimal String pointed by ESI Receives: EAX = Number, ESI= Address of hex string Returns: 'F' String pointed by ESI is filled with hex characters '0' to Conv. To. Hex. Str PROC USES ebx ecx esi mov ecx, 8 ; 8 iterations, why? L 1: rol eax, 4 ; rotate upper 4 bits mov ebx, eax and ebx, 0 Fh ; keep only lower 4 bits mov bl, Hex. Char[ebx] ; convert to a hex char mov [esi], bl ; store hex char in string inc esi loop L 1 ; loop 8 times mov BYTE PTR [esi], 0 ; append a null byte ret Hex. Char BYTE "0123456789 ABCDEF" Conv. To. Hex. Str ENDP Integer Arithmetic COE 205 – KFUPM © Muhamed Mudawar – slide 23
Isolating a Bit String v MS-DOS date packs the year, month, & day into 16 bits ² Year is relative to 1980 In this example: Day = 10 Month = 3 Year = 1980 + 19 Date = March 10, 1999 Isolate the Month field: mov shr and mov Integer Arithmetic ax, dx ax, 5 al, 00001111 b month, al ; ; Assume DX = 16 -bit MS-DOS date shift right 5 bits clear bits 4 -7 save in month variable COE 205 – KFUPM © Muhamed Mudawar – slide 24
Next. . . v Shift and Rotate Instructions v Shift and Rotate Applications v Multiplication and Division Instructions v Translating Arithmetic Expressions v Decimal String to Number Conversions Integer Arithmetic COE 205 – KFUPM © Muhamed Mudawar – slide 25
MUL Instruction v The MUL instruction is used for unsigned multiplication v Multiplies 8 -, 16 -, or 32 -bit operand by AL, AX, or EAX v The instruction formats are: MUL r/m 8; AX = AL * r/m 8 MUL r/m 16 ; DX: AX MUL r/m 32 ; EDX: EAX = EAX * r/m 32 Integer Arithmetic COE 205 – KFUPM = AX * r/m 16 © Muhamed Mudawar – slide 26
MUL Examples Example 1: Multiply 16 -bit var 1 (2000 h) * var 2 (100 h). data var 1 WORD 2000 h The Carry and Overflow flags are set if var 2 WORD 100 h upper half of the product is non-zero. code mov ax, var 1 mul var 2 ; DX: AX = 00200000 h, CF = OF = 1 Example 2: Multiply EAX (12345 h) * EBX (1000 h) mov eax, 12345 h mov ebx, 1000 h mul ebx ; EDX: EAX = 000012345000 h, CF=OF=0 Integer Arithmetic COE 205 – KFUPM © Muhamed Mudawar – slide 27
Your Turn. . . What will be the hexadecimal values of DX, AX, and the Carry flag after the following instructions execute? mov ax, 1234 h mov bx, 100 h mul bx Solution DX = 0012 h, AX = 3400 h, CF = 1 What will be the hexadecimal values of EDX, EAX, and the Carry flag after the following instructions execute? mov eax, 00128765 h mov ecx, 10000 h mul ecx Integer Arithmetic Solution EDX = 00000012 h, EAX = 87650000 h, CF = OF = 1 COE 205 – KFUPM © Muhamed Mudawar – slide 28
IMUL Instruction v The IMUL instruction is used for signed multiplication ² Preserves the sign of the product by sign-extending it v One-Operand formats, as in MUL IMUL r/m 8 ; AX IMUL r/m 16 IMUL r/m 32 = AL * r/m 8 ; DX: AX = AX * r/m 16 ; EDX: EAX = EAX * r/m 32 v Two-Operand formats: IMUL r 16, r 16/m 16/imm 8/imm 16 IMUL r 32, r 32/m 32/imm 8/imm 32 v Three-Operand formats: IMUL r 16, r 16/m 16, imm 8/imm 16 IMUL r 32, r 32/m 32, imm 8/imm 32 Integer Arithmetic COE 205 – KFUPM The Carry and Overflow flags are set if the upper half of the product is not a sign extension of the lower half © Muhamed Mudawar – slide 29
IMUL Examples v Multiply AL = 48 by BL = 4 mov al, 48 mov bl, 4 imul bl ; AX = 00 C 0 h, CF = OF = 1 because AH is not a sign extension of AL v Your Turn: What will be DX, AX and OF ? mov ax, 8760 h mov bx, 100 h imul bx DX = FF 87 h, AX = 6000 h, OF = CF = 1 Integer Arithmetic COE 205 – KFUPM © Muhamed Mudawar – slide 30
Two and Three Operand Formats. data wval SWORD -4 dval SDWORD 4. code mov ax, -16 mov bx, 2 imul bx, ax imul bx, 2 imul bx, wval imul bx, 5000 mov edx, -16 imul edx, dval imul bx, wval, -16 imul ebx, dval, -16 imul eax, ebx, 200000 Integer Arithmetic ; ; BX BX BX OF = = BX BX BX CF ; ; EDX BX EBX OF = = EDX * dval = -64 wval * -16 = 64 dval * -16 = -64 CF = 1 COE 205 – KFUPM * * * = AX 2 wval 1 = -32 = -64 = 256 © Muhamed Mudawar – slide 31
DIV Instruction v The DIV instruction is used for unsigned division v A single operand (divisor) is supplied ² Divisor is an 8 -bit, 16 -bit, or 32 -bit register or memory ² Dividend is implicit and is either AX, DX: AX, or EDX: EAX v The instruction formats are: DIV r/m 8 DIV r/m 16 DIV r/m 32 Integer Arithmetic COE 205 – KFUPM © Muhamed Mudawar – slide 32
DIV Examples Divide AX = 8003 h by CX = 100 h mov mov div dx, 0 ax, 8003 h cx, 100 h cx ; ; clear dividend, high dividend, low divisor AX = 0080 h, DX = 3 (Remainder) Your turn: what will be the hexadecimal values of DX and AX after the following instructions execute? mov mov div Integer Arithmetic dx, 0087 h ax, 6023 h bx, 100 h bx Solution: DX = 0023 h, AX = 8760 h COE 205 – KFUPM © Muhamed Mudawar – slide 33
Divide Overflow v Divide Overflow occurs when … ² Quotient cannot fit into the destination operand, or when ² Dividing by Zero v Divide Overflow causes a CPU interrupt ² The current program halts and an error dialog box is produced v Example of a Divide Overflow mov mov div Integer Arithmetic dx, 0087 h ax, 6002 h bx, 10 h bx Divide overflow: Quotient = 87600 h Cannot fit in AX COE 205 – KFUPM © Muhamed Mudawar – slide 34
Signed Integer Division v Signed integers must be sign-extended before division ² Fill high byte, word, or double-word with a copy of the sign bit v CBW, CWD, and CDQ instructions ² Provide important sign-extension operations before division ² CBW: Convert Byte to Word, sign-extends AL into AH ² CWD: Convert Word to Double, sign-extends AX into DX ² CDQ: Convert Double to Quad, sign-extends EAX into EDX v Example: mov ax, 0 FE 9 Bh cwd Integer Arithmetic ; AX = -357 ; DX: AX = FFFFFF 9 Bh COE 205 – KFUPM © Muhamed Mudawar – slide 35
IDIV Instruction v IDIV performs signed integer division v Same syntax and operands as DIV instruction IDIV r/m 8 IDIV r/m 16 IDIV r/m 32 v Example: divide eax (-503) by ebx (10) mov eax, -503 cdq mov ebx, 10 idiv ebx ; EAX = -50, EDX = -3 Integer Arithmetic COE 205 – KFUPM All status flags are undefined after executing DIV and IDIV © Muhamed Mudawar – slide 36
IDIV Examples Example: Divide DX: AX (-48) by BX (-5) mov ax, -48 cwd mov bx, -5 idiv bx ; sign-extend AX into DX ; AX = 9, DX = -3 Example: Divide EDX: EAX (48) by EBX (-5) mov eax, 48 cdq mov ebx, -5 idiv ebx Integer Arithmetic ; sign-extend EAX into EDX ; EAX = -9, COE 205 – KFUPM EDX = 3 © Muhamed Mudawar – slide 37
Next. . . v Shift and Rotate Instructions v Shift and Rotate Applications v Multiplication and Division Instructions v Translating Arithmetic Expressions v Decimal String to Number Conversions Integer Arithmetic COE 205 – KFUPM © Muhamed Mudawar – slide 38
Translating Arithmetic Expressions v Some good reasons to translate arithmetic expressions ² Learn how compilers do it ² Test your understanding of MUL, IMUL, DIV, and IDIV ² Check for Carry and Overflow flags v Two Types of Arithmetic Expressions ² Unsigned arithmetic expressions § Unsigned variables and values are used only § Use MUL and DIV for unsigned multiplication and division ² Signed arithmetic expressions § Signed variables and values § Use IMUL and IDIV for signed multiplication and division Integer Arithmetic COE 205 – KFUPM © Muhamed Mudawar – slide 39
Unsigned Arithmetic Expressions v Example: var 4 = (var 1 + var 2) * var 3 v All variables are 32 -bit unsigned integers v Translation: mov eax, var 1 add eax, var 2 jc too. Big mul var 3 jc too. Big mov var 4, eax jmp next too. Big: . . . next: Integer Arithmetic ; ; ; EAX = var 1 + var 2 check for carry EAX = EAX * var 3 check for carry save result ; display error message COE 205 – KFUPM © Muhamed Mudawar – slide 40
Signed Arithmetic Expressions Example: var 4 = (-var 1 * var 2) + var 3 mov neg imul jo add jo mov eax, var 1 eax var 2 too. Big eax, var 3 too. Big var 4, eax ; signed multiplication ; check for overflow ; save result Example: var 4 = (var 1 * 5) / (var 2 – 3) mov imul mov sub idiv mov Integer Arithmetic eax, var 1 ebx, 5 ebx, var 2 ebx, 3 ebx var 4, eax ; EDX: EAX = product ; right side ; EAX = quotient COE 205 – KFUPM © Muhamed Mudawar – slide 41
Your Turn. . . Translate: var 5 = (var 1 * -var 2)/(var 3 – var 4) Assume signed 32 -bit integers mov neg imul mov sub idiv mov Integer Arithmetic eax, var 1 edx, var 2 edx ecx, var 3 ecx, var 4 ecx var 5, eax ; EDX: EAX = product ; EAX = quotient COE 205 – KFUPM © Muhamed Mudawar – slide 42
Next. . . v Shift and Rotate Instructions v Shift and Rotate Applications v Multiplication and Division Instructions v Translating Arithmetic Expressions v Decimal String to Number Conversions Integer Arithmetic COE 205 – KFUPM © Muhamed Mudawar – slide 43
Convert Decimal String to Number Task: Convert decimal string pointed by ESI to a number Receives: ESI = address of decimal string Returns: EAX = number in binary format Algorithm: Start by initializing EAX to 0 For each decimal character in string (example: "1083") Move one decimal character of string into EDX Convert EDX to digit (0 to 9): EDX = EDX – '0' Integer Arithmetic COE 205 – KFUPM Muhamed Mudawar – slide 44 Compute: EAX = EAX * 10 + © EDX
Convert Decimal String – cont'd ; Assumes: String should contain only decimal chars ; String should not be empty ; Procedure does not detect invalid input ; Procedure does not skip leading spaces Conv. Dec. Str PROC USES edx esi mov eax, 0 L 1: imul eax, 10 movzx edx, BYTE PTR [esi] sub edx, '0' add eax, edx inc esi cmp BYTE PTR [esi], 0 jne L 1 ret Conv. Dec. Str ENDP Integer Arithmetic COE 205 – KFUPM ; ; ; ; Initialize EAX = EAX * 10 EDX = '0' to '9' EDX = 0 to 9 EAX = EAX*10 + EDX point at next char NULL byte? ; return © Muhamed Mudawar – slide 45
Convert Number to Decimal String Task: Convert Number in EAX to a Decimal String Receives: EAX = Number, ESI = String Address Returns: String is filled with decimal characters '0' to '9' Algorithm: Divide EAX by 10 (Example: EAX = 1083) mov div add EBX, 10 ; divisor = EBX = 10 EDX, 0 ; dividend = EDX: EAX EBX ; EDX (rem) = 3, EAX = 108 dl, '0' ; DL = '3' Repeat division until EAX becomes 0 Remainder chars are computed backwards: '3', '8', '0', '1' Store characters in reverse order in string pointed by ESI Integer Arithmetic COE 205 – KFUPM © Muhamed Mudawar – slide 46
Convert to Decimal String – cont'd Conv. To. Dec. Str PROC pushad ; save all since most are used mov ecx, 0 ; Used to count decimal digits mov ebx, 10 ; divisor = 10 L 1: mov edx, 0 ; dividend = EDX: EAX div ebx ; EDX = remainder = 0 to 9 add dl, '0' ; convert DL to '0' to '9' push dx ; save decimal character inc ecx ; and count it cmp eax, 0 jnz L 1 ; loop back if EAX != 0 L 2: pop dx ; pop in reverse order mov [esi], dl ; store decimal char in string inc esi loop L 2 mov BYTE PTR [esi], 0 ; Terminate with a NULL char popad ; restore all registers ret ; return Conv. To. Dec. Str ENDP Integer Arithmetic COE 205 – KFUPM © Muhamed Mudawar – slide 47
Summary v Shift and rotate instructions ² Provide finer control over bits than high-level languages ² Can shift and rotate more than one bit left or right ² SHL, SHR, SAR, SHLD, SHRD, ROL, ROR, RCL, RCR ² Shifting left by n bits is a multiplication by 2 n ² Shifting right does integer division (use SAR to preserve sign) v MUL, IMUL, DIV, and IDIV instructions ² Provide signed and unsigned multiplication and division ² One operand format: one of the operands is always implicit ² Two and three operand formats for IMUL instruction only ² CBW, CDQ, CWD: extend AL, AX, and EAX for signed division Integer Arithmetic COE 205 – KFUPM © Muhamed Mudawar – slide 48
- Slides: 48