Download as pdf or txt
Download as pdf or txt
You are on page 1of 11

Logical Operations

 bitwise logical operations


 AND, OR, EOR, NOT 1's complement

1 operand

 “mask” = setting specific bits to zero


 e.g. mask high four bits in NUM
NUM DC.B $4C AND.B #$0F, NUM

$0C

 for bit operations, could mask out all the bits you
are not interested in testing
DOORS DC.B $05 ;1 indicates door is open
00000101

Door 7/8 Door 0/1


Shift Operations
 directions: left, right

 types: logical, arithmetic, circular


Rotate

 length: 1 bit, > 1 bit

 flags: Z, N, C, sometimes X
Shift Operations
1 shift = divide by two

Left: Right:
logical ≡ arithmetic logical ≠ arithmetic
X2 (shift) 0 C lsr

X
asl C 0
lsl C asr
X Preserves sign
X

rol C C ror

roxl C X X C roxr

Rotate thru extend bit left


Whatever shifted off the end is fed to x bit, then whatever
is in x bit is shifted to bottom. Mostly used for easy testing?
Bit Operations
 instructions operate on one bit in Dn
or memory
 bit numbering:
Dn =
(long) 31 … 10
Most significant bit #
Least significant bit #

memory 7 6 5 4 3 2 1 0
(byte)
....
1010 0010

e.g. NUM1 DC.B $A2


Bit Operations D0=$0000F8AB
....0000 1010 1111 1000 1010 1011

Bit 15 Bit 0
1. Bit Test BTST #$F,D0
z ← ~(<bit number>) of destination
Z <~ (1)
<~ 0
Bit 15
ALWAYS SET TO 1
2. Bit Set = test a bit then set BSET #3,NUM
z ← ~(<bit number>) of destination 1010 0010 Z<~(0)
<bit number> of destination ← 1 1010 1010 <- 1

Bit Clear = test a bit then clear BCLR #8,D0 Z <~ (0)
ALWAYS SET TO 0
3.
z ← ~(<bit number>) of destination <- 1
<bit number> of destination ← 0 Do no change

Changes between binary of 1 and 0


4. Bit Change = test a bit then toggle BCHG #1,NUM Z <~ (1)
z ← ~(<bit number>) of destination 1010 0010
<- 0

<bit no> of dest ← ~<bit no> of dest


1010 0000 A0

* source operand could be Dn (contains bit position)

Can be used to invert a black and white image!!!


Set according to condition
 used primarily to set/clear flags
 Scc = set a byte according to condition cc
if (condition true) then
destination ← $FF ; set to “true”
else
destination ← $00 ; set to “false”
endif

e.g. SEQ Flag If z = 1 then


Flag =$FF
… ELSE
Flag =$00
Flag DC.B $00
Test and Set
 TAS <ea>
 compare [destination] with 0
bit <7> of [destination] ← 1
 indivisible operation

 permits one processor in a multiprocessor


system to test a resource for availability
and claim it if is free
 will not use in this course
From long long68k language library

/***************************************************************/
/* Purpose..: Compute result = left << right */
/* Input....: pointer to result */
/* Input....: left operand */
/* Input....: shift count */
/* Return...: pointer to result */
/***************************************************************/
extern asm CInt64* __rt_shl64(CInt64 *result,CInt64 left,short count)
{
move.l LEFT_LO,d0
move.l LEFT_HI,d1
move.w SHIFT_COUNT,d2
and.w #0x003F,d2
bra.s l1
l0: add.l d0,d0 Take left side and add it to itself, same as doing shift
addx.l d1,d1
l1: dbf d2,l0
This limits the shift to
move.l RESULT,a0 less than 64
move.l d0,RESULT_LO
Straight counting move.l d1,RESULT_HI
loop rts
}

Note: Aliases are set elsewhere. We will see this code again later.
/***************************************************************/
/* Purpose..: Compute result = left >> right (signed) */
/* Input....: pointer to result */
/* Input....: left operand */
/* Input....: shift count */
/* Return...: pointer to result */
/***************************************************************/
extern asm CInt64* __rt_shrs64(CInt64 *result,CInt64 left,short count)
{
move.l LEFT_LO,d0
move.l LEFT_HI,d1
move.w SHIFT_COUNT,d2
and.w #0x003F,d2
bra.s l1
l0: lsr.l #1,d0
asr.l #1,d1
bcc.s l1
bset #31,d0
l1: dbf d2,l0
move.l RESULT,a0
move.l d0,RESULT_LO
move.l d1,RESULT_HI
rts
}
/***************************************************************/
/* Purpose..: Compute result = __rol(left,right) */
/* Input....: pointer to result */
/* Input....: left operand */
/* Input....: shift count */
/* Return...: pointer to result */
/***************************************************************/
extern asm CInt64* __rt_rotl64(CInt64 *result,CInt64 left,short count)
{
move.l LEFT_LO,d0
move.l LEFT_HI,d1
move.w SHIFT_COUNT,d2
and.w #0x003F,d2
bra.s l1
l0: add.l d0,d0
addx.l d1,d1
bcc.s l1
addq.w #1,d0
l1: dbf d2,l0
move.l RESULT,a0
move.l d0,RESULT_LO
move.l d1,RESULT_HI
rts
}
Reading, Expectations
Reading:
 M68000 Assembly Language [pdf, 92p; N. Znotinas]
 review operation of instructions covered in presentation
 examples were taken from the PalmOS 64 bit arithmetic
library, LongLong68K.c

Expectations:
 you can explain the operation of and the differences
between the various shifts and rotates
 you can read/write code that uses all of the above
instructions, eg. the PalmOS 64 bit arithmetic library
Data area
Prompt

Nums DS.b 10 (10 nums, a byte each)


User data (input)

You might also like