1 Arithmetic and Logical Operations - Part I. Boolean Operations A boolean variable can only have...

20
1 Arithmetic and Logical Operations - Part I

Transcript of 1 Arithmetic and Logical Operations - Part I. Boolean Operations A boolean variable can only have...

Page 1: 1 Arithmetic and Logical Operations - Part I. Boolean Operations A boolean variable can only have one of the two values, i.e, can either be 1 or 0. Given.

1

Arithmetic and Logical Operations - Part I

Page 2: 1 Arithmetic and Logical Operations - Part I. Boolean Operations A boolean variable can only have one of the two values, i.e, can either be 1 or 0. Given.

Boolean Operations

A boolean variable can only have one of the two values, i.e, can either be 1 or 0.

Given a sequence of bits, each of these bits can be considered one boolean variable.

A boolean operation is applied simultaneously to each bit.

Logical operations are defined for boolean variables.

Page 3: 1 Arithmetic and Logical Operations - Part I. Boolean Operations A boolean variable can only have one of the two values, i.e, can either be 1 or 0. Given.

Unary boolean operations

Unary boolean operations

input zero one invert same

0 0 1 1 0

1 0 1 0 1

MAL provides the not operator

Page 4: 1 Arithmetic and Logical Operations - Part I. Boolean Operations A boolean variable can only have one of the two values, i.e, can either be 1 or 0. Given.

Logical Operationsa b and or nand nor xor xnor

0 0 0 0 1 1 0 1

0 1 0 1 1 0 1 0

1 0 0 1 1 0 1 0

1 1 1 1 0 0 0 1

Some MAL instructionsnot D,S1

and D,S1,S2

nor D,S1,S2

Page 5: 1 Arithmetic and Logical Operations - Part I. Boolean Operations A boolean variable can only have one of the two values, i.e, can either be 1 or 0. Given.

Masking and Merging Masking is the process of extracting a

portion of the variables from a cell. A mask can be used to extract the

appropriate bits needed for future operations.

The and instruction can used to extract bits. Merging is simply inserting a bit sequence

in the appropriate part of the cell. The or instruction can be used to merge.

Page 6: 1 Arithmetic and Logical Operations - Part I. Boolean Operations A boolean variable can only have one of the two values, i.e, can either be 1 or 0. Given.

Example 14.1The following can be used as masks:

mask1: .word 0xff000000

mask2: .word 0x007fffff

01000001100000000000000000110101

and 00000000011111111111111111111111

00000000000000000000000000110101

mask

Extractedbits

Page 7: 1 Arithmetic and Logical Operations - Part I. Boolean Operations A boolean variable can only have one of the two values, i.e, can either be 1 or 0. Given.

Example 14.2

What is the result of using mask1 with the given bit sequence?

01000001100000000000000000110101

and 11111111000000000000000000000000

Page 8: 1 Arithmetic and Logical Operations - Part I. Boolean Operations A boolean variable can only have one of the two values, i.e, can either be 1 or 0. Given.

Example 14.3

01000001100000000000000000110101

and 00000000011111111111111111111111

00000000000000000000000000110101

00000000000000000000000000110101

or 01000011100000000000000000000000

01000011100000000000000000110101

merged

cleared

Page 9: 1 Arithmetic and Logical Operations - Part I. Boolean Operations A boolean variable can only have one of the two values, i.e, can either be 1 or 0. Given.

Example 14.4

Here’s a SAL code that demonstrates masking and merging:

mask1: .word 0xff000000

smallc: .word 0x63000000

charc: .word 0x43000000

. . .

not mask, mask1

and charc, mask, charc #clear bits

or charc, charc, smallc #merge

Page 10: 1 Arithmetic and Logical Operations - Part I. Boolean Operations A boolean variable can only have one of the two values, i.e, can either be 1 or 0. Given.

Shift Operations Sometimes it is necessary to move a

sequence of bits to different positions within the word, e.g. we need to align variables before merging

A shift operation rearranges the bits within the cell by shifting them

There are three types of shift operation: logical, rotate and arithmetic.

Page 11: 1 Arithmetic and Logical Operations - Part I. Boolean Operations A boolean variable can only have one of the two values, i.e, can either be 1 or 0. Given.

Logical Shift A logical shift moves the bits within the

cell one position to the right or to the left In a logical right shift, the least

significant bit (lsb) is discarded and the most significant bit (msb) is assigned 0.

In a logical left shift, the lsb is assigned 0 and the msb is discarded.

A shift instruction will have an operand that specifies how many times the one position shift is applied.

Page 12: 1 Arithmetic and Logical Operations - Part I. Boolean Operations A boolean variable can only have one of the two values, i.e, can either be 1 or 0. Given.

Example 14.5

1010100

01010000

in

discard

MAL instruction: srl D,S1,AMT

y

x

Page 13: 1 Arithmetic and Logical Operations - Part I. Boolean Operations A boolean variable can only have one of the two values, i.e, can either be 1 or 0. Given.

Example 14.6

1010100

0010101

SAL instruction: sll D,S1,AMT

0 indiscard

y

x

Page 14: 1 Arithmetic and Logical Operations - Part I. Boolean Operations A boolean variable can only have one of the two values, i.e, can either be 1 or 0. Given.

Rotate A rotate operation shifts the bits within

the cell without discarding. Unlike the logical shift, no information is lost in a rotate operation.

A rotate operation can either be left or right. A rotate left has an equivalent rotate right operation.

A rotate right places the lsb into the msb position. A rotate left places the msb into the lsb position.

Page 15: 1 Arithmetic and Logical Operations - Part I. Boolean Operations A boolean variable can only have one of the two values, i.e, can either be 1 or 0. Given.

Example 14.7

SAL instruction: rol x,y,1

1010100

0101010

y

x

Page 16: 1 Arithmetic and Logical Operations - Part I. Boolean Operations A boolean variable can only have one of the two values, i.e, can either be 1 or 0. Given.

Example 14.8

1010100y

What is the result of SAL’s ror x,y,2 ?

Page 17: 1 Arithmetic and Logical Operations - Part I. Boolean Operations A boolean variable can only have one of the two values, i.e, can either be 1 or 0. Given.

Arithmetic Shift A right shift is equivalent to integer

division by two. A left shift is equivalent to multiplication

by two. In 2’s complement, positive or negative,

a logical left shift, is equivalent to multiplication by two.

An arithmetic left shift is the same as a logical left shift.

Page 18: 1 Arithmetic and Logical Operations - Part I. Boolean Operations A boolean variable can only have one of the two values, i.e, can either be 1 or 0. Given.

An arithmetic right shift replicates the sign bit, instead of filling in with 0’s as in the logical right shift.

In 2’s complement, positive or negative, division by two is accomplished via an arithmetic right shift.

There is no arithmetic left shift instruction because the logical left shift instruction produces identical result.

Page 19: 1 Arithmetic and Logical Operations - Part I. Boolean Operations A boolean variable can only have one of the two values, i.e, can either be 1 or 0. Given.

Example 14.9

1010101

0101011

replicated

y

xdiscard

SAL instruction: sra x,y,1

Page 20: 1 Arithmetic and Logical Operations - Part I. Boolean Operations A boolean variable can only have one of the two values, i.e, can either be 1 or 0. Given.

20

Example 14.10

1010100

What is the result of multiplying this by two?