UMBC CMSC 211 Fall 1999

CSEE | 211 | 211 F'99 | lectures | news | help


Shift Operations

Shifting the contents of a word or byte left or right can also be a useful operation. With binary numbers, left shifts are the the same as multiplying by two for each position shifted left. Likewise, shifting right is like dividing by two (assuming it is an unsigned integer!)

The simplest type of shift instruction is to shift zeroes into the vacated positions and to throw away the bits shifted out the other end. This is called a logical shift. The instructions are:

shl
shr

The last bit shifted out is stored in the Carry Flag so that it can be tested. The form is:

shl/shr mem/reg, count The is also a arithmetic shift, where the only difference is during a right shift, inside of the high order bit becoming a zero, it keeps the value that it had, which is useful with signed integers.

The form is:

sal/sar mem/reg, count

NOTE: Count is either the number 1 or the cl register.

The last shifts are called rotates. This is a circular shift (which is what they used to be called.) The bit that comes out of the register or memory location on one side goes back in on the other side, with a copy of that bit stored in the Carry Flag.

The form is:

ror/rol mem/reg, count

Another Example -- revisited

The directory information for a disk file constains the date of last modification in a single work in the following format:
year - 1900 bits 9 - 15 (leftmost seven bits)
month bits 5 - 8 (January is 1)
day bits 0 - 4 (rightmost five bits)

Now we can do the following:

.MODEL small
.STACK 100h
YEARMASK EQU 1111111000000000B
MONTHMASK EQU 0000000111100000B
DAYMASK EQU 0000000000011111B
.DATA
modDate DW ?
funnyYear DW ? ; The year is still in bits 9 - 15
funnyMonth DW ? ; The month is still in bits 5 - 8
OKYear DW ? ; The day is in bits 0 - 4 and
; all other bits are set to 0
OKMon DW ?
OKDay DW ?
.CODE
example2a PROC
mov ax, @data
mov ds, ax
; ... ; Do something to get the
; modification date
mov ax, modDate
and ax, YEARMASK
mov funnyYear, ax
mov cl, 9
shr ax, cl ; Move year to bit 0
mov OKYear, ax ; save the good version
mov ax, modDate
and ax, MONTHMASK
mov funnyMonth, ax
mov cl, 5
shr ax, cl ; Move year to bit 0
mov OKMonth, ax ; save the good version
mov ax, modDate
and ax, DAYMASK
mov ax, OKDay
; At this point, the values
; are not usualable yet
mov al,0
mov ah, 4ch
int 21h
example2a ENDP
END example2a


CSEE | 211 | 211 F'99 | lectures | news | help